فهرست منبع

- query saving is now restricted to logged-in users
- form-level #validate and #submit are now kept during transform, along with #qbf. These are the only kept form-level properties
- various minor phpdocs and function declaration tweaks

Frederic G. Marand 15 سال پیش
والد
کامیت
0d8d3dfab0
1فایلهای تغییر یافته به همراه87 افزوده شده و 40 حذف شده
  1. 87 40
      qbf.module

+ 87 - 40
qbf.module

@@ -12,7 +12,7 @@
  * @package QBF
  */
 
-// $Id: qbf.module,v 1.9.4.13 2009-03-22 19:40:29 marand Exp $
+// $Id: qbf.module,v 1.9.4.14 2009-03-24 10:55:27 marand Exp $
 
 /**
  * Saved error reporting level.
@@ -287,11 +287,10 @@ function _qbf_extract_query($element_id, $form, $form_values)
  *
  * @param array $form
  * @param array $form_state
+ * @return void
  */
 function _qbf_form_perform_submit($form, &$form_state)
   {
-  // dsm($form);
-  // dsm($form_state);
   $callback = $form_state['values']['qbf_query'];
   if (function_exists(($callback)))
     {
@@ -306,21 +305,25 @@ function _qbf_form_perform_submit($form, &$form_state)
  *
  * @param array $form
  * @param array $form_state
+ *   Can be passed by reference if changes are needed
+ * @return void
  */
-//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
-//  }
+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
+ *   Can be passed by reference if changes are needed
  * @return integer
  *   The id of the saved query.
  */
-function _qbf_form_save_submit($form, &$form_state)
+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".',
@@ -335,6 +338,7 @@ function _qbf_form_save_submit($form, &$form_state)
  *
  * @param array $form
  * @param array $form_state
+ * @return void
  */
 //function _qbf_form_save_validate($form, &$form_state)
 //  {
@@ -537,9 +541,12 @@ function _qbf_save($form_id, $form_state)
  * See QBF_* constants
  *
  * @ingroup forms
+ *
  * @param string $key
  * @param array $element
- * @return void
+ * @param array $form_state
+ * @param object $query
+ * @return array
  */
 function _qbf_transform($key, $element, $form_state, $query)
   {
@@ -595,9 +602,9 @@ function _qbf_transform($key, $element, $form_state, $query)
       '#ref'           => NULL,
       '#required'      => NULL,
       '#rows'          => NULL,
-      '#submit'        => NULL,
+      // '#submit'        => NULL,
       '#tree'          => NULL,
-      '#validate'      => NULL,
+      // '#validate'      => NULL,
       );
 
     /**
@@ -775,18 +782,22 @@ function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
 
   // 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['#id'] = $qbf_form_id;
   $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)))
+  // On the form element itself, only keep the QBF properties, the handlers, and the children
+  foreach (element_properties($form) as $key)
     {
-    $qbf_form += $form['#qbf'];
+    if (in_array($key, array('#qbf', '#validate', '#submit')))
+      {
+      $qbf_form[$key] = $form[$key];
+      }
     }
 
+  // Transform the children tree
   foreach (element_children($form) as $key)
     {
     $new_element = _qbf_transform($key, $form[$key], $form_state, $query);
@@ -796,8 +807,6 @@ function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
       }
     }
 
-  $qbf_form['#id'] = $qbf_form_id;
-
   $qbf_form['qbf'] = array
     (
     '#type'   => 'fieldset',
@@ -818,11 +827,66 @@ function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
       }
     }
 
-  $qbf_form['qbf']['qbf_save_type'] = array
-    (
-    '#type'          => 'hidden',
-    '#value'         => $query_info['form'],
-    );
+  /**
+   * Offer query save feature only to logged-in users with query permission.
+   * We know they have QBF_PERM_QUERY since it is needed to access this form,
+   * from qbf_menu().
+   */
+  if (user_is_anonymous())
+    {
+    $qbf_form['qbf']['save'] = array
+      (
+      '#type'  => 'markup',
+      '#value' => t('<p>You need to be logged in order to save a query.</p>'),
+      );
+    }
+  else
+    {
+    global $user;
+    $ar_queries = qbf_get_queries_by_user($user->uid);
+    $max_queries = variable_get(QBF_VAR_MAX_QUERIES, QBF_DEF_MAX_QUERIES);
+    if (count($ar_queries) >= $max_queries)
+      {
+      $qbf_form['qbf']['save'] = array
+        (
+        '#type'  => 'markup',
+        '#value' => t('<p>You already have reached the maximum number of saved queries (@max). To save this query, first <a href="!queries">remove</a> at least one saved query.</p>',
+          array
+            (
+            '@max'         => $max_queries,
+            '!queries'     => url("user/$user->uid/edit/qbf"),
+            )
+          ),
+        );
+      }
+    else
+      {
+      $qbf_form['qbf']['qbf_save_type'] = array
+        (
+        '#type'          => 'hidden',
+        '#value'         => $query_info['form'],
+        );
+
+      $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_save'] = array
+        (
+        '#submit'        => array('_qbf_form_save_submit'),
+        '#validate'      => array('_qbf_form_save_validate'),
+        '#type'          => 'submit',
+        '#value'         => t('Save query'),
+        '#weight'        => 2,
+        );
+      }
+    }
 
   $qbf_form['qbf']['qbf_query'] = array
     (
@@ -830,30 +894,13 @@ function qbf_form(&$form_state, $query_info, $qbf_form_id, $query = NULL)
     '#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'),
+    '#weight'        => 1,
     );
 
   return $qbf_form;