Browse Source

moved entities stuff into it's own modules and reworking

Stephen Ficklin 9 years ago
parent
commit
9718888ea3

+ 0 - 68
tripal_cv/entities/tripal_cv.entities.inc

@@ -1,68 +0,0 @@
-<?php
-
-require_once 'tripal_cv.vocabulary.inc';
-require_once 'tripal_cv.vocabulary_term.inc';
-require_once 'tripal_cv.organism.inc';
-require_once 'tripal_cv.feature.inc';
-
-/**
- * Implements hook_entity_info().
- */
-function tripal_cv_entity_info() {
-  $entities = array();
-
-  // The vocabulary and vocabulary term entities.
-  $entities['trp_vocabulary'] = array(
-    'label' => t('Controlled Vocabulary'),
-    'plural label' => t('Controlled Vocabularies'),
-    'entity class' => 'TrpVocabulary',
-    'controller class' => 'TrpVocabularyController',
-    'entity keys' => array(
-      'id' => 'internal_id'
-    ),
-  );
-  $entities['trp_vocabulary_term'] = array(
-    'label' => t('Controlled Vocabulary Term'),
-    'plural label' => t('Controlled Vocabulary Terms'),
-    'entity class' => 'TrpVocabularyTerm',
-    'controller class' => 'TrpVocabularyTermController',
-    'entity keys' => array(
-      'id' => 'internal_id'
-    ),
-  );
-
-  // We want to dynamically add entities for terms specifically set
-  // by the site admin
-  $values = array(
-    'cv_id' => array(
-      'name' => 'sequence'
-    ),
-    'name' => 'gene'
-  );
-  $cvterm = chado_generate_var('cvterm', $values);
-  $entity_id = 'trp_' . $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
-  $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
-  $plural_label = $label . '(s)';
-  $entity_class = 'TrpTerm' . $entity_id;
-  $entities[$entity_id] = array(
-    'label' => $label,
-    'plural label' => $plural_label,
-    'entity class' => $entity_class,
-    'controller class' => 'TrpFeatureController',
-    'entity keys' => array(
-      'id' => 'internal_id'
-    ),
-  );
-
-  // The organism entity.
-  $entities['trp_organism'] = array(
-    'label' => t('Organism'),
-    'plural label' => t('Organism'),
-    'entity class' => 'TrpOrganism',
-    'controller class' => 'TrpOrganismController',
-    'entity keys' => array(
-      'id' => 'internal_id'
-    ),
-  );
-  return $entities;
-}

+ 0 - 101
tripal_cv/entities/tripal_cv.feature.inc

