qbf.module 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.1 2008-08-22 16:58:01 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. * The $arHooks array contains hooks to be invoked, indexed by hook name, with
  49. * their arguments
  50. *
  51. * @param string $form_id
  52. * @param array $arHooks
  53. * @return array
  54. */
  55. function _qbf_transform_form($form_id, $arHooks = array())
  56. {
  57. // Fetch the basic form and rename it
  58. $form = drupal_retrieve_form($form_id, NULL);
  59. $newFormId = "qbf_$form_id";
  60. // Invoke the optional hooks extending it
  61. foreach ($arHooks as $hookName => $arHookArguments)
  62. {
  63. dsm("Firing $hookName");
  64. foreach (module_implements($hookName) as $module)
  65. {
  66. $function = $module .'_'. $hookName;
  67. switch ($hookName)
  68. {
  69. case 'form':
  70. // @todo FIXME needs node with type property set
  71. // $formAdditions = node_invoke(new stdClass(), $hookName, $arHookArguments);
  72. break;
  73. default:
  74. $formAdditions = module_invoke_all($hookName, $arHookArguments);
  75. break;
  76. }
  77. // dsm(array($function => $formAdditions));
  78. }
  79. if (is_array($formAdditions))
  80. {
  81. $form = array_merge_recursive($form, $formAdditions);
  82. }
  83. }
  84. // Only keep the children of the form
  85. $elements = array();
  86. $newForm = array();
  87. $form['mark'] = array('#type' => 'textarea', '#value' => 'goo');
  88. foreach (element_children($form) as $key)
  89. {
  90. //dsm("Transforming $key, type " . $form[$key]['#type']);
  91. $newElement = _qbf_transform_element($key, $form[$key]);
  92. if (!is_null($newElement))
  93. {
  94. $newForm[$key] = $newElement;
  95. }
  96. }
  97. $newForm['#id'] = $newFormId;
  98. //dsm($newForm);
  99. return $newForm;
  100. }
  101. /**
  102. * Transform a form element for QBF
  103. *
  104. * QBF-specific properties are:
  105. * - #qbf_level : @see QBF_* constants
  106. *
  107. * @param array &$element
  108. * @return void
  109. */
  110. function _qbf_transform_element($key, array $element)
  111. {
  112. //dsm(array('key' => $key, 'element' => $element));
  113. // Types without a default transformation are not transformed
  114. static $arDefaultTypeTransformations = array
  115. (
  116. 'button' => NULL,
  117. 'file' => NULL,
  118. 'hidden' => NULL,
  119. 'markup' => NULL,
  120. 'password' => NULL,
  121. 'radio' => NULL,
  122. 'submit' => NULL,
  123. 'textarea' => 'textfield',
  124. 'value' => NULL,
  125. );
  126. // Properties without a default transformation are not transformed
  127. static $arDefaultPropertyTransformations = array
  128. (
  129. // Standard properties
  130. '#action' => NULL,
  131. '#after_build' => NULL,
  132. '#base' => NULL,
  133. '#button_type' => NULL,
  134. '#built' => NULL,
  135. '#description' => NULL,
  136. '#method' => NULL,
  137. '#parents' => NULL,
  138. '#redirect' => NULL,
  139. '#ref' => NULL,
  140. '#required' => NULL,
  141. '#rows' => NULL,
  142. '#submit' => NULL,
  143. '#tree' => NULL,
  144. '#validate' => NULL,
  145. '#value' => NULL,
  146. );
  147. // Property values causing element removal
  148. static $arKillerProperties = array
  149. (
  150. '#disabled' => TRUE,
  151. );
  152. // Transform type
  153. $sourceType = $element['#type'];
  154. $destType = array_key_exists($sourceType, $arDefaultTypeTransformations)
  155. ? $arDefaultTypeTransformations[$sourceType]
  156. : $sourceType;
  157. if (is_null($destType))
  158. {
  159. $ret = NULL;
  160. }
  161. else
  162. {
  163. $ret = $element;
  164. $ret['#type'] = $destType;
  165. if (!array_key_exists('#qbf_level', $element) || $element['#qbf_level'] == QBF_LEVEL_REMOVE)
  166. {
  167. $ret = NULL;
  168. }
  169. else
  170. {
  171. foreach (element_properties($element) as $propertyName)
  172. {
  173. // Apply killer properties first to avoid useless work
  174. if (array_key_exists($propertyName, $arKillerProperties) && ($element[$propertyName] = $arKillerProperties[$propertyName]))
  175. {
  176. $ret = NULL;
  177. break;
  178. }
  179. // Now transform or copy remaining properties
  180. if (array_key_exists($propertyName, $arDefaultPropertyTransformations))
  181. {
  182. $ret[$propertyName] = $arDefaultPropertyTransformations[$propertyName];
  183. }
  184. else
  185. {
  186. $ret[$propertyName] = $element[$propertyName];
  187. }
  188. }
  189. foreach (element_children($element) as $childName)
  190. {
  191. $child = _qbf_transform_element($childName, $element[$childName]);
  192. if (!is_null($child))
  193. {
  194. $ret[$childName] = $child;
  195. }
  196. }
  197. }
  198. }
  199. //dsm(array('key' => $key, 'transformed element' => $ret));
  200. return $ret;
  201. }