qbf.module 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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.3 2009-03-17 18:48:53 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. class Qbf_Query
  83. {
  84. public $qid;
  85. public $uid;
  86. public $name;
  87. public $query;
  88. public $created;
  89. public $updated;
  90. /**
  91. * Constructor
  92. *
  93. * @param string $name
  94. * @param array $ar_values
  95. * @return void
  96. */
  97. public function __construct($name, $ar_values)
  98. {
  99. global $user;
  100. $this->qid = 0; // will be autoset by the DB serial
  101. $this->uid = $user->uid;
  102. $this->name = $name;
  103. $this->query = serialize($ar_values);
  104. $this->created = $this->updated = time();
  105. }
  106. /**
  107. * Save a named query to the DB, erasing previous homonym queries is any exists.
  108. *
  109. * @return int
  110. */
  111. public function save()
  112. {
  113. // Avoid duplicates
  114. if (!empty($this->name))
  115. {
  116. $sq = "DELETE FROM {%s} WHERE name = '%s'";
  117. db_query($sq, QBF_TABLE_NAME, $name);
  118. }
  119. $ret = drupal_write_record(QBF_TABLE_NAME, $this); // no update param: we just deleted the previous version
  120. dsm($this);
  121. if ($ret) // has to be SAVED_NEW, by construction
  122. {
  123. $ret = $this->qid; // from serial
  124. }
  125. return $ret;
  126. }
  127. }
  128. /**
  129. * Recursively build a query array from the form and its values
  130. *
  131. * In the current version, element names are supposed to be unique, even at
  132. * different levels in the tree.
  133. *
  134. * @ingroup forms
  135. * @param array $form
  136. * @param array $form_values
  137. */
  138. function _qbf_extract_query($form, $form_values)
  139. {
  140. $name = $form['#parents'][0];
  141. // Elements which are removed or display-only have no place in the query
  142. if (array_key_exists('#qbf', $form) && array_key_exists('#level', $form['#qbf'])
  143. && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
  144. {
  145. $ret = array($name => $form_values[$name]);
  146. }
  147. else
  148. {
  149. $ret = array();
  150. }
  151. // QBF level is not inherited, so this loop is outside the "if" above
  152. foreach (element_children($form) as $child_name)
  153. {
  154. $ret += _qbf_extract_query($form[$child_name], $form_values);
  155. }
  156. return $ret;
  157. }
  158. /**
  159. * Delete a query by qid
  160. *
  161. * In the qbf/<qid>/delete case, $query has been tested for validity and access
  162. * in qbf_query_load(), so it is safe and accessible.
  163. *
  164. * Outside this context, the function can also be invoken with just a qid, and
  165. * the same check via qbf_query_load() will be performed.
  166. *
  167. * @param mixed $query
  168. * int or object
  169. */
  170. function _qbf_query_delete($query)
  171. {
  172. global $user;
  173. if (is_int($query))
  174. {
  175. $query = qbf_query_load($query);
  176. }
  177. if ($query) // access already checked in explicit or implicit qbf_query_load
  178. {
  179. $qid = $query->qid;
  180. $sq = 'DELETE FROM %s WHERE qid = %d ';
  181. $q = db_query($sq, QBF_TABLE_NAME, $qid);
  182. $message = t('Query @id "@name" has been deleted.', array
  183. (
  184. '@id' => $qid,
  185. '@name' => $query->name,
  186. ));
  187. drupal_set_message($message, 'status');
  188. $link = l($qid, QBF_PATH_MAIN .'/'. $qid .'/delete');
  189. $notify = variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE);
  190. watchdog('qbf', $message, NULL, WATCHDOG_NOTICE, $link);
  191. // access check: we only send the message to the query owner, so access is
  192. // granted without an additional check
  193. if ($notify /* && $query->uid != $user->uid */)
  194. {
  195. $owner = user_load(array('uid' => $query->uid));
  196. $language = user_preferred_language($account);
  197. $params = array
  198. (
  199. 'query' => $query,
  200. 'owner' => $owner, // unused by default, but can be used in a hook_mail_alter() implementation
  201. 'deletor' => $user,
  202. 'language' => $language,
  203. );
  204. $ret = drupal_mail('qbf', __FUNCTION__, $user->mail, $language, $params, $user->mail);
  205. drupal_set_message(t('User !link has been informed', array
  206. (
  207. '!link' => l($account->name, 'user/'. $query->uid),
  208. )));
  209. // dsm(array("QQD, ret" => $ret));
  210. }
  211. }
  212. else {
  213. $message = t('Failed attempt to delete query @qid. Administrator has been alerted.', array
  214. (
  215. '@qid' => $qid,
  216. ));
  217. drupal_set_message($message, 'error');
  218. watchdog('qbf', $message, NULL, WATCHDOG_ERROR, $link);
  219. }
  220. drupal_goto();
  221. }
  222. /**
  223. * Transform a form element for QBF.
  224. *
  225. * QBF-specific properties are:
  226. * - #qbf : array of properties
  227. * - #level: only within #qbf
  228. *
  229. * See QBF_* constants
  230. *
  231. * @ingroup forms
  232. * @param string $key
  233. * @param array $element
  234. * @return void
  235. */
  236. function _qbf_transform_element($key, $element) {
  237. // dsm(array('key' => $key, 'element' => $element));
  238. /**
  239. * List default type transformations applied to widget by FAPI.
  240. *
  241. * Types without a default transformation are not transformed
  242. */
  243. static $ar_default_type_transformations = array
  244. (
  245. 'button' => NULL, // no content
  246. 'file' => NULL, // non-querable (yet ?)
  247. 'image_button' => NULL, // new in D6
  248. 'markup' => NULL, // no content
  249. 'password' => NULL, // forbidden
  250. 'radio' => NULL, // single radio is useless, unlike a set of them
  251. 'submit' => NULL, // no content
  252. 'textarea' => 'textfield', // reduce text for searches
  253. // Don't transform these:
  254. // 'checkbox' => NULL,
  255. // 'checkboxes' => NULL,
  256. // 'date' => NULL,
  257. // 'fieldset' => NULL, // useful visually
  258. // 'form' => NULL, // removing it would delete the whole shebang
  259. // 'hidden' => NULL, // non-querable visually, but may be useful
  260. // 'item' => NULL,
  261. // 'radios' => NULL,
  262. // 'select' => NULL,
  263. // 'textfield' => NULL,
  264. // 'value' => 'value',
  265. // 'weight' => NULL,
  266. );
  267. /**
  268. * List default property transformations applied to widget by FAPI property.
  269. *
  270. * Properties without a default transformation are not transformed
  271. */
  272. static $ar_default_property_transformations = array
  273. (
  274. // Standard properties
  275. '#action' => NULL,
  276. '#after_build' => NULL,
  277. // '#base' => NULL, // gone in D6
  278. '#button_type' => NULL,
  279. '#built' => NULL,
  280. '#description' => NULL,
  281. '#method' => NULL,
  282. '#parents' => NULL,
  283. '#redirect' => NULL,
  284. '#ref' => NULL,
  285. '#required' => NULL,
  286. '#rows' => NULL,
  287. '#submit' => NULL,
  288. '#tree' => NULL,
  289. '#validate' => NULL,
  290. );
  291. /**
  292. * List properties causing causing element removal.
  293. *
  294. * The key is the property name, the value is the one causing removal.
  295. */
  296. static $ar_killer_properties = array
  297. (
  298. '#disabled' => TRUE,
  299. );
  300. // Transform type
  301. $source_type = $element['#type'];
  302. // .. Default transformation
  303. $dest_type = array_key_exists($source_type, $ar_default_type_transformations)
  304. ? $ar_default_type_transformations[$source_type]
  305. : $source_type;
  306. // .. Apply form-defined type override
  307. if (isset($element['#qbf']['#type']))
  308. {
  309. $dest_type = $element['#qbf']['#type'];
  310. }
  311. if (is_null($dest_type))
  312. {
  313. $ret = NULL;
  314. }
  315. else
  316. {
  317. $ret = $element;
  318. $ret['#type'] = $dest_type;
  319. if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
  320. {
  321. $ret = NULL;
  322. }
  323. else
  324. {
  325. foreach (element_properties($element) as $property_name)
  326. {
  327. // Apply killer properties first to avoid useless work
  328. if (array_key_exists($property_name, $ar_killer_properties)
  329. && ($element[$property_name] = $ar_killer_properties[$property_name]))
  330. {
  331. $ret = NULL;
  332. break;
  333. }
  334. // Now transform or copy remaining properties
  335. if (array_key_exists($property_name, $ar_default_property_transformations))
  336. {
  337. $ret[$property_name] = $ar_default_property_transformations[$property_name];
  338. }
  339. else
  340. {
  341. $ret[$property_name] = $element[$property_name];
  342. }
  343. // And apply form-defined property overrides
  344. if ($property_name == '#qbf')
  345. {
  346. foreach ($element[$property_name] as $override_name => $override_value)
  347. {
  348. $ret[$override_name] = $override_value;
  349. }
  350. }
  351. }
  352. // Recursively transform children
  353. foreach (element_children($element) as $child_name)
  354. {
  355. $child = _qbf_transform_element($child_name, $element[$child_name]);
  356. if (is_null($child))
  357. {
  358. unset($ret[$child_name]);
  359. }
  360. else
  361. {
  362. $ret[$child_name] = $child;
  363. }
  364. }
  365. }
  366. }
  367. //dsm(array('key' => $key, 'transformed element' => $ret));
  368. return $ret;
  369. }
  370. /**
  371. * Implement the former hook_settings().
  372. *
  373. * @return array
  374. */
  375. function qbf_admin_settings()
  376. {
  377. $form = array();
  378. $form[QBF_VAR_NOTIFY_DELETE] = array
  379. (
  380. '#type' => 'checkbox',
  381. '#default_value' => variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE),
  382. '#title' => t('Notify users when one of their saved searches has been deleted'),
  383. );
  384. return system_settings_form($form);
  385. }
  386. /**
  387. * Implement hook_forms().
  388. *
  389. * @ingroup forms
  390. * @ingroup hooks
  391. * @return array
  392. */
  393. function qbf_forms()
  394. {
  395. $hook_name = 'qbf_register';
  396. // More efficient than using module_invoke_all: we avoid array-merging + re-looping
  397. foreach (module_implements($hook_name) as $module)
  398. {
  399. foreach (module_invoke($module, $hook_name) as $form_name)
  400. {
  401. $forms["qbf_$form_name"] = array
  402. (
  403. 'callback' => 'qbf_transform_form',
  404. 'callback arguments' => array($form_name),
  405. );
  406. }
  407. }
  408. return $forms;
  409. }
  410. /**
  411. * List queries owned by a given user.
  412. *
  413. * @param int $uid > 0
  414. * @return array
  415. */
  416. function qbf_get_queries_by_user($uid = NULL)
  417. {
  418. if (is_null($uid))
  419. {
  420. global $user;
  421. $uid = $user->uid;
  422. }
  423. $sq = 'SELECT qq.qid, qq.uid, qq.name, qq.query, qq.updated '
  424. . 'FROM {%s} qq '
  425. . 'WHERE qq.uid = %d '
  426. . 'ORDER BY qq.name ';
  427. // no db_rewrite_sql: this function is not in a menu callback, so it is up to
  428. // the caller to check access
  429. $q = db_query($sq, QBF_TABLE_NAME, $uid);
  430. $ret = array();
  431. while ($o = db_fetch_object($q))
  432. {
  433. $ret[$o->qid] = $o; // qid is the PK, so it is present and unique
  434. }
  435. return $ret;
  436. }
  437. /**
  438. * Implement hook_menu().
  439. *
  440. * @return array
  441. */
  442. function qbf_menu()
  443. {
  444. $items = array();
  445. $items[QBF_PATH_SETTINGS] = array
  446. (
  447. 'title' => 'Query-By-Form',
  448. 'access arguments' => array(QBF_PERM_ADMIN),
  449. 'page callback' => 'drupal_get_form',
  450. 'page arguments' => array('qbf_admin_settings'),
  451. 'type' => MENU_NORMAL_ITEM,
  452. );
  453. $items[QBF_PATH_MAIN . '/%qbf_query/delete'] = array
  454. (
  455. 'type' => MENU_CALLBACK,
  456. 'access arguments' => array(QBF_PERM_QUERY),
  457. 'page callback' => '_qbf_query_delete',
  458. 'page arguments' => array(1),
  459. );
  460. return $items;
  461. }
  462. /**
  463. * Implement hook_mail().
  464. *
  465. * @param string $key
  466. * @param array $message
  467. * @param array $params
  468. * @return void
  469. */
  470. function qbf_mail($key, &$message, $params)
  471. {
  472. // dsm(array('QBF_mail key' => $key, 'message' => $message, 'params' => $params));
  473. $deletor_tokens = user_mail_tokens($params['deletor'], $params['language']->language);
  474. $tokens = array_merge($deletor_tokens, array
  475. (
  476. '!qname' => $params['query']->name,
  477. '!qid' => $params['query']->qid,
  478. ));
  479. $message['subject'] = t('Effacement d\'une recherche !site enregistrée', $tokens);
  480. $message['body'] = t("!date\n\nVotre recherche !qid: !qname\nsur le site !site vient d'être effacée par !username.", $tokens);
  481. }
  482. /**
  483. * Implement hook_perm().
  484. *
  485. * @todo D7: Format will change
  486. * @see http://drupal.org/node/224333#descriptions-permissions
  487. *
  488. * @ingroup hooks
  489. * @return array
  490. */
  491. function qbf_perm()
  492. {
  493. $ret = array
  494. (
  495. QBF_PERM_ADMIN,
  496. QBF_PERM_QUERY,
  497. );
  498. return $ret;
  499. }
  500. /**
  501. * Load a saved QBF query.
  502. *
  503. * @see qbf_import_values()
  504. * @link http://drupal.org/node/109153#load @endlink
  505. *
  506. * @param int $qid
  507. * @return array A form_values array usable by qbf_import_values
  508. */
  509. function qbf_query_load($qid)
  510. {
  511. static $query = NULL;
  512. if (is_null($query))
  513. {
  514. $sq = 'SELECT qq.qid, qq.uid, qq.query, qq.name '
  515. . 'FROM {%s} qq '
  516. . 'WHERE qq.qid = %d ';
  517. // db_rewrite_sql does not apply here: access control is further down
  518. $q = db_query($sq, QBF_TABLE_NAME, $qid);
  519. $query = db_fetch_object($q); // 0 or 1 row: we are querying on the primary key
  520. // FALSE does not happen: only NULL or a value can be here
  521. if ($query !== NULL)
  522. {
  523. $query->query = unserialize($query->query);
  524. //dsm($query);
  525. }
  526. }
  527. global $user;
  528. $ret = (isset($query) && isset($query->uid) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
  529. ? $query
  530. : FALSE;
  531. return $ret;
  532. }
  533. /**
  534. * Provide an optional automatic mapping mechanism for query building.
  535. *
  536. * This function takes a partly built query map $ar_queryMap, and a defaults
  537. * array to complete it in $ar_defaults, and returns a fully built query array
  538. * ready to be used for querying.
  539. *
  540. * @param array $ar_query_map
  541. * @param array $ar_defaults
  542. * @return array
  543. */
  544. function qbf_query_mapper($ar_query_map = array(), $ar_defaults = array())
  545. {
  546. $ret = array();
  547. foreach ($ar_query_map as $name => $value)
  548. {
  549. // accept NULL, empty strings...
  550. if (!is_array($value))
  551. {
  552. $value = array();
  553. }
  554. $item = $value;
  555. foreach ($ar_defaults as $default_key => $default_value)
  556. {
  557. if (!array_key_exists($default_key, $item))
  558. {
  559. $item[$default_key] = is_null($default_value)
  560. ? $name
  561. : $default_value;
  562. }
  563. // else if is already in $item, so we don't touch it
  564. }
  565. $ret[$name] = $item;
  566. }
  567. return $ret;
  568. }
  569. error_reporting($_qbf_er);
  570. // ======== D6 LIMIT ==================================================================================================
  571. /* TODO Node previews and adding form fields to the node form.
  572. There is a subtle but important difference in the way node previews (and other
  573. such operations) are carried out when adding or editing a node. With the new
  574. Forms API, the node form is handled as a multi-step form. When the node form
  575. is previewed, all the form values are submitted, and the form is rebuilt with
  576. those form values put into $form['#node']. Thus, form elements that are added
  577. to the node form will lose any user input unless they set their '#default_value'
  578. elements using this embedded node object. */
  579. /**
  580. * Transform a form array for QBF.
  581. *
  582. * This function obtains the form array using Forms API, and transforms it by
  583. * modifying widgets to other types where needed.
  584. *
  585. * Any additional parameter passed to the function is transmitted to the form
  586. * generating function.
  587. *
  588. * @ingroup forms
  589. * @param string $form_id
  590. * @return array
  591. */
  592. function qbf_transform_form($form_id) {
  593. // @todo GW: function qbf_transform_form(&$form_state, $form_id) {
  594. $ar_args = func_get_args();
  595. //dsm(array('qtf' => $ar_args));
  596. // Fetch the basic form and rename it, passing it the caller's arguments
  597. $form = call_user_func_array('drupal_retrieve_form', $ar_args);
  598. $new_form_id = "qbf_$form_id";
  599. // Only keep the children of the form and QBF properties on the form itself
  600. $elements = array();
  601. $new_form = array();
  602. $new_form['#qbf_source_form_id'] = $form_id;
  603. if (in_array('#qbf', element_properties($form))) {
  604. $new_form += $form['#qbf'];
  605. }
  606. foreach (element_children($form) as $key) {
  607. // dsm("Transforming $key, type " . $form[$key]['#type']);
  608. $new_element = _qbf_transform_element($key, $form[$key]);
  609. if (!is_null($new_element)) {
  610. $new_form[$key] = $new_element;
  611. }
  612. }
  613. $new_form['#id'] = $new_form_id;
  614. $new_form['#multistep'] = TRUE;
  615. // Do not set #redirect, even to FALSE (submit handlers)
  616. // $new_form['#redirect'] = FALSE;
  617. $new_form['#after_build'][] = 'qbf_after_build';
  618. $new_form['#submit'] = array('qbf_submit' => array());
  619. // dsm($new_form);
  620. return $new_form;
  621. }
  622. /**
  623. * Insert the query results at the bottom of the query form.
  624. *
  625. * @ingroup forms
  626. * @param array $form
  627. * @param array $form_values
  628. * @return array
  629. */
  630. function qbf_after_build($form, $form_values) {
  631. if (empty($form['#post'])) {
  632. return $form;
  633. }
  634. // If #post is not empty, we are indeed querying
  635. $ar_query = _qbf_extract_query($form, $form_values);
  636. /* This function is called at the end of the form building process, which
  637. * means that child properties of #qbf have already been upgraded to element
  638. * properties. So we look for $form['#callback'] and not
  639. * $form['#qbf']['#callback']
  640. */
  641. if (isset($form['#callback']) && function_exists($function = $form['#callback'])) {
  642. $results = $function($ar_query);
  643. }
  644. else {
  645. drupal_set_message(t('QBF: incorrect callback function for search'), 'error');
  646. }
  647. $form['qbf_query_results'] = array
  648. (
  649. '#type' => 'markup',
  650. '#value' => $results,
  651. '#weight' => 10,
  652. );
  653. return $form;
  654. }
  655. /**
  656. * Load a form_values array into a form used by QBF.
  657. *
  658. * This is typically useful when loading saved queries using qbf_query_load().
  659. * For other cases, the mechanisms built within FAPI should be used instead.
  660. *
  661. * @see qbf_query_load()
  662. *
  663. * @ingroup forms
  664. * @param array $form
  665. * @param array $form_values
  666. * @return array The modified form
  667. */
  668. function qbf_import_values($element, $form_values) {
  669. foreach (element_children($element) as $child_name) {
  670. if (!empty($form_values[$child_name])) {
  671. $element[$child_name]['#qbf']['#default_value'] = $form_values[$child_name];
  672. }
  673. $element[$child_name] = qbf_import_values($element[$child_name], $form_values);
  674. }
  675. return $element;
  676. }
  677. /**
  678. * Submit handler for query save form.
  679. *
  680. * @ingroup forms
  681. * @param $form_id string
  682. * @param $form_values array
  683. * @return string
  684. */
  685. function qbf_submit($form_id, $form_values) {
  686. /*
  687. function qbf_submit($form, &$form_state) {
  688. /* @TODO GW The 'op' element in the form values is deprecated.
  689. Each button can have #validate and #submit functions associated with it.
  690. Thus, there should be one button that submits the form and which invokes
  691. the normal form_id_validate and form_id_submit handlers. Any additional
  692. buttons which need to invoke different validate or submit functionality
  693. should have button-specific functions.
  694. switch ($form_state['values']['op']) { */
  695. switch ($form_values['op']) {
  696. case t('Search'):
  697. $ret = FALSE;
  698. break;
  699. case t('Save query'):
  700. _qbf_save($form_id, $form_values);
  701. //@TODO GW _qbf_save($form_state['values']['form_id'], $form_state['values']);
  702. drupal_set_message(t('Your query was saved as "@name".',
  703. array('@name' => $form_values['save-name'])));
  704. //@TODO GW _qbf_save($form_state['values']['form_id'], $form_state['values']);
  705. global $user;
  706. $ret = "user/$user->uid/edit/job";;
  707. break;
  708. }
  709. //dsm(array('QS' => $form_values));
  710. return $ret;
  711. /* @todo GW
  712. //dsm(array('QS' => $form_state['values']));
  713. $form_state['redirect'] = $ret;
  714. */
  715. }
  716. /**
  717. * Save a query and return its qid.
  718. *
  719. * @ingroup forms
  720. *
  721. * @param $form_id string
  722. * @param $form_state array
  723. * @return int
  724. */
  725. function _qbf_save($form_id, $form_state)
  726. {
  727. if (user_is_anonymous())
  728. {
  729. $warning = t('Attempt by anonymous user to save a QBF query. Should not happen.');
  730. drupal_set_message($warning, 'error');
  731. watchdog('qbf', $warning, NULL, WATCHDOG_WARNING);
  732. $ret = 0;
  733. }
  734. else
  735. {
  736. // @FIXME check whether form_state is now needed. It wasn't in QBF for D5
  737. $form = drupal_retrieve_form($form_id, $form_state);
  738. dsm($form, "retrieve");
  739. drupal_prepare_form($form_id, $form, $form_state);
  740. dsm($form, "prepare");
  741. $name = $form_state['post']['save-name'];
  742. $form_values = _qbf_extract_query($form, $form_state['post']);
  743. $ar_values = array();
  744. foreach ($form_values as $key => $value)
  745. {
  746. if (empty($value))
  747. {
  748. continue;
  749. }
  750. $ar_values[$key] = $value;
  751. }
  752. $query = new Qbf_Query($name, $ar_values);
  753. $ret = $query->save();
  754. }
  755. return $ret;
  756. }