@@ -1,101 +0,0 @@
-<?php
-/**
- *
- *
- */
-class TrpFeatureController extends DrupalDefaultEntityController {
-
-  /**
-   *
-   * @param $ids
-   *   An array of entity IDs, or FALSE to load all entities.
-   * @param $conditions
-   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
-   *   base table.Values will be compared for equality. All the comparisons will
-   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
-   *   instead.
-   * @return multitype:An
-   */
-  public function load($ids = array(), $conditions = array()) {
-    $entities = array();
-    $queried_entities = array();
-
-    $values = array('feature_id' => $ids);
-    $options = array('return_array' => TRUE);
-    $features = chado_generate_var('feature', $values, $options);
-    foreach ($features as $result) {
-      $feature = new stdClass();
-      $feature->internal_id = $result->feature_id;
-      $feature->type_id = $result->type_id->dbxref_id->db_id->name . ':' . $result->type_id->dbxref_id->accession;
-
-      // Add the names by which this feature is known.
-      $names = array();
-      $names[] = $result->name;
-      $names[] = $result->uniquename;
-      $feature->name = array_unique($names);
-
-      // Add in the synonyms.
-      $sql = "
-        SELECT *
-        FROM {feature_synonym} FS
-          INNER JOIN {synonym} S ON FS.synonym_id = S.synonym_id
-        WHERE FS.feature_id = :feature_id
-      ";
-      $synonym_results = chado_query($sql, array(':feature_id' => $result->feature_id));
-      $synonyms = array();
-      while ($synonym = $synonym_results->fetchObject()) {
-        $synonyms[] = $synonym->name;
-      }
-      if (count($synonyms) > 0) {
-        $feature->synonyms = $synonyms;
-      }
-
-      // Add in database cross-references.
-      $xrefs = array();
-      if ($result->dbxref_id) {
-        $xrefs[] = $result->dbxref_id->db_id->name . ':' . $result->dbxref_id->accession;
-      }
-      $sql = "
-        SELECT DB.name as namespace, DBX.accession
-        FROM {feature_dbxref} FDBX
-          INNER JOIN {dbxref} DBX ON DBX.dbxref_id = FDBX.dbxref_id
-          INNER JOIN {db} DB      ON DB.db_id      = DBX.db_id
-        WHERE FDBX.feature_id = :feature_id
-      ";
-      $dbxref_results = chado_query($sql, array(':feature_id' => $result->feature_id));
-
-      while ($dbxref = $dbxref_results->fetchObject()) {
-        if ($dbxref->namespace != 'GFF_source') {
-          $xrefs[] = $dbxref->namespace . ':' . $dbxref->accession;
-        }
-      }
-      if (count($xrefs) > 0) {
-        $feature->xref_id = $xrefs;
-      }
-
-      // Add in the expand array
-      $expand = array();
-      $expand[] = 'chado:organism';
-      $feature->expand = $expand;
-
-      // Add in the embeded elements
-      $embedded = array();
-      $embedded['chado:organism'] = entity_load('trp_organism', array($result->organism_id->organism_id));
-      $feature->embedded = $embedded;
-
-      $queried_entities[$feature->internal_id] = $feature;
-    }
-
-    // Pass all entities loaded from the database through $this->attachLoad(),
-    // which attaches fields (if supported by the entity type) and calls the
-    // entity type specific load callback, for example hook_node_load().
-    if (!empty($queried_entities)) {
-      //      $this->attachLoad($queried_entities);
-      $entities += $queried_entities;
-    }
-
-    // Ensure that the returned array is ordered the same as the original
-    // $ids array if this was passed in and remove any invalid ids.
-    return $entities;
-  }
-}

+ 0 - 72
tripal_cv/entities/tripal_cv.organism.inc

@@ -1,72 +0,0 @@
-<?php
-/**
- *
- *
- */
-class TrpOrganismController extends DrupalDefaultEntityController {
-
-  /**
-   *
-   * @param $ids
-   *   An array of entity IDs, or FALSE to load all entities.
-   * @param $conditions
-   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
-   *   base table.Values will be compared for equality. All the comparisons will
-   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
-   *   instead.
-   * @return multitype:An
-   */
-  public function load($ids = array(), $conditions = array()) {
-    $entities = array();
-    $queried_entities = array();
-
-    $values = array('organism_id' => $ids);
-    $options = array('return_array' => TRUE);
-    $organisms = chado_generate_var('organism', $values, $options);
-    $organisms = chado_expand_var($organisms, 'field', 'organism.comment');
-    foreach ($organisms as $result) {
-      $organism = new stdClass();
-      $organism->internal_id = $result->organism_id;
-
-      // Add the names by which this feature is known.
-      $names = array();
-      $names[] = $result->genus . ' ' . $result->species;
-      $names[] = $result->common_name;
-      $names[] = $result->abbreviation;
-      $organism->name = array_unique($names);
-
-      // Add in properties.
-
-      // Add in the expand array.
-      $expand = array();
-      $organism->expand = $expand;
-
-      // Add in the embeded elements.
-      $embedded = array();
-      $organism->embedded = $embedded;
-
-
-      // Add in site-specific properties.
-      $organism->genus = $result->genus;
-      $organism->species = $result->species;
-      $organism->description = $result->comment;
-
-      // Add in the phylogeny if the NCBI taxonomy is loaded.
-
-
-      $queried_entities[$organism->internal_id] = $organism;
-    }
-
-    // Pass all entities loaded from the database through $this->attachLoad(),
-    // which attaches fields (if supported by the entity type) and calls the
-    // entity type specific load callback, for example hook_node_load().
-    if (!empty($queried_entities)) {
-      //      $this->attachLoad($queried_entities);
-      $entities += $queried_entities;
-    }
-
-    // Ensure that the returned array is ordered the same as the original
-    // $ids array if this was passed in and remove any invalid ids.
-    return $entities;
-  }
-}

