Ver código fonte

Added admin page for collections and automatic expiration

Stephen Ficklin 7 anos atrás
pai
commit
a2cb5e5eb3

+ 50 - 0
tripal/api/tripal.collections.api.inc

@@ -45,6 +45,56 @@ function tripal_create_collection($details) {
   return $collection;
 }
 
+/**
+ * Retrieves an array of collections for a user.
+ *
+ * @param $uid
+ *   The User ID
+ * @return
+ *   An array of TripalEntityCollection objects.
+ */
+function tripal_get_user_collections($uid) {
+  if (!$uid) {
+    throw new Exception('tripal_get_user_collections: Missing the $uid argument.');
+  }
+  $user = user_load($uid);
+  if (!$user) {
+    throw new Exception('tripal_get_user_collections: Invalid $uid provided.');
+  }
+
+  $collections = array();
+  $results = db_select('tripal_collection', 'tc')
+    ->fields('tc', array('collection_id'))
+    ->condition('uid', $uid)
+    ->execute();
+  while ($collection_id = $results->fetchField()) {
+    $collection = new TripalEntityCollection();
+    $collection->load($collection_id);
+    $collections[] = $collection;
+  }
+  return $collections;
+}
+
+/**
+ * Deletes all collections that have surpassed their lifespan
+ */
+function tripal_expire_collections() {
+  $max_hours = variable_get('tripal_data_collections_lifespan', 7);
+  $ctime = time();
+
+  $query = db_select('tripal_collection', 'tc');
+  $query->fields('tc', array('collection_id'));
+  $query->where("(($ctime - create_date) / 60) / 60 >= $max_hours");
+  $results = $query->execute();
+  while ($collection_id = $results->fetchField()) {
+    $collection = new TripalEntityCollection();
+    $collection->load($collection_id);
+    $collections[] = $collection;
+    $collection->delete();
+  }
+  return $collections;
+}
+
 /**
  * Retrieve a collection using the collection ID
  *

+ 1 - 1
tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc

@@ -4,7 +4,7 @@ class TripalCSVDownloader extends TripalFieldDownloader {
   /**
    * Sets the label shown to the user describing this formatter.
    */
