123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?php
- // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
- // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
- /**
- * Implement hook_entity_info().
- */
- function tripal_entities_entity_info() {
- $entities = array();
- $entities['chado_data'] = 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' => 'entity_id',
- 'bundle' => 'bundle',
- ),
- // 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($entity_id = NULL, $reset = FALSE){
- $ids = (isset ($entity_id) ? array($entity_id) : array());
- $chado_data = tripal_entities_load_multiple($entity_ids, $reset);
- return $chado_data ? reset ($chado_data) : FALSE;
- }
- function tripal_entities_load_multiple($entity_ids = array(), $conditions = array(), $reset = FALSE){
- return entity_load('chado_data', $entity_ids, $conditions, $reset);
- }
- function tripal_entities_menu() {
- $items['admin/structure/chado_data/manage'] = array(
- 'title' => 'Term Admin',
- 'description' => 'Manage chado_data structure',
- 'page callback' => 'chado_data_info',
- 'access arguments' => array('administer chado_datas'),
- );
- $items['chado_data/%'] = array(
- 'title callback' => 'chado_data_page_title',
- 'title arguments' => array(1),
- 'page callback' => 'chado_data_page_view',
- 'page arguments' => array(1),
- 'access arguments' => array('view chado_data'),
- 'type' => MENU_CALLBACK,
- );
- $items['data/add'] = array(
- 'title' => 'Add new data',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_entities_add_form'),
- 'access arguments' => array('create chado_data'),
- );
- return $items;
- }
- function tripal_entities_permission(){
- return array(
- 'administer chado_datas' => array(
- 'title' => t('Administer chado_datas'),
- 'restrict access' => TRUE,
- ),
- 'view postsits' => array(
- 'title' => t('View chado_datas'),
- )
- );
- }
- function chado_data_info() {
- return ('Welcome to the administration page for your CV Terms!');
- }
- function chado_data_page_title($chado_data){
- return $chado_data->entity_id;
- }
- function chado_data_page_view($chado_data, $view_mode = 'full'){
- $chado_data->content = array();
- // Build fields content.
- field_attach_prepare_view('chado_data', array($chado_data->entity_id => $chado_data), $view_mode);
- entity_prepare_view('chado_data', array($chado_data->entity_id => $chado_data));
- $chado_data->content += field_attach_view('chado_data', $chado_data, $view_mode);
- return $chado_data->content;
- }
- /**
- *
- */
- function tripal_entities_vocbulary_term_uri($entity) {
- return array(
- 'path' => 'chado_data/' . $entity->entity_id,
- );
- }
- /**
- *
- */
- function tripal_entities_add_form($form, &$form_state) {
- // Set the defaults.
- $cv_id = NULL;
- $term_name = NULL;
- if (array_key_exists('values', $form_state)) {
- $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
- $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
- }
- // Let the user select the vocabulary and chado_data but only if they haven't
- // already selected a chado_data.
- $cvs = tripal_get_cv_select_options();
- if (!$term_name) {
- $form['cv_id'] = array(
- '#type' => 'select',
- '#title' => t('Vocabulary'),
- '#options' => $cvs,
- '#required' => TRUE,
- '#description' => t('Select a vocabulary that contains the term for the type of data you want to add.'),
- '#default_value' => $cv_id,
- '#ajax' => array(
- 'callback' => "tripal_entities_add_form_ajax_callback",
- 'wrapper' => 'tripal_entities_add_form',
- 'effect' => 'fade',
- 'method' => 'replace'
- )
- );
- }
- // If we have a CV ID then we want to provide an autocomplete field
- if ($cv_id and !$term_name) {
- $form['cvterm_select']['term_name'] = array(
- '#title' => t('Record Type'),
- '#type' => 'textfield',
- '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."),
- '#required' => TRUE,
- '#default_value' => $term_name,
- '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
- );
- $form['cvterm_select']['select_button'] = array(
- '#type' => 'submit',
- '#value' => t('Use this term'),
- '#name' => 'select_cvterm',
- );
- }
- // Once the CV term is selected then provide the other fields.
- if ($term_name) {
- // Get the cvterm that matches
- $values = array(
- 'cv_id' => $cv_id,
- 'name' => $term_name
- );
- $cvterm = tripal_get_cvterm($values);
- $form['cv_id'] = array(
- '#type' => 'hidden',
- '#value' => $cv_id,
- );
- $form['cvterm_id'] = array(
- '#type' => 'hidden',
- '#value' => $cvterm->cvterm_id,
- );
- $form['bundle'] = array(
- '#type' => 'hidden',
- '#value' => $term_name,
- );
- $form['cv_name'] = array(
- '#type' => 'item',
- '#title' => 'Vocabulary',
- '#markup' => $cvterm->cv_id->name,
- '#weight' => -100,
- );
- $form['term_name_shown'] = array(
- '#type' => 'item',
- '#title' => 'Record Type',
- '#markup' => $cvterm->name,
- '#weight' => -100,
- );
- /* // Drupal field types and settings:
- // https://www.drupal.org/node/1879542
- $field = array(
- 'field_name' => 'feature__name',
- 'type' => 'text',
- 'cardinality' => 1,
- 'storage' => array(
- 'type' => 'tripal_entities_storage'
- ),
- );
- field_create_field($field);
- $field_instance = array(
- 'field_name' => 'feature__name',
- 'label' => 'Name',
- 'widget' => array(
- 'type' => 'text_textfield'
- ),
- 'entity_type' => 'chado_data',
- 'required' => 'true',
- 'settings' => array(
- 'max_length' => 255
- ),
- 'bundle' => $cvterm->name,
- );
- field_create_instance($field_instance);
- $field = array(
- 'field_name' => 'feature__uniquename',
- 'type' => 'text',
- 'cardinality' => 1,
- 'storage' => array(
- 'type' => 'tripal_entities_storage'
- ),
- );
- field_create_field($field);
- $field_instance = array(
- 'field_name' => 'feature__uniquename',
- 'label' => 'Unique Name',
- 'widget' => array(
- 'type' => 'text_textfield'
- ),
- 'entity_type' => 'chado_data',
- 'required' => 'true',
- 'settings' => array(
- 'max_length' => 255
- ),
- 'bundle' => $cvterm->name,
- );
- field_create_instance($field_instance);
- $field = array(
- 'field_name' => 'feature__organism_id',
- 'type' => 'organism_id',
- 'cardinality' => 1,
- 'storage' => array(
- 'type' => 'tripal_entities_storage'
- ),
- );
- field_create_field($field);
- $field_instance = array(
- 'field_name' => 'feature__organism_id',
- 'label' => 'Organism',
- 'entity_type' => 'chado_data',
- 'required' => 'true',
- 'settings' => array(),
- 'bundle' => $cvterm->name,
- );
- field_create_instance($field_instance);*/
- // Create the object for this entity instance. The entity instance type
- // is always the name of the term.
- $entity = new stdClass();
- $entity->entity_id = NULL;
- $entity->bundle = $cvterm->name;
- $form['#parents'] = array('attached');
- field_attach_form('chado_data', $entity, $form, $form_state);
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Add a new ' . $cvterm->name),
- '#name' => 'add_data',
- '#weight' => 1000
- );
- }
- $form['#prefix'] = '<div id="tripal_entities_add_form">';
- $form['#suffix'] = '</div>';
- return $form;
- }
- /**
- * Implements hook_field_info().
- */
- function tripal_entities_field_info() {
- $fields = array(
- 'organism_id' => array(
- 'label' => t('Organism'),
- 'description' => t('A field for specifying an organism.'),
- 'default_widget' => 'tripal_entities_organism_select_widget',
- 'default_formatter' => 'tripal_entities_organism_formatter',
- 'settings' => array(),
- 'storage' => array(
- 'type' => 'tripal_entities_storage',
- 'module' => 'tripal_entities',
- 'active' => TRUE
- ),
- ),
- );
- return $fields;
- }
- /**
- * Implements hook_field_widget_info().
- */
- function tripal_entities_field_widget_info() {
- return array(
- 'tripal_entities_organism_select_widget' => array(
- 'label' => t('Organism Select'),
- 'field types' => array('organism_id')
- ),
- );
- }
- /**
- * Implements hook_field_formatter_info().
- */
- function tripal_entities_field_formatter_info() {
- return array(
- 'tripal_entities_organism_formatter' => array(
- 'label' => t('Organism'),
- 'field types' => array('organism_id')
- ),
- );
- }
- /**
- * Implements hook_field_widget_form().
- */
- function tripal_entities_field_widget_form(&$form, &$form_state, $field,
- $instance, $langcode, $items, $delta, $element) {
- $widget = $element;
- $widget['#delta'] = $delta;
- switch ($instance['widget']['type']) {
- case 'tripal_entities_organism_select_widget':
- $options = tripal_get_organism_select_options();
- $widget += array(
- '#type' => 'select',
- '#title' => $element['#title'],
- '#description' => $element['#description'],
- '#options' => $options,
- '#default_value' => '',
- '#required' => $element['#required'],
- '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
- '#delta' => $delta,
- '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
- );
- $element['value'] = $widget;
- break;
- }
- return $element;
- }
- /**
- * Callback function for validating the tripal_entities_organism_select_widget.
- */
- function tripal_entities_organism_select_widget_validate($element, &$form_state) {
- $field_name = $element['#field_name'];
- // Make sure we have a valid organism
- foreach ($form_state['values'][$field_name] as $langcode => $items) {
- foreach ($items as $delta => $value) {
- $organism_id = chado_select_record('organism', array('organism_id'),
- array('organism_id' => $value['value']), array('has_record' => TRUE));
- if (!$organism_id) {
- form_error($element, t("Please specify an organism that already exists in the database."));
- }
- }
- }
- }
- /**
- * Implements hook_field_is_empty().
- */
- function tripal_entities_field_is_empty($item, $field) {
- if (empty($item['tripal_entities_organism_select_widget'])) {
- return TRUE;
- }
- }
- /**
- * Implements hook_field_storage_info().
- */
- function tripal_entities_field_storage_info() {
- return array(
- 'tripal_entities_storage' => array(
- 'label' => t('Chado storage'),
- 'description' => t('Stores fields in the local Chado database.'),
- 'settings' => array(),
- ),
- );
- }
- /**
- * Implements hook_field_storage_write().
- */
- function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields) {
- // Get the IDs for this entity.
- list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
- // Find out which table should receive the insert.
- $tablename = 'feature';
- $type_field = 'type_id';
- $schema = chado_get_schema($tablename);
- $pkey_field = $schema['primary key'][0];
- // Construct the values array that will be used to insert into the table.
- $values = array();
- foreach ($fields as $field_id) {
- $field = field_info_field_by_id($field_id);
- $field_name = $field['field_name'];
- $matches = array();
- if (preg_match('/^' . $tablename . '__(.*)/', $field_name, $matches)) {
- $chado_field = $matches[1];
- // Currently, we only support one language, but for for the sake of
- // thoroughness we'll iterate through all possible languages.
- $all_languages = field_available_languages($entity_type, $field);
- $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
- foreach ($field_languages as $langcode) {
- $items = (array) $entity->{$field_name}[$langcode];
- // The number of items is related to the cardinatily of the field.
- foreach ($items as $delta => $item) {
- $values[$chado_field] = $item['value'];
- }
- }
- }
- }
- // Add in the type_id field.
- $values[$type_field] = $entity->cvterm_id;
- switch ($op) {
- case FIELD_STORAGE_INSERT:
- $record = chado_insert_record($tablename, $values);
- if (!$record) {
- drupal_set_message('Could not insert record.', 'error');
- }
- $entity->record_id = $record[$pkey_field];
- break;
- case FIELD_STORAGE_UPDATE:
- $match[$pkey_field] = $entity->record_id;
- chado_update_record($tablename, $match, $values);
- break;
- }
- }
- /**
- * Implements hook_field_storage_load().
- *
- * Responsible for loading the fields from the Chado database and adding
- * their values to the entity.
- */
- function tripal_entities_field_storage_load($entity_type, $entities, $age, $fields, $options) {
- }
- /**
- * An Ajax callback for the tripal_entities_add_form.
- */
- function tripal_entities_add_form_ajax_callback($form, $form_state) {
- // return the form so Drupal can update the content on the page
- return $form;
- }
- /**
- * Implements hook_validate() for the tripal_entities_add_form.
- */
- function tripal_entities_add_form_validate($form, &$form_state) {
- if ($form_state['clicked_button']['#name'] == 'add_data') {
- $chado_data = (object) $form_state['values'];
- field_attach_form_validate('chado_data', $chado_data, $form, $form_state);
- }
- }
- /**
- * Implements hook_submit() for the tripal_entities_add_form.
- *
- */
- function tripal_entities_add_form_submit($form, &$form_state) {
- if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
- // don't do anything, we just need to know what the term name is.
- $form_state['rebuild'] = TRUE;
- }
- else {
- // Build and entity instance object.
- $entity = (object) $form_state['values'];
- // This is an update if the entity_id is in the form_state.
- if (array_key_exists('entity_id', $form_state['values'])) {
- $entity->entity_id = $form_state['values']['entity_id'];
- field_attach_update('chado_data', $entity);
- }
- else {
- field_attach_insert('chado_data', $entity);
- }
- // Now save the entity
- $entity = entity_get_controller('chado_data')->save($entity);
- // $form_state['redirect'] = "chado_data/$chado_data->entity_id";
- }
- }
- /**
- * Implements hook_theme().
- */
- function tripal_entities_theme($existing, $type, $theme, $path) {
- return array(
- );
- }
- /**
- *
- *
- */
- class TrpVocabularyTermController extends DrupalDefaultEntityController {
- public function save($entity) {
- global $user;
- $tablename = 'feature';
- $type_field = 'type_id';
- $schema = chado_get_schema($tablename);
- $pkey_field = $schema['primary key'][0];
- $record = array(
- 'cvterm_id' => $entity->cvterm_id,
- 'tablename' => $tablename,
- 'record_id' => $entity->record_id,
- 'title' => 'title',
- 'uid' => $user->uid,
- 'created' => time(),
- 'changed' => time(),
- );
- $success = drupal_write_record('tripal_entity', $record);
- return $entity;
- }
- }
|