+ 0 - 65
tripal_cv/entities/tripal_cv.vocabulary.inc

@@ -1,65 +0,0 @@
-<?php
-/**
- *
- *
- */
-class TrpVocabularyController extends DrupalDefaultEntityController {
-
-  /**
-   *
-   * @param $ids
-   *   An array of entity IDs, or FALSE to load all entities.
-   * @param $conditions
-   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
-   *   base table.Values will be compared for equality. All the comparisons will
-   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
-   *   instead.
-   * @return multitype:An
-   */
-  public function load($ids = array(), $conditions = array()) {
-    $entities = array();
-    $queried_entities = array();
-
-    // Get the list of records that match the loading criteria
-    $sql = "
-      SELECT CVDB.cv_id, CVDB.name, CVDB.namespace, DB.url, DB.urlprefix
-      FROM {cv_db} CVDB
-        INNER JOIN {db} DB on DB.db_id = CVDB.db_id
-        LEFT JOIN {cvprop} CVP on CVP.cv_id = CVDB.cv_id
-    ";
-    $where = '';
-    if ($ids) {
-      $where .= ' CVDB.cv_id IN (:ids)';
-    }
-    if ($where) {
-      $sql .= "WHERE $where";
-    }
-    $sql .= "ORDER BY CVDB.name ";
-    $sql .= "LIMIT 10 OFFSET 0 ";
-
-    $results = chado_query($sql, array(':ids' => $ids));
-    while ($result = $results->fetchObject()) {
-       $cv = new stdClass();
-       $cv->internal_id = $result->cv_id;
-       $cv->name = $result->name;
-       $cv->format = 'OBO v1.4';
-       $cv->namespace = $result->namespace;
-       $cv->url = $result->url;
-       $cv->term_url = $result->urlprefix ? $result->urlprefix . '{id}' : '';
-       $queried_entities[$cv->internal_id] = $cv;
-    }
-
-    // Pass all entities loaded from the database through $this->attachLoad(),
-    // which attaches fields (if supported by the entity type) and calls the
-    // entity type specific load callback, for example hook_node_load().
-    if (!empty($queried_entities)) {
-//      $this->attachLoad($queried_entities);
-      $entities += $queried_entities;
-    }
-
-    // Ensure that the returned array is ordered the same as the original
-    // $ids array if this was passed in and remove any invalid ids.
-    return $entities;
-  }
-
-}

+ 0 - 149
tripal_cv/entities/tripal_cv.vocabulary_term.inc

