qbf.module 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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.10 2009-03-22 11:26:54 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. * @ingroup vars
  78. */
  79. define('QBF_VAR_NOTIFY_DELETE', 'qbf_notify_delete');
  80. /**
  81. * Name of the profile category under which the list of saved queries will be
  82. * displayed.
  83. *
  84. * @ingroup vars
  85. *
  86. * @see qbf_admin_settings(), qbf_profile_alter()
  87. */
  88. define('QBF_VAR_PROFILE_CATEGORY', 'qbf_profile_category');
  89. /**
  90. * Notify owner about saved query deletions, default value.
  91. *
  92. * @ingroup vars
  93. */
  94. define('QBF_DEF_NOTIFY_DELETE', FALSE);
  95. /**
  96. * Default value for the profile category
  97. *
  98. * @ingroup vars
  99. *
  100. * See QBF_VAR_PROFILE_CATEGORY
  101. */
  102. define('QBF_DEF_PROFILE_CATEGORY', 'Saved queries');
  103. /**
  104. * A class wrapper for saved QBF queries
  105. */
  106. class Qbf_Query
  107. {
  108. public $qid;
  109. public $uid;
  110. public $name;
  111. public $type;
  112. public $query;
  113. public $created;
  114. public $updated;
  115. /**
  116. * Constructor
  117. *
  118. * @param string $name
  119. * @param array $ar_values
  120. * @return void
  121. */
  122. public function __construct($type, $name = NULL, $ar_values = NULL)
  123. {
  124. global $user;
  125. $this->qid = 0; // will be autoset by the DB serial
  126. $this->uid = $user->uid;
  127. $this->type = $type;
  128. $this->name = $name;
  129. if (!empty($ar_values))
  130. {
  131. $this->query = serialize($ar_values);
  132. }
  133. $this->created = $this->updated = time();
  134. }
  135. /**
  136. * Save a named query to the DB, erasing previous homonym queries is any exists.
  137. *
  138. * @return int
  139. */
  140. public function save()
  141. {
  142. // Avoid duplicates
  143. if (!empty($this->name))
  144. {
  145. $sq = "DELETE FROM {%s} WHERE name = '%s' AND uid = '%d' ";
  146. db_query($sq, QBF_TABLE_NAME, $this->name, $this->uid);
  147. // $n = db_affected_rows(); // Know how many homonym queries we deleted
  148. }
  149. $ret = drupal_write_record(QBF_TABLE_NAME, $this); // no update param: we just deleted the previous version
  150. if ($ret) // has to be SAVED_NEW, by construction
  151. {
  152. $ret = $this->qid; // from serial
  153. }
  154. return $ret;
  155. }
  156. }
  157. /**
  158. * Recursively build a query array from the form and its values
  159. *
  160. * In the current version, element names are supposed to be unique, even at
  161. * different levels in the tree.
  162. *
  163. * @ingroup forms
  164. * @param array $form
  165. * @param array $form_values
  166. */
  167. function _qbf_extract_query($element_id, $form, $form_values)
  168. {
  169. // Elements which are unnamed (form), removed, or display-only have no place in the query
  170. if (!empty($element_id) && array_key_exists('#qbf', $form) && array_key_exists('#level', $form['#qbf'])
  171. && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
  172. {
  173. $ret = array($element_id => $form_values[$element_id]);
  174. }
  175. else
  176. {
  177. $ret = array();
  178. }
  179. // QBF level is not inherited, so this loop is outside the "if" above
  180. foreach (element_children($form) as $child_name)
  181. {
  182. $ret += _qbf_extract_query($child_name, $form[$child_name], $form_values);
  183. }
  184. return $ret;
  185. }
  186. /**
  187. * Submit handler for qbf_form, Perform search button.
  188. *
  189. * @param array $form
  190. * @param array $form_state
  191. */
  192. function _qbf_form_perform_submit($form, &$form_state)
  193. {
  194. // dsm($form);
  195. // dsm($form_state);
  196. $callback = $form_state['values']['qbf_query'];
  197. if (function_exists(($callback)))
  198. {
  199. $ar_query = _qbf_extract_query(NULL, $form, $form_state['values']);
  200. $form_state['qbf_results'] = $callback($ar_query);
  201. }
  202. $form_state['rebuild'] = TRUE;
  203. }
  204. /**
  205. * Validate handler for qbf_form, Perform search button.
  206. *
  207. * @param array $form
  208. * @param array $form_state
  209. */
  210. function _qbf_form_perform_validate($form, &$form_state)
  211. {
  212. // @todo validate searches: checkboxes sets needs at least one value checked, otherwise there won't be any result
  213. }
  214. /**
  215. * Submit handler for qbf_form, Save search button.
  216. *
  217. * @param array $form
  218. * @param array $form_state
  219. * @return integer
  220. * The id of the saved query.
  221. */
  222. function _qbf_form_save_submit($form, &$form_state)
  223. {
  224. $qid = _qbf_save($form_state['values']['form_id'], $form_state);
  225. drupal_set_message(t('Your query was saved as "@name".',
  226. array('@name' => $form_state['values']['qbf_save_name'])));
  227. global $user;
  228. $form_state['redirect'] = "user/$user->uid/edit/qbf";
  229. return $qid;
  230. }
  231. /**
  232. * Validate handler for qbf_form, Save search button.
  233. *
  234. * @param array $form
  235. * @param array $form_state
  236. */
  237. function _qbf_form_save_validate($form, &$form_state)
  238. {
  239. // @todo validate saves. Check whether any validation is necessary.
  240. }
  241. /**
  242. * Delete a query by qid
  243. *
  244. * In the qbf/<qid>/delete case, $query has been tested for validity and access
  245. * in qbf_query_load(), so it is safe and accessible.
  246. *
  247. * Outside this context, the function can also be invoken with just a qid, and
  248. * the same check via qbf_query_load() will be performed.
  249. *
  250. * @param mixed $query
  251. * int or object
  252. */
  253. function _qbf_query_delete($query)
  254. {
  255. global $user;
  256. if (is_int($query))
  257. {
  258. $query = qbf_query_load($query);
  259. }
  260. if ($query) // access already checked in explicit or implicit qbf_query_load
  261. {
  262. $qid = $query->qid;
  263. $sq = 'DELETE FROM %s WHERE qid = %d ';
  264. $q = db_query($sq, QBF_TABLE_NAME, $qid);
  265. $message = t('Query @id "@name" has been deleted.', array
  266. (
  267. '@id' => $qid,
  268. '@name' => $query->name,
  269. ));
  270. drupal_set_message($message, 'status');
  271. $link = l($qid, QBF_PATH_MAIN .'/'. $qid .'/delete');
  272. $notify = variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE);
  273. watchdog('qbf', $message, NULL, WATCHDOG_NOTICE, $link);
  274. // access check: we only send the message to the query owner, so access is
  275. // granted without an additional check
  276. if ($notify /* && $query->uid != $user->uid */)
  277. {
  278. $owner = user_load(array('uid' => $query->uid));
  279. $language = user_preferred_language($account);
  280. $params = array
  281. (
  282. 'query' => $query,
  283. 'owner' => $owner, // unused by default, but can be used in a hook_mail_alter() implementation
  284. 'deletor' => $user,
  285. 'language' => $language,
  286. );
  287. $ret = drupal_mail('qbf', __FUNCTION__, $user->mail, $language, $params, $user->mail);
  288. drupal_set_message(t('User !link has been informed', array
  289. (
  290. '!link' => l($account->name, 'user/'. $query->uid),
  291. )));
  292. // dsm(array("QQD, ret" => $ret));
  293. }
  294. }
  295. else {
  296. $message = t('Failed attempt to delete query @qid. Administrator has been alerted.', array
  297. (
  298. '@qid' => $qid,
  299. ));
  300. drupal_set_message($message, 'error');
  301. watchdog('qbf', $message, NULL, WATCHDOG_ERROR, $link);
  302. }
  303. drupal_goto();
  304. }
  305. /**
  306. * Main query page.
  307. *
  308. * This returns the query form if a valid query id or query type is specified,
  309. * or the list of available query types if several exisit, or jumps to the single
  310. * available query type if only one exists.
  311. *
  312. * @param object $query
  313. * Valid query, loaded by qbf_query_load().
  314. * @return string
  315. */
  316. function _qbf_query_form($query = NULL )
  317. {
  318. if (!empty($query))
  319. {
  320. $qbf_form_id = 'qbf_' . $query->type;
  321. $ret = drupal_get_form($qbf_form_id, $query);
  322. }
  323. else
  324. {
  325. $ar_forms = qbf_forms();
  326. $arRet = array();
  327. foreach ($ar_forms as $qbf_form_id => $form_info)
  328. {
  329. $form_id = $form_info['callback arguments'][0]['form'];
  330. $arRet[QBF_PATH_MAIN . "/$form_id"] = l($form_info['callback arguments'][0]['label'],
  331. QBF_PATH_MAIN . "/$form_id");
  332. }
  333. // If there is only one form type, no need to ask the user.
  334. if (count($arRet) == 1)
  335. {
  336. reset($arRet);
  337. drupal_goto(key($arRet));
  338. }
  339. else
  340. {
  341. $ret = theme('item_list', $arRet, t('Choose a query type'));
  342. }
  343. }
  344. return $ret;
  345. }
  346. /**
  347. * Save a query and return its qid.
  348. *
  349. * This is not a hook_save() implementation, hence the "_".
  350. *
  351. * @ingroup forms
  352. *
  353. * @param $form_id string
  354. * @param $form_state array
  355. * @return int
  356. */
  357. function _qbf_save($form_id, $form_state)
  358. {
  359. if (user_is_anonymous())
  360. {
  361. $warning = t('Attempt by anonymous user to save a QBF query. Should not happen.');
  362. drupal_set_message($warning, 'error');
  363. watchdog('qbf', $warning, NULL, WATCHDOG_WARNING);
  364. $ret = 0;
  365. }
  366. else
  367. {
  368. // @FIXME check whether form_state is now needed. It wasn't in QBF for D5
  369. $form = drupal_retrieve_form($form_id, $form_state);
  370. // dsm($form, "retrieve");
  371. drupal_prepare_form($form_id, $form, $form_state);
  372. // dsm($form, "prepare");
  373. $name = $form_state['values']['qbf_save_name'];
  374. $type = $form_state['values']['qbf_save_type'];
  375. // dsm($form_state);
  376. $form_values = _qbf_extract_query(NULL, $form, $form_state['values']);
  377. // dsm($form_values);
  378. $ar_values = array();
  379. foreach ($form_values as $key => $value)
  380. {
  381. if (empty($value))
  382. {
  383. continue;
  384. }
  385. $ar_values[$key] = $value;
  386. }
  387. $query = new Qbf_Query($type, $name, $ar_values);
  388. $ret = $query->save();
  389. }
  390. return $ret;
  391. }
  392. /**
  393. * Transform a form element for QBF.
  394. *
  395. * QBF-specific properties are:
  396. * - #qbf : array of properties
  397. * - #level: only within #qbf
  398. *
  399. * See QBF_* constants
  400. *
  401. * @ingroup forms
  402. * @param string $key
  403. * @param array $element
  404. * @return void
  405. */
  406. function _qbf_transform($key, $element, $form_state, $query)
  407. {
  408. // dsm(array('key' => $key, 'element' => $element));
  409. /**
  410. * List default type transformations applied to widget by FAPI.
  411. * Types without a default transformation are not transformed
  412. */
  413. static $ar_default_type_transformations = array
  414. (
  415. 'button' => NULL, // no content
  416. 'file' => NULL, // non-querable (yet ?)
  417. 'image_button' => NULL, // new in D6
  418. 'markup' => NULL, // no content
  419. 'password' => NULL, // forbidden
  420. 'radio' => NULL, // single radio is useless, unlike a set of them
  421. 'submit' => NULL, // no content
  422. 'textarea' => 'textfield', // reduce text for searches
  423. // Don't transform these:
  424. // 'checkbox' => NULL,
  425. // 'checkboxes' => NULL,
  426. // 'date' => NULL,
  427. // 'fieldset' => NULL, // useful visually
  428. // 'form' => NULL, // removing it would delete the whole shebang
  429. // 'hidden' => NULL, // non-querable visually, but may be useful
  430. // 'item' => NULL,
  431. // 'radios' => NULL,
  432. // 'select' => NULL,
  433. // 'textfield' => NULL,
  434. // 'value' => 'value',
  435. // 'weight' => NULL,
  436. );
  437. /**
  438. * List default property transformations applied to widget by FAPI property.
  439. *
  440. * Properties without a default transformation are not transformed
  441. */
  442. static $ar_default_property_transformations = array
  443. (
  444. // Standard properties
  445. '#action' => NULL,
  446. '#after_build' => NULL,
  447. // '#base' => NULL, // gone in D6
  448. '#button_type' => NULL,
  449. '#built' => NULL,
  450. '#description' => NULL,
  451. '#method' => NULL,
  452. '#parents' => NULL,
  453. '#redirect' => NULL,
  454. '#ref' => NULL,
  455. '#required' => NULL,
  456. '#rows' => NULL,
  457. '#submit' => NULL,
  458. '#tree' => NULL,
  459. '#validate' => NULL,
  460. );
  461. /**
  462. * List properties causing causing element removal.
  463. *
  464. * The key is the property name, the value is the one causing removal.
  465. */
  466. static $ar_killer_properties = array
  467. (
  468. '#disabled' => TRUE,
  469. );
  470. // Transform type
  471. $source_type = $element['#type'];
  472. // .. Default transformation
  473. $dest_type = array_key_exists($source_type, $ar_default_type_transformations)
  474. ? $ar_default_type_transformations[$source_type]
  475. : $source_type;
  476. // .. Apply form-defined type override
  477. if (isset($element['#qbf']['#type']))
  478. {
  479. $dest_type = $element['#qbf']['#type'];
  480. }
  481. if (is_null($dest_type))
  482. {
  483. $ret = NULL;
  484. }
  485. else
  486. {
  487. $ret = $element;
  488. $ret['#type'] = $dest_type;
  489. if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
  490. {
  491. $ret = NULL;
  492. }
  493. else
  494. {
  495. foreach (element_properties($element) as $property_name)
  496. {
  497. // Apply killer properties first to avoid useless work
  498. if (array_key_exists($property_name, $ar_killer_properties)
  499. && ($element[$property_name] = $ar_killer_properties[$property_name]))
  500. {
  501. $ret = NULL;
  502. break;
  503. }
  504. // Now transform or copy remaining properties
  505. if (array_key_exists($property_name, $ar_default_property_transformations))
  506. {
  507. $ret[$property_name] = $ar_default_property_transformations[$property_name];
  508. }
  509. else
  510. {
  511. $ret[$property_name] = $element[$property_name];
  512. }
  513. // And apply form-defined property overrides
  514. if ($property_name == '#qbf')
  515. {
  516. foreach ($element[$property_name] as $override_name => $override_value)
  517. {
  518. $ret[$override_name] = $override_value;
  519. }
  520. }
  521. }
  522. if (isset($form_state['values'][$key]))
  523. {
  524. $ret['#default_value'] = $form_state['values'][$key];
  525. }
  526. elseif (isset($query->query[$key]))
  527. {
  528. $ret['#default_value'] = $query->query[$key];
  529. }
  530. // Recursively transform children
  531. foreach (element_children($element) as $child_name)
  532. {
  533. $child = _qbf_transform($child_name, $element[$child_name], $form_state, $query);
  534. if (is_null($child))
  535. {
  536. unset($ret[$child_name]);
  537. }
  538. else
  539. {
  540. $ret[$child_name] = $child;
  541. }
  542. }
  543. }
  544. }
  545. //dsm(array('key' => $key, 'transformed element' => $ret));
  546. return $ret;
  547. }
  548. /**
  549. * Implement the former hook_settings().
  550. *
  551. * @return array
  552. */
  553. function qbf_admin_settings()
  554. {
  555. $form = array();
  556. $form[QBF_VAR_NOTIFY_DELETE] = array
  557. (
  558. '#type' => 'checkbox',
  559. '#default_value' => variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE),
  560. '#title' => t('Notify users when one of their saved searches has been deleted'),
  561. );
  562. return system_settings_form($form);
  563. }
  564. /**
  565. * The QBF form builder.
  566. *
  567. * @param array $form_state
  568. * @param array $query_info
  569. * The query structure array
  570. * @param string $qbf_form_id
  571. * The name of the QBF form
  572. * @param string $query
  573. * The saved query.
  574. */
  575. function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
  576. {
  577. $form_id = $query_info['form'];
  578. // Fetch the basic form and rename it, passing it the previous values
  579. $node = new stdClass();
  580. $form = $form_id($node, $form_state);
  581. $qbf_form = array();
  582. $qbf_form['#qbf_source_form_id'] = $form_id;
  583. // On the form element itself, only keep the QBF properties and the children
  584. if (in_array('#qbf', element_properties($form)))
  585. {
  586. $qbf_form += $form['#qbf'];
  587. }
  588. foreach (element_children($form) as $key)
  589. {
  590. $new_element = _qbf_transform($key, $form[$key], $form_state, $query);
  591. if (!is_null($new_element))
  592. {
  593. $qbf_form[$key] = $new_element;
  594. }
  595. }
  596. $qbf_form['#id'] = $qbf_form_id;
  597. $qbf_form['qbf'] = array
  598. (
  599. '#type' => 'fieldset',
  600. '#title' => t('Query'),
  601. );
  602. if (isset($form_state['values']) && !empty($form_state['values']))
  603. {
  604. if (isset($form_state['qbf_results']))
  605. {
  606. $qbf_form['qbf']['qbf_results'] = array
  607. (
  608. '#type' => 'markup',
  609. '#prefix' => '<p>',
  610. '#value' => $form_state['qbf_results'],
  611. '#suffix' => '</p>',
  612. );
  613. }
  614. }
  615. $qbf_form['qbf']['qbf_save_type'] = array
  616. (
  617. '#type' => 'hidden',
  618. '#value' => $query_info['form'],
  619. );
  620. $qbf_form['qbf']['qbf_query'] = array
  621. (
  622. '#type' => 'hidden',
  623. '#value' => $query_info['callback'],
  624. );
  625. $qbf_form['qbf']['qbf_save_name'] = array
  626. (
  627. '#title' => t('Name of query in your save list'),
  628. '#type' => 'textfield',
  629. '#required' => TRUE,
  630. '#default_value' => empty($query->name)
  631. ? t('@label - @time', array('@label' => $query_info['label'], '@time' => format_date(time(), 'large')))
  632. : $query->name,
  633. );
  634. $qbf_form['qbf']['qbf_perform'] = array
  635. (
  636. '#submit' => array('_qbf_form_perform_submit'),
  637. '#validate' => array('_qbf_form_perform_validate'),
  638. '#type' => 'submit',
  639. '#value' => t('Perform query'),
  640. );
  641. $qbf_form['qbf']['qbf_save'] = array
  642. (
  643. '#submit' => array('_qbf_form_save_submit'),
  644. '#validate' => array('_qbf_form_save_validate'),
  645. '#type' => 'submit',
  646. '#value' => t('Save query'),
  647. );
  648. return $qbf_form;
  649. }
  650. /**
  651. * Implement hook_forms().
  652. *
  653. * @link http://drupal.org/node/144132#hook-forms @endlink
  654. *
  655. * hook_qbf_register() returns an array of QBF-able node types, indexed by the
  656. * node type, with the following properties:
  657. * - form: the name of the hook_form() implementation (a $form_id)
  658. * - label: the human-readable type name under which the queries are saved by QBF
  659. * - callback: the function QBF must invoke to query the node type. It will
  660. * receive the query type and a filtered version of $form_state['values']
  661. * containing only valid node fields, and must return a themed grid of query
  662. * results, which will be displayed as a #markup FAPI element. In advanced
  663. * uses, a single callback can be used for several query types by using the
  664. * query type parameter to know what the values apply to.
  665. *
  666. * @ingroup forms
  667. * @ingroup hooks
  668. *
  669. * @param array $args
  670. * @return array
  671. */
  672. function qbf_forms($args = NULL)
  673. {
  674. static $forms = array();
  675. if (empty($forms))
  676. {
  677. $hook_name = 'qbf_register';
  678. // dsm(array("QBF_forms $qbf_form_id" => $args));
  679. // More efficient than using module_invoke_all: we avoid array-merging + re-looping
  680. foreach (module_implements($hook_name) as $module)
  681. {
  682. $arImplementations = module_invoke($module, $hook_name);
  683. // dsm($arImplementations);
  684. foreach ($arImplementations as /* $node_type => */ $query_info)
  685. {
  686. $qbf_form_id = 'qbf_' . $query_info['form'];
  687. $forms[$qbf_form_id] = array
  688. (
  689. 'callback' => 'qbf_form',
  690. 'callback arguments' => array($query_info, $qbf_form_id),
  691. );
  692. } // foreach implementation
  693. } // foreach module
  694. } // if empty
  695. return $forms;
  696. }
  697. /**
  698. * List queries owned by a given user.
  699. *
  700. * @param int $uid > 0
  701. * @return array
  702. */
  703. function qbf_get_queries_by_user($uid = NULL)
  704. {
  705. if (is_null($uid))
  706. {
  707. global $user;
  708. $uid = $user->uid;
  709. }
  710. $sq = 'SELECT qq.qid, qq.uid, qq.name, qq.query, qq.updated '
  711. . 'FROM {%s} qq '
  712. . 'WHERE qq.uid = %d '
  713. . 'ORDER BY qq.name ';
  714. // no db_rewrite_sql: this function is not in a menu callback, so it is up to
  715. // the caller to check access
  716. $q = db_query($sq, QBF_TABLE_NAME, $uid);
  717. $ret = array();
  718. while ($o = db_fetch_object($q))
  719. {
  720. $ret[$o->qid] = $o; // qid is the PK, so it is present and unique
  721. }
  722. return $ret;
  723. }
  724. /**
  725. * Implement hook_mail().
  726. *
  727. * @param string $key
  728. * @param array $message
  729. * @param array $params
  730. * @return void
  731. */
  732. function qbf_mail($key, &$message, $params)
  733. {
  734. // dsm(array('QBF_mail key' => $key, 'message' => $message, 'params' => $params));
  735. $deletor_tokens = user_mail_tokens($params['deletor'], $params['language']->language);
  736. $tokens = array_merge($deletor_tokens, array
  737. (
  738. '!qname' => $params['query']->name,
  739. '!qid' => $params['query']->qid,
  740. ));
  741. $message['subject'] = t('Effacement d\'une recherche !site enregistrée', $tokens);
  742. $message['body'] = t("!date\n\nVotre recherche !qid: !qname\nsur le site !site vient d'être effacée par !username.", $tokens);
  743. }
  744. /**
  745. * Implement hook_menu().
  746. *
  747. * @return array
  748. */
  749. function qbf_menu()
  750. {
  751. $items = array();
  752. $items[QBF_PATH_SETTINGS] = array
  753. (
  754. 'title' => 'Query-By-Form',
  755. 'access arguments' => array(QBF_PERM_ADMIN),
  756. 'page callback' => 'drupal_get_form',
  757. 'page arguments' => array('qbf_admin_settings'),
  758. 'type' => MENU_NORMAL_ITEM,
  759. );
  760. $items[QBF_PATH_MAIN] = array
  761. (
  762. 'type' => MENU_CALLBACK,
  763. 'access arguments' => array(QBF_PERM_QUERY),
  764. 'page callback' => '_qbf_query_form',
  765. );
  766. $items[QBF_PATH_MAIN . '/%qbf_query'] = array
  767. (
  768. 'type' => MENU_CALLBACK,
  769. 'access arguments' => array(QBF_PERM_QUERY),
  770. 'page callback' => '_qbf_query_form',
  771. 'page arguments' => array(1),
  772. );
  773. $items[QBF_PATH_MAIN . '/%qbf_query/delete'] = array
  774. (
  775. 'type' => MENU_CALLBACK,
  776. 'access arguments' => array(QBF_PERM_QUERY),
  777. 'page callback' => '_qbf_query_delete',
  778. 'page arguments' => array(1),
  779. );
  780. return $items;
  781. }
  782. /**
  783. * Implement hook_perm().
  784. *
  785. * @todo D7: Format will change
  786. * @see http://drupal.org/node/224333#descriptions-permissions
  787. *
  788. * @ingroup hooks
  789. * @return array
  790. */
  791. function qbf_perm()
  792. {
  793. $ret = array
  794. (
  795. QBF_PERM_ADMIN,
  796. QBF_PERM_QUERY,
  797. );
  798. return $ret;
  799. }
  800. /**
  801. * Load a saved QBF query, or an empty query by type
  802. *
  803. * @link http://drupal.org/node/109153#load @endlink
  804. *
  805. * @param int $us_qid
  806. * @return array
  807. * A form_values array
  808. */
  809. function qbf_query_load($us_qid)
  810. {
  811. static $query = NULL;
  812. // Only allow query loading by logged-in users
  813. if (user_is_anonymous())
  814. {
  815. return FALSE;
  816. }
  817. // Filter out visibly invalid values
  818. $qid = (is_numeric($us_qid) && ($us_qid > 0))
  819. ? $us_qid
  820. : 0;
  821. // If this is not a saved query, it may be a QBF query type
  822. if ($qid === 0)
  823. {
  824. $ar_forms = qbf_forms();
  825. foreach ($ar_forms as $qbf_form_id => $form_info)
  826. {
  827. if ($us_qid === $form_info['callback arguments'][0]['form'])
  828. {
  829. $query = new Qbf_Query($us_qid);
  830. break;
  831. }
  832. }
  833. }
  834. if (is_null($query) && $qid)
  835. {
  836. $sq = 'SELECT qq.qid, qq.uid, qq.type, qq.name, qq.query '
  837. . 'FROM {%s} qq '
  838. . 'WHERE qq.qid = %d ';
  839. // db_rewrite_sql does not apply here: access control is further down
  840. $q = db_query($sq, QBF_TABLE_NAME, $qid);
  841. $query = db_fetch_object($q); // 0 or 1 row: we are querying on the primary key
  842. if ($query !== FALSE)
  843. {
  844. $query->query = unserialize($query->query);
  845. // dsm($query);
  846. }
  847. }
  848. global $user;
  849. $ret = (isset($query) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
  850. ? $query
  851. : FALSE;
  852. return $ret;
  853. }
  854. /**
  855. * Provide an optional automatic mapping mechanism for query building.
  856. *
  857. * This function takes a partly built query map $ar_queryMap, and a defaults
  858. * array to complete it in $ar_defaults, and returns a fully built query array
  859. * ready to be used for querying.
  860. *
  861. * @param array $ar_query_map
  862. * @param array $ar_defaults
  863. * @return array
  864. */
  865. function qbf_query_mapper($ar_query_map = array(), $ar_defaults = array())
  866. {
  867. $ret = array();
  868. foreach ($ar_query_map as $name => $value)
  869. {
  870. // accept NULL, empty strings...
  871. if (!is_array($value))
  872. {
  873. $value = array();
  874. }
  875. $item = $value;
  876. foreach ($ar_defaults as $default_key => $default_value)
  877. {
  878. if (!array_key_exists($default_key, $item))
  879. {
  880. $item[$default_key] = is_null($default_value)
  881. ? $name
  882. : $default_value;
  883. }
  884. // else if is already in $item, so we don't touch it
  885. }
  886. $ret[$name] = $item;
  887. }
  888. return $ret;
  889. }
  890. error_reporting($_qbf_er);
  891. function qbf___________________________________________________(){}
  892. // ======== D6 LIMIT ==================================================================================================
  893. /**
  894. * Implement hook_profile_alter().
  895. *
  896. * Add saved queries to the profile rendering
  897. *
  898. * @ingroup hooks
  899. * @param object $account
  900. * @return void
  901. */
  902. function qbf_profile_alter(&$account)
  903. {
  904. // @TODO D6 use $account->content instead of $fields: check whether this is sufficient
  905. global $user;
  906. // Only allow field to QBF admins and own user
  907. if ($user->uid != $account->uid && !user_access(QBF_PERM_ADMIN))
  908. {
  909. return;
  910. }
  911. $ar_queries = qbf_get_queries_by_user($account->uid);
  912. $count = count($ar_queries);
  913. // @todo Create New link needs to use hook_qbf_register info
  914. $new_query_link = l(t('Create new query'), JOB_PATH_MAIN);
  915. $none_message = ($account->uid == $user->uid)
  916. ? t('None yet. !newQuery', array('!newQuery' => $new_query_link))
  917. : t('None yet.');
  918. $saved = ($count > 0)
  919. ? format_plural($count, 'One saved query. ', '@count saved queries. ')
  920. . l(t('View/edit'), "user/$account->uid/edit/qbf")
  921. : $none_message;
  922. // @todo Support per-application/per-node profile categories
  923. $cat_name = variable_get(QBF_VAR_PROFILE_CATEGORY, QBF_DEF_PROFILE_CATEGORY);
  924. /*
  925. * Due to the way profile.module works, existing categories are never empty,
  926. * so any existing category name points to a non-empty array
  927. */
  928. if (!array_key_exists($cat_name, $account->content))
  929. {
  930. $account->content[$cat_name] = array();
  931. }
  932. // @FIXME: breaks drupal_render()
  933. // $account->content[$cat_name] += array
  934. // (
  935. // 'job-saved' => array
  936. // (
  937. // 'title' => t('Saved queries'),
  938. // 'value' => $saved,
  939. // 'class' => 'job-saved-queries',
  940. // ),
  941. // );
  942. // dsm(array("jpa fields" => $account->content));
  943. }