qbf.module 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.3 2008-08-25 17:40:53 marand Exp $
  11. * @license CeCILL 2.0
  12. * @package QBF
  13. */
  14. define('QBF_LEVEL_REMOVE', 0);
  15. define('QBF_LEVEL_OPTIONAL', 1);
  16. define('QBF_LEVEL_REQUIRED', 2);
  17. function _qbf_view($usNodeType = NULL)
  18. {
  19. $nodeType = filter_xss($usNodeType, array());
  20. $arNodeTypes = node_get_types();
  21. //dsm($arNodeTypes);
  22. $arFuncs1 = get_defined_functions();
  23. $arFuncs1 = $arFuncs1['user'];
  24. $node = new stdClass();
  25. $node->type = 'poll';
  26. $arFuncs1 = node_forms();
  27. //dsm($arFuncs1);
  28. $form = node_form($node, array('qbe-key' => 'qbe-value'));
  29. //dsm($form);
  30. return 'foo';
  31. }
  32. function qbf_rewrite_form($form)
  33. {
  34. $hookName = 'qbf_rewrite_form';
  35. foreach (module_implements($hookName) as $module)
  36. {
  37. $function = "${module}_$hookName";
  38. $form = $function($form);
  39. }
  40. return drupal_get_form($form);
  41. }
  42. /**
  43. * Transform a form array for QBF
  44. *
  45. * This function obtains the form array using Forms API, and transforms it by
  46. * modifying widgets to other types where needed.
  47. *
  48. * Any additional parameter passed to the function is transmitted to the form
  49. * generating function.
  50. *
  51. * @param string $form_id
  52. * @return array
  53. */
  54. function qbf_transform_form($form_id)
  55. {
  56. $arArgs = func_get_args();
  57. // Fetch the basic form and rename it, passing it the caller's arguments
  58. $form = call_user_func_array('drupal_retrieve_form', $arArgs);
  59. $newFormId = "qbf_$form_id";
  60. // Only keep the children of the form and QBF properties on the form itself
  61. $elements = array();
  62. $newForm = array();
  63. if (in_array('#qbf', element_properties($form)))
  64. {
  65. $newForm += $form['#qbf'];
  66. }
  67. foreach (element_children($form) as $key)
  68. {
  69. // dsm("Transforming $key, type " . $form[$key]['#type']);
  70. $newElement = _qbf_transform_element($key, $form[$key]);
  71. if (!is_null($newElement))
  72. {
  73. $newForm[$key] = $newElement;
  74. }
  75. }
  76. $newForm['#id'] = $newFormId;
  77. // dsm($newForm);
  78. return $newForm;
  79. }
  80. /**
  81. * Transform a form element for QBF
  82. *
  83. * QBF-specific properties are:
  84. * - #qbf : array of properties
  85. * - #level: only within #qbf @see QBF_* constants
  86. *
  87. * @param array &$element
  88. * @return void
  89. */
  90. function _qbf_transform_element($key, array $element)
  91. {
  92. // dsm(array('key' => $key, 'element' => $element));
  93. // Types without a default transformation are not transformed
  94. static $arDefaultTypeTransformations = array
  95. (
  96. 'button' => NULL,
  97. 'file' => NULL,
  98. 'hidden' => NULL,
  99. 'markup' => NULL,
  100. 'password' => NULL,
  101. 'radio' => NULL,
  102. 'submit' => NULL,
  103. 'textarea' => 'textfield',
  104. 'value' => NULL,
  105. );
  106. // Properties without a default transformation are not transformed
  107. static $arDefaultPropertyTransformations = array
  108. (
  109. // Standard properties
  110. '#action' => NULL,
  111. '#after_build' => NULL,
  112. '#base' => NULL,
  113. '#button_type' => NULL,
  114. '#built' => NULL,
  115. '#description' => NULL,
  116. '#method' => NULL,
  117. '#parents' => NULL,
  118. '#redirect' => NULL,
  119. '#ref' => NULL,
  120. '#required' => NULL,
  121. '#rows' => NULL,
  122. '#submit' => NULL,
  123. '#tree' => NULL,
  124. '#validate' => NULL,
  125. '#value' => 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 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) && ($element[$propertyName] = $arKillerProperties[$propertyName]))
  161. {
  162. $ret = NULL;
  163. break;
  164. }
  165. // Now transform or copy remaining properties
  166. if (array_key_exists($propertyName, $arDefaultPropertyTransformations))
  167. {
  168. $ret[$propertyName] = $arDefaultPropertyTransformations[$propertyName];
  169. }
  170. else
  171. {
  172. $ret[$propertyName] = $element[$propertyName];
  173. }
  174. // And apply form-defined overrides
  175. if ($propertyName == '#qbf')
  176. {
  177. foreach ($element[$propertyName] as $overrideName => $overrideValue)
  178. {
  179. $ret[$overrideName] = $overrideValue;
  180. }
  181. }
  182. }
  183. foreach (element_children($element) as $childName)
  184. {
  185. $child = _qbf_transform_element($childName, $element[$childName]);
  186. if (is_null($child))
  187. {
  188. unset($ret[$childName]);
  189. }
  190. else
  191. {
  192. $ret[$childName] = $child;
  193. }
  194. }
  195. }
  196. }
  197. //dsm(array('key' => $key, 'transformed element' => $ret));
  198. return $ret;
  199. }
  200. function qbf_search()
  201. {
  202. dsm('qs');
  203. }
  204. function qbf_perm()
  205. {
  206. $ret = array('use qbf search functions');
  207. return $ret;
  208. }
  209. function qbf_menu($may_cache)
  210. {
  211. $items = array();
  212. if ($may_cache)
  213. {
  214. $items[] = array
  215. (
  216. 'path' => 'qbf/query',
  217. 'access' => user_access('use qbf search functions'),
  218. 'callback' => 'qbf_query',
  219. );
  220. }
  221. return $items;
  222. }
  223. function qbf_query($form_id)
  224. {
  225. dsm("qq $form_id");
  226. return "QQ $form_id";
  227. }