@@ -1,149 +0,0 @@
-<?php
-/**
- *
- *
- */
-class TrpVocabularyTermController extends DrupalDefaultEntityController {
-
-  /**
-   *
-   * @param $ids
-   *   An array of entity IDs, or FALSE to load all entities.
-   * @param $conditions
-   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
-   *   base table.Values will be compared for equality. All the comparisons will
-   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
-   *   instead.
-   * @return multitype:An
-   */
-  public function load($ids = array(), $conditions = array()) {
-    $entities = array();
-    $queried_entities = array();
-
-    // Get the list of records that match the loading criteria
-    $sql = "
-      SELECT CVT.cvterm_id, CVT.name, CVT.definition,
-        DB.name as namespace, DBX.accession, CV.name as vocabulary,
-        CV.cv_id
-      FROM {cvterm} CVT
-        INNER JOIN {cv} CV      ON CV.cv_id      = CVT.cv_id
-        INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVT.dbxref_id
-        INNER JOIN {db} DB      ON DB.db_id      = DBX.db_id
-    ";
-    $where = '';
-    if ($ids) {
-      $where .= 'CVT.cvterm_id IN (:ids) ';
-    }
-    if ($where) {
-      $sql .= "WHERE $where ";
-    }
-    $sql .= "ORDER BY CVT.name ";
-    $sql .= "LIMIT 10 OFFSET 0 ";
-
-    $results = chado_query($sql, array(':ids' => $ids));
-    while ($result = $results->fetchObject()) {
-      $cvterm = new stdClass();
-      $cvterm->vocabulary_internal_id = $result->cv_id;
-      $cvterm->vocabulary = $result->vocabulary;
-      $cvterm->namespace = $result->namespace;
-      $cvterm->internal_id = $result->cvterm_id;
-      $cvterm->id = $result->namespace . ':' . $result->accession;
-      $cvterm->name = $result->name;
-      $cvterm->def = $result->definition;
-
-      // Find any alternate IDs or xrefs for this term.
-      $sql = "
-        SELECT DB.name as namespace, DBX.accession
-        FROM {cvterm_dbxref} CVTDBX
-          INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVTDBX.dbxref_id
-          INNER JOIN {db} DB      ON DB.db_id = DBX.db_id
-        WHERE CVTDBX.cvterm_id = :cvterm_id
-      ";
-      $dbxref_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
-      $xrefs = array();
-      $alt_ids = array();
-      while ($dbxref = $dbxref_results->fetchObject()) {
-        if ($dbxref->namespace = $result->namespace) {
-          $alt_ids[] = $dbxref->namespace . ':' . $dbxref->accession;
-        }
-        else {
-          $xrefs[] = $dbxref->namespace . ':' . $dbxref->accession;
-        }
-      }
-      if (count($xrefs) > 0) {
-        $cvterm->xref_id = $xrefs;
-      }
-      if (count($alt_ids) > 0) {
-        $cvterm->alt_id = $alt_ids;
-      }
-
-      // Find any comments for this term.
-      $sql = "
-        SELECT CVTP.value
-        FROM {cvtermprop} CVTP
-          INNER JOIN {cvterm} CVT on CVT.cvterm_id = CVTP.type_id
-        WHERE CVT.name = 'comment' and CVTP.cvterm_id = :cvterm_id
-      ";
-      $comment_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
-      $comment = $comment_results->fetchObject();
-      if ($comment) {
-        $cvterm->comment = $comment->value;
-      }
-
-      // Find any relationships of this term
-      $sql = "
-        SELECT CVT.name as rel, CVTO.name, DB.name as namespace, DBX.accession
-        FROM {cvterm_relationship} CVTR
-          INNER JOIN {cvterm} CVT  ON CVT.cvterm_id  = CVTR.type_id
-          INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = CVTR.object_id
-          INNER JOIN {dbxref} DBX  ON DBX.dbxref_id  = CVTO.dbxref_id
-          INNER JOIN {db}     DB   ON DB.db_id       = DBX.db_id
-        WHERE CVT.name = 'is_a' and CVTR.subject_id = :cvterm_id
-      ";
-      $rel_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
-      $rels = array();
-      while ($rel = $rel_results->fetchObject()) {
-        $rel_text = $rel->namespace . ':' . $rel->accession;
-        if ($rel->name) {
-          $rel_text .= ' ! ' . $rel->name;
-        }
-        $rels[$rel->rel][] = $rel_text;
-      }
-      foreach ($rels as $rel_type => $rel_list) {
-        $cvterm->$rel_type = $rel_list;
-      }
-
-      // Find any synonyms for this term
-      $sql = "
-        SELECT CVTS.synonym, CVTT.name as scope
-        FROM {cvtermsynonym} CVTS
-          INNER JOIN {cvterm} CVT  ON  CVT.cvterm_id = CVTS.cvterm_id
-          INNER JOIN {cvterm} CVTT ON CVTT.cvterm_id = CVTS.type_id
-        WHERE CVTS.cvterm_id = :cvterm_id
-      ";
-      $synonym_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
-      $synonyms = array();
-      while ($synonym = $synonym_results->fetchObject()) {
-        $synonyms[] = '"' . $synonym->synonym . '" ' . strtoupper($synonym->scope);
-      }
-      if (count($synonyms) > 0) {
-        $cvterm->synonyms = $synonyms;
-      }
-
-      $queried_entities[$cvterm->internal_id] = $cvterm;
-    }
-
-    // Pass all entities loaded from the database through $this->attachLoad(),
-    // which attaches fields (if supported by the entity type) and calls the
-    // entity type specific load callback, for example hook_node_load().
-    if (!empty($queried_entities)) {
-//      $this->attachLoad($queried_entities);
-      $entities += $queried_entities;
-    }
-
-    // Ensure that the returned array is ordered the same as the original
-    // $ids array if this was passed in and remove any invalid ids.
-    return $entities;
-  }
-
-}

