123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- /**
- * Query By Form
- *
- * This module allows node modules to add a query by form tab for their node
- * types to the default search form
- *
- * @copyright 2008 Ouest Systemes Informatiques (OSInet)
- * @author Frederic G. MARAND
- * @version $Id: qbf.module,v 1.3 2008-08-25 17:40:53 marand Exp $
- * @license CeCILL 2.0
- * @package QBF
- */
- define('QBF_LEVEL_REMOVE', 0);
- define('QBF_LEVEL_OPTIONAL', 1);
- define('QBF_LEVEL_REQUIRED', 2);
- function _qbf_view($usNodeType = NULL)
- {
- $nodeType = filter_xss($usNodeType, array());
- $arNodeTypes = node_get_types();
- //dsm($arNodeTypes);
- $arFuncs1 = get_defined_functions();
- $arFuncs1 = $arFuncs1['user'];
- $node = new stdClass();
- $node->type = 'poll';
- $arFuncs1 = node_forms();
- //dsm($arFuncs1);
- $form = node_form($node, array('qbe-key' => 'qbe-value'));
- //dsm($form);
- return 'foo';
- }
- function qbf_rewrite_form($form)
- {
- $hookName = 'qbf_rewrite_form';
- foreach (module_implements($hookName) as $module)
- {
- $function = "${module}_$hookName";
- $form = $function($form);
- }
- return drupal_get_form($form);
- }
- /**
- * Transform a form array for QBF
- *
- * This function obtains the form array using Forms API, and transforms it by
- * modifying widgets to other types where needed.
- *
- * Any additional parameter passed to the function is transmitted to the form
- * generating function.
- *
- * @param string $form_id
- * @return array
- */
- function qbf_transform_form($form_id)
- {
- $arArgs = func_get_args();
- // Fetch the basic form and rename it, passing it the caller's arguments
- $form = call_user_func_array('drupal_retrieve_form', $arArgs);
- $newFormId = "qbf_$form_id";
- // Only keep the children of the form and QBF properties on the form itself
- $elements = array();
- $newForm = array();
- if (in_array('#qbf', element_properties($form)))
- {
- $newForm += $form['#qbf'];
- }
- foreach (element_children($form) as $key)
- {
- // dsm("Transforming $key, type " . $form[$key]['#type']);
- $newElement = _qbf_transform_element($key, $form[$key]);
- if (!is_null($newElement))
- {
- $newForm[$key] = $newElement;
- }
- }
- $newForm['#id'] = $newFormId;
- // dsm($newForm);
- return $newForm;
- }
- /**
- * Transform a form element for QBF
- *
- * QBF-specific properties are:
- * - #qbf : array of properties
- * - #level: only within #qbf @see QBF_* constants
- *
- * @param array &$element
- * @return void
- */
- function _qbf_transform_element($key, array $element)
- {
- // dsm(array('key' => $key, 'element' => $element));
- // Types without a default transformation are not transformed
- static $arDefaultTypeTransformations = array
- (
- 'button' => NULL,
- 'file' => NULL,
- 'hidden' => NULL,
- 'markup' => NULL,
- 'password' => NULL,
- 'radio' => NULL,
- 'submit' => NULL,
- 'textarea' => 'textfield',
- 'value' => NULL,
- );
- // Properties without a default transformation are not transformed
- static $arDefaultPropertyTransformations = array
- (
- // Standard properties
- '#action' => NULL,
- '#after_build' => NULL,
- '#base' => NULL,
- '#button_type' => NULL,
- '#built' => NULL,
- '#description' => NULL,
- '#method' => NULL,
- '#parents' => NULL,
- '#redirect' => NULL,
- '#ref' => NULL,
- '#required' => NULL,
- '#rows' => NULL,
- '#submit' => NULL,
- '#tree' => NULL,
- '#validate' => NULL,
- '#value' => NULL,
- );
- // Property values causing element removal
- static $arKillerProperties = array
- (
- '#disabled' => TRUE,
- );
- // Transform type
- $sourceType = $element['#type'];
- // .. Default transformation
- $destType = array_key_exists($sourceType, $arDefaultTypeTransformations)
- ? $arDefaultTypeTransformations[$sourceType]
- : $sourceType;
- // .. Apply type override
- if (isset($element['#qbf']['#type']))
- {
- $destType = $element['#qbf']['#type'];
- }
- if (is_null($destType))
- {
- $ret = NULL;
- }
- else
- {
- $ret = $element;
- $ret['#type'] = $destType;
- if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
- {
- $ret = NULL;
- }
- else
- {
- foreach (element_properties($element) as $propertyName)
- {
- // Apply killer properties first to avoid useless work
- if (array_key_exists($propertyName, $arKillerProperties) && ($element[$propertyName] = $arKillerProperties[$propertyName]))
- {
- $ret = NULL;
- break;
- }
- // Now transform or copy remaining properties
- if (array_key_exists($propertyName, $arDefaultPropertyTransformations))
- {
- $ret[$propertyName] = $arDefaultPropertyTransformations[$propertyName];
- }
- else
- {
- $ret[$propertyName] = $element[$propertyName];
- }
- // And apply form-defined overrides
- if ($propertyName == '#qbf')
- {
- foreach ($element[$propertyName] as $overrideName => $overrideValue)
- {
- $ret[$overrideName] = $overrideValue;
- }
- }
- }
- foreach (element_children($element) as $childName)
- {
- $child = _qbf_transform_element($childName, $element[$childName]);
- if (is_null($child))
- {
- unset($ret[$childName]);
- }
- else
- {
- $ret[$childName] = $child;
- }
- }
- }
- }
- //dsm(array('key' => $key, 'transformed element' => $ret));
- return $ret;
- }
- function qbf_search()
- {
- dsm('qs');
- }
- function qbf_perm()
- {
- $ret = array('use qbf search functions');
- return $ret;
- }
- function qbf_menu($may_cache)
- {
- $items = array();
- if ($may_cache)
- {
- $items[] = array
- (
- 'path' => 'qbf/query',
- 'access' => user_access('use qbf search functions'),
- 'callback' => 'qbf_query',
- );
- }
- return $items;
- }
- function qbf_query($form_id)
- {
- dsm("qq $form_id");
- return "QQ $form_id";
- }
|