qbf.module 26 KB

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