Selaa lähdekoodia

- Can now both load a saved query, automatically loading the appropriate form, and execute a fresh query for an app

Frederic G. Marand 15 vuotta sitten
vanhempi
sitoutus
cce1fff358
1 muutettua tiedostoa jossa 125 lisäystä ja 85 poistoa
  1. 125 85
      qbf.module

+ 125 - 85
qbf.module

@@ -12,7 +12,7 @@
  * @package QBF
  */
 
-// $Id: qbf.module,v 1.9.4.6 2009-03-21 16:53:31 marand Exp $
+// $Id: qbf.module,v 1.9.4.7 2009-03-21 19:41:34 marand Exp $
 
 /**
  * Saved error reporting level.
@@ -257,12 +257,12 @@ function _qbf_query_delete($query)
  * @param array $element
  * @return void
  */
-function _qbf_transform_element($key, $element) {
+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
@@ -291,76 +291,78 @@ function _qbf_transform_element($key, $element) {
     // '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 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,
+      );
 
-  /**
-   * 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'];
 
-  // 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'];
-    }
+    // .. Default transformation
+    $dest_type = array_key_exists($source_type, $ar_default_type_transformations)
+      ? $ar_default_type_transformations[$source_type]
+      : $source_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)
+    // .. Apply form-defined type override
+    if (isset($element['#qbf']['#type']))
+      {
+      $dest_type = $element['#qbf']['#type'];
+      }
+
+    if (is_null($dest_type))
       {
       $ret = NULL;
       }
     else
       {
-      foreach (element_properties($element) as $property_name)
+      $ret = $element;
+      $ret['#type'] = $dest_type;
+      if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
         {
-        // 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;
+        }
+      else
+        {
+        foreach (element_properties($element) as $property_name)
           {
-          $ret = NULL;
-          break;
-          }
+          // 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))
@@ -376,16 +378,25 @@ function _qbf_transform_element($key, $element) {
         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]);
+        $child = _qbf_transform_element($child_name, $element[$child_name], $form_state, $query);
         if (is_null($child))
           {
           unset($ret[$child_name]);
@@ -474,7 +485,7 @@ function qbf_forms($qbf_form_id, $args)
       $forms['qbf_' . $query_info['form']] = array
         (
         'callback'           => 'qbf_form',
-        'callback arguments' => array($query_info, $qbf_form_id, $args),
+        'callback arguments' => array($query_info, $qbf_form_id),
         );
       }
     }
@@ -537,6 +548,14 @@ function qbf_menu()
     '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,
@@ -597,16 +616,28 @@ function qbf_perm()
  * @see qbf_import_values()
  * @link http://drupal.org/node/109153#load @endlink
  *
- * @param int $qid
- * @return array A form_values array usable by qbf_import_values
+ * @param int $us_qid
+ * @return array
+ *   A form_values array
  */
-function qbf_query_load($qid)
+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.query, qq.name '
+    $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
@@ -616,12 +647,12 @@ function qbf_query_load($qid)
     if ($query !== NULL)
       {
       $query->query = unserialize($query->query);
-      //dsm($query);
+      // dsm($query);
       }
     }
 
   global $user;
-  $ret = (isset($query) && isset($query->uid) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
+  $ret = (isset($query) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
     ? $query
     : FALSE;
 
@@ -843,26 +874,27 @@ function _qbf_save($form_id, $form_state)
  * The QBF form builder.
  *
  * @param array $form_state
- * @param string $form_id
- *   The original form to transform
+ * @param array $query_info
+ *   The query structure array
  * @param string $qbf_form_id
- *   The QBF form itself
- * @param strign $save_type
- *   Name of search type, to use when saving.
+ *   The name of the QBF form
+ * @param string $query
+ *   The saved query.
  */
-function qbf_form($form_state, $query_info, $qbf_form_id)
+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]", 'save_type' => $save_type));
-  // dsm($form_state);
+  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 itselfn only keep the QBF properties and the children
+  // On the form element itself, only keep the QBF properties and the children
   if (in_array('#qbf', element_properties($form)))
     {
     $qbf_form += $form['#qbf'];
@@ -870,8 +902,7 @@ function qbf_form($form_state, $query_info, $qbf_form_id)
 
   foreach (element_children($form) as $key)
     {
-    // dsm("Transforming $key, type " . $form[$key]['#type']);
-    $new_element = _qbf_transform_element($key, $form[$key], $form_state);
+    $new_element = _qbf_transform_element($key, $form[$key], $form_state, $query);
     if (!is_null($new_element))
       {
       $qbf_form[$key] = $new_element;
@@ -906,7 +937,7 @@ function qbf_form($form_state, $query_info, $qbf_form_id)
   $qbf_form['qbf']['qbf_save_type'] = array
     (
     '#type'          => 'hidden',
-    '#value'         => $query_info['label'],
+    '#value'         => $query_info['form'],
     );
   $qbf_form['qbf']['qbf_query'] = array
     (
@@ -919,7 +950,9 @@ function qbf_form($form_state, $query_info, $qbf_form_id)
     '#title'         => t('Name of query in your save list'),
     '#type'          => 'textfield',
     '#required'      => TRUE,
-    '#default_value' => substr($form_id, 0, -5) . ' ' . format_date(time(), 'large'),
+    '#default_value' => empty($query->name)
+      ? substr($form_id, 0, -5) . ' ' . format_date(time(), 'large')
+      : $query->name,
     );
   $qbf_form['qbf']['qbf_save'] = array
     (
@@ -961,3 +994,10 @@ function qbf_form_perform_submit($form, &$form_state)
     }
   $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;
+  }