Forráskód Böngészése

new dashboard tying new field notifications into the cron run

Shawna Spoor 8 éve
szülő
commit
f0265f2837

+ 121 - 0
tripal/api/tripal.entities.api.inc

@@ -392,6 +392,127 @@ function tripal_get_content_types() {
     ->fetchAll();
 }
 
+/**
+ * Implements hook_cron().
+ */
+function tripal_cron() {
+  if (variable_get('tripal_admin_notification_creation_during_cron', TRUE)) {
+    tripal_refresh_bundle_fields_cron();
+    watchdog('tripal_cron', 'tripal_cron ran');
+  }
+}
+
+/**
+ * Refreshes the bundle such that new fields added by modules will be found during cron.
+ *
+ * @param $bundle_name
+ *   The name of the bundle to refresh (e.g. bio_data_4).
+ */
+function tripal_refresh_bundle_fields_cron() {
+  $num_created = 0;
+
+  // Get all bundle names to cycle through.
+  $all_bundles = db_select('tripal_bundle', 'tb')
+    ->fields('tb', array('name'))
+    ->execute()->fetchAll();
+
+  foreach ($all_bundles as $bundle_name){
+    // Get the bundle object.
+    $bundle = tripal_load_bundle_entity(array('name' => $bundle_name->name));
+    if (!$bundle) {
+      tripal_report_error('tripal', TRIPAL_ERROR, "Unrecognized bundle name '%bundle'.",
+        array('%bundle' => $bundle_name));
+      return FALSE;
+    }
+    // Allow modules to add fields to the new bundle.
+    $modules = module_implements('bundle_create_fields');
+    foreach ($modules as $module) {
+      $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');
+        $num_created++;
+      }
+    }
+
+    // Allow modules to add instances to the new bundle.
+    $modules = module_implements('bundle_create_instances');
+    foreach ($modules as $module) {
+      $function = $module . '_bundle_create_instances';
+      $info = $function('TripalEntity', $bundle);
+      foreach ($info as $field_name => $details) {
+        // If the field is already attached to this bundle then skip it.
+        $field = field_info_field($details['field_name']);
+        if ($field and array_key_exists('bundles', $field) and
+          array_key_exists('TripalEntity', $field['bundles']) and
+          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');
+        $num_created++;
+      }
+    }
+    if ($num_created == 0) {
+      watchdog('tripal_cron', '<pre>No new fields for '. print_r($bundle_name->name, TRUE) .'</pre>');
+    }
+  }
+}
+
+
+/**
+ * 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.
  *

+ 126 - 0
tripal/includes/tripal_admin_usage_page.inc

@@ -0,0 +1,126 @@
+<?php
+function tripal_admin_usage_page() {
+  // set the breadcrumb
+  $breadcrumb = array();
+  $breadcrumb[] = l('Home', '<front>');
+  $breadcrumb[] = l('Administration', 'admin');
+  $breadcrumb[] = l('Tripal', 'admin/tripal');
+  drupal_set_breadcrumb($breadcrumb);
+  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')
+    ->condition('enabled', 1, '=')
+    ->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'] .= l(t('Dismiss Notification'), 'admin/disable/notification/' . $result->note_id);
+    $rows[] = array(
+      'Title' => $result->title,
+      'Details' => $result->info,
+      'Operations' => $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)));
+}
+
+/**
+ * Import the field from the admin notification table on
+ * the dashboard.
+ *
+ * @param $field_name
+ *   The name of the field to be imported.
+ *
+ *  * @param $bundle_id
+ *   The ID of the bundle associated with that field.
+ *
+ */
+function tripal_admin_notification_import_field($field_name_note, $bundle_id, $module) {
+  // Get the bundle object.
+  $bundle = tripal_load_bundle_entity(array('name' => $bundle_id));
+  if (!$bundle) {
+    tripal_report_error('tripal', TRIPAL_ERROR, "Unrecognized bundle name '%bundle'.",
+      array('%bundle' => $bundle_id));
+    return FALSE;
+  }
+
+  $function = $module . '_bundle_create_fields';
+  $info = $function('TripalEntity', $bundle);
+  foreach ($info as $field_name => $details) {
+    if($details['field_name'] == $field_name_note) {
+      // Create the field.
+      $field = field_create_field($details);
+      if (!$field) {
+        tripal_set_message(t("Could not create new field: %field.",
+          array('%field' =>  $details['field_name'])), TRIPAL_ERROR);
+      }
+    }
+  }
+  $function = $module . '_bundle_create_instances';
+  $info = $function('TripalEntity', $bundle);
+  foreach ($info as $field_name => $details) {
+    if($details['field_name'] == $field_name_note) {
+      // Create the field instance.
+      $instance = field_create_instance($details);
+      drupal_set_message(t("Created field: %field", array('%field' => $info[ $field_name ]['label'])));
+    }
+  }
+  if($instance){
+    // Delete the notification table entry.
+    db_delete('tripal_admin_notfications')
+      ->condition('bundle_id', $bundle_id)
+      ->condition('title', $field_name_note)
+      ->execute();
+  }
+  else{
+    drupal_set_message(t("There was a problem creating: %field", array('%field' => $info[ $field_name ]['label'])));
+
+  }
+  drupal_goto("admin/tripal/dashboard");
+}
+
+/**
+ * Disable the notification of the field on the dashboard.
+ *
+ * @param $note_id
+ *   The ID of the note in the tripal_admin_notifications table
+ * that will be dismissed.
+ */
+function tripal_disable_admin_notification($note_id) {
+  $success = db_update('tripal_admin_notfications')
+          ->fields(array(
+            'enabled' => 0,
+          ))
+          ->condition('note_id', $note_id, '=')
+          ->execute();
+  if($success){
+    drupal_set_message("That notification has been dismissed and will no longer appear.");
+  }
+  else {
+    drupal_set_message("Could not dismiss notification.", 'error');
+  }
+  drupal_goto("admin/tripal/dashboard");
+}

