qbf.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Query By Form
  4. *
  5. * This module allows node modules to add a query by form tab for their node
  6. * types to the default search form
  7. *
  8. * @copyright 2008 Ouest Systemes Informatiques (OSInet)
  9. * @author Frederic G. MARAND
  10. * @version $Id: qbf.module,v 1.5 2008-08-27 10:45:50 marand Exp $
  11. * @license CeCILL 2.0
  12. * @package QBF
  13. */
  14. define('QBF_LEVEL_REMOVE', 0);
  15. define('QBF_LEVEL_DISPLAY', 1); // Display only, don't use
  16. define('QBF_LEVEL_OPTIONAL', 2);
  17. define('QBF_LEVEL_REQUIRED', 3);
  18. define('QBF_PATH_QUERY', 'qbf/query');
  19. define('QBF_PERM_QUERY', 'use qbf search functions');
  20. /**
  21. * Experiment in creating a new "hook_qbf_rewrite_form()".
  22. *
  23. * @todo check whether this is redundant with hook_form_alter()
  24. *
  25. * @param array $form
  26. * @return array
  27. */
  28. function qbf_rewrite_form($form)
  29. {
  30. $hookName = 'qbf_rewrite_form';
  31. foreach (module_implements($hookName) as $module)
  32. {
  33. $function = "${module}_$hookName";
  34. $form = $function($form);
  35. }
  36. return drupal_get_form($form);
  37. }
  38. /**
  39. * Transform a form array for QBF.
  40. *
  41. * This function obtains the form array using Forms API, and transforms it by
  42. * modifying widgets to other types where needed.
  43. *
  44. * Any additional parameter passed to the function is transmitted to the form
  45. * generating function.
  46. *
  47. * @param string $form_id
  48. * @return array
  49. */
  50. function qbf_transform_form($form_id)
  51. {
  52. $arArgs = func_get_args();
  53. //dsm(array('qtf' => $arArgs));
  54. // Fetch the basic form and rename it, passing it the caller's arguments
  55. $form = call_user_func_array('drupal_retrieve_form', $arArgs);
  56. $newFormId = "qbf_$form_id";
  57. // Only keep the children of the form and QBF properties on the form itself
  58. $elements = array();
  59. $newForm = array();
  60. $newForm['#qbf_source_form_id'] = $form_id;
  61. if (in_array('#qbf', element_properties($form)))
  62. {
  63. $newForm += $form['#qbf'];
  64. }
  65. foreach (element_children($form) as $key)
  66. {
  67. // dsm("Transforming $key, type " . $form[$key]['#type']);
  68. $newElement = _qbf_transform_element($key, $form[$key]);
  69. if (!is_null($newElement))
  70. {
  71. $newForm[$key] = $newElement;
  72. }
  73. }
  74. $newForm['#id'] = $newFormId;
  75. $newForm['#multistep'] = TRUE;
  76. $newForm['#redirect'] = FALSE;
  77. $newForm['#after_build'][] = 'qbf_after_build';
  78. // dsm($newForm);
  79. return $newForm;
  80. }
  81. /**
  82. * Transform a form element for QBF.
  83. *
  84. * QBF-specific properties are:
  85. * - #qbf : array of properties
  86. * - #level: only within #qbf @see QBF_* constants
  87. *
  88. * @param array &$element
  89. * @return void
  90. */
  91. function _qbf_transform_element($key, array $element)
  92. {
  93. // dsm(array('key' => $key, 'element' => $element));
  94. // Types without a default transformation are not transformed
  95. static $arDefaultTypeTransformations = array
  96. (
  97. 'button' => NULL,
  98. 'file' => NULL,
  99. // 'hidden' => NULL,
  100. 'markup' => NULL,
  101. 'password' => NULL,
  102. 'radio' => NULL,
  103. 'submit' => NULL,
  104. 'textarea' => 'textfield',
  105. // 'value' => 'value',
  106. );
  107. // Properties without a default transformation are not transformed
  108. static $arDefaultPropertyTransformations = array
  109. (
  110. // Standard properties
  111. '#action' => NULL,
  112. '#after_build' => NULL,
  113. '#base' => NULL,
  114. '#button_type' => NULL,
  115. '#built' => NULL,
  116. '#description' => NULL,
  117. '#method' => NULL,
  118. '#parents' => NULL,
  119. '#redirect' => NULL,
  120. '#ref' => NULL,
  121. '#required' => NULL,
  122. '#rows' => NULL,
  123. '#submit' => NULL,
  124. '#tree' => NULL,
  125. '#validate' => NULL,
  126. );
  127. // Property values causing element removal
  128. static $arKillerProperties = array
  129. (
  130. '#disabled' => TRUE,
  131. );
  132. // Transform type
  133. $sourceType = $element['#type'];
  134. // .. Default transformation
  135. $destType = array_key_exists($sourceType, $arDefaultTypeTransformations)
  136. ? $arDefaultTypeTransformations[$sourceType]
  137. : $sourceType;
  138. // .. Apply form-defined type override
  139. if (isset($element['#qbf']['#type']))
  140. {
  141. $destType = $element['#qbf']['#type'];
  142. }
  143. if (is_null($destType))
  144. {
  145. $ret = NULL;
  146. }
  147. else
  148. {
  149. $ret = $element;
  150. $ret['#type'] = $destType;
  151. if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
  152. {
  153. $ret = NULL;
  154. }
  155. else
  156. {
  157. foreach (element_properties($element) as $propertyName)
  158. {
  159. // Apply killer properties first to avoid useless work
  160. if (array_key_exists($propertyName, $arKillerProperties)
  161. && ($element[$propertyName] = $arKillerProperties[$propertyName]))
  162. {
  163. $ret = NULL;
  164. break;
  165. }
  166. // Now transform or copy remaining properties
  167. if (array_key_exists($propertyName, $arDefaultPropertyTransformations))
  168. {
  169. $ret[$propertyName] = $arDefaultPropertyTransformations[$propertyName];
  170. }
  171. else
  172. {
  173. $ret[$propertyName] = $element[$propertyName];
  174. }
  175. // And apply form-defined property overrides
  176. if ($propertyName == '#qbf')
  177. {
  178. foreach ($element[$propertyName] as $overrideName => $overrideValue)
  179. {
  180. $ret[$overrideName] = $overrideValue;
  181. }
  182. }
  183. }
  184. // Recursively transform children
  185. foreach (element_children($element) as $childName)
  186. {
  187. $child = _qbf_transform_element($childName, $element[$childName]);
  188. if (is_null($child))
  189. {
  190. unset($ret[$childName]);
  191. }
  192. else
  193. {
  194. $ret[$childName] = $child;
  195. }
  196. }
  197. }
  198. }
  199. //dsm(array('key' => $key, 'transformed element' => $ret));
  200. return $ret;
  201. }
  202. /**
  203. * Implement hook_perm().
  204. *
  205. * @return array
  206. */
  207. function qbf_perm()
  208. {
  209. $ret = array
  210. (
  211. QBF_PERM_QUERY,
  212. );
  213. return $ret;
  214. }
  215. /**
  216. * Implement hook_forms().
  217. *
  218. * @todo dynamically build the list of forms
  219. *
  220. * @return array
  221. */
  222. function qbf_forms()
  223. {
  224. $forms['qbf_job_form'] = array
  225. (
  226. 'callback' => 'qbf_transform_form',
  227. 'callback arguments' => array('job_form'),
  228. );
  229. return $forms;
  230. }
  231. /**
  232. * Insert the query results at the bottom of the query form.
  233. *
  234. * @param array $form
  235. * @param array $form_values
  236. * @return array
  237. */
  238. function qbf_after_build($form, $form_values)
  239. {
  240. $arQuery = _qbf_extract_query($form, $form_values);
  241. $form['food'] = array
  242. (
  243. '#type' => 'markup',
  244. '#value' => "<p>" . dvr($arQuery, true) . "</p>",
  245. );
  246. return $form;
  247. }
  248. /**
  249. * Recursively build a query array from the form and its values
  250. *
  251. * In the current version, element names are supposed to be unique, even at
  252. * different levels in the tree.
  253. *
  254. * @param array $form
  255. * @param array $form_values
  256. */
  257. function _qbf_extract_query($form, $form_values)
  258. {
  259. $name = $form['#parents'][0];
  260. // Elements which are removed or display-only have no place in the query
  261. if (array_key_exists('#qbf', $form) && array_key_exists('#level', $form['#qbf'])
  262. && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
  263. {
  264. $ret = array($name => $form_values[$name]);
  265. }
  266. else
  267. {
  268. $ret = array();
  269. }
  270. // QBF level is not inherited, so this loop is outside the "if" above
  271. foreach (element_children($form) as $childName)
  272. {
  273. $ret += _qbf_extract_query($form[$childName], $form_values);
  274. }
  275. return $ret;
  276. }