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

start of data collections storing remotely so I do not overwrite it by accident for a third time

Shawna Spoor 7 роки тому
батько
коміт
4e57a008a9
2 змінених файлів з 136 додано та 42 видалено
  1. 74 31
      tripal/includes/TripalEntityCollection.inc
  2. 62 11
      tripal/tripal.install

+ 74 - 31
tripal/includes/TripalEntityCollection.inc

@@ -5,7 +5,7 @@ class TripalEntityCollection {
   /**
    * The name of the bundle (i.e. content type) to which the entities belong.
    */
-  protected $bundle_name = '';
+  protected $bundles = array();
 
   /**
    * The collection ID
@@ -75,16 +75,13 @@ class TripalEntityCollection {
 
       // Reset the class to defaults.
       $this->collection_id = NULL;
-      $this->bundle_name = '';
       $this->collection_name = '';
       $this->create_date = '';
       $this->description = '';
-      $this->fields = array();
-      $this->ids = array();
 
     }
     catch (Exception $e) {
-      throw new Exception('Cannot delete collection: ' .  $e->getMessage());
+      throw new Exception('Cannot delete collection: ' . $e->getMessage());
     }
   }
 
@@ -114,12 +111,9 @@ class TripalEntityCollection {
     }
 
     // Fix the date/time fields.
-    $this->bundle_name = $collection->bundle_name;
     $this->collection_name = $collection->collection_name;
     $this->create_date = $collection->create_date;
     $this->user = user_load($collection->uid);
-    $this->ids = unserialize($collection->ids);
-    $this->fields = unserialize($collection->fields);
     $this->description = $collection->description;
     $this->collection_id = $collection->collection_id;
 
@@ -178,22 +172,7 @@ class TripalEntityCollection {
     if (!$details['collection_name']) {
       throw new Exception("Must provide a 'collection_name' key to TripalEntityCollection::create().");
     }
-    if (!$details['bundle_name']) {
-      throw new Exception("Must provide a 'bundle_name' key to TripalEntityCollection::create().");
-    }
-    if (!$details['ids']) {
-      throw new Exception("Must provide a 'ids' key to TripalEntityCollection::create().");
-    }
-    if (!$details['fields']) {
-      throw new Exception("Must provide a 'fields' key to TripalEntityCollection::create().");
-    }
 
-    if (!is_array($details['ids'])) {
-      throw new Exception("The 'ids' key must be an array key to TripalEntityCollection::create().");
-    }
-    if (!is_array($details['fields'])) {
-      throw new Exception("The 'ids' key must be an array key to TripalEntityCollection::create().");
-    }
 
     // Before inserting the new collection make sure we don't violote the unique
     // constraint that a user can only have one collection of the give name.
@@ -211,9 +190,6 @@ class TripalEntityCollection {
       $collection_id = db_insert('tripal_collection')
         ->fields(array(
           'collection_name' => $details['collection_name'],
-          'bundle_name' => $details['bundle_name'],
-          'ids' => serialize($details['ids']),
-          'fields' => serialize($details['fields']),
           'create_date' => time(),
           'uid' => $details['uid'],
           'description' => array_key_exists('description', $details) ? $details['description'] : '',
@@ -223,10 +199,59 @@ class TripalEntityCollection {
       $this->load($collection_id);
     }
     catch (Exception $e) {
-      throw new Exception('Cannot create collection: ' .  $e->getMessage());
+      throw new Exception('Cannot create collection: ' . $e->getMessage());
+    }
+  }
+
+  /**
+   * Creates a new tripal_collection_bundle entry.
+   *
+   * @param  $details
+   *   An association array containing the details for a collection. The
+   *   details must include the following key/value pairs:
+   *   - bundle_name:  The name of the TripalEntity content type.
+   *   - ids:  An array of the entity IDs that form the collection.
+   *   - fields: An array of the field IDs that the collection is limited to.
+   *
+   * @throws Exception
+   */
+  public function add($details) {
+    if (!$details['bundle_name']) {
+      throw new Exception("Must provide a 'bundle_name' key to TripalEntityCollection::add().");
+    }
+    if (!$details['ids']) {
+      throw new Exception("Must provide a 'ids' key to TripalEntityCollection::add().");
+    }
+    if (!$details['fields']) {
+      throw new Exception("Must provide a 'fields' key to TripalEntityCollection::add().");
+    }
+
+    try {
+      $collection_bundle_id = db_insert('tripal_collection_bundle')
+        ->fields(array(
+          'bundle_name' => $details['bundle_name'],
+          'ids' => $details['ids'],
+          'fields' => $details['fields'],
+        ))
+        ->execute();
+      // Now load the job into this object.
+      $this->load($collection_bundle_id);
+    }
+    catch (Exception $e) {
+      throw new Exception('Cannot create collection: ' . $e->getMessage());
     }
   }
 
+  /**
+   * Retrieves the list of bundles associated with the collection.
+   *
+   * @return
+   *   An array of bundles.
+   */
+  public function getBundles() {
+    return $this->bundles;
+  }
+
   /**
    * Retrieves the list of appropriate download formatters for the basket.
    *
@@ -244,8 +269,17 @@ class TripalEntityCollection {
    * @return
    *   An array of numeric enity IDs.
    */
-  public function getEntityIDs(){
-    return $this->ids;
+  public function getEntityIDs($bundle_name) {
+    $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'))
+      ->condition('collection_id', $collection_id, '=')
+      ->condition('bundle_name', $bundle_name, '=')
+      ->execute()
+      ->fetchAll();
+
+    return $result;
   }
 
   /**
@@ -254,8 +288,17 @@ class TripalEntityCollection {
    * @return
    *   An array of numeric field IDs.
    */
-  public function getFields() {
-    return $this->fields;
+  public function getFields($bundle_name) {
+    $collection_id = $this->collection_id;
+    // Return the bundles from the collection_bundle table.
+    $result = db_select('tripal_collection_bundle')
+      ->fields('tripal_collection_bundle', array('fields'))
+      ->condition('collection_id', $collection_id, '=')
+      ->condition('bundle_name', $bundle_name, '=')
+      ->execute()
+      ->fetchAll();
+
+    return $result;
   }
 
   /**

+ 62 - 11
tripal/tripal.install

@@ -194,6 +194,7 @@ function tripal_schema() {
   $schema['tripal_bundle'] = tripal_tripal_bundle_schema();
   $schema['tripal_import'] = tripal_tripal_import_schema();
   $schema['tripal_collection'] = tripal_tripal_collection_schema();
+  $schema['tripal_collection_bundle'] = tripal_tripal_collection_bundle_schema();
 
   // Adds a table for additional information related to bundles.
   $schema['tripal_bundle_variables'] = tripal_tripal_bundle_variables_schema();
@@ -329,7 +330,39 @@ function tripal_tripal_collection_schema() {
         'type' => 'text',
         'not null' => FALSE
       ),
-      'bundle_name' => array(
+      'uid' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'description' => 'The user Id of the person who created the collection.'
+      ),
+      'create_date' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'description' => 'UNIX integer start time'
+      ),
+    ),
+    'indexes' => array(
+      'uid' => array('uid')
+    ),
+    'unique keys' => array(
+      'user_collection' => array('uid', 'collection_name'),
+    ),
+    'primary key' => array('collection_id'),
+  );
+}
+
+/**
+ * Returns the Drupal Schema API array for the tripal_jobs table.
+ */
+function tripal_tripal_collection_bundle_schema() {
+  return array(
+    'fields' => array(
+      'collection_id' => array(
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE
+      ),
+     'bundle_name' => array(
         'type' => 'varchar',
         'length' => 1024,
         'not null' => TRUE
@@ -346,16 +379,6 @@ function tripal_tripal_collection_schema() {
         'not null' => TRUE,
         'description' => 'An array of numeric field IDs.'
       ),
-      'uid' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'description' => 'The user Id of the person who created the collection.'
-      ),
-      'create_date' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'description' => 'UNIX integer start time'
-      ),
     ),
     'indexes' => array(
       'bundle_name' => array('bundle_name'),
@@ -367,6 +390,7 @@ function tripal_tripal_collection_schema() {
     'primary key' => array('collection_id'),
   );
 }
+
 /**
  * Returns the Drupal Schema API array for the tripal_jobs table.
  */
@@ -1011,4 +1035,31 @@ function tripal_update_7306() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Could not add the tripal_collection table:' . $error);
   }
+}
+
+/**
+ * Remove the bundle_name, ids, fields from the tripal collections table.
+ * And add the new tripal_tripal_collection_bundle_schema
+ */
+function tripal_update_7307() {
+  $transaction = db_transaction();
+  try {
+    if (db_field_exists('tripal_collection', 'bundle_name')) {
+      db_drop_field('tripal_collection', 'bundle_name');
+    }
+    if (db_field_exists('tripal_collection', 'ids')) {
+      db_drop_field('tripal_collection', 'ids');
+    }
+    if (db_field_exists('tripal_collection', 'fields')) {
+      db_drop_field('tripal_collection', 'fields');
+    }
+     $schema = array();
+    $schema['tripal_collection_bundle'] = tripal_tripal_collection_bundle_schema();
+    db_create_table('tripal_collection_bundle', $schema['tripal_collection_bundle']);
+  }
+  catch (\PDOException $e) {
+    $transaction->rollback();
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not add the tripal_collection table:' . $error);
+  }
 }