Explorar el Código

- First D6 version able to perform a QBF query with the first D6 version of job.module
- _qbf_extract_query now takes the element id as its first parameter
- hook_qbf_register now returns an array of query info arrays, not just a form id, to enable multi-node-type, multi-application support
- qbf_form() no longer uses a per-form submit handler
- qbf_form() now uses the new query info from hook_qbf_register to pass query callback info to the perform handler
- qbf_form_perform_submit() now uses the new query info from hook_qbf_register to invoke the per-node/per-app query callback

Frederic G. Marand hace 15 años
padre
commit
91e941f53c
Se han modificado 1 ficheros con 41 adiciones y 47 borrados
  1. 41 47
      qbf.module

+ 41 - 47
qbf.module

@@ -12,7 +12,7 @@
  * @package QBF
  */
 
-// $Id: qbf.module,v 1.9.4.5 2009-03-20 10:00:39 marand Exp $
+// $Id: qbf.module,v 1.9.4.6 2009-03-21 16:53:31 marand Exp $
 
 /**
  * Saved error reporting level.
@@ -153,14 +153,13 @@ class Qbf_Query
  * @param array $form
  * @param array $form_values
  */
-function _qbf_extract_query($form, $form_values)
+function _qbf_extract_query($element_id, $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'])
+  // 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($name => $form_values[$name]);
+    $ret = array($element_id => $form_values[$element_id]);
     }
   else
     {
@@ -170,7 +169,7 @@ function _qbf_extract_query($form, $form_values)
   // QBF level is not inherited, so this loop is outside the "if" above
   foreach (element_children($form) as $child_name)
     {
-    $ret += _qbf_extract_query($form[$child_name], $form_values);
+    $ret += _qbf_extract_query($child_name, $form[$child_name], $form_values);
     }
 
   return $ret;
@@ -443,6 +442,17 @@ 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:
+ * - 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
@@ -452,17 +462,19 @@ function qbf_form_save_submit($form, &$form_state)
 function qbf_forms($qbf_form_id, $args)
   {
   $hook_name = 'qbf_register';
-  // dsm(array("QBF_forms $form_id" => $args));
+  // 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)
     {
-    foreach (module_invoke($module, $hook_name) as $form_id => $search_title)
+    $arImplementations = module_invoke($module, $hook_name);
+    // dsm($arImplementations);
+    foreach ($arImplementations as $query_type => $query_info)
       {
-      $forms["qbf_$form_id"] = array
+      $forms['qbf_' . $query_info['form']] = array
         (
         'callback'           => 'qbf_form',
-        'callback arguments' => array($form_id, $qbf_form_id, $search_title, $args),
+        'callback arguments' => array($query_info, $qbf_form_id, $args),
         );
       }
     }
@@ -808,7 +820,7 @@ function _qbf_save($form_id, $form_state)
     $name = $form_state['values']['qbf_save_name'];
     $type = $form_state['values']['qbf_save_type'];
     // dsm($form_state);
-    $form_values = _qbf_extract_query($form, $form_state['values']);
+    $form_values = _qbf_extract_query(NULL, $form, $form_state['values']);
     // dsm($form_values);
     $ar_values = array();
     foreach ($form_values as $key => $value)
@@ -838,8 +850,9 @@ function _qbf_save($form_id, $form_state)
  * @param strign $save_type
  *   Name of search type, to use when saving.
  */
-function qbf_form($form_state, $form_id, $qbf_form_id, $save_type)
+function qbf_form($form_state, $query_info, $qbf_form_id)
   {
+  $form_id = $query_info['form'];
   // dsm(array("QBF_Form" => "Base = [$form_id], QBF ID = [$qbf_form_id]", 'save_type' => $save_type));
   // dsm($form_state);
 
@@ -868,8 +881,6 @@ function qbf_form($form_state, $form_id, $qbf_form_id, $save_type)
   $qbf_form['#id'] = $qbf_form_id;
 
 //  $qbf_form['#after_build'][] = 'qbf_after_build';
-//  $qbf_form['#validate'][] = 'qbf_form_validate';
-  $qbf_form['#submit'] = array('qbf_form_submit');
   // dsm($qbf_form);
 
   $qbf_form['qbf'] = array
@@ -879,7 +890,7 @@ function qbf_form($form_state, $form_id, $qbf_form_id, $save_type)
     );
   if (isset($form_state['values']) && !empty($form_state['values']))
     {
-    dsm(array("QBF form: we must restore these values" => $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
@@ -895,7 +906,12 @@ function qbf_form($form_state, $form_id, $qbf_form_id, $save_type)
   $qbf_form['qbf']['qbf_save_type'] = array
     (
     '#type'          => 'hidden',
-    '#value'         => $save_type,
+    '#value'         => $query_info['label'],
+    );
+  $qbf_form['qbf']['qbf_query'] = array
+    (
+    '#type'          => 'hidden',
+    '#value'         => $query_info['callback'],
     );
 
   $qbf_form['qbf']['qbf_save_name'] = array
@@ -934,36 +950,14 @@ function qbf_form($form_state, $form_id, $qbf_form_id, $save_type)
  */
 function qbf_form_perform_submit($form, &$form_state)
   {
-  dsm('PERF SUB');
+  // dsm('PERF SUB');
   //dsm($form);
-  dsm($form_state);
-  $form_state['qbf_results'] = "Les résultats sont là";
+  // 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)
-  {
-  dsm('PERF VAL');
-  //dsm($form);
-  dsm($form_state);
-  }
-
-function qbf_form_submit($form, &$form_state)
-  {
-  dsm('FORM SUB');
-  //dsm($form);
-  dsm($form_state);
-  }
-
-function qbf_form_validate($form, &$form_state)
-  {
-  dsm('FORM VAL');
-  //dsm($form);
-  dsm($form_state);
-  }