+ 0 - 2
tripal_cv/tripal_cv.module

@@ -24,8 +24,6 @@ require_once 'includes/tripal_cv.cv_form.inc';
 require_once 'includes/tripal_cv.cvterm_form.inc';
 require_once 'includes/tripal_cv.cvtermpath_form.inc';
 
-require_once 'entities/tripal_cv.entities.inc';
-
 /**
  * Implements hook_init().
  * Adds CSS and JS needed for this modules rendered content

+ 11 - 0
tripal_entities/tripal_entities.info

@@ -0,0 +1,11 @@
+name = Tripal Entities
+description = Exposes data in Chado as Drupal Entities.
+core = 7.x
+project = tripal
+package = Tripal
+version = 7.x-2.0
+configure = admin/tripal/chado/tripal_entities
+
+dependencies[] = tripal_core
+dependencies[] = tripal_views
+dependencies[] = tripal_db

+ 0 - 0
tripal_entities/tripal_entities.install


+ 181 - 0
tripal_entities/tripal_entities.module

@@ -0,0 +1,181 @@
+<?php
+// http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
+/**
+ * Implement hook_entity_info().
+ */
+function tripal_entities_entity_info() {
+  $entities = array();
+
+  $entities['cvterm'] = array(
+    'label' => t('Vocabulary Term'),
+    'uri callback' => 'tripal_entities_vocbulary_term_uri',
+    'plural label' => t('Vocabulary Terms'),
+    'entity class' => 'TrpVocabularyTerm',
+    'controller class' => 'TrpVocabularyTermController',
+    'fieldable' => TRUE,
+    'entity keys' => array(
+      'id' => 'internal_id'
+    ),
+    // Bundles are defined by the model types below
+    'bundles' => array(),
+  );
+
+  // We want to dynamically add the bundles (or term types) to the entity.
+  $values = array(
+    'cv_id' => array(
+      'name' => 'sequence'
+    ),
+    'name' => 'gene'
+  );
+  $cvterm = chado_generate_var('cvterm', $values);
+  $bundle_id = 'trp_' . $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
+  $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
+  $entities['trp_vocabulary_term']['bundles'][$bundle_id] = array(
+    'label' => $label,
+  );
+  return $entities;
+}
+
+function tripal_entities_load($pid = NULL, $reset = FALSE){
+  $pids = (isset ($pid) ? array($pid) : array());
+  $cvterm = trp_vocabulary_term_load_multiple($pids, $reset);
+  return $cvterm ? reset ($cvterm) : FALSE;
+}
+
+function tripal_entities_load_multiple($pids = array(), $conditions = array(), $reset = FALSE){
+  return entity_load('cvterm', $pids, $conditions, $reset);
+}
+
+function tripal_entities_menu() {
+  $items['admin/structure/cvterm/manage'] = array(
+    'title' => 'Term Admin',
+    'description' => 'Manage cvterm structure',
+    'page callback' => 'cvterm_info',
+    'access arguments' => array('administer cvterms'),
+  );
+  $items['cvterm/%cvterm'] = array(
+    'title callback' => 'cvterm_page_title',
+    'title arguments' => array(1),
+    'page callback' => 'cvterm_page_view',
+    'page arguments' => array(1),
+    'access arguments' => array('view cvterms'),
+    'type' => MENU_CALLBACK,
+  );
+
+
+  $items['data/gene/add'] = array(
+    'title' => 'Add a gene',
+    'page callback' => 'cvterm_add',
+    'access arguments' => array('create cvterm'),
+  );
+  return $items;
+}
+
+function tripal_entities_permission(){
+  return array(
+    'administer cvterms' =>  array(
+      'title' => t('Administer cvterms'),
+      'restrict access' => TRUE,
+    ),
+    'view postsits' => array(
+      'title' => t('View cvterms'),
+    )
+  );
+}
+
+
+function cvterm_info() {
+  return ('Welcome to the administration page for your CV Terms!');
+}
+
+function cvterm_page_title($cvterm){
+  return $cvterm->pid;
+}
+
+function cvterm_page_view($cvterm, $view_mode = 'full'){
+  $cvterm->content = array();
+
+  // Build fields content.
+  field_attach_prepare_view('cvterm', array($cvterm->pid => $cvterm), $view_mode);
+  entity_prepare_view('cvterm', array($cvterm->pid => $cvterm));
+  $cvterm->content += field_attach_view('cvterm', $cvterm, $view_mode);
+
+  return $cvterm->content;
+}
+
+
+function tripal_entities_field_extra_fields() {
+  $return = array();
+  $return['cvterm']['cvterm'] = array(
+    'form' => array(
+      'note' => array(
+        'label' => t('Note'),
+        'description' => t('Term Note'),
+      ),
+    ),
+  );
+
+  return $return;
+}
+function tripal_entities_vocbulary_term_uri($cvterm) {
+  return array(
+    'path' => 'cvterm/' . $cvterm->id,
+  );
+}
+
+function cvterm_add() {
+  $cvterm = (object) array (
+    'pid' => '',
+    'type' => 'cvterm',
+    'note' => '',
+  );
+
+  return drupal_get_form('cvterm_add_form', $cvterm);
+}
+
+
+function cvterm_add_form($form, &$form_state, $cvterm) {
+  $form['note'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Note'),
+    '#required' => TRUE,
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+
+  field_attach_form('cvterm', $cvterm, $form, $form_state);
+
+  return $form;
+}
+function cvterm_add_form_validate($form, &$form_state) {
+  $cvterm_submission = (object) $form_state['values'];
+  field_attach_form_validate('cvterm', $cvterm_submission, $form, $form_state);
+}
+
+function cvterm_add_form_submit($form, &$form_state) {
+  $cvterm_submission = (object) $form_state['values'];
+  field_attach_submit('cvterm', $cvterm_submission, $form, $form_state);
+  $cvterm = cvterm_save($cvterm_submission);
+  $form_state['redirect'] = "cvterm/$cvterm->pid";
+}
+
+function cvterm_save(&$cvterm) {
+  return entity_get_controller('cvterm')->save($cvterm);
+}
+/**
+ *
+ *
+ */
+class TrpVocabularyTermController extends DrupalDefaultEntityController {
+
+  public function save($cvterm) {
+//     drupal_write_record('cvterm', $cvterm);
+//     field_attach_insert('cvterm', $cvterm);
+//     module_invoke_all('entity_insert', 'cvterm', $cvterm);
+    return $cvterm;
+  }
+
+}