+ 117 - 2
tripal/tripal.install

@@ -191,7 +191,8 @@ function tripal_schema() {
 
   // Adds a table for additional information related to bundles.
   $schema['tripal_bundle_variables'] = tripal_tripal_bundle_variables_schema();
-
+  // Adds a table for administrative notifications on the dashboard.
+  $schema['tripal_admin_notfications'] = tripal_tripal_admin_notifications_schema();
   return $schema;
 }
 function tripal_tripal_jobs_schema() {
@@ -702,6 +703,62 @@ function tripal_tripal_bundle_variables_schema() {
   return $schema;
 }
 
+/**
+ * Additional Tripal Admin Notification Information.
+ *
+ * This table is used for information describing administrative
+ * notifications. For example, when new fields are available.
+ */
+function tripal_tripal_admin_notifications_schema() {
+
+  $schema = 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,
+      ),
+    ),
+    'primary key' => array (
+      0 => 'note_id',
+    ),
+  );
+
+  return $schema;
+}
+
 /**
  * Change tripal_vocab.vocabulary to varchar(128)
  */
@@ -718,4 +775,62 @@ function tripal_update_7300() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Could not perform update: '. $error);
   }
-}
+}
+
+/**
+ * Create new admin notifications table.
+ */
+function tripal_update_7301() {
+  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,
+          ),
+        ),
+        'primary key' => array (
+          0 => 'note_id',
+        ),
+    );
+    db_create_table('tripal_admin_notfications', $schema['tripal_admin_notfications']);
+  }
+  catch (\PDOException $e) {
+      $transaction->rollback();
+      $error = $e->getMessage();
+      throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
+}

+ 33 - 0
tripal/tripal.module

@@ -118,6 +118,18 @@ function tripal_menu() {
     'weight' => 8,
     'access arguments' => array('administer tripal'),
   );
+
+  $items['admin/tripal/dashboard'] = array(
+    'title' => 'Dashboard',
+    'description' => t("A dashboard view of Tripal including new fields for entities."),
+    'weight' => 0,
+    'page callback' => 'tripal_admin_usage_page',
+    'access arguments' => array('administer tripal'),
+    'type' => MENU_NORMAL_ITEM,
+    'file' => 'includes/tripal_admin_usage_page.inc',
+    'file path' => drupal_get_path('module', 'tripal'),
+  );
+
   $items['admin/tripal/terms/import'] = array(
     'title' => 'Import Vocabulary',
     'description' => t("Import vocabularies and terms in OBO format."),
@@ -238,6 +250,27 @@ function tripal_menu() {
     'file path' => drupal_get_path('module', 'tripal'),
   );
 
+  /*
+   * Dashboard Action Item callbacks.
+   */
+  $items['admin/disable/notification/%'] = array(
+    'page callback' => 'tripal_disable_admin_notification',
+    'page arguments' => array(3),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+    'file' => 'includes/tripal_admin_usage_page.inc',
+    'file path' => drupal_get_path('module', 'tripal'),
+  );
+
+  $items['admin/import/field/%/%'] = array(
+    'page callback' => 'tripal_admin_notification_import_field',
+    'page arguments' => array(3, 4),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+    'file' => 'includes/tripal_admin_usage_page.inc',
+    'file path' => drupal_get_path('module', 'tripal'),
+  );
+
   /*
    * Term Lookup
    */