Преглед на файлове

- Modifiy coding style to passer coder.module validation

Frederic G. Marand преди 16 години
родител
ревизия
44f1df1b81
променени са 2 файла, в които са добавени 200 реда и са изтрити 212 реда
  1. 54 21
      qbf.install
  2. 146 191
      qbf.module

+ 54 - 21
qbf.install

@@ -10,7 +10,7 @@
  * License GPL2 or later
  */
 
-// $Id: qbf.install,v 1.1.2.3 2008-10-03 13:47:41 marand Exp $
+// $Id: qbf.install,v 1.1.2.4 2008-10-13 13:43:18 marand Exp $
 
 /**
  * Implement hook_install().
@@ -25,25 +25,48 @@
  * @return void
  */
 function qbf_install() {
-  switch ($GLOBALS['db_type'])
-    {
+
+  // Initialize settings
+  $ar_const = get_defined_constants();
+  $ar_const = array_keys($ar_const);
+  $count = 0;
+  $init_count = 0;
+  foreach ($ar_const as $name) {
+    if (strpos($name, 'QBF_VAR_') === 0) {
+      $count++;
+      $default = 'QBF_DEF_'. drupal_substr($name, drupal_strlen('QBF_VAR_'));
+      if (defined($default)) {
+        variable_set(constant($name), constant($default));
+        $init_count++;
+      }
+    }
+  }
+  drupal_set_message(t('Initialized QBF module settings (@initCount/@count parameters). You can <a href="!link">complete setup</a>.',
+    array
+      (
+      '@initCount' => $init_count,
+      '@count'     => $count,
+      '!link'      => url(QBF_PATH_SETTINGS),
+      )));
+
+  switch ($GLOBALS['db_type']) {
     case 'mysql':
     case 'mysqli':
       $sq = 'CREATE TABLE {qbf_queries} '
-          . '  ( '
-          . "  `qid`                   INT(10)          NOT NULL DEFAULT '0' COMMENT 'Query ID', "
-          . "  `uid`                   INT(10)          NOT NULL DEFAULT '0' COMMENT 'User ID', "
-          . "  `name`                  VARCHAR(40)      NOT NULL DEFAULT ''  COMMENT 'Query name', "
-          . "  `query`                 TEXT             NOT NULL             COMMENT 'Query array', "
-          . "  `created`               INT(10)          NOT NULL DEFAULT '0' COMMENT 'Creation timestamp', "
-          . "  `updated`               INT(10)          NOT NULL DEFAULT '0' COMMENT 'Update timestamp', "
-          . '  PRIMARY KEY          (`qid`) , '
-          . '  KEY         `uid`    (`uid`), '
-          . '  KEY         `updated`(`updated`) '
-          . '  ) '
-          . 'ENGINE = MyISAM '
-          . 'CHARACTER SET = utf8 '
-          . "COMMENT = 'QBF Query store' ";
+          .'  ( '
+          ."  `qid`                   INT(10)          NOT NULL DEFAULT '0' COMMENT 'Query ID', "
+          ."  `uid`                   INT(10)          NOT NULL DEFAULT '0' COMMENT 'User ID', "
+          ."  `name`                  VARCHAR(40)      NOT NULL DEFAULT ''  COMMENT 'Query name', "
+          ."  `query`                 TEXT             NOT NULL             COMMENT 'Query array', "
+          ."  `created`               INT(10)          NOT NULL DEFAULT '0' COMMENT 'Creation timestamp', "
+          ."  `updated`               INT(10)          NOT NULL DEFAULT '0' COMMENT 'Update timestamp', "
+          .'  PRIMARY KEY          (`qid`) , '
+          .'  KEY         `uid`    (`uid`), '
+          .'  KEY         `updated`(`updated`) '
+          .'  ) '
+          .'ENGINE = MyISAM '
+          .'CHARACTER SET = utf8 '
+          ."COMMENT = 'QBF Query store' ";
       db_query($sq);
       break;
 
@@ -52,7 +75,7 @@ function qbf_install() {
       drupal_set_message(t('Unsupported database backend for QBF module: @db',
         array('@db' => $GLOBALS['db_type'])), 'error');
       break;
-    }
+  }
 }
 
 /**
@@ -70,8 +93,18 @@ function qbf_install() {
  * @return void
  */
 function qbf_uninstall() {
-  switch ($GLOBALS['db_type'])
-    {
+
+  // Remove settings
+  $ar_const = get_defined_constants();
+  $ar_const = array_keys($ar_const);
+  foreach ($ar_const as $name) {
+    if (strpos($name, 'QBF_VAR_') === 0) {
+      variable_del($name);
+    }
+  }
+  drupal_set_message(t('Removed QBF module settings.'));
+
+  switch ($GLOBALS['db_type']) {
     case 'mysql':
     case 'mysqli':
     case 'pgsql':
@@ -82,7 +115,7 @@ function qbf_uninstall() {
       drupal_set_message(t('Unsupported database backend for QBF module: @db',
         array('@db' => $GLOBALS['db_type'])), 'error');
       break;
-    }
+  }
 }
 
 /**

+ 146 - 191
qbf.module

@@ -12,7 +12,7 @@
  * @package QBF
  */
 
-// $Id: qbf.module,v 1.9.2.6 2008-10-07 09:43:06 marand Exp $
+// $Id: qbf.module,v 1.9.2.7 2008-10-13 13:43:18 marand Exp $
 
 /**
  * Saved error reporting level.
@@ -102,39 +102,36 @@ define('QBF_DEF_NOTIFY_DELETE',      FALSE);
  * @return array
  */
 function qbf_transform_form($form_id) {
-  $arArgs = func_get_args();
-//dsm(array('qtf' => $arArgs));
+  $ar_args = func_get_args();
+//dsm(array('qtf' => $ar_args));
   // Fetch the basic form and rename it, passing it the caller's arguments
-  $form = call_user_func_array('drupal_retrieve_form', $arArgs);
-  $newFormId = "qbf_$form_id";
+  $form = call_user_func_array('drupal_retrieve_form', $ar_args);
+  $new_form_id = "qbf_$form_id";
 
   // Only keep the children of the form and QBF properties on the form itself
   $elements = array();
-  $newForm = array();
-  $newForm['#qbf_source_form_id'] = $form_id;
-  if (in_array('#qbf', element_properties($form)))
-    {
-    $newForm += $form['#qbf'];
-    }
+  $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)
-    {
+  foreach (element_children($form) as $key) {
     // dsm("Transforming $key, type " . $form[$key]['#type']);
-    $newElement = _qbf_transform_element($key, $form[$key]);
-    if (!is_null($newElement))
-      {
-      $newForm[$key] = $newElement;
-      }
+    $new_element = _qbf_transform_element($key, $form[$key]);
+    if (!is_null($new_element)) {
+      $new_form[$key] = $new_element;
     }
+  }
 
-  $newForm['#id'] = $newFormId;
-  $newForm['#multistep'] = TRUE;
+  $new_form['#id'] = $new_form_id;
+  $new_form['#multistep'] = TRUE;
   // Do not set #redirect, even to FALSE (submit handlers)
-  // $newForm['#redirect']  = FALSE;
-  $newForm['#after_build'][] = 'qbf_after_build';
-  $newForm['#submit'] = array('qbf_submit' => array());
-// dsm($newForm);
-  return $newForm;
+  // $new_form['#redirect']  = FALSE;
+  $new_form['#after_build'][] = 'qbf_after_build';
+  $new_form['#submit'] = array('qbf_submit' => array());
+// dsm($new_form);
+  return $new_form;
 }
 
 /**
@@ -159,7 +156,7 @@ function _qbf_transform_element($key, $element) {
    *
    * Types without a default transformation are not transformed
    */
-  static $arDefaultTypeTransformations = array
+  static $ar_default_type_transformations = array
     (
     'button'         => NULL,
     'file'           => NULL,
@@ -177,7 +174,7 @@ function _qbf_transform_element($key, $element) {
    *
    * Properties without a default transformation are not transformed
    */
-  static $arDefaultPropertyTransformations = array
+  static $ar_default_property_transformations = array
     (
     // Standard properties
     '#action'        => NULL,
@@ -202,82 +199,68 @@ function _qbf_transform_element($key, $element) {
    *
    * The key is the property name, the value is the one causing removal.
    */
-  static $arKillerProperties = array
+  static $ar_killer_properties = array
     (
     '#disabled'      => TRUE,
     );
 
   // Transform type
-  $sourceType = $element['#type'];
+  $source_type = $element['#type'];
   // .. Default transformation
-  $destType = array_key_exists($sourceType, $arDefaultTypeTransformations)
-    ? $arDefaultTypeTransformations[$sourceType]
-    : $sourceType;
+  $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']))
-    {
-    $destType = $element['#qbf']['#type'];
-    }
+  if (isset($element['#qbf']['#type'])) {
+    $dest_type = $element['#qbf']['#type'];
+  }
 
-  if (is_null($destType))
-    {
+  if (is_null($dest_type)) {
     $ret = NULL;
-    }
-  else
-    {
+  }
+  else {
     $ret = $element;
-    $ret['#type'] = $destType;
-    if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE)
-      {
+    $ret['#type'] = $dest_type;
+    if (!array_key_exists('#qbf', $element) || $element['#qbf']['#level'] == QBF_LEVEL_REMOVE) {
       $ret = NULL;
-      }
-    else
-      {
-      foreach (element_properties($element) as $propertyName)
-        {
+    }
+    else {
+      foreach (element_properties($element) as $property_name) {
         // Apply killer properties first to avoid useless work
-        if (array_key_exists($propertyName, $arKillerProperties)
-          && ($element[$propertyName] = $arKillerProperties[$propertyName]))
-          {
+        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($propertyName, $arDefaultPropertyTransformations))
-          {
-          $ret[$propertyName] = $arDefaultPropertyTransformations[$propertyName];
-          }
-        else
-          {
-          $ret[$propertyName] = $element[$propertyName];
-          }
+        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 ($propertyName == '#qbf')
-          {
-          foreach ($element[$propertyName] as $overrideName => $overrideValue)
-            {
-            $ret[$overrideName] = $overrideValue;
-            }
+        if ($property_name == '#qbf') {
+          foreach ($element[$property_name] as $override_name => $override_value) {
+            $ret[$override_name] = $override_value;
           }
         }
+      }
 
       // Recursively transform children
-      foreach (element_children($element) as $childName)
-        {
-        $child = _qbf_transform_element($childName, $element[$childName]);
-        if (is_null($child))
-          {
-          unset($ret[$childName]);
-          }
-        else
-          {
-          $ret[$childName] = $child;
-          }
+      foreach (element_children($element) as $child_name) {
+        $child = _qbf_transform_element($child_name, $element[$child_name]);
+        if (is_null($child)) {
+          unset($ret[$child_name]);
+        }
+        else {
+          $ret[$child_name] = $child;
         }
       }
     }
+  }
 
   //dsm(array('key' => $key, 'transformed element' => $ret));
   return $ret;
@@ -306,19 +289,17 @@ function qbf_perm() {
  * @return array
  */
 function qbf_forms() {
-  $hookName = 'qbf_register';
+  $hook_name = 'qbf_register';
 
-  foreach (module_implements($hookName) as $module)
-    {
-    foreach (module_invoke($module, $hookName) as $formName)
-      {
-      $forms["qbf_$formName"] = array
+  foreach (module_implements($hook_name) as $module) {
+    foreach (module_invoke($module, $hook_name) as $form_name) {
+      $forms["qbf_$form_name"] = array
         (
         'callback'           => 'qbf_transform_form',
-        'callback arguments' => array($formName),
+        'callback arguments' => array($form_name),
         );
-      }
     }
+  }
 
   return $forms;
 }
@@ -332,27 +313,24 @@ function qbf_forms() {
  * @return array
  */
 function qbf_after_build($form, $form_values) {
-  if (empty($form['#post']))
-    {
+  if (empty($form['#post'])) {
     return $form;
-    }
+  }
 
   // If #post is not empty, we are indeed querying
-  $arQuery = _qbf_extract_query($form, $form_values);
+  $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($arQuery);
-    }
-  else
-    {
+  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',
@@ -376,20 +354,17 @@ function _qbf_extract_query($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'])
-    && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL)
-    {
+    && $form['#qbf']['#level'] >= QBF_LEVEL_OPTIONAL) {
     $ret = array($name => $form_values[$name]);
-    }
-  else
-    {
+  }
+  else {
     $ret = array();
-    }
+  }
 
   // QBF level is not inherited, so this loop is outside the "if" above
-  foreach (element_children($form) as $childName)
-    {
-    $ret += _qbf_extract_query($form[$childName], $form_values);
-    }
+  foreach (element_children($form) as $child_name) {
+    $ret += _qbf_extract_query($form[$child_name], $form_values);
+  }
 
   return $ret;
 }
@@ -397,37 +372,34 @@ function _qbf_extract_query($form, $form_values) {
 /**
  * Provide an optional automatic mapping mechanism for query building.
  *
- * This function takes a partly built query map $arQueryMap, and a defaults
- * array to complete it in $arDefaults, and returns a fully built query array
+ * 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 $arQuery
- * @param array $arDefaults
+ * @param array $ar_query
+ * @param array $ar_defaults
  * @return array
  */
-function qbf_query_mapper($arQueryMap = array(), $arDefaults = array()) {
+function qbf_query_mapper($ar_queryMap = array(), $ar_defaults = array()) {
   $ret = array();
 
-  foreach ($arQueryMap as $name => $value)
-    {
-    if (!is_array($value)) // accept NULL, empty strings...
-      {
+  foreach ($ar_queryMap as $name => $value) {
+    // accept NULL, empty strings...
+    if (!is_array($value)) {
       $value = array();
-      }
+    }
     $item = $value;
 
-    foreach ($arDefaults as $defaultKey => $defaultValue)
-      {
-      if (!array_key_exists($defaultKey, $item))
-        {
-        $item[$defaultKey] = is_null($defaultValue)
+    foreach ($ar_defaults as $default_key => $default_value) {
+      if (!array_key_exists($default_key, $item)) {
+        $item[$default_key] = is_null($default_value)
           ? $name
-          : $defaultValue;
-        }
-      // else if is already in $item, so we don't touch it
+          : $default_value;
       }
-    $ret[$name] = $item;
+      // else if is already in $item, so we don't touch it
     }
+    $ret[$name] = $item;
+  }
   return $ret;
 }
 
@@ -445,14 +417,12 @@ function qbf_query_mapper($arQueryMap = array(), $arDefaults = array()) {
  * @return array The modified form
  */
 function qbf_import_values($element, $form_values) {
-  foreach (element_children($element) as $childName)
-    {
-    if (!empty($form_values[$childName]))
-      {
-      $element[$childName]['#qbf']['#default_value'] = $form_values[$childName];
-      }
-    $element[$childName] = qbf_import_values($element[$childName], $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;
 }
 
@@ -469,20 +439,19 @@ function qbf_import_values($element, $form_values) {
  */
 function qbf_query_load($qid) {
   $sq = 'SELECT qq.qid, qq.uid, qq.query, qq.name '
-      . 'FROM {%s} qq '
-      . 'WHERE qq.qid = %d ';
+      .'FROM {%s} qq '
+      .'WHERE qq.qid = %d ';
   // db_rewrite_sql does not apply here until we add more advanced support for access control
   $q = db_query($sq, QBF_TABLE_NAME, $qid);
   $ret = db_fetch_object($q); // 0 or 1 row: we are querying on the primary key
-  if ($ret === NULL) // FALSE does not happen
-    {
+  // FALSE does not happen
+  if ($ret === NULL) {
     $ret = NULL;
-    }
-  else
-    {
+  }
+  else {
     $ret->query = unserialize($ret->query);
     //dsm($ret);
-    }
+  }
   return $ret;
 }
 
@@ -495,8 +464,7 @@ function qbf_query_load($qid) {
  * @return string
  */
 function qbf_submit($form_id, $form_values) {
-  switch ($form_values['op'])
-    {
+  switch ($form_values['op']) {
     case t('Search'):
       $ret = FALSE;
       break;
@@ -508,7 +476,7 @@ function qbf_submit($form_id, $form_values) {
       global $user;
       $ret = "user/$user->uid/edit/job";;
       break;
-    }
+  }
   //dsm(array('QS' => $form_values));
   return $ret;
 }
@@ -520,22 +488,20 @@ function qbf_submit($form_id, $form_values) {
  * @return array
  */
 function qbf_get_queries_by_user($uid = NULL) {
-  if (is_null($uid))
-    {
+  if (is_null($uid)) {
     global $user;
     $uid = $user->uid;
-    }
+  }
 
   $sq = 'SELECT qq.qid, qq.uid, qq.name, qq.query '
-      . 'FROM {%s} qq '
-      . 'WHERE qq.uid = %d '
-      . 'ORDER BY qq.name ';
+      .'FROM {%s} qq '
+      .'WHERE qq.uid = %d '
+      .'ORDER BY qq.name ';
   $q = db_query($sq, QBF_TABLE_NAME, $uid);
   $ret = array();
-  while ($o = db_fetch_object($q))
-    {
+  while ($o = db_fetch_object($q)) {
     $ret[$o->qid] = $o; // qid is the PK, so it is present and unique
-    }
+  }
   return $ret;
 }
 
@@ -550,41 +516,36 @@ function qbf_get_queries_by_user($uid = NULL) {
 function _qbf_save($form_id, $form_values) {
   global $user;
 
-  if ($user->uid == 0)
-    {
+  if ($user->uid == 0) {
     $warning = t('Attempt by anonymous user to save a QBF query. Should not happen.');
     drupal_set_message($warning, 'error');
     watchdog('qbf', $warning, WATCHDOG_WARNING);
     $ret = 0;
-    }
-  else
-    {
+  }
+  else {
     $form = drupal_retrieve_form($form_id);
     drupal_prepare_form($form_id, $form);
     $name = $form_values['save-name'];
     $form_values = _qbf_extract_query($form, $form_values);
-    $arValues = array();
-    foreach ($form_values as $key => $value)
-      {
-    	if (empty($value))
-    	  {
-    	  continue;
-    	  }
-    	$arValues[$key] = $value;
+    $ar_values = array();
+    foreach ($form_values as $key => $value) {
+      if (empty($value)) {
+        continue;
       }
+      $ar_values[$key] = $value;
+    }
 
     // Avoid duplicates
-    if (!empty($name))
-      {
+    if (!empty($name)) {
       $sq = "DELETE FROM {%s} WHERE name = '%s'";
       db_query($sq, QBF_TABLE_NAME, $name);
-      }
+    }
 
     $sq = 'INSERT INTO {%s} (qid, uid, name, query) '
          ."VALUES           (%d,  %d,  '%s', '%s' ) ";
     $ret = db_next_id('qbf_qid');
-    $q = db_query($sq, QBF_TABLE_NAME, $ret, $user->uid, $name, serialize($arValues));
-    }
+    $q = db_query($sq, QBF_TABLE_NAME, $ret, $user->uid, $name, serialize($ar_values));
+  }
 
   return $ret;
 }
@@ -598,8 +559,7 @@ function _qbf_save($form_id, $form_values) {
 function qbf_menu($may_cache) {
 
   $items = array();
-  if ($may_cache)
-    {
+  if ($may_cache) {
     $items[] = array
       (
       'path'               => QBF_PATH_SETTINGS,
@@ -609,23 +569,21 @@ function qbf_menu($may_cache) {
       'callback arguments' => 'qbf_admin_settings',
       'type'               => MENU_NORMAL_ITEM,
       );
-    }
-  else
-    {
-    if ((arg(0) == QBF_PATH_MAIN) && is_numeric(arg(1)) && arg(1) > 0 && arg(2) == 'delete')
-      {
+  }
+  else {
+    if ((arg(0) == QBF_PATH_MAIN) && is_numeric(arg(1)) && arg(1) > 0 && arg(2) == 'delete') {
       $qid = arg(1);
-      $querorAccess = user_access(QBF_PERM_QUERY);
+      $queror_access = user_access(QBF_PERM_QUERY);
       $items[] = array
         (
-        'path'     => QBF_PATH_MAIN . '/' . $qid . '/delete',
+        'path'     => QBF_PATH_MAIN .'/'. $qid .'/delete',
         'type'     => MENU_CALLBACK,
-        'access'   => $querorAccess,
+        'access'   => $queror_access,
         'callback' => '_qbf_query_delete',
         'callback arguments' => array($qid),
         );
-      }
     }
+  }
 
   return $items;
 }
@@ -644,11 +602,10 @@ function _qbf_query_delete($qid) {
 
   $query = qbf_query_load($qid);
   $notify = variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE);
-  $link = l($qid, QBF_PATH_MAIN . '/' . $qid . '/delete');
+  $link = l($qid, QBF_PATH_MAIN .'/'. $qid .'/delete');
 
   // only valid if valid query, and owner or admin
-  if (isset($query->uid) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN)))
-    {
+  if (isset($query->uid) && (($query->uid == $user->uid) || user_access(QBF_PERM_ADMIN))) {
     $sq = 'DELETE FROM %s WHERE qid = %d ';
     $q = db_query($sq, QBF_TABLE_NAME, $qid);
     $message = t('Query @id "@name" has been deleted.', array
@@ -658,19 +615,17 @@ function _qbf_query_delete($qid) {
       ));
     drupal_set_message($message, 'status');
     watchdog('qbf', $message, WATCHDOG_NOTICE, $link);
-    if (variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE) && $query->uid != $user->uid)
-      {
+    if (variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE) && $query->uid != $user->uid) {
       $ret = drupal_mail(__FUNCTION__, $user->mail, $message, $message, $user->mail);
       $account = user_load(array('uid' => $query->uid));
       drupal_set_message(t('User !link has been informed', array
         (
-        '!link' => l($account->name, 'user/' . $query->uid),
+        '!link' => l($account->name, 'user/'. $query->uid),
         )));
       dsm($ret);
-      }
     }
-  else
-    {
+  }
+  else {
     $message = t('Failed attempt to delete query @qid. Administrators has been alerted.', array
       (
       '@qid' => $qid,