123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?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.5 2008-08-27 10:45:50 marand Exp $
- * @license CeCILL 2.0
- * @package QBF
- */
- define('QBF_LEVEL_REMOVE', 0);
- define('QBF_LEVEL_DISPLAY', 1); // Display only, don't use
- define('QBF_LEVEL_OPTIONAL', 2);
- define('QBF_LEVEL_REQUIRED', 3);
- define('QBF_PATH_QUERY', 'qbf/query');
- define('QBF_PERM_QUERY', 'use qbf search functions');
- /**
- * Experiment in creating a new "hook_qbf_rewrite_form()".
- *
- * @todo check whether this is redundant with hook_form_alter()
- *
- * @param array $form
- * @return array
- */
- 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();
- //dsm(array('qtf' => $arArgs));
- // 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();
- $newForm['#qbf_source_form_id'] = $form_id;
- 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;
- $newForm['#multistep'] = TRUE;
- $newForm['#redirect'] = FALSE;
- $newForm['#after_build'][] = 'qbf_after_build';
- // 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' => 'value',
- );
- // 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,
- );
- // 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 form-defined 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 property overrides
- if ($propertyName == '#qbf')
- {
- foreach ($element[$propertyName] as $overrideName => $overrideValue)
- {
- $ret[$overrideName] = $overrideValue;
- }
- }
- }
- // Recursively transform children
- 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;
- }
- /**
- * Implement hook_perm().
- *
- * @return array
- */
- function qbf_perm()
- {
- $ret = array
- (
- QBF_PERM_QUERY,
- );
- return $ret;
- }
- /**
- * Implement hook_forms().
- *
- * @todo dynamically build the list of forms
- *
- * @return array
- */
- function qbf_forms()
- {
- $forms['qbf_job_form'] = array
- (
- 'callback' => 'qbf_transform_form',
- 'callback arguments' => array('job_form'),
- );
- return $forms;
- }
- /**
- * Insert the query results at the bottom of the query form.
- *
- * @param array $form
- * @param array $form_values
- * @return array
- */
- function qbf_after_build($form, $form_values)
- {
- $arQuery = _qbf_extract_query($form, $form_values);
- $form['food'] = array
- (
- '#type' => 'markup',
- '#value' => "<p>" . dvr($arQuery, true) . "</p>",
- );
- return $form;
- }
- /**
- * Recursively build a query array from the form and its values
- *
- * In the current version, element names are supposed to be unique, even at
- * different levels in the tree.
- *
- * @param array $form
- * @param array $form_values
- */
- function _qbf_extract_query($form, $form_values)
- {
- $name = $form['#parents'][0];
- // Elements which are removed or display-only have no place in the query
- if (array_key_exists('#qbf', $form) && array_key_exists('#level', $form['#qbf'])
- && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
- {
- $ret = array($name => $form_values[$name]);
- }
- else
- {
- $ret = array();
- }
- // QBF level is not inherited, so this loop is outside the "if" above
- foreach (element_children($form) as $childName)
- {
- $ret += _qbf_extract_query($form[$childName], $form_values);
- }
- return $ret;
- }
|