Jelajahi Sumber

working adding process but still working on the file download button

Shawna Spoor 7 tahun lalu
induk
melakukan
2e749c1809

+ 67 - 38
tripal/includes/TripalEntityCollection.inc

@@ -120,47 +120,55 @@ class TripalEntityCollection {
     /* Add the IDs, Fields, Bundles for this collection from the
      * collection_bundle table.
      */
-    $this->bundles = getBundles();
+    $this->bundles = $this->getBundles();
     // If more than one bundle plop into associative array.
+    $bundle_name = "";
     if (count($this->bundles) > 1) {
       foreach ($this->bundles as $bundle) {
-        $ids[$bundle] = unserialize(getEntityIDs($this->bundle));
-        $fields[$bundle] = unserialize(getFields($this->bundle));
+        $bundle_name = $this->bundles[0]->bundle_name;
+        $ids[$bundle_name] = $this->getEntityIDs($bundle_name);
+        $fields[$bundle_name] = $this->getFields($bundle_name);
       }
       $this->ids = $ids;
       $this->fields = $fields;
     }
     else {
-      $this->ids = unserialize(getEntityIDs($this->bundles));
-      $this->fields = unserialize(getFields($this->bundles));
+      if (!empty($this->bundles)) {
+        $bundle_name = $this->bundles[0]->bundle_name;
+        $this->ids = $this->getEntityIDs($bundle_name);
+        $this->fields = $this->getFields($bundle_name);
+      }
     }
 
     // Iterate through the fields and find out what download formats are
     // supported for this basket.
-    foreach ($this->fields as $field_id) {
-      $field = field_info_field_by_id($field_id);
-      if (!$field) {
-        continue;
-      }
-      $field_name = $field['field_name'];
-      $field_type = $field['type'];
-      $field_module = $field['module'];
-      $instance = field_info_instance('TripalEntity', $field_name, $this->bundles);
-      $downloaders = array();
-
-      // All fields should support the Tab and CSV downloaders.
-      tripal_load_include_downloader_class('TripalTabDownloader');
-      $this->downloaders['TripalTabDownloader'] = TripalTabDownloader::$label;
-      tripal_load_include_downloader_class('TripalCSVDownloader');
-      $this->downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$label;
-
-      if (tripal_load_include_field_class($field_type)) {
-        $settings = $field_type::$default_instance_settings;
-        if (array_key_exists('download_formatters', $settings)) {
-          foreach ($settings['download_formatters'] as $class_name) {
-            if (!array_key_exists($class_name, $this->downloaders)) {
-              tripal_load_include_downloader_class($class_name);
-              $this->downloaders[$class_name] = $class_name::$label;
+    foreach ($this->fields as $field_group) {
+      foreach ($field_group as $field_id) {
+        $field = field_info_field_by_id($field_id);
+        if (!$field) {
+          continue;
+        }
+        $field_name = $field['field_name'];
+        $field_type = $field['type'];
+        $field_module = $field['module'];
+        $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+
+        $downloaders = array();
+
+        // All fields should support the Tab and CSV downloaders.
+        tripal_load_include_downloader_class('TripalTabDownloader');
+        $this->downloaders['TripalTabDownloader'] = TripalTabDownloader::$label;
+        tripal_load_include_downloader_class('TripalCSVDownloader');
+        $this->downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$label;
+
+        if (tripal_load_include_field_class($field_type)) {
+          $settings = $field_type::$default_instance_settings;
+          if (array_key_exists('download_formatters', $settings)) {
+            foreach ($settings['download_formatters'] as $class_name) {
+              if (!array_key_exists($class_name, $this->downloaders)) {
+                tripal_load_include_downloader_class($class_name);
+                $this->downloaders[$class_name] = $class_name::$label;
+              }
             }
           }
         }
@@ -213,6 +221,8 @@ class TripalEntityCollection {
           'description' => array_key_exists('description', $details) ? $details['description'] : '',
         ))
         ->execute();
+      // Now add the second table with bundle info.
+      $this->add($details, $collection_id);
       // Now load the job into this object.
       $this->load($collection_id);
     }
@@ -233,7 +243,7 @@ class TripalEntityCollection {
    *
    * @throws Exception
    */
-  public function add($details) {
+  public function add($details, $collection_id) {
     if (!$details['bundle_name']) {
       throw new Exception("Must provide a 'bundle_name' key to TripalEntityCollection::add().");
     }
@@ -248,12 +258,13 @@ class TripalEntityCollection {
       $collection_bundle_id = db_insert('tripal_collection_bundle')
         ->fields(array(
           'bundle_name' => $details['bundle_name'],
-          'ids' => $details['ids'],
-          'fields' => $details['fields'],
+          'ids' => serialize($details['ids']),
+          'fields' => serialize($details['fields']),
+          'collection_id' => $collection_id,
         ))
         ->execute();
       // Now load the job into this object.
-      $this->load($collection_bundle_id);
+      //$this->load($collection_bundle_id);
     }
     catch (Exception $e) {
       throw new Exception('Cannot create collection: ' . $e->getMessage());
@@ -270,7 +281,7 @@ class TripalEntityCollection {
     $collection_id = $this->collection_id;
     // Return the bundles from the collection_bundle table.
     $result = db_select('tripal_collection_bundle')
-      ->fields('tripal_collection_bundle', array('ids'))
+      ->fields('tripal_collection_bundle', array('bundle_name'))
       ->condition('collection_id', $collection_id, '=')
       ->execute()
       ->fetchAll();
@@ -305,7 +316,16 @@ class TripalEntityCollection {
       ->execute()
       ->fetchAll();
 
-    return $result;
+    // Unserialize the array of standard class objects.
+    $unserialized_result = [];
+    foreach ($result as $id_list) {
+      $unserialized_id_list = unserialize($id_list->ids);
+      foreach ($id_list as $item) {
+        $unserialized_result[] = $unserialized_id_list;
+      }
+    }
+
+    return $unserialized_result;
   }
 
   /**
@@ -324,7 +344,16 @@ class TripalEntityCollection {
       ->execute()
       ->fetchAll();
 
-    return $result;
+     // Unserialize the array of standard class objects.
+    $unserialized_result = [];
+    foreach ($result as $field_list) {
+      $unserialized_field_list = unserialize($field_list->fields);
+      foreach ($field_list as $item) {
+        $unserialized_result[] = $unserialized_field_list;
+      }
+    }
+ 
+    return $unserialized_result;
   }
 
   /**
@@ -495,8 +524,8 @@ class TripalEntityCollection {
    * @throws Exception
    */
   public function write($formatter) {
-
-    if(!$this->isFormatterCompatible($formatter)) {
+    dpm($formatter);
+    if (!$this->isFormatterCompatible($formatter)) {
       throw new Exception(t('The formatter, "@formatter", is not compatible with this data collection.', array('@formatter' => $formatter)));
 
     }

+ 2 - 2
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -68,11 +68,11 @@ abstract class TripalFieldDownloader {
     if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
       $message = 'Could not access the directory on the server for storing this file.';
       watchdog('tripal', $message, array(), WATCHDOG_ERROR);
-      drupal_json_output(array(
+      /*drupal_json_output(array(
         'status'  => 'failed',
         'message' => $message,
         'file_id' => '',
-      ));
+      ));*/
       return;
     }
 

+ 16 - 10
tripal/includes/tripal.collections.inc

@@ -7,7 +7,7 @@
 function tripal_user_collections_page() {
   global $user;
 
-  $headers = array('Name', 'Description', 'Create Date', 'Downloads Formats', 'Actions');
+  $headers = array('Name', 'Description', 'Create Date', 'Downloads Formats', 'Generate File', 'Delete');
   $rows = array();
 
   $collections = db_select('tripal_collection', 'tc')
@@ -45,8 +45,8 @@ function tripal_user_collections_page() {
         $collection->getDescription(),
         $collection->getCreateDate(),
         $download_list,
+        l('Generate File for Download', 'user/' . $user->uid . '/data-collections/generate/' . $collection_id),
         l('Delete', 'user/' . $user->uid . '/data-collections/' . $collection_id . '/delete'),
-        l('Generate File for Download', 'user/' . $user->uid . '/data-collections/' . $collection_id . '/generate'),
       ),
     );
   }
@@ -114,13 +114,12 @@ function tripal_user_collections_delete_form_submit($form, &$form_state) {
   drupal_goto('user/' . $user->uid . '/data-collections');
 }
 
-function tripal_user_collections_generate_file($form, &$form_state,  $uid, $collection_id) {
+function tripal_user_collections_generate_file_form($form, &$form_state,  $uid, $collection_id) {
   $form_state['collection_id'] = $collection_id;
-  $form['#submit'][] = 'tripal_user_collections_delete_form_submit';
+  $form['#submit'][] = 'tripal_user_collections_generate_file_form_submit';
 
   $collection  = new TripalEntityCollection();
   $collection->load($collection_id);
-
   $form = confirm_form($form,
       t('Click the button below to generate the collection filed: %title',
           array('%title' => $collection->getName())), 'user/' . $uid . '/data-collections',
@@ -134,16 +133,23 @@ function tripal_user_collections_generate_file($form, &$form_state,  $uid, $coll
  * @param $collection_id
  *   The ID of the collection to delete.
  */
-function tripal_user_collections_generate_file_submit($form, &$form_state) {
+function tripal_user_collections_generate_file_form_submit($form, &$form_state) {
   global $user;
   $collection_id = $form_state['collection_id'];
   $collection  = new TripalEntityCollection();
   $collection->load($collection_id);
 
-  // Add the job to write the collection download files.
-  $args = array($collection_id);
-  tripal_add_job('Create data collection files for ' . $user->name, 'tripal',
+  if ($collection->getUserID() == $user->uid) {
+    try {
+      // Add the job to write the collection download files.
+      $args = array($collection_id);
+      tripal_add_job('Create data collection files for ' . $user->name, 'tripal',
       'tripal_create_collection_files', $args, $user->uid, 10, array());
-      
+    }
+    catch (Exception $e) {
+      drupal_set_message('There was a problem creating the file for the data collection please contact the site to report the error.', 'error');
+    }
+  }
+
   drupal_goto('user/' . $user->uid . '/data-collections');
 }

+ 1 - 5
tripal/tripal.install

@@ -381,11 +381,7 @@ function tripal_tripal_collection_bundle_schema() {
       ),
     ),
     'indexes' => array(
-      'bundle_name' => array('bundle_name'),
-      'uid' => array('uid')
-    ),
-    'unique keys' => array(
-      'user_collection' => array('uid', 'collection_name'),
+      'collection_id' => array('collection_id')
     ),
     'primary key' => array('collection_id'),
   );

+ 3 - 3
tripal/tripal.module

@@ -361,11 +361,11 @@ function tripal_menu() {
     'file' => 'includes/tripal.collections.inc',
     'file path' => drupal_get_path('module', 'tripal'),
   );
-  $items['user/%/data-collections/%/generate'] = array (
+  $items['user/%/data-collections/generate/%'] = array (
     'title' => 'Generate a file for download of a Collections',
-    'description' => 'Deletes a data collection.',
+    'description' => 'Generates a data collection file for download.',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_user_collections_generate_file', 1, 3),
+    'page arguments' => array('tripal_user_collections_generate_file_form', 1, 4),
     'access callback' => 'tripal_accesss_user_collections',
     'access arguments' => array(1),
     'type' => MENU_CALLBACK,