1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003 |
- <?php
- /**
- * @file
- * 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-2009 Ouest Systemes Informatiques (OSInet)
- * @author Frederic G. MARAND
- * @license Licensed under the CeCILL 2.0 and the General Public Licence version 2 or later
- * @package QBF
- */
- // $Id: qbf.module,v 1.9.4.7 2009-03-21 19:41:34 marand Exp $
- /**
- * Saved error reporting level.
- *
- * QBF module is supposed to pass parsing at E_ALL|E_STRICT, but other modules
- * may not be so strict, so we save the level at the start of the module and
- * restore it at the end of the module.
- */
- global $_qbf_er;
- $_qbf_er = error_reporting(E_ALL | E_STRICT);
- /**
- * Remove this element from the generated form
- */
- define('QBF_LEVEL_REMOVE', 0);
- /**
- * This element is only for display in the generated form: do not include it
- * in the query vector.
- */
- define('QBF_LEVEL_DISPLAY', 1);
- /**
- * Include this element in the generated form and in the query vector, but do
- * not mark it as required.
- */
- define('QBF_LEVEL_OPTIONAL', 2);
- /**
- * Include this element in the generated form and in the query vector, and
- * mark it as required.
- */
- define('QBF_LEVEL_REQUIRED', 3);
- /**
- * The main QBF path.
- *
- * It MUST be a single component path, without a "/", otherwise qbf_menu() will
- * need to be changed.
- *
- * @ingroup paths
- * @see qbf_menu()
- */
- define('QBF_PATH_MAIN', 'qbf');
- /**
- * The QBF autocomplete path for search fields
- * @ingroup paths
- */
- define('QBF_PATH_AC', 'qbf/ac');
- /**
- * The path to the QBF settings page
- */
- define('QBF_PATH_SETTINGS', 'admin/settings/qbf');
- /**
- * Authorize use of QBF searches
- */
- define('QBF_PERM_QUERY', 'use QBF search functions');
- /**
- * Authorize QBF administration
- */
- define('QBF_PERM_ADMIN', 'administer QBF');
- /**
- * The name of the table used to store queries
- */
- define('QBF_TABLE_NAME', 'qbf_queries');
- /**
- * Notify owner about saved query deletions, variable name.
- */
- define('QBF_VAR_NOTIFY_DELETE', 'qbf_notify_delete');
- /**
- * Notify owner about saved query deletions, default value.
- */
- define('QBF_DEF_NOTIFY_DELETE', FALSE);
- /**
- * A class wrapper for saved QBF queries
- */
- class Qbf_Query
- {
- public $qid;
- public $uid;
- public $name;
- public $type;
- public $query;
- public $created;
- public $updated;
- /**
- * Constructor
- *
- * @param string $name
- * @param array $ar_values
- * @return void
- */
- public function __construct($type, $name, $ar_values)
- {
- global $user;
- $this->qid = 0; // will be autoset by the DB serial
- $this->uid = $user->uid;
- $this->type = $type;
- $this->name = $name;
- $this->query = serialize($ar_values);
- $this->created = $this->updated = time();
- }
- /**
- * Save a named query to the DB, erasing previous homonym queries is any exists.
- *
- * @return int
- */
- public function save()
- {
- // Avoid duplicates
- if (!empty($this->name))
- {
- $sq = "DELETE FROM {%s} WHERE name = '%s' AND uid = '%d' ";
- db_query($sq, QBF_TABLE_NAME, $this->name, $this->uid);
- // $n = db_affected_rows(); // Know how many homonym queries we deleted
- }
- $ret = drupal_write_record(QBF_TABLE_NAME, $this); // no update param: we just deleted the previous version
- if ($ret) // has to be SAVED_NEW, by construction
- {
- $ret = $this->qid; // from serial
- }
- return $ret;
- }
- }
- /**
- * 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.
- *
- * @ingroup forms
- * @param array $form
- * @param array $form_values
- */
- function _qbf_extract_query($element_id, $form, $form_values)
- {
- // Elements which are unnamed (form), removed, or display-only have no place in the query
- if (!empty($element_id) && array_key_exists('#qbf', $form) && array_key_exists('#level', $form['#qbf'])
- && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
- {
- $ret = array($element_id => $form_values[$element_id]);
- }
- else
- {
- $ret = array();
- }
- // QBF level is not inherited, so this loop is outside the "if" above
- foreach (element_children($form) as $child_name)
- {
- $ret += _qbf_extract_query($child_name, $form[$child_name], $form_values);
- }
- return $ret;
- }
- /**
- * Delete a query by qid
- *
- * In the qbf/<qid>/delete case, $query has been tested for validity and access
- * in qbf_query_load(), so it is safe and accessible.
- *
- * Outside this context, the function can also be invoken with just a qid, and
- * the same check via qbf_query_load() will be performed.
- *
- * @param mixed $query
- * int or object
- */
- function _qbf_query_delete($query)
- {
- global $user;
- if (is_int($query))
- {
- $query = qbf_query_load($query);
- }
- if ($query) // access already checked in explicit or implicit qbf_query_load
- {
- $qid = $query->qid;
- $sq = 'DELETE FROM %s WHERE qid = %d ';
- $q = db_query($sq, QBF_TABLE_NAME, $qid);
- $message = t('Query @id "@name" has been deleted.', array
- (
- '@id' => $qid,
- '@name' => $query->name,
- ));
- drupal_set_message($message, 'status');
- $link = l($qid, QBF_PATH_MAIN .'/'. $qid .'/delete');
- $notify = variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE);
- watchdog('qbf', $message, NULL, WATCHDOG_NOTICE, $link);
- // access check: we only send the message to the query owner, so access is
- // granted without an additional check
- if ($notify /* && $query->uid != $user->uid */)
- {
- $owner = user_load(array('uid' => $query->uid));
- $language = user_preferred_language($account);
- $params = array
- (
- 'query' => $query,
- 'owner' => $owner, // unused by default, but can be used in a hook_mail_alter() implementation
- 'deletor' => $user,
- 'language' => $language,
- );
- $ret = drupal_mail('qbf', __FUNCTION__, $user->mail, $language, $params, $user->mail);
- drupal_set_message(t('User !link has been informed', array
- (
- '!link' => l($account->name, 'user/'. $query->uid),
- )));
- // dsm(array("QQD, ret" => $ret));
- }
- }
- else {
- $message = t('Failed attempt to delete query @qid. Administrator has been alerted.', array
- (
- '@qid' => $qid,
- ));
- drupal_set_message($message, 'error');
- watchdog('qbf', $message, NULL, WATCHDOG_ERROR, $link);
- }
- drupal_goto();
- }
- /**
- * Transform a form element for QBF.
- *
- * QBF-specific properties are:
- * - #qbf : array of properties
- * - #level: only within #qbf
- *
- * See QBF_* constants
- *
- * @ingroup forms
- * @param string $key
- * @param array $element
- * @return void
- */
- function _qbf_transform_element($key, $element, $form_state, $query)
- {
- // dsm(array('key' => $key, 'element' => $element));
- /**
- * List default type transformations applied to widget by FAPI.
- * Types without a default transformation are not transformed
- */
- static $ar_default_type_transformations = array
- (
- 'button' => NULL, // no content
- 'file' => NULL, // non-querable (yet ?)
- 'image_button' => NULL, // new in D6
- 'markup' => NULL, // no content
- 'password' => NULL, // forbidden
- 'radio' => NULL, // single radio is useless, unlike a set of them
- 'submit' => NULL, // no content
- 'textarea' => 'textfield', // reduce text for searches
- // Don't transform these:
- // 'checkbox' => NULL,
- // 'checkboxes' => NULL,
- // 'date' => NULL,
- // 'fieldset' => NULL, // useful visually
- // 'form' => NULL, // removing it would delete the whole shebang
- // 'hidden' => NULL, // non-querable visually, but may be useful
- // 'item' => NULL,
- // 'radios' => NULL,
- // 'select' => NULL,
- // 'textfield' => NULL,
- // 'value' => 'value',
- // 'weight' => NULL,
- );
- /**
- * List default property transformations applied to widget by FAPI property.
- *
- * Properties without a default transformation are not transformed
- */
- static $ar_default_property_transformations = array
- (
- // Standard properties
- '#action' => NULL,
- '#after_build' => NULL,
- // '#base' => NULL, // gone in D6
- '#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,
- );
- /**
- * List properties causing causing element removal.
- *
- * The key is the property name, the value is the one causing removal.
- */
- static $ar_killer_properties = array
- (
- '#disabled' => TRUE,
- );
- // Transform type
- $source_type = $element['#type'];
- // .. Default transformation
- $dest_type = array_key_exists($source_type, $ar_default_type_transformations)
- ? $ar_default_type_transformations[$source_type]
- : $source_type;
- // .. Apply form-defined type override
- if (isset($element['#qbf']['#type']))
- {
- $dest_type = $element['#qbf']['#type'];
- }
- if (is_null($dest_type))
- {
- $ret = NULL;
- }
- else
- {
- $ret = $element;
- $ret['#type'] = $dest_type;
- if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
- {
- $ret = NULL;
- }
- else
- {
- foreach (element_properties($element) as $property_name)
- {
- // Apply killer properties first to avoid useless work
- if (array_key_exists($property_name, $ar_killer_properties)
- && ($element[$property_name] = $ar_killer_properties[$property_name]))
- {
- $ret = NULL;
- break;
- }
- // Now transform or copy remaining properties
- if (array_key_exists($property_name, $ar_default_property_transformations))
- {
- $ret[$property_name] = $ar_default_property_transformations[$property_name];
- }
- else
- {
- $ret[$property_name] = $element[$property_name];
- }
- // And apply form-defined property overrides
- if ($property_name == '#qbf')
- {
- foreach ($element[$property_name] as $override_name => $override_value)
- {
- $ret[$override_name] = $override_value;
- }
- }
- }
- if (isset($form_state['values'][$key]))
- {
- $ret['#default_value'] = $form_state['values'][$key];
- }
- elseif (isset($query->query[$key]))
- {
- $ret['#default_value'] = $query->query[$key];
- }
- // Recursively transform children
- foreach (element_children($element) as $child_name)
- {
- $child = _qbf_transform_element($child_name, $element[$child_name], $form_state, $query);
- if (is_null($child))
- {
- unset($ret[$child_name]);
- }
- else
- {
- $ret[$child_name] = $child;
- }
- }
- }
- }
- //dsm(array('key' => $key, 'transformed element' => $ret));
- return $ret;
- }
- /**
- * Implement the former hook_settings().
- *
- * @return array
- */
- function qbf_admin_settings()
- {
- $form = array();
- $form[QBF_VAR_NOTIFY_DELETE] = array
- (
- '#type' => 'checkbox',
- '#default_value' => variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE),
- '#title' => t('Notify users when one of their saved searches has been deleted'),
- );
- return system_settings_form($form);
- }
- /**
- * Submit handler for qbf_form, Save search button.
- *
- * @param array $form
- * @param array $form_state
- * @return integer
- * The id of the saved query.
- */
- function qbf_form_save_submit($form, &$form_state)
- {
- $qid = _qbf_save($form_state['values']['form_id'], $form_state);
- drupal_set_message(t('Your query was saved as "@name".',
- array('@name' => $form_state['values']['qbf_save_name'])));
- global $user;
- $form_state['redirect'] = "user/$user->uid/qbf";
- return $qid;
- }
- /**
- * Implement hook_forms().
- *
- * @link http://drupal.org/node/144132#hook-forms @endlink
- *
- * hook_qbf_register() returns an array of QBF-able node types, indexed by the
- * query type, with the following properties:
- * - form: the name of the hook_form() implementation (a $form_id)
- * - label: the human-readable type name under which the queries are saved by QBF
- * - callback: the function QBF must invoke to query the node type. It will
- * receive the query type and a filtered version of $form_state['values']
- * containing only valid node fields, and must return a themed grid of query
- * results, which will be displayed as a #markup FAPI element. In advanced
- * uses, a single callback can be used for several query types by using the
- * query type parameter to know what the values apply to.
- *
- * @ingroup forms
- * @ingroup hooks
- * @param string $form_id
- * @param array $args
- * @return array
- */
- function qbf_forms($qbf_form_id, $args)
- {
- $hook_name = 'qbf_register';
- // dsm(array("QBF_forms $qbf_form_id" => $args));
- // More efficient than using module_invoke_all: we avoid array-merging + re-looping
- foreach (module_implements($hook_name) as $module)
- {
- $arImplementations = module_invoke($module, $hook_name);
- // dsm($arImplementations);
- foreach ($arImplementations as $query_type => $query_info)
- {
- $forms['qbf_' . $query_info['form']] = array
- (
- 'callback' => 'qbf_form',
- 'callback arguments' => array($query_info, $qbf_form_id),
- );
- }
- }
- return $forms;
- }
- /**
- * List queries owned by a given user.
- *
- * @param int $uid > 0
- * @return array
- */
- function qbf_get_queries_by_user($uid = NULL)
- {
- if (is_null($uid))
- {
- global $user;
- $uid = $user->uid;
- }
- $sq = 'SELECT qq.qid, qq.uid, qq.name, qq.query, qq.updated '
- . 'FROM {%s} qq '
- . 'WHERE qq.uid = %d '
- . 'ORDER BY qq.name ';
- // no db_rewrite_sql: this function is not in a menu callback, so it is up to
- // the caller to check access
- $q = db_query($sq, QBF_TABLE_NAME, $uid);
- $ret = array();
- while ($o = db_fetch_object($q))
- {
- $ret[$o->qid] = $o; // qid is the PK, so it is present and unique
- }
- return $ret;
- }
- /**
- * Implement hook_menu().
- *
- * @return array
- */
- function qbf_menu()
- {
- $items = array();
- $items[QBF_PATH_MAIN . '/demo'] = array
- (
- 'title' => 'QBF Demo',
- 'access arguments' => array(QBF_PERM_QUERY),
- 'page callback' => 'qbf_show',
- 'type' => MENU_NORMAL_ITEM,
- );
- $items[QBF_PATH_SETTINGS] = array
- (
- 'title' => 'Query-By-Form',
- 'access arguments' => array(QBF_PERM_ADMIN),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('qbf_admin_settings'),
- 'type' => MENU_NORMAL_ITEM,
- );
- $items[QBF_PATH_MAIN . '/%qbf_query'] = array
- (
- 'type' => MENU_CALLBACK,
- 'access arguments' => array(QBF_PERM_QUERY),
- 'page callback' => 'qbf_query_form',
- 'page arguments' => array(1),
- );
- $items[QBF_PATH_MAIN . '/%qbf_query/delete'] = array
- (
- 'type' => MENU_CALLBACK,
- 'access arguments' => array(QBF_PERM_QUERY),
- 'page callback' => '_qbf_query_delete',
- 'page arguments' => array(1),
- );
- return $items;
- }
- /**
- * Implement hook_mail().
- *
- * @param string $key
- * @param array $message
- * @param array $params
- * @return void
- */
- function qbf_mail($key, &$message, $params)
- {
- // dsm(array('QBF_mail key' => $key, 'message' => $message, 'params' => $params));
- $deletor_tokens = user_mail_tokens($params['deletor'], $params['language']->language);
- $tokens = array_merge($deletor_tokens, array
- (
- '!qname' => $params['query']->name,
- '!qid' => $params['query']->qid,
- ));
- $message['subject'] = t('Effacement d\'une recherche !site enregistrée', $tokens);
- $message['body'] = t("!date\n\nVotre recherche !qid: !qname\nsur le site !site vient d'être effacée par !username.", $tokens);
- }
- /**
- * Implement hook_perm().
- *
- * @todo D7: Format will change
- * @see http://drupal.org/node/224333#descriptions-permissions
- *
- * @ingroup hooks
- * @return array
- */
- function qbf_perm()
- {
- $ret = array
- (
- QBF_PERM_ADMIN,
- QBF_PERM_QUERY,
- );
- return $ret;
- }
- /**
- * Load a saved QBF query.
- *
- * @see qbf_import_values()
- * @link http://drupal.org/node/109153#load @endlink
- *
- * @param int $us_qid
- * @return array
- * A form_values array
- */
- function qbf_query_load($us_qid)
- {
- static $query = NULL;
- // Only allow query loading by logged-in users
- if (user_is_anonymous())
- {
- return FALSE;
- }
- // Filter out visibly invalid values
- $qid = (is_numeric($us_qid) && ($us_qid > 0))
- ? $us_qid
- : 0;
- if (is_null($query))
- {
- $sq = 'SELECT qq.qid, qq.uid, qq.type, qq.name, qq.query '
- . 'FROM {%s} qq '
- . 'WHERE qq.qid = %d ';
- // db_rewrite_sql does not apply here: access control is further down
- $q = db_query($sq, QBF_TABLE_NAME, $qid);
- $query = db_fetch_object($q); // 0 or 1 row: we are querying on the primary key
- // FALSE does not happen: only NULL or a value can be here
- if ($query !== NULL)
- {
- $query->query = unserialize($query->query);
- // dsm($query);
- }
- }
- global $user;
- $ret = (isset($query) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
- ? $query
- : FALSE;
- return $ret;
- }
- /**
- * Provide an optional automatic mapping mechanism for query building.
- *
- * This function takes a partly built query map $ar_queryMap, and a defaults
- * array to complete it in $ar_defaults, and returns a fully built query array
- * ready to be used for querying.
- *
- * @param array $ar_query_map
- * @param array $ar_defaults
- * @return array
- */
- function qbf_query_mapper($ar_query_map = array(), $ar_defaults = array())
- {
- $ret = array();
- foreach ($ar_query_map as $name => $value)
- {
- // accept NULL, empty strings...
- if (!is_array($value))
- {
- $value = array();
- }
- $item = $value;
- foreach ($ar_defaults as $default_key => $default_value)
- {
- if (!array_key_exists($default_key, $item))
- {
- $item[$default_key] = is_null($default_value)
- ? $name
- : $default_value;
- }
- // else if is already in $item, so we don't touch it
- }
- $ret[$name] = $item;
- }
- return $ret;
- }
- error_reporting($_qbf_er);
- function qbf___________________________________________________(){}
- // ======== D6 LIMIT ==================================================================================================
- /* TODO Node previews and adding form fields to the node form.
- There is a subtle but important difference in the way node previews (and other
- such operations) are carried out when adding or editing a node. With the new
- Forms API, the node form is handled as a multi-step form. When the node form
- is previewed, all the form values are submitted, and the form is rebuilt with
- those form values put into $form['#node']. Thus, form elements that are added
- to the node form will lose any user input unless they set their '#default_value'
- elements using this embedded node object. */
- /**
- * Transform a form array for QBF.
- *
- * This function obtains the form array from the hook_form() implementation, and
- * transforms it by modifying widgets to other types where needed.
- *
- * Any additional parameter passed to the function is transmitted to the
- * hook_form() implementation.
- *
- * @ingroup forms
- * @param string $form_id
- * @param array $form_state
- * @return array
- */
- function qbf_transform_form($form_state, $form_id)
- {
- // Fetch the basic form and rename it, passing it the caller's arguments
- $form = drupal_retrieve_form($form_id, $form_state, NULL, $form_state);
- $node = new stdClass();
- $form = $form_id($node, $form_state);
- $new_form_id = "qbf_$form_id";
- // Only keep the children of the form and QBF properties on the form itself
- $elements = array();
- $new_form = array();
- $new_form['#qbf_source_form_id'] = $form_id;
- if (in_array('#qbf', element_properties($form)))
- {
- $new_form += $form['#qbf'];
- }
- foreach (element_children($form) as $key)
- {
- // dsm("Transforming $key, type " . $form[$key]['#type']);
- $new_element = _qbf_transform_element($key, $form[$key]);
- if (!is_null($new_element))
- {
- $new_form[$key] = $new_element;
- }
- }
- $new_form['#id'] = $new_form_id;
- // @todo #multistep -> #rebuild au submit
- // $new_form['#multistep'] = TRUE;
- // Do not set #redirect, even to FALSE (submit handlers)
- // $new_form['#redirect'] = FALSE;
- $new_form['#after_build'][] = 'qbf_after_build';
- $new_form['#submit'] = array('qbf_submit');
- // dsm($new_form);
- return $new_form;
- }
- /**
- * Insert the query results at the bottom of the query form.
- *
- * @ingroup forms
- * @param array $form
- * @param array $form_values
- * @return array
- */
- function qbf_after_build($form, $form_values) {
- if (empty($form['#post'])) {
- return $form;
- }
- // If #post is not empty, we are indeed querying
- $ar_query = _qbf_extract_query($form, $form_values);
- /* This function is called at the end of the form building process, which
- * means that child properties of #qbf have already been upgraded to element
- * properties. So we look for $form['#callback'] and not
- * $form['#qbf']['#callback']
- */
- if (isset($form['#callback']) && function_exists($function = $form['#callback'])) {
- $results = $function($ar_query);
- }
- else {
- drupal_set_message(t('QBF: incorrect callback function for search'), 'error');
- }
- $form['qbf_query_results'] = array
- (
- '#type' => 'markup',
- '#value' => $results,
- '#weight' => 10,
- );
- return $form;
- }
- /**
- * Load a form_values array into a form used by QBF.
- *
- * This is typically useful when loading saved queries using qbf_query_load().
- * For other cases, the mechanisms built within FAPI should be used instead.
- *
- * @see qbf_query_load()
- *
- * @ingroup forms
- * @param array $form
- * @param array $form_values
- * @return array The modified form
- */
- function qbf_import_values($element, $form_values) {
- foreach (element_children($element) as $child_name) {
- if (!empty($form_values[$child_name])) {
- $element[$child_name]['#qbf']['#default_value'] = $form_values[$child_name];
- }
- $element[$child_name] = qbf_import_values($element[$child_name], $form_values);
- }
- return $element;
- }
- /**
- * Save a query and return its qid.
- *
- * @ingroup forms
- *
- * @param $form_id string
- * @param $form_state array
- * @return int
- */
- function _qbf_save($form_id, $form_state)
- {
- if (user_is_anonymous())
- {
- $warning = t('Attempt by anonymous user to save a QBF query. Should not happen.');
- drupal_set_message($warning, 'error');
- watchdog('qbf', $warning, NULL, WATCHDOG_WARNING);
- $ret = 0;
- }
- else
- {
- // @FIXME check whether form_state is now needed. It wasn't in QBF for D5
- $form = drupal_retrieve_form($form_id, $form_state);
- // dsm($form, "retrieve");
- drupal_prepare_form($form_id, $form, $form_state);
- // dsm($form, "prepare");
- $name = $form_state['values']['qbf_save_name'];
- $type = $form_state['values']['qbf_save_type'];
- // dsm($form_state);
- $form_values = _qbf_extract_query(NULL, $form, $form_state['values']);
- // dsm($form_values);
- $ar_values = array();
- foreach ($form_values as $key => $value)
- {
- if (empty($value))
- {
- continue;
- }
- $ar_values[$key] = $value;
- }
- $query = new Qbf_Query($type, $name, $ar_values);
- $ret = $query->save();
- }
- return $ret;
- }
- /**
- * The QBF form builder.
- *
- * @param array $form_state
- * @param array $query_info
- * The query structure array
- * @param string $qbf_form_id
- * The name of the QBF form
- * @param string $query
- * The saved query.
- */
- function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
- {
- $form_id = $query_info['form'];
- dsm(array("QBF_Form" => "Base = [$form_id], QBF ID = [$qbf_form_id]", 'query' => $query));
- dsm($form_state);
- // Fetch the basic form and rename it, passing it the previous values
- $node = new stdClass();
- $form = $form_id($node, $form_state);
- $qbf_form = array();
- $qbf_form['#qbf_source_form_id'] = $form_id;
- // On the form element itself, only keep the QBF properties and the children
- if (in_array('#qbf', element_properties($form)))
- {
- $qbf_form += $form['#qbf'];
- }
- foreach (element_children($form) as $key)
- {
- $new_element = _qbf_transform_element($key, $form[$key], $form_state, $query);
- if (!is_null($new_element))
- {
- $qbf_form[$key] = $new_element;
- }
- }
- $qbf_form['#id'] = $qbf_form_id;
- // $qbf_form['#after_build'][] = 'qbf_after_build';
- // dsm($qbf_form);
- $qbf_form['qbf'] = array
- (
- '#type' => 'fieldset',
- '#title' => t('Query'),
- );
- if (isset($form_state['values']) && !empty($form_state['values']))
- {
- // dsm(array("QBF form: we must restore these values" => $form_state));
- if (isset($form_state['qbf_results']))
- {
- $qbf_form['qbf']['qbf_results'] = array
- (
- '#type' => 'markup',
- '#prefix' => '<p>',
- '#value' => $form_state['qbf_results'],
- '#suffix' => '</p>',
- );
- }
- }
- $qbf_form['qbf']['qbf_save_type'] = array
- (
- '#type' => 'hidden',
- '#value' => $query_info['form'],
- );
- $qbf_form['qbf']['qbf_query'] = array
- (
- '#type' => 'hidden',
- '#value' => $query_info['callback'],
- );
- $qbf_form['qbf']['qbf_save_name'] = array
- (
- '#title' => t('Name of query in your save list'),
- '#type' => 'textfield',
- '#required' => TRUE,
- '#default_value' => empty($query->name)
- ? substr($form_id, 0, -5) . ' ' . format_date(time(), 'large')
- : $query->name,
- );
- $qbf_form['qbf']['qbf_save'] = array
- (
- '#submit' => array('qbf_form_save_submit'),
- '#validate' => array('qbf_form_save_validate'),
- '#type' => 'submit',
- '#value' => t('Save query'),
- '#weight' => 5,
- );
- $qbf_form['qbf']['qbf_perform'] = array
- (
- '#submit' => array('qbf_form_perform_submit'),
- // '#validate' => array('qbf_form_perform_validate'),
- '#type' => 'submit',
- '#value' => t('Perform query'),
- '#weight' => 4,
- );
- return $qbf_form;
- }
- /**
- * Submit handler for qbf_form, Perform search button.
- *
- * @param array $form
- * @param array $form_state
- */
- function qbf_form_perform_submit($form, &$form_state)
- {
- // dsm('PERF SUB');
- //dsm($form);
- // dsm($form_state);
- $callback = $form_state['values']['qbf_query'];
- if (function_exists(($callback)))
- {
- $ar_query = _qbf_extract_query(NULL, $form, $form_state['values']);
- $form_state['qbf_results'] = $callback($ar_query);
- }
- $form_state['rebuild'] = TRUE;
- }
- function qbf_query_form($query)
- {
- $qbf_form_id = 'qbf_' . $query->type;
- $form = drupal_get_form($qbf_form_id, $query);
- return $form;
- }
|