qbf.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. <?php
  2. /**
  3. * @file
  4. * Query By Form
  5. *
  6. * This module allows node modules to add a query by form tab for their node
  7. * types to the default search form
  8. *
  9. * @copyright 2008-2009 Ouest Systemes Informatiques (OSInet)
  10. * @author Frederic G. MARAND
  11. * @license Licensed under the CeCILL 2.0 and the General Public Licence version 2 or later
  12. * @package QBF
  13. */
  14. // $Id: qbf.module,v 1.9.4.5 2009-03-20 10:00:39 marand Exp $
  15. /**
  16. * Saved error reporting level.
  17. *
  18. * QBF module is supposed to pass parsing at E_ALL|E_STRICT, but other modules
  19. * may not be so strict, so we save the level at the start of the module and
  20. * restore it at the end of the module.
  21. */
  22. global $_qbf_er;
  23. $_qbf_er = error_reporting(E_ALL | E_STRICT);
  24. /**
  25. * Remove this element from the generated form
  26. */
  27. define('QBF_LEVEL_REMOVE', 0);
  28. /**
  29. * This element is only for display in the generated form: do not include it
  30. * in the query vector.
  31. */
  32. define('QBF_LEVEL_DISPLAY', 1);
  33. /**
  34. * Include this element in the generated form and in the query vector, but do
  35. * not mark it as required.
  36. */
  37. define('QBF_LEVEL_OPTIONAL', 2);
  38. /**
  39. * Include this element in the generated form and in the query vector, and
  40. * mark it as required.
  41. */
  42. define('QBF_LEVEL_REQUIRED', 3);
  43. /**
  44. * The main QBF path.
  45. *
  46. * It MUST be a single component path, without a "/", otherwise qbf_menu() will
  47. * need to be changed.
  48. *
  49. * @ingroup paths
  50. * @see qbf_menu()
  51. */
  52. define('QBF_PATH_MAIN', 'qbf');
  53. /**
  54. * The QBF autocomplete path for search fields
  55. * @ingroup paths
  56. */
  57. define('QBF_PATH_AC', 'qbf/ac');
  58. /**
  59. * The path to the QBF settings page
  60. */
  61. define('QBF_PATH_SETTINGS', 'admin/settings/qbf');
  62. /**
  63. * Authorize use of QBF searches
  64. */
  65. define('QBF_PERM_QUERY', 'use QBF search functions');
  66. /**
  67. * Authorize QBF administration
  68. */
  69. define('QBF_PERM_ADMIN', 'administer QBF');
  70. /**
  71. * The name of the table used to store queries
  72. */
  73. define('QBF_TABLE_NAME', 'qbf_queries');
  74. /**
  75. * Notify owner about saved query deletions, variable name.
  76. */
  77. define('QBF_VAR_NOTIFY_DELETE', 'qbf_notify_delete');
  78. /**
  79. * Notify owner about saved query deletions, default value.
  80. */
  81. define('QBF_DEF_NOTIFY_DELETE', FALSE);
  82. /**
  83. * A class wrapper for saved QBF queries
  84. */
  85. class Qbf_Query
  86. {
  87. public $qid;
  88. public $uid;
  89. public $name;
  90. public $type;
  91. public $query;
  92. public $created;
  93. public $updated;
  94. /**
  95. * Constructor
  96. *
  97. * @param string $name
  98. * @param array $ar_values
  99. * @return void
  100. */
  101. public function __construct($type, $name, $ar_values)
  102. {
  103. global $user;
  104. $this->qid = 0; // will be autoset by the DB serial
  105. $this->uid = $user->uid;
  106. $this->type = $type;
  107. $this->name = $name;
  108. $this->query = serialize($ar_values);
  109. $this->created = $this->updated = time();
  110. }
  111. /**
  112. * Save a named query to the DB, erasing previous homonym queries is any exists.
  113. *
  114. * @return int
  115. */
  116. public function save()
  117. {
  118. // Avoid duplicates
  119. if (!empty($this->name))
  120. {
  121. $sq = "DELETE FROM {%s} WHERE name = '%s' AND uid = '%d' ";
  122. db_query($sq, QBF_TABLE_NAME, $this->name, $this->uid);
  123. // $n = db_affected_rows(); // Know how many homonym queries we deleted
  124. }
  125. $ret = drupal_write_record(QBF_TABLE_NAME, $this); // no update param: we just deleted the previous version
  126. if ($ret) // has to be SAVED_NEW, by construction
  127. {
  128. $ret = $this->qid; // from serial
  129. }
  130. return $ret;
  131. }
  132. }
  133. /**
  134. * Recursively build a query array from the form and its values
  135. *
  136. * In the current version, element names are supposed to be unique, even at
  137. * different levels in the tree.
  138. *
  139. * @ingroup forms
  140. * @param array $form
  141. * @param array $form_values
  142. */
  143. function _qbf_extract_query($form, $form_values)
  144. {
  145. $name = $form['#parents'][0];
  146. // Elements which are removed or display-only have no place in the query
  147. if (array_key_exists('#qbf', $form) && array_key_exists('#level', $form['#qbf'])
  148. && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
  149. {
  150. $ret = array($name => $form_values[$name]);
  151. }
  152. else
  153. {
  154. $ret = array();
  155. }
  156. // QBF level is not inherited, so this loop is outside the "if" above
  157. foreach (element_children($form) as $child_name)
  158. {
  159. $ret += _qbf_extract_query($form[$child_name], $form_values);
  160. }
  161. return $ret;
  162. }
  163. /**
  164. * Delete a query by qid
  165. *
  166. * In the qbf/<qid>/delete case, $query has been tested for validity and access
  167. * in qbf_query_load(), so it is safe and accessible.
  168. *
  169. * Outside this context, the function can also be invoken with just a qid, and
  170. * the same check via qbf_query_load() will be performed.
  171. *
  172. * @param mixed $query
  173. * int or object
  174. */
  175. function _qbf_query_delete($query)
  176. {
  177. global $user;
  178. if (is_int($query))
  179. {
  180. $query = qbf_query_load($query);
  181. }
  182. if ($query) // access already checked in explicit or implicit qbf_query_load
  183. {
  184. $qid = $query->qid;
  185. $sq = 'DELETE FROM %s WHERE qid = %d ';
  186. $q = db_query($sq, QBF_TABLE_NAME, $qid);
  187. $message = t('Query @id "@name" has been deleted.', array
  188. (
  189. '@id' => $qid,
  190. '@name' => $query->name,
  191. ));
  192. drupal_set_message($message, 'status');
  193. $link = l($qid, QBF_PATH_MAIN .'/'. $qid .'/delete');
  194. $notify = variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE);
  195. watchdog('qbf', $message, NULL, WATCHDOG_NOTICE, $link);
  196. // access check: we only send the message to the query owner, so access is
  197. // granted without an additional check
  198. if ($notify /* && $query->uid != $user->uid */)
  199. {
  200. $owner = user_load(array('uid' => $query->uid));
  201. $language = user_preferred_language($account);
  202. $params = array
  203. (
  204. 'query' => $query,
  205. 'owner' => $owner, // unused by default, but can be used in a hook_mail_alter() implementation
  206. 'deletor' => $user,
  207. 'language' => $language,
  208. );
  209. $ret = drupal_mail('qbf', __FUNCTION__, $user->mail, $language, $params, $user->mail);
  210. drupal_set_message(t('User !link has been informed', array
  211. (
  212. '!link' => l($account->name, 'user/'. $query->uid),
  213. )));
  214. // dsm(array("QQD, ret" => $ret));
  215. }
  216. }
  217. else {
  218. $message = t('Failed attempt to delete query @qid. Administrator has been alerted.', array
  219. (
  220. '@qid' => $qid,
  221. ));
  222. drupal_set_message($message, 'error');
  223. watchdog('qbf', $message, NULL, WATCHDOG_ERROR, $link);
  224. }
  225. drupal_goto();
  226. }
  227. /**
  228. * Transform a form element for QBF.
  229. *
  230. * QBF-specific properties are:
  231. * - #qbf : array of properties
  232. * - #level: only within #qbf
  233. *
  234. * See QBF_* constants
  235. *
  236. * @ingroup forms
  237. * @param string $key
  238. * @param array $element
  239. * @return void
  240. */
  241. function _qbf_transform_element($key, $element) {
  242. // dsm(array('key' => $key, 'element' => $element));
  243. /**
  244. * List default type transformations applied to widget by FAPI.
  245. *
  246. * Types without a default transformation are not transformed
  247. */
  248. static $ar_default_type_transformations = array
  249. (
  250. 'button' => NULL, // no content
  251. 'file' => NULL, // non-querable (yet ?)
  252. 'image_button' => NULL, // new in D6
  253. 'markup' => NULL, // no content
  254. 'password' => NULL, // forbidden
  255. 'radio' => NULL, // single radio is useless, unlike a set of them
  256. 'submit' => NULL, // no content
  257. 'textarea' => 'textfield', // reduce text for searches
  258. // Don't transform these:
  259. // 'checkbox' => NULL,
  260. // 'checkboxes' => NULL,
  261. // 'date' => NULL,
  262. // 'fieldset' => NULL, // useful visually
  263. // 'form' => NULL, // removing it would delete the whole shebang
  264. // 'hidden' => NULL, // non-querable visually, but may be useful
  265. // 'item' => NULL,
  266. // 'radios' => NULL,
  267. // 'select' => NULL,
  268. // 'textfield' => NULL,
  269. // 'value' => 'value',
  270. // 'weight' => NULL,
  271. );
  272. /**
  273. * List default property transformations applied to widget by FAPI property.
  274. *
  275. * Properties without a default transformation are not transformed
  276. */
  277. static $ar_default_property_transformations = array
  278. (
  279. // Standard properties
  280. '#action' => NULL,
  281. '#after_build' => NULL,
  282. // '#base' => NULL, // gone in D6
  283. '#button_type' => NULL,
  284. '#built' => NULL,
  285. '#description' => NULL,
  286. '#method' => NULL,
  287. '#parents' => NULL,
  288. '#redirect' => NULL,
  289. '#ref' => NULL,
  290. '#required' => NULL,
  291. '#rows' => NULL,
  292. '#submit' => NULL,
  293. '#tree' => NULL,
  294. '#validate' => NULL,
  295. );
  296. /**
  297. * List properties causing causing element removal.
  298. *
  299. * The key is the property name, the value is the one causing removal.
  300. */
  301. static $ar_killer_properties = array
  302. (
  303. '#disabled' => TRUE,
  304. );
  305. // Transform type
  306. $source_type = $element['#type'];
  307. // .. Default transformation
  308. $dest_type = array_key_exists($source_type, $ar_default_type_transformations)
  309. ? $ar_default_type_transformations[$source_type]
  310. : $source_type;
  311. // .. Apply form-defined type override
  312. if (isset($element['#qbf']['#type']))
  313. {
  314. $dest_type = $element['#qbf']['#type'];
  315. }
  316. if (is_null($dest_type))
  317. {
  318. $ret = NULL;
  319. }
  320. else
  321. {
  322. $ret = $element;
  323. $ret['#type'] = $dest_type;
  324. if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
  325. {
  326. $ret = NULL;
  327. }
  328. else
  329. {
  330. foreach (element_properties($element) as $property_name)
  331. {
  332. // Apply killer properties first to avoid useless work
  333. if (array_key_exists($property_name, $ar_killer_properties)
  334. && ($element[$property_name] = $ar_killer_properties[$property_name]))
  335. {
  336. $ret = NULL;
  337. break;
  338. }
  339. // Now transform or copy remaining properties
  340. if (array_key_exists($property_name, $ar_default_property_transformations))
  341. {
  342. $ret[$property_name] = $ar_default_property_transformations[$property_name];
  343. }
  344. else
  345. {
  346. $ret[$property_name] = $element[$property_name];
  347. }
  348. // And apply form-defined property overrides
  349. if ($property_name == '#qbf')
  350. {
  351. foreach ($element[$property_name] as $override_name => $override_value)
  352. {
  353. $ret[$override_name] = $override_value;
  354. }
  355. }
  356. }
  357. // Recursively transform children
  358. foreach (element_children($element) as $child_name)
  359. {
  360. $child = _qbf_transform_element($child_name, $element[$child_name]);
  361. if (is_null($child))
  362. {
  363. unset($ret[$child_name]);
  364. }
  365. else
  366. {
  367. $ret[$child_name] = $child;
  368. }
  369. }
  370. }
  371. }
  372. //dsm(array('key' => $key, 'transformed element' => $ret));
  373. return $ret;
  374. }
  375. /**
  376. * Implement the former hook_settings().
  377. *
  378. * @return array
  379. */
  380. function qbf_admin_settings()
  381. {
  382. $form = array();
  383. $form[QBF_VAR_NOTIFY_DELETE] = array
  384. (
  385. '#type' => 'checkbox',
  386. '#default_value' => variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE),
  387. '#title' => t('Notify users when one of their saved searches has been deleted'),
  388. );
  389. return system_settings_form($form);
  390. }
  391. /**
  392. * Submit handler for qbf_form, Save search button.
  393. *
  394. * @param array $form
  395. * @param array $form_state
  396. * @return integer
  397. * The id of the saved query.
  398. */
  399. function qbf_form_save_submit($form, &$form_state)
  400. {
  401. $qid = _qbf_save($form_state['values']['form_id'], $form_state);
  402. drupal_set_message(t('Your query was saved as "@name".',
  403. array('@name' => $form_state['values']['qbf_save_name'])));
  404. global $user;
  405. $form_state['redirect'] = "user/$user->uid/qbf";
  406. return $qid;
  407. }
  408. /**
  409. * Implement hook_forms().
  410. *
  411. * @link http://drupal.org/node/144132#hook-forms @endlink
  412. *
  413. * @ingroup forms
  414. * @ingroup hooks
  415. * @param string $form_id
  416. * @param array $args
  417. * @return array
  418. */
  419. function qbf_forms($qbf_form_id, $args)
  420. {
  421. $hook_name = 'qbf_register';
  422. // dsm(array("QBF_forms $form_id" => $args));
  423. // More efficient than using module_invoke_all: we avoid array-merging + re-looping
  424. foreach (module_implements($hook_name) as $module)
  425. {
  426. foreach (module_invoke($module, $hook_name) as $form_id => $search_title)
  427. {
  428. $forms["qbf_$form_id"] = array
  429. (
  430. 'callback' => 'qbf_form',
  431. 'callback arguments' => array($form_id, $qbf_form_id, $search_title, $args),
  432. );
  433. }
  434. }
  435. return $forms;
  436. }
  437. /**
  438. * List queries owned by a given user.
  439. *
  440. * @param int $uid > 0
  441. * @return array
  442. */
  443. function qbf_get_queries_by_user($uid = NULL)
  444. {
  445. if (is_null($uid))
  446. {
  447. global $user;
  448. $uid = $user->uid;
  449. }
  450. $sq = 'SELECT qq.qid, qq.uid, qq.name, qq.query, qq.updated '
  451. . 'FROM {%s} qq '
  452. . 'WHERE qq.uid = %d '
  453. . 'ORDER BY qq.name ';
  454. // no db_rewrite_sql: this function is not in a menu callback, so it is up to
  455. // the caller to check access
  456. $q = db_query($sq, QBF_TABLE_NAME, $uid);
  457. $ret = array();
  458. while ($o = db_fetch_object($q))
  459. {
  460. $ret[$o->qid] = $o; // qid is the PK, so it is present and unique
  461. }
  462. return $ret;
  463. }
  464. /**
  465. * Implement hook_menu().
  466. *
  467. * @return array
  468. */
  469. function qbf_menu()
  470. {
  471. $items = array();
  472. $items[QBF_PATH_MAIN . '/demo'] = array
  473. (
  474. 'title' => 'QBF Demo',
  475. 'access arguments' => array(QBF_PERM_QUERY),
  476. 'page callback' => 'qbf_show',
  477. 'type' => MENU_NORMAL_ITEM,
  478. );
  479. $items[QBF_PATH_SETTINGS] = array
  480. (
  481. 'title' => 'Query-By-Form',
  482. 'access arguments' => array(QBF_PERM_ADMIN),
  483. 'page callback' => 'drupal_get_form',
  484. 'page arguments' => array('qbf_admin_settings'),
  485. 'type' => MENU_NORMAL_ITEM,
  486. );
  487. $items[QBF_PATH_MAIN . '/%qbf_query/delete'] = array
  488. (
  489. 'type' => MENU_CALLBACK,
  490. 'access arguments' => array(QBF_PERM_QUERY),
  491. 'page callback' => '_qbf_query_delete',
  492. 'page arguments' => array(1),
  493. );
  494. return $items;
  495. }
  496. /**
  497. * Implement hook_mail().
  498. *
  499. * @param string $key
  500. * @param array $message
  501. * @param array $params
  502. * @return void
  503. */
  504. function qbf_mail($key, &$message, $params)
  505. {
  506. // dsm(array('QBF_mail key' => $key, 'message' => $message, 'params' => $params));
  507. $deletor_tokens = user_mail_tokens($params['deletor'], $params['language']->language);
  508. $tokens = array_merge($deletor_tokens, array
  509. (
  510. '!qname' => $params['query']->name,
  511. '!qid' => $params['query']->qid,
  512. ));
  513. $message['subject'] = t('Effacement d\'une recherche !site enregistrée', $tokens);
  514. $message['body'] = t("!date\n\nVotre recherche !qid: !qname\nsur le site !site vient d'être effacée par !username.", $tokens);
  515. }
  516. /**
  517. * Implement hook_perm().
  518. *
  519. * @todo D7: Format will change
  520. * @see http://drupal.org/node/224333#descriptions-permissions
  521. *
  522. * @ingroup hooks
  523. * @return array
  524. */
  525. function qbf_perm()
  526. {
  527. $ret = array
  528. (
  529. QBF_PERM_ADMIN,
  530. QBF_PERM_QUERY,
  531. );
  532. return $ret;
  533. }
  534. /**
  535. * Load a saved QBF query.
  536. *
  537. * @see qbf_import_values()
  538. * @link http://drupal.org/node/109153#load @endlink
  539. *
  540. * @param int $qid
  541. * @return array A form_values array usable by qbf_import_values
  542. */
  543. function qbf_query_load($qid)
  544. {
  545. static $query = NULL;
  546. if (is_null($query))
  547. {
  548. $sq = 'SELECT qq.qid, qq.uid, qq.query, qq.name '
  549. . 'FROM {%s} qq '
  550. . 'WHERE qq.qid = %d ';
  551. // db_rewrite_sql does not apply here: access control is further down
  552. $q = db_query($sq, QBF_TABLE_NAME, $qid);
  553. $query = db_fetch_object($q); // 0 or 1 row: we are querying on the primary key
  554. // FALSE does not happen: only NULL or a value can be here
  555. if ($query !== NULL)
  556. {
  557. $query->query = unserialize($query->query);
  558. //dsm($query);
  559. }
  560. }
  561. global $user;
  562. $ret = (isset($query) && isset($query->uid) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
  563. ? $query
  564. : FALSE;
  565. return $ret;
  566. }
  567. /**
  568. * Provide an optional automatic mapping mechanism for query building.
  569. *
  570. * This function takes a partly built query map $ar_queryMap, and a defaults
  571. * array to complete it in $ar_defaults, and returns a fully built query array
  572. * ready to be used for querying.
  573. *
  574. * @param array $ar_query_map
  575. * @param array $ar_defaults
  576. * @return array
  577. */
  578. function qbf_query_mapper($ar_query_map = array(), $ar_defaults = array())
  579. {
  580. $ret = array();
  581. foreach ($ar_query_map as $name => $value)
  582. {
  583. // accept NULL, empty strings...
  584. if (!is_array($value))
  585. {
  586. $value = array();
  587. }
  588. $item = $value;
  589. foreach ($ar_defaults as $default_key => $default_value)
  590. {
  591. if (!array_key_exists($default_key, $item))
  592. {
  593. $item[$default_key] = is_null($default_value)
  594. ? $name
  595. : $default_value;
  596. }
  597. // else if is already in $item, so we don't touch it
  598. }
  599. $ret[$name] = $item;
  600. }
  601. return $ret;
  602. }
  603. error_reporting($_qbf_er);
  604. function qbf___________________________________________________(){}
  605. // ======== D6 LIMIT ==================================================================================================
  606. /* TODO Node previews and adding form fields to the node form.
  607. There is a subtle but important difference in the way node previews (and other
  608. such operations) are carried out when adding or editing a node. With the new
  609. Forms API, the node form is handled as a multi-step form. When the node form
  610. is previewed, all the form values are submitted, and the form is rebuilt with
  611. those form values put into $form['#node']. Thus, form elements that are added
  612. to the node form will lose any user input unless they set their '#default_value'
  613. elements using this embedded node object. */
  614. /**
  615. * Transform a form array for QBF.
  616. *
  617. * This function obtains the form array from the hook_form() implementation, and
  618. * transforms it by modifying widgets to other types where needed.
  619. *
  620. * Any additional parameter passed to the function is transmitted to the
  621. * hook_form() implementation.
  622. *
  623. * @ingroup forms
  624. * @param string $form_id
  625. * @param array $form_state
  626. * @return array
  627. */
  628. function qbf_transform_form($form_state, $form_id)
  629. {
  630. // Fetch the basic form and rename it, passing it the caller's arguments
  631. $form = drupal_retrieve_form($form_id, $form_state, NULL, $form_state);
  632. $node = new stdClass();
  633. $form = $form_id($node, $form_state);
  634. $new_form_id = "qbf_$form_id";
  635. // Only keep the children of the form and QBF properties on the form itself
  636. $elements = array();
  637. $new_form = array();
  638. $new_form['#qbf_source_form_id'] = $form_id;
  639. if (in_array('#qbf', element_properties($form)))
  640. {
  641. $new_form += $form['#qbf'];
  642. }
  643. foreach (element_children($form) as $key)
  644. {
  645. // dsm("Transforming $key, type " . $form[$key]['#type']);
  646. $new_element = _qbf_transform_element($key, $form[$key]);
  647. if (!is_null($new_element))
  648. {
  649. $new_form[$key] = $new_element;
  650. }
  651. }
  652. $new_form['#id'] = $new_form_id;
  653. // @todo #multistep -> #rebuild au submit
  654. // $new_form['#multistep'] = TRUE;
  655. // Do not set #redirect, even to FALSE (submit handlers)
  656. // $new_form['#redirect'] = FALSE;
  657. $new_form['#after_build'][] = 'qbf_after_build';
  658. $new_form['#submit'] = array('qbf_submit');
  659. // dsm($new_form);
  660. return $new_form;
  661. }
  662. /**
  663. * Insert the query results at the bottom of the query form.
  664. *
  665. * @ingroup forms
  666. * @param array $form
  667. * @param array $form_values
  668. * @return array
  669. */
  670. function qbf_after_build($form, $form_values) {
  671. if (empty($form['#post'])) {
  672. return $form;
  673. }
  674. // If #post is not empty, we are indeed querying
  675. $ar_query = _qbf_extract_query($form, $form_values);
  676. /* This function is called at the end of the form building process, which
  677. * means that child properties of #qbf have already been upgraded to element
  678. * properties. So we look for $form['#callback'] and not
  679. * $form['#qbf']['#callback']
  680. */
  681. if (isset($form['#callback']) && function_exists($function = $form['#callback'])) {
  682. $results = $function($ar_query);
  683. }
  684. else {
  685. drupal_set_message(t('QBF: incorrect callback function for search'), 'error');
  686. }
  687. $form['qbf_query_results'] = array
  688. (
  689. '#type' => 'markup',
  690. '#value' => $results,
  691. '#weight' => 10,
  692. );
  693. return $form;
  694. }
  695. /**
  696. * Load a form_values array into a form used by QBF.
  697. *
  698. * This is typically useful when loading saved queries using qbf_query_load().
  699. * For other cases, the mechanisms built within FAPI should be used instead.
  700. *
  701. * @see qbf_query_load()
  702. *
  703. * @ingroup forms
  704. * @param array $form
  705. * @param array $form_values
  706. * @return array The modified form
  707. */
  708. function qbf_import_values($element, $form_values) {
  709. foreach (element_children($element) as $child_name) {
  710. if (!empty($form_values[$child_name])) {
  711. $element[$child_name]['#qbf']['#default_value'] = $form_values[$child_name];
  712. }
  713. $element[$child_name] = qbf_import_values($element[$child_name], $form_values);
  714. }
  715. return $element;
  716. }
  717. /**
  718. * Save a query and return its qid.
  719. *
  720. * @ingroup forms
  721. *
  722. * @param $form_id string
  723. * @param $form_state array
  724. * @return int
  725. */
  726. function _qbf_save($form_id, $form_state)
  727. {
  728. if (user_is_anonymous())
  729. {
  730. $warning = t('Attempt by anonymous user to save a QBF query. Should not happen.');
  731. drupal_set_message($warning, 'error');
  732. watchdog('qbf', $warning, NULL, WATCHDOG_WARNING);
  733. $ret = 0;
  734. }
  735. else
  736. {
  737. // @FIXME check whether form_state is now needed. It wasn't in QBF for D5
  738. $form = drupal_retrieve_form($form_id, $form_state);
  739. // dsm($form, "retrieve");
  740. drupal_prepare_form($form_id, $form, $form_state);
  741. // dsm($form, "prepare");
  742. $name = $form_state['values']['qbf_save_name'];
  743. $type = $form_state['values']['qbf_save_type'];
  744. // dsm($form_state);
  745. $form_values = _qbf_extract_query($form, $form_state['values']);
  746. // dsm($form_values);
  747. $ar_values = array();
  748. foreach ($form_values as $key => $value)
  749. {
  750. if (empty($value))
  751. {
  752. continue;
  753. }
  754. $ar_values[$key] = $value;
  755. }
  756. $query = new Qbf_Query($type, $name, $ar_values);
  757. $ret = $query->save();
  758. }
  759. return $ret;
  760. }
  761. /**
  762. * The QBF form builder.
  763. *
  764. * @param array $form_state
  765. * @param string $form_id
  766. * The original form to transform
  767. * @param string $qbf_form_id
  768. * The QBF form itself
  769. * @param strign $save_type
  770. * Name of search type, to use when saving.
  771. */
  772. function qbf_form($form_state, $form_id, $qbf_form_id, $save_type)
  773. {
  774. // dsm(array("QBF_Form" => "Base = [$form_id], QBF ID = [$qbf_form_id]", 'save_type' => $save_type));
  775. // dsm($form_state);
  776. // Fetch the basic form and rename it, passing it the previous values
  777. $node = new stdClass();
  778. $form = $form_id($node, $form_state);
  779. $qbf_form = array();
  780. $qbf_form['#qbf_source_form_id'] = $form_id;
  781. // On the form element itselfn only keep the QBF properties and the children
  782. if (in_array('#qbf', element_properties($form)))
  783. {
  784. $qbf_form += $form['#qbf'];
  785. }
  786. foreach (element_children($form) as $key)
  787. {
  788. // dsm("Transforming $key, type " . $form[$key]['#type']);
  789. $new_element = _qbf_transform_element($key, $form[$key], $form_state);
  790. if (!is_null($new_element))
  791. {
  792. $qbf_form[$key] = $new_element;
  793. }
  794. }
  795. $qbf_form['#id'] = $qbf_form_id;
  796. // $qbf_form['#after_build'][] = 'qbf_after_build';
  797. // $qbf_form['#validate'][] = 'qbf_form_validate';
  798. $qbf_form['#submit'] = array('qbf_form_submit');
  799. // dsm($qbf_form);
  800. $qbf_form['qbf'] = array
  801. (
  802. '#type' => 'fieldset',
  803. '#title' => t('Query'),
  804. );
  805. if (isset($form_state['values']) && !empty($form_state['values']))
  806. {
  807. dsm(array("QBF form: we must restore these values" => $form_state['values']));
  808. if (isset($form_state['qbf_results']))
  809. {
  810. $qbf_form['qbf']['qbf_results'] = array
  811. (
  812. '#type' => 'markup',
  813. '#prefix' => '<p>',
  814. '#value' => $form_state['qbf_results'],
  815. '#suffix' => '</p>',
  816. );
  817. }
  818. }
  819. $qbf_form['qbf']['qbf_save_type'] = array
  820. (
  821. '#type' => 'hidden',
  822. '#value' => $save_type,
  823. );
  824. $qbf_form['qbf']['qbf_save_name'] = array
  825. (
  826. '#title' => t('Name of query in your save list'),
  827. '#type' => 'textfield',
  828. '#required' => TRUE,
  829. '#default_value' => substr($form_id, 0, -5) . ' ' . format_date(time(), 'large'),
  830. );
  831. $qbf_form['qbf']['qbf_save'] = array
  832. (
  833. '#submit' => array('qbf_form_save_submit'),
  834. '#validate' => array('qbf_form_save_validate'),
  835. '#type' => 'submit',
  836. '#value' => t('Save query'),
  837. '#weight' => 5,
  838. );
  839. $qbf_form['qbf']['qbf_perform'] = array
  840. (
  841. '#submit' => array('qbf_form_perform_submit'),
  842. // '#validate' => array('qbf_form_perform_validate'),
  843. '#type' => 'submit',
  844. '#value' => t('Perform query'),
  845. '#weight' => 4,
  846. );
  847. return $qbf_form;
  848. }
  849. /**
  850. * Submit handler for qbf_form, Perform search button.
  851. *
  852. * @param array $form
  853. * @param array $form_state
  854. */
  855. function qbf_form_perform_submit($form, &$form_state)
  856. {
  857. dsm('PERF SUB');
  858. //dsm($form);
  859. dsm($form_state);
  860. $form_state['qbf_results'] = "Les résultats sont là";
  861. $form_state['rebuild'] = TRUE;
  862. }
  863. /**
  864. * Validate handler for qbf_form, Perform search button.
  865. *
  866. * @param array $form
  867. * @param array $form_state
  868. */
  869. function qbf_form_perform_validate($form, &$form_state)
  870. {
  871. dsm('PERF VAL');
  872. //dsm($form);
  873. dsm($form_state);
  874. }
  875. function qbf_form_submit($form, &$form_state)
  876. {
  877. dsm('FORM SUB');
  878. //dsm($form);
  879. dsm($form_state);
  880. }
  881. function qbf_form_validate($form, &$form_state)
  882. {
  883. dsm('FORM VAL');
  884. //dsm($form);
  885. dsm($form_state);
  886. }