Sfoglia il codice sorgente

- Saved query deletion with redirect now works
- Safety needs to be checkd for redirect
- Needs settings page for notification default

Frederic G. Marand 16 anni fa
parent
commit
156ceb01b9
1 ha cambiato i file con 95 aggiunte e 4 eliminazioni
  1. 95 4
      qbf.module

+ 95 - 4
qbf.module

@@ -12,7 +12,7 @@
  * @package QBF
  */
 
-// $Id: qbf.module,v 1.9.2.2 2008-10-03 13:48:59 marand Exp $
+// $Id: qbf.module,v 1.9.2.3 2008-10-03 17:40:40 marand Exp $
 
 /**
  * Saved error reporting level.
@@ -46,8 +46,13 @@ define('QBF_LEVEL_OPTIONAL',         2);
 define('QBF_LEVEL_REQUIRED',         3);
 
 /**
- * The main QBF path
+ * The main QBF path.
+ *
+ * It MUST be a single component path, without a "/", otherwise qbf_menu() will
+ * need to be changed.
+ *
  * @ingroup paths
+ * @see qbf_menu()
  */
 define('QBF_PATH_MAIN',              'qbf');
 /**
@@ -70,6 +75,15 @@ define('QBF_PERM_ADMIN',             'administer QBF');
  */
 define('QBF_TABLE_NAME',             'qbf_queries');
 
+/**
+ * Notify owner about saved query deletions, variable name.
+ */
+define('QBF_VAR_NOTIFY_DELETE',      'qbf_notify_delete');
+/**
+ * Notify owner about saved query deletions, default value.
+ */
+define('QBF_DEF_NOTIFY_DELETE',      FALSE);
+
 /**
  * Transform a form array for QBF.
  *
@@ -447,13 +461,13 @@ function qbf_import_values($element, $form_values) {
  * @return array A form_values array usable by qbf_import_values
  */
 function qbf_load($qid) {
-  $sq = 'SELECT qq.qid, qq.uid, qq.query '
+  $sq = 'SELECT qq.qid, qq.uid, qq.query, qq.name '
       . '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 === FALSE)
+  if ($ret === NULL) // FALSE does not happen
     {
     $ret = NULL;
     }
@@ -546,4 +560,81 @@ function _qbf_save($form_id, $form_values) {
   return $ret;
 }
 
+/**
+ * Implement hook_menu().
+ *
+ * @param $may_cache boolean
+ * @return array
+ */
+function qbf_menu($may_cache) {
+
+  $items = array();
+  if ($may_cache)
+    {
+    }
+  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);
+      $items[] = array
+        (
+        'path'     => QBF_PATH_MAIN . '/' . $qid . '/delete',
+        'type'     => MENU_CALLBACK,
+        'access'   => $querorAccess,
+        'callback' => '_qbf_query_delete',
+        'callback arguments' => array($qid),
+        );
+      }
+    }
+
+  return $items;
+}
+
+/**
+ * Delete a query by qid
+ *
+ * $qid has been tested in qbf_menu() to be a positive integer, so it is a safe
+ * number, but we still need to know more about it.
+ *
+ * @param $qid integer
+ */
+function _qbf_query_delete($qid) {
+
+  global $user;
+
+  $query = qbf_load($qid);
+  $notify = variable_get(QBF_VAR_NOTIFY_DELETE, QBF_DEF_NOTIFY_DELETE);
+  $link = l($qid, QBF_PATH_MAIN . '/' . $qid . '/delete');
+
+  // @todo Check safety, this seem dangerous
+  $usArgs = func_get_args();
+  $path = implode('/', array_slice($usArgs, 1));
+
+  // only valid if valid query, and owner or 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
+      (
+      '@id'    => $qid,
+      '@name' => $query->name,
+      ));
+    drupal_set_message($message, 'status');
+    watchdog('qbf', $message, WATCHDOG_NOTICE, $link);
+    }
+  else
+    {
+    $message = t('Failed attempt to delete query @qid. Administrators has been alerted.', array
+      (
+      '@qid' => $qid,
+      ));
+    drupal_set_message($message, 'error');
+    watchdog('qbf', $message, WATCHDOG_ERROR, $link);
+    }
+  drupal_goto($path);
+}
+
 error_reporting($_qbf_er);