Procházet zdrojové kódy

- renamed several functions by prefixing them with "_" because they are not means for public use
- qbf_forms now uses a static cache for reduced load
- qbf_query_load() no longer broken on missing QIDs
- reordered module code by function name
- removed lots of cruft: extra dsm() calls, debug comments, etc.
- only one feature remains to be cleaned up: the user page

Frederic G. Marand před 15 roky
rodič
revize
883dbbf3ae
1 změnil soubory, kde provedl 245 přidání a 334 odebrání
  1. 245 334
      qbf.module

+ 245 - 334
qbf.module

@@ -10,11 +10,9 @@
  * @author Frederic G. MARAND
  * @license Licensed under the CeCILL 2.0 and the General Public Licence version 2 or later
  * @package QBF
- *
- * @todo validate searches: checkboxes sets needs at least one value checked, otherwise there won't be any result
  */
 
-// $Id: qbf.module,v 1.9.4.8 2009-03-21 20:15:30 marand Exp $
+// $Id: qbf.module,v 1.9.4.9 2009-03-22 10:22:59 marand Exp $
 
 /**
  * Saved error reporting level.
@@ -201,6 +199,66 @@ function _qbf_extract_query($element_id, $form, $form_values)
   return $ret;
   }
 
+/**
+ * Submit handler for qbf_form, Perform search button.
+ *
+ * @param array $form
+ * @param array $form_state
+ */
+function _qbf_form_perform_submit($form, &$form_state)
+  {
+  // 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;
+  }
+
+/**
+ * Validate handler for qbf_form, Perform search button.
+ *
+ * @param array $form
+ * @param array $form_state
+ */
+function _qbf_form_perform_validate($form, &$form_state)
+  {
+  // @todo validate searches: checkboxes sets needs at least one value checked, otherwise there won't be any result
+  }
+
+/**
+ * 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/edit/qbf";
+  return $qid;
+  }
+
+
+/**
+ * Validate handler for qbf_form, Save search button.
+ *
+ * @param array $form
+ * @param array $form_state
+ */
+function _qbf_form_save_validate($form, &$form_state)
+  {
+  // @todo validate saves. Check whether any validation is necessary.
+  }
+
 /**
  * Delete a query by qid
  *
@@ -269,6 +327,69 @@ function _qbf_query_delete($query)
   drupal_goto();
 }
 
+/**
+ * Main query page.
+ *
+ * @param object $query
+ *   Valid query, loaded by qbf_query_load().
+ * @return string
+ */
+function _qbf_query_form($query)
+  {
+  $qbf_form_id = 'qbf_' . $query->type;
+  $form = drupal_get_form($qbf_form_id, $query);
+  return $form;
+  }
+
+/**
+ * Save a query and return its qid.
+ *
+ * This is not a hook_save() implementation, hence the "_".
+ *
+ * @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;
+  }
+
 /**
  * Transform a form element for QBF.
  *
@@ -457,21 +578,104 @@ function qbf_admin_settings()
   }
 
 /**
- * Submit handler for qbf_form, Save search button.
+ * The QBF form builder.
  *
- * @param array $form
  * @param array $form_state
- * @return integer
- *   The id of the saved query.
+ * @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_save_submit($form, &$form_state)
+function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
   {
-  $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/edit/qbf";
-  return $qid;
+  $form_id = $query_info['form'];
+
+  // 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['qbf'] = array
+    (
+    '#type'   => 'fieldset',
+    '#title'  => t('Query'),
+    );
+
+  if (isset($form_state['values']) && !empty($form_state['values']))
+    {
+    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)
+      ? t('@label - @time', array('@label' => $query_info['label'], '@time' => format_date(time(), 'large')))
+      : $query->name,
+    );
+
+  $qbf_form['qbf']['qbf_perform'] = array
+    (
+    '#submit'        => array('_qbf_form_perform_submit'),
+    '#validate'      => array('_qbf_form_perform_validate'),
+    '#type'          => 'submit',
+    '#value'         => t('Perform query'),
+    );
+
+  $qbf_form['qbf']['qbf_save'] = array
+    (
+    '#submit'        => array('_qbf_form_save_submit'),
+    '#validate'      => array('_qbf_form_save_validate'),
+    '#type'          => 'submit',
+    '#value'         => t('Save query'),
+    );
+
+  return $qbf_form;
   }
 
 /**
@@ -480,7 +684,7 @@ function qbf_form_save_submit($form, &$form_state)
  * @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:
+ * node 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
@@ -492,29 +696,36 @@ function qbf_form_save_submit($form, &$form_state)
  *
  * @ingroup forms
  * @ingroup hooks
- * @param string $form_id
+ *
+ * @param string $qbf_form_id
  * @param array $args
  * @return array
  */
-function qbf_forms($qbf_form_id, $args)
+function qbf_forms($args = NULL)
   {
-  $hook_name = 'qbf_register';
-  // dsm(array("QBF_forms $qbf_form_id" => $args));
+  static $forms = array();
 
-  // More efficient than using module_invoke_all: we avoid array-merging + re-looping
-  foreach (module_implements($hook_name) as $module)
+  if (empty($forms))
     {
-    $arImplementations = module_invoke($module, $hook_name);
-    // dsm($arImplementations);
-    foreach ($arImplementations as $query_type => $query_info)
+    $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)
       {
-      $forms['qbf_' . $query_info['form']] = array
-        (
-        'callback'           => 'qbf_form',
-        'callback arguments' => array($query_info, $qbf_form_id),
-        );
-      }
-    }
+      $arImplementations = module_invoke($module, $hook_name);
+      // dsm($arImplementations);
+      foreach ($arImplementations as /* $node_type => */ $query_info)
+        {
+        $qbf_form_id = 'qbf_' . $query_info['form'];
+        $forms[$qbf_form_id] = array
+          (
+          'callback'           => 'qbf_form',
+          'callback arguments' => array($query_info, $qbf_form_id),
+          );
+        } // foreach implementation
+      } // foreach module
+    } // if empty
 
   return $forms;
   }
@@ -578,7 +789,7 @@ function qbf_menu()
     (
     'type'             => MENU_CALLBACK,
     'access arguments' => array(QBF_PERM_QUERY),
-    'page callback'    => 'qbf_query_form',
+    'page callback'    => '_qbf_query_form',
     'page arguments'   => array(1),
     );
 
@@ -639,7 +850,6 @@ function qbf_perm()
 /**
  * Load a saved QBF query.
  *
- * @see qbf_import_values()
  * @link http://drupal.org/node/109153#load @endlink
  *
  * @param int $us_qid
@@ -669,8 +879,7 @@ function qbf_query_load($us_qid)
     // 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)
+    if ($query !== FALSE)
       {
       $query->query = unserialize($query->query);
       // dsm($query);
@@ -727,306 +936,8 @@ function qbf_query_mapper($ar_query_map = array(), $ar_defaults = array())
 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;
-  }
+// ======== D6 LIMIT ==================================================================================================
 
 /**
  * Implement hook_profile_alter().