Переглянути джерело

creating tripal crn hook function for db write and dashboard cleanup

Shawna Spoor 8 роки тому
батько
коміт
0e8cfc47ca

+ 76 - 52
tripal/api/tripal.entities.api.inc

@@ -192,6 +192,60 @@ function hook_bundle_create(&$bundle, $storage_args) {
  */
 function hook_bundle_postcreate(&$bundle) {
 
+}
+/**
+ * Allows a module to add admin notifications to the associated tripal table
+ * during the cron run.
+ *
+ */
+function hook_tripal_cron_notification() {
+
+}
+/**
+ * Allows a module to write to the admin notification table
+ * during the cron run.
+ *
+ * @param $title
+ *   A generic phrase indicating what the notification is for.
+ * @param $details
+ *   A human-readable sentence or two describing the issue.
+ * @param $type
+ *   A one word type indicating the type of notification. Tripal types include:  Jobs, Fields.
+ *   If not type is required please pass NULL.
+ * @param $actions
+ *   A serialized PHP associative array containing the link and URL for each action.
+ *   If not type is required please pass NULL.
+ * @param $submitter_id
+ *   A unique ID provided by the submitter for checking to make sure that the notification is not added more than once.
+ */
+function tripal_add_notifcation($title, $details, $type, $actions, $submitter_id) {
+  $transaction = db_transaction();
+
+  // Check the notification isn't already in the admin notification table.
+  $dedup = db_select('tripal_admin_notfications', 'tan')
+    ->fields('tan')
+    ->condition('submitter_id', $submitter_id, '=')
+    ->execute()->fetchAll();
+
+  if (empty($dedup)) {
+    try {
+      $record = new stdClass;
+      $record->details = $details;
+      $record->title = $title;
+      $record->submitter_id = $submitter_id;
+      $record->actions = serialize($actions);
+      $record->enabled = 1;
+      $record->type = $type;
+      drupal_write_record('tripal_admin_notfications', $record);
+    }
+    catch (Exception $e) {
+      $transaction->rollback();
+      watchdog('tripal_cron', 'Could not write notification to database.');
+    }
+    if ($success) {
+      watchdog('tripal_cron', 'New field notification created.');
+    }
+  }
 }
 /**
  * Creates a new Tripal Entity type (i.e. bundle).
@@ -397,7 +451,11 @@ function tripal_get_content_types() {
  */
 function tripal_cron() {
   if (variable_get('tripal_admin_notification_creation_during_cron', TRUE)) {
-    tripal_refresh_bundle_fields_cron();
+    $modules = module_implements('tripal_cron_notification');
+    foreach ($modules as $module) {
+      $function = $module . '_tripal_cron_notification';
+      $function();
+    }
     watchdog('tripal_cron', 'tripal_cron ran');
   }
 }
@@ -408,7 +466,7 @@ function tripal_cron() {
  * @param $bundle_name
  *   The name of the bundle to refresh (e.g. bio_data_4).
  */
-function tripal_refresh_bundle_fields_cron() {
+function tripal_tripal_cron_notification() {
   $num_created = 0;
 
   // Get all bundle names to cycle through.
@@ -430,25 +488,20 @@ function tripal_refresh_bundle_fields_cron() {
       $function = $module . '_bundle_create_fields';
       $info = $function('TripalEntity', $bundle);
       foreach ($info as $field_name => $details) {
-        $field_type = $details['type'];
-
         // If the field already exists then skip it.
         $field = field_info_field($details['field_name']);
         if ($field) {
           continue;
         }
+
         // Create notification that new fields exist.
-        $detail_info = 'Bundle: ' . $bundle_name->name. '. Type: ' . $details['storage']['type'] . '.  Field name: ' . $details['field_name'] . '.';
-        // Check the notification isn't already in the admin notification table.
-        $notifications = db_select('tripal_admin_notfications', 'tan')
-          ->fields('tan')
-          ->condition('title', $details['field_name'], '=')
-          ->condition('bundle_id', $bundle_name->name, '=')
-          ->execute()->fetchAll();
-        if ($notifications) {
-          continue;
-        }
-        tripal_admin_notifications_creation($detail_info, $details['field_name'], $bundle_name->name, $module, TRUE, 'Field');
+        $detail_info = ' Tripal has detected a field named ' . $details['field_name'] . ' is available for the ' . $bundle->label. ' content type.';
+        $title = 'New field available';
+        $actions['import'] = 'admin/import/field/' . $details['field_name'] . '/' . $bundle_name->name . '/' . $module;
+        $type = 'Field';
+        $submitter_id = $details['field_name'] . '-' . $bundle_name->name . '-' . $module;
+
+        tripal_add_notifcation($title, $detail_info, $type, $actions, $submitter_id);
         $num_created++;
       }
     }
@@ -466,18 +519,15 @@ function tripal_refresh_bundle_fields_cron() {
           in_array($bundle->name, $field['bundles']['TripalEntity'])) {
           continue;
         }
+
         // Create notification that new fields exist.
-        $detail_info = 'Bundle: ' . $bundle_name->name. '. Type: ' . $details['storage']['type'] . '.  Field name: ' . $details['field_name'] . '.';
-        // Check the notification isn't already in the admin notification table.
-        $notifications = db_select('tripal_admin_notfications', 'tan')
-          ->fields('tan')
-          ->condition('title', $details['field_name'], '=')
-          ->condition('bundle_id', $bundle_name->name, '=')
-          ->execute()->fetchAll();
-        if ($notifications) {
-          continue;
-        }
-        tripal_admin_notifications_creation($detail_info, $details['field_name'], $bundle_name->name, $module, TRUE, 'Field');
+        $detail_info = ' Tripal has detected a field named ' . $details['field_name'] . ' is available for the ' . $bundle->name. ' content type.';
+        $title = 'New field available';
+        $actions['import'] = 'admin/import/field/' . $details['field_name'] . '/' . $bundle->label . '/' . $module;
+        $type = 'Field';
+        $submitter_id = $details['field_name'] . '-' . $bundle_name->name . '-' . $module;
+
+        tripal_add_notifcation($title, $detail_info, $type, $actions, $submitter_id);
         $num_created++;
       }
     }
@@ -487,32 +537,6 @@ function tripal_refresh_bundle_fields_cron() {
   }
 }
 
-
-/**
- * Implements .
- */
-function tripal_admin_notifications_creation($info, $title, $bundle, $module, $enabled, $type) {
-  try {
-    $success = db_insert('tripal_admin_notfications')
-      ->fields( array(
-        'info' => $info,
-        'title' => $title,
-        'bundle_id' => $bundle,
-        'module' => $module,
-        'enabled' => $enabled,
-        'type' => $type,
-      ))
-      ->execute();
-  }
-  catch(Exception $e) {
-    watchdog('tripal_cron', 'Could not write notification to database.');
-  }
-  if($success){
-    watchdog('tripal_cron', 'New field notification created.');
-  }
-}
-
-
 /**
  * Retrieves information about a given content type.
  *

+ 54 - 29
tripal/includes/tripal_admin_usage_page.inc

@@ -14,42 +14,67 @@ function tripal_admin_usage_page() {
   tripal_add_d3js();
   //drupal_add_js(drupal_get_path ('module', 'tripal') . '/theme/js/tripal_galaxy.dashboard.js');
   drupal_add_css(drupal_get_path ('module', 'tripal') . '/theme/css/tripal.dashboard.css');
-  $results = db_select('tripal_admin_notfications', 'tan')
-    ->fields('tan')
+
+  // Prepare table header
+  $header = array(
+    'title' => array('data' => t('Title')),
+    'details' => array('data' => t('Details')),
+    'type' => array('data' => t('Type'), 'field' => 'tan.type'),
+    'operations' => array('date' => t('Operations'))
+  );
+
+  $query = db_select('tripal_admin_notfications', 'tan')
+    ->extend('TableSort');
+
+  $results = $query->fields('tan')
     ->condition('enabled', 1, '=')
+    ->orderByHeader($header)
     ->execute()->fetchAll();
   $rows = array();
 
   foreach($results as $result){
-    $data['operation'] = l(t('Import'), 'admin/import/field/' . $result->title . '/' .$result->bundle_id . '/' . $result->module);
-    $data['operation'] .= "|";
+    $data['operation'] = ' | ';
     $data['operation'] .= l(t('Dismiss Notification'), 'admin/disable/notification/' . $result->note_id);
+
+    $actions = unserialize($result->actions);
+    foreach($actions as $action){
+      $label = key($actions);
+      $link = $action;
+    }
+
     $rows[] = array(
       'Title' => $result->title,
-      'Details' => $result->info,
-      'Operations' => $data['operation'],
+      'Details' => $result->details,
+      'Type' => $result->type,
+      'Operations' => l(t($label), $link) . $data['operation'],
       );
   }
-  //Number of records shown in per page
-  $per_page = 20;
-  $current_page = pager_default_initialize(count($rows), $per_page);
-  $chunks = array_chunk($rows, $per_page, TRUE);
-  // Prepare table header
-  $header = array(t('Title'), t('Details'), t('Operations'));
-  // Output of table with the paging
-  $output = theme('table',
-    array(
-      "header" => $header,
-      "rows" => $chunks[$current_page],
-      "attributes" => array(),
-      "sticky" => TRUE,
-      "caption" => "",
-      "colgroups" => array(),
-      "empty" => t("No Fields for Import")
-    )
-  );
-  //return pager with limited number of records.
-  return $output .= theme('pager', array('quantity', count($rows)));
+  if(!empty($rows)) {
+    //Number of records shown in per page
+    $per_page = 20;
+    $current_page = pager_default_initialize(count($rows), $per_page);
+    $chunks = array_chunk($rows, $per_page, TRUE);
+
+    // Output of table with the paging
+    $output = '<h2>Notifications</h2>';
+    $output .= theme('table',
+      array(
+        "header" => $header,
+        "rows" => $chunks[ $current_page ],
+        "attributes" => array(),
+        "sticky" => TRUE,
+        "caption" => "",
+        "colgroups" => array(),
+        "empty" => t("No notifications.")
+      )
+    );
+    //return pager with limited number of records.
+    return $output .= theme('pager', array('quantity', count($rows)));
+  }
+  else {
+    $output = 'There are no notifications at this time.';
+    return $output;
+  }
 }
 
 /**
@@ -80,7 +105,7 @@ function tripal_admin_notification_import_field($field_name_note, $bundle_id, $m
       $field = field_create_field($details);
       if (!$field) {
         tripal_set_message(t("Could not create new field: %field.",
-          array('%field' =>  $details['field_name'])), TRIPAL_ERROR);
+          array('%field' =>  $field_name_note)), TRIPAL_ERROR);
       }
     }
   }
@@ -93,11 +118,11 @@ function tripal_admin_notification_import_field($field_name_note, $bundle_id, $m
       drupal_set_message(t("Created field: %field", array('%field' => $info[ $field_name ]['label'])));
     }
   }
+  $submitter_id = $field_name_note . '-' . $bundle_id . '-' . $module;
   if($instance){
     // Delete the notification table entry.
     db_delete('tripal_admin_notfications')
-      ->condition('bundle_id', $bundle_id)
-      ->condition('title', $field_name_note)
+      ->condition('submitter_id', $submitter_id, '=')
       ->execute();
   }
   else{

+ 47 - 46
tripal/tripal.install

@@ -719,23 +719,23 @@ function tripal_tripal_admin_notifications_schema() {
         'type' => 'serial',
         'not null' => TRUE,
       ),
-      'info' => array (
+      'details' => array (
         'description' => 'Description and additional information relating to the notification.',
         'type' => 'text',
-        'not null' => FALSE,
+        'not null' => TRUE,
       ),
       'title' => array (
         'description' => 'Title of the notification.',
         'type' => 'text',
         'not null' => TRUE,
       ),
-      'bundle_id' => array (
-        'description' => 'Bundle ID of the entity associated with the field.',
+      'actions' => array (
+        'description' => 'Actions that can be performed on the notification, like disimissal or import.',
         'type' => 'text',
-        'not null' => TRUE,
+        'not null' => FALSE,
       ),
-      'module' => array (
-        'description' => 'Module that has added the field.',
+      'submitter_id' => array (
+        'description' => 'A unique id that should be specific to the notification to ensure notifications are not duplicated.',
         'type' => 'text',
         'not null' => TRUE,
       ),
@@ -748,7 +748,7 @@ function tripal_tripal_admin_notifications_schema() {
       'type' => array (
         'description' => 'Type of the notification, relating to what tripal function the notification belongs to, IE Fields, Jobs, Vocabulary.',
         'type' => 'text',
-        'not null' => TRUE,
+        'not null' => FALSE,
       ),
     ),
     'primary key' => array (
@@ -781,47 +781,48 @@ function tripal_update_7300() {
  * Create new admin notifications table.
  */
 function tripal_update_7301() {
+  $transaction = db_transaction();
   try {
     $schema['tripal_admin_notfications'] = array(
-        'description' => 'This table is used for information describing administrative
-         notifications. For example, when new fields are available.',
-        'fields' => array (
-          'note_id' => array (
-            'type' => 'serial',
-            'not null' => TRUE,
-          ),
-          'info' => array (
-            'description' => 'Description and additional information relating to the notification.',
-            'type' => 'text',
-            'not null' => FALSE,
-          ),
-          'title' => array (
-            'description' => 'Title of the notification.',
-            'type' => 'text',
-            'not null' => TRUE,
-          ),
-          'bundle_id' => array (
-            'description' => 'Bundle ID of the entity associated with the field.',
-            'type' => 'text',
-            'not null' => TRUE,
-          ),
-          'module' => array (
-            'description' => 'Module that has added the field.',
-            'type' => 'text',
-            'not null' => TRUE,
-          ),
-          'enabled' => array (
-            'description' => 'Boolean indicating whether the notification is enabled or disabled (disabled will not be shown on the dashboard).',
-            'type' => 'int',
-            'not null' => TRUE,
-            'default' => 1,
-          ),
-          'type' => array (
-            'description' => 'Type of the notification, relating to what tripal function the notification belongs to, IE Fields, Jobs, Vocabulary.',
-            'type' => 'text',
-            'not null' => TRUE,
-          ),
+      'description' => 'This table is used for information describing administrative
+       notifications. For example, when new fields are available.',
+      'fields' => array (
+        'note_id' => array (
+          'type' => 'serial',
+          'not null' => TRUE,
         ),
+        'details' => array (
+          'description' => 'Description and additional information relating to the notification.',
+          'type' => 'text',
+          'not null' => TRUE,
+        ),
+        'title' => array (
+          'description' => 'Title of the notification.',
+          'type' => 'text',
+          'not null' => TRUE,
+        ),
+        'actions' => array (
+          'description' => 'Actions that can be performed on the notification, like disimissal or import.',
+          'type' => 'text',
+          'not null' => FALSE,
+        ),
+        'submitter_id' => array (
+          'description' => 'A unique id that should be specific to the notification to ensure notifications are not duplicated.',
+          'type' => 'text',
+          'not null' => TRUE,
+        ),
+        'enabled' => array (
+          'description' => 'Boolean indicating whether the notification is enabled or disabled (disabled will not be shown on the dashboard).',
+          'type' => 'int',
+          'not null' => TRUE,
+          'default' => 1,
+        ),
+        'type' => array (
+          'description' => 'Type of the notification, relating to what tripal function the notification belongs to, IE Fields, Jobs, Vocabulary.',
+          'type' => 'text',
+          'not null' => FALSE,
+        ),
+      ),
         'primary key' => array (
           0 => 'note_id',
         ),