-  static public $label = 'CSV';
+  static public $label = 'CSV (comma separated)';
 
   /**
    * Indicates the default extension for the outputfile.

+ 25 - 3
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -90,15 +90,24 @@ abstract class TripalFieldDownloader {
    * Removes the downloadable file.
    */
   public function delete() {
-    if (file_exists($this->outfile)) {
-      unlink($this->outfile);
-    }
+    $fid = db_select('file_managed', 'fm')
+     ->fields('fm', array('fid'))
+     ->condition('uri', $this->outfile)
+     ->execute()
+     ->fetchField();
+     if ($fid) {
+       $file = file_load($fid);
+       file_usage_delete($file, 'tripal', 'data-collection');
+       file_delete($file, TRUE);
+     }
   }
 
   /**
    * Creates the downloadable file.
    */
   public function write() {
+    global $user;
+
     $fh = fopen(drupal_realpath($this->outfile), "w");
     if (!$fh) {
       throw new Exception("Cannout open collection file: " . $this->outfile);
@@ -121,6 +130,19 @@ abstract class TripalFieldDownloader {
       }
     }
     fclose($fh);
+
+    $file = new stdClass();
+    $file->uri = $this->outfile;
+    $file->filename = basename($this->outfile);
+    $file->filemime = file_get_mimetype($this->outfile);
+    $file->uid = $user->uid;
+    $file->status = FILE_STATUS_PERMANENT;
+    $file = file_save($file);
+    $fid = $file->fid;
+    $file = file_load($fid);
+    // We use the fid for the last argument because these files
+    // aren't really associated with any entity, but we need a value.
+    file_usage_add($file, 'tripal', 'data-collection', $fid);
   }
 
   /**

+ 65 - 0
tripal/includes/tripal.admin.inc

@@ -1,5 +1,70 @@
 <?php
 
+/**
+ *
+ * @param unknown $form
+ * @param unknown $form_state
+ */
+function tripal_admin_data_collection_form($form, &$form_state) {
+  $form = array();
+
+  $options = array(0 => t('Disabled'), 1 => t('Enabled'));
+  $form['enabled'] = array(
+    '#type' => 'radios',
+    '#title' => t('Activate Data Collections'),
+    '#options' => $options,
+    '#description' => t('Data collections allow users to save results of search queries for later download or use by other tools.'),
+    '#default_value' => variable_get('tripal_data_collections_enabled', 1),
+    '#required' => TRUE,
+  );
+
+  $form['lifespan'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Collection Lifespan'),
+    '#description' => t('Enter the number of hours (e.g. 24 is 1 day, 720 is 30 days)
+       that data collections exist.  Collections will be automatically removed after the lifespan
+       period has passed.  Removal of data collections occurs when the
+       sites Drupal cron executes.'),
+    '#default_value' => variable_get('tripal_data_collections_lifespan', 7),
+    '#required' => TRUE,
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save changes'),
+  );
+
+  return $form;
+}
+
+/**
+ * Validate function for the tripal_admin_data_collection_form form.
+ */
+function tripal_admin_data_collection_form_validate($form, &$form_state) {
+  $enable = $form_state['values']['enabled'];
+  $lifespan = $form_state['values']['lifespan'];
+
+  if (!preg_match('/^\d+$/',  $lifespan)) {
+    form_set_error('filespan', 'Please provide a valid numeric value for the number of hours collections exist.');
+  }
+  else {
+    if ($lifespan == 0) {
+      form_set_error('filespan', 'Please provide a positive numeric value for the number of hours collections exist.');
+    }
+  }
+}
+/**
+ * Sumbit function for the tripal_admin_data_collection_form form.
+ */
+function tripal_admin_data_collection_form_submit($form, &$form_state) {
+  $enabled = $form_state['values']['enabled'];
+  $lifespan = $form_state['values']['lifespan'];
+  variable_set('tripal_data_collections_enabled', $enabled);
+  variable_set('tripal_data_collections_lifespan', $lifespan);
+  menu_cache_clear_all();
+  drupal_set_message('Settings saved.');
+}
+
 /**
  * Provides a form for importing vocabularies and their terms.
  *

+ 37 - 5
tripal/tripal.module

@@ -99,6 +99,9 @@ function tripal_menu() {
     'file path' => drupal_get_path('module', 'system'),
   );
 
+  /**
+   * Tripal Extensions
+   */
   $items['admin/tripal/storage'] = array(
     'title' => 'Data Storage',
     'description' => t("Tripal is designed to access biological
@@ -147,7 +150,9 @@ function tripal_menu() {
 //     'weight' => -100,
 //   );
 
-  // Jobs Management
+  /**
+   * Jobs Management
+   */
   $items['admin/tripal/tripal_jobs'] = array(
     'title' => 'Jobs',
     'description' => t('Provides tools for managing jobs submitted to Tripal.  In some
@@ -331,16 +336,18 @@ function tripal_menu() {
     }
   }
 
+  /**
+   * Data Collections
+   */
   $items['user/%/data-collections'] = array (
     'title' => 'Data Collections',
     'description' => 'Your list of saved data collections',
     'page callback' => 'tripal_user_collections_page',
-    'access callback' => 'tripal_access_user_data',
+    'access callback' => 'tripal_accesss_user_collections',
     'access arguments' => array(1),
     'type' => MENU_LOCAL_TASK,
     'file' => 'includes/tripal.collections.inc',
     'file path' => drupal_get_path('module', 'tripal'),
-    'weight' => 10,
   );
 
   $items['user/%/data-collections/%/delete'] = array (
@@ -348,16 +355,39 @@ function tripal_menu() {
     'description' => 'Deletes a data collection.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_user_collections_delete_form', 1, 3),
-    'access callback' => 'tripal_access_user_data',
+    'access callback' => 'tripal_accesss_user_collections',
     'access arguments' => array(1),
     'type' => MENU_CALLBACK,
     'file' => 'includes/tripal.collections.inc',
     'file path' => drupal_get_path('module', 'tripal'),
-    'weight' => 10,
   );
+
+  $items['admin/tripal/data-collections'] = array(
+    'title' => 'Data Collections',
+    'description' => t('Site-wide settings for data collections'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_admin_data_collection_form'),
+    'access arguments' => array('administer tripal'),
+    'type' => MENU_NORMAL_ITEM,
+    'weight' => 0,
+    'file' => 'includes/tripal.admin.inc',
+    'file path' => drupal_get_path('module', 'tripal'),
+  );
+
   return $items;
 }
 
+function tripal_accesss_user_collections($uid) {
+  if (!tripal_access_user_data($uid)) {
+    return FALSE;
+  }
+  $collections_enabled = variable_get('tripal_data_collections_enabled', 1);
+  if (!$collections_enabled) {
+    return FALSE;
+  }
+  return TRUE;
+}
+
 /**
  * Access callback for accessing a user's Tripal-added private data.
  *
@@ -984,6 +1014,8 @@ function tripal_cron() {
     }
     watchdog('tripal_cron', 'tripal_cron ran');
   }
+
+  tripal_expire_collections();
 }
 
 /**

+ 24 - 1
tripal/views_handlers/tripal_views_handler_area_collections.inc

@@ -11,6 +11,12 @@ class tripal_views_handler_area_collections extends views_handler_area_result {
    */
   function render($empty = FALSE) {
 
+    // If collections are dispabled then don't show anything.
+    $collections_enabled = variable_get('tripal_data_collections_enabled', 1);
+    if (!$collections_enabled) {
+      return '';
+    }
+
     // This will only work with Tripal content types and the tripal_views_query
     // plugin. So don't show anything for others.
     if ($this->query->plugin_name != 'tripal_views_query') {
@@ -136,6 +142,23 @@ function tripal_views_handler_area_collections_form_submit($form, $form_state) {
   $uid = $user->uid;
   $bundle_name = $bundle->name;
 
+  $selected_fids = array();
+  foreach ($field_ids as $field_id => $is_selected) {
+    if ($is_selected) {
+      $selected_fids[] = $field_id;
+    }
+  }
+
+  if (count($selected_fids) == 0) {
+    $fields = field_info_instances('TripalEntity', $bundle->name);
+    foreach ($fields as $field_name => $instance) {
+      if ($instance['field_name'] == 'entity_id') {
+        continue;
+      }
+      $selected_fids[] = $instance['field_id'];
+    }
+  }
+
   // Get the entity Ids that match results
   $query->range['length'] = $view->total_rows;
   $results = $query->execute();
@@ -148,7 +171,7 @@ function tripal_views_handler_area_collections_form_submit($form, $form_state) {
     'collection_name' => $collection_name,
     'bundle_name' => $bundle_name,
     'ids' => $entities,
-    'fields' => $field_ids,
+    'fields' => $selected_fids,
     'description'  => $description,
   ));
 }