123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- <?php
- require_once "api/tripal_entities.api.inc";
- require_once "includes/tripal_entities.chado_entity.inc";
- require_once "includes/tripal_entities.entity_form.inc";
- require_once "includes/tripal_entities.tables.inc";
- require_once "includes/TripalEntity.inc";
- require_once "includes/TripalEntityController.inc";
- require_once "includes/TripalEntityUIController.inc";
- require_once "includes/TripalBundle.inc";
- require_once "includes/TripalBundleController.inc";
- require_once "includes/TripalBundleUIController.inc";
- /**
- * Implements hook_views_api().
- */
- function tripal_entities_views_api() {
- return array(
- 'api' => 3,
- );
- }
- /**
- * Implements hook_menu().
- */
- function tripal_entities_menu() {
- $items = array();
- // The administative settings menu.
- $items['admin/tripal/bio_data'] = array(
- 'title' => 'Biological Data',
- 'description' => 'Tools for publishing, configurating and managing biological data.',
- 'page callback' => 'tripal_entities_admin_view',
- 'access arguments' => array('administer tripal data types'),
- 'file' => 'includes/tripal_entities.admin.inc',
- 'type' => MENU_NORMAL_ITEM,
- );
- // The default tab.
- $items['admin/tripal/bio_data/default'] = array(
- 'title' => 'Biological Data',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => 1,
- );
- $items['admin/tripal/bio_data/publish'] = array(
- 'title' => 'Publish',
- 'description' => 'Publish Data',
- 'page callback' => 'tripal_entities_publish',
- 'access arguments' => array('administer tripal data types'),
- 'file' => 'includes/tripal_entities.admin.inc',
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 2
- );
- $items['admin/tripal/bio_data/publish/%'] = array(
- 'title' => 'Publish',
- 'description' => 'Publish Data',
- 'page callback' => 'tripal_entities_publish',
- 'page arguments' => array(4),
- 'access arguments' => array('administer tripal data types'),
- 'file' => 'includes/tripal_entities.admin.inc',
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/bio_data/access'] = array(
- 'title' => 'Access',
- 'description' => 'Set default access permissions for collections of data.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_entities_admin_access_form'),
- 'access arguments' => array('administer tripal data types'),
- 'file' => 'includes/tripal_entities.admin.inc',
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 3
- );
- return $items;
- }
- /**
- * Implements hook_permission().
- */
- function tripal_entities_permission() {
- // We set up permisssions to manage entity types, manage all entities and the
- // permissions for each individual entity
- $permissions = array(
- 'administer tripal data types' => array(
- 'title' => t('Administer Tripal data types'),
- 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
- ),
- 'administer tripal data' => array(
- 'title' => t('Administer Tripal data'),
- 'description' => t('Edit and delete all tripal data'),
- ),
- );
- return $permissions;
- }
- /**
- * Implements hook_theme().
- */
- function tripal_entities_theme($existing, $type, $theme, $path) {
- return array(
- 'tripal_entity' => array(
- 'render element' => 'elements',
- 'template' => 'tripal_entity',
- 'path' => "$path/theme/templates"
- ),
- 'tripal_entities_date_combo' => array(
- 'render element' => 'element',
- 'file' => 'includes/tripal_entities.chado_entity.inc',
- ),
- );
- }
- /**
- * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
- */
- function tripal_entities_rdf_mapping() {
- return array();
- /* return array(
- 'type' => 'tripal_entity',
- 'bundle' => 'gene',
- 'mapping' => array(
- 'rdftype' => array('sioc:Item', 'foaf:Document'),
- 'title' => array(
- 'predicates' => array('dc:title'),
- ),
- 'uid' => array(
- 'predicates' => array('sioc:has_creator'),
- 'type' => 'rel',
- ),
- 'name' => array(
- 'predicates' => array('foaf:name'),
- ),
- 'uniquename' => array(
- 'predicates' => array('foaf:name'),
- ),
- 'organism_id' => array(
- 'predicates' => array('sioc:has_parent'),
- 'type' => 'rel'
- )
- ),
- ); */
- }
- // 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();
- // Get a list of published vocabularies from 'tripal_vocabulary
- $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
- foreach ($published_vocs as $voc) {
- $entities[$voc->db_id->name] = array (
- // A human readable label to identify our entity.
- 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
- 'plural label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
- // The entity class and controller class extend the classes provided by the
- // Entity API.
- 'entity class' => 'TripalEntity',
- 'controller class' => 'TripalEntityController',
- // The table for this entity defined in hook_schema()
- 'base table' => 'tripal_entity',
- // Returns the uri elements of an entity.
- 'uri callback' => 'tripal_entities_vocbulary_term_uri',
- // IF fieldable == FALSE, we can't attach fields.
- 'fieldable' => TRUE,
- // entity_keys tells the controller what database fields are used for key
- // functions. It is not required if we don't have bundles or revisions.
- // Here we do not support a revision, so that entity key is omitted.
- 'entity keys' => array (
- 'id' => 'id',
- 'bundle' => 'bundle'
- ),
- 'bundle keys' => array (
- 'bundle' => 'bundle'
- ),
- // Callback function for access to this entity.
- 'access callback' => 'tripal_entity_access',
- // FALSE disables caching. Caching functionality is handled by Drupal core.
- 'static cache' => FALSE,
- // Bundles are added in the hook_entities_info_alter() function.
- 'bundles' => array (),
- 'label callback' => 'tripal_entity_label',
- // The information below is used by the TripalEntityUIController
- // (which extends the EntityDefaultUIController). The admin_ui
- // key here is mean to appear on the 'Find Content' page of the
- // administrative menu.
- 'admin ui' => array (
- 'path' => 'admin/content/bio_data',
- 'controller class' => 'TripalEntityUIController',
- 'menu wildcard' => '%tripal_entity',
- 'file' => 'includes/TripalEntityUIController.inc'
- ),
- 'view modes' => array (
- 'full' => array (
- 'label' => t ( 'Full content' ),
- 'custom settings' => FALSE
- ),
- 'teaser' => array (
- 'label' => t ( 'Teaser' ),
- 'custom settings' => TRUE
- )
- )
- );
- // The entity that holds information about the entity types.
- $entities [$voc->db_id->name . '_bundle'] = array (
- 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
- 'entity class' => 'TripalBundle',
- 'controller class' => 'TripalBundleController',
- 'base table' => 'tripal_bundle',
- 'fieldable' => FALSE,
- 'exportable' => FALSE,
- 'entity keys' => array (
- 'id' => 'id',
- 'name' => 'bundle',
- 'label' => 'label'
- ),
- 'access callback' => 'tripal_bundle_access',
- 'module' => 'tripal_entities',
- // Enable the entity API's admin UI.
- 'admin ui' => array (
- 'path' => 'admin/structure/bio_data/' . $voc->db_id->name,
- 'controller class' => 'TripalBundleUIController',
- 'file' => 'includes/TripalBundleUIController.inc',
- 'menu wildcard' => '%tripal_bundle',
- )
- );
- }
- return $entities;
- }
- /**
- * Implements hook_entity_info_alter().
- *
- * We are adding the info about the tripal data types via a hook to avoid a
- * recursion issue as loading the model types requires the entity info as well.
- *
- */
- function tripal_entities_entity_info_alter(&$entity_info) {
- // Get a list of published terms from 'tripal_term
- $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
- foreach ($published_terms as $term) {
- // Bundles are alternative groups of fields or configuration
- // associated with a base entity type.
- // We want to dynamically add the bundles (or term types) to the entity.
- $cvterm = $term->cvterm_id;
- $entity_type = $cvterm->dbxref_id->db_id->name;
- $accession = $cvterm->dbxref_id->accession;
- $bundle_id = $entity_type . '_' . $accession;
- $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
- $entity_info[$entity_type]['bundles'][$bundle_id] = array (
- 'label' => $label,
- 'admin' => array (
- 'path' => 'admin/structure/bio_data/' . $entity_type . '/manage/%tripal_bundle',
- 'real path' => 'admin/structure/bio_data/' . $entity_type . '/manage/' . $bundle_id,
- 'bundle argument' => 5,
- 'access arguments' => array (
- 'administer tripal data types'
- )
- )
- );
- }
- }
- /**
- * Get published vocabularies as select options
- * @return multitype:NULL
- */
- function tripal_entities_get_published_vocabularies_as_select_options() {
- $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
- $options = array();
- foreach ($published_vocs as $voc) {
- $options [$voc->cv_id->cv_id] = $voc->cv_id->name;
- }
- return $options;
- }
- /**
- * Get published vocabularies as select options
- * @return multitype:NULL
- */
- function tripal_entities_get_db_names_for_published_vocabularies() {
- $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
- $db = array();
- foreach ($published_vocs as $voc) {
- $db [$voc->db_id->db_id] = $voc->db_id->name;
- }
- return $db;
- }
- /**
- * Get published terms as select options
- * @return multitype:NULL
- */
- function tripal_entities_get_published_terms_as_select_options($cv_id = NULL) {
- $where = array('publish' => 1);
- $published_terms = chado_generate_var('tripal_term', $where, array('return_array' => 1));
- $options = array();
- foreach ($published_terms as $term) {
- if (!$cv_id) {
- $options [$term->cvterm_id->name] = $term->cvterm_id->name;
- } else {
- if ($term->cvterm_id->cv_id->cv_id == $cv_id) {
- $options [$term->cvterm_id->name] = $term->cvterm_id->name;
- }
- }
- }
- return $options;
- }
- /**
- * Menu argument loader; Load a tripal data type by string.
- *
- * @param $type
- * The machine-readable name of a tripal data type to load.
- * @return
- * A tripal data type array or FALSE if $type does not exist.
- */
- function tripal_bundle_load($bundle_type, $reset = FALSE) {
- // Get the type of entity by the ID.
- $bundle_types = db_select('tripal_bundle', 'tdt')
- ->fields('tdt', array('id', 'type'))
- ->condition('bundle', $bundle_type)
- ->execute()
- ->fetchObject();
- // Load the tripal_bundle entity. These entities are always the same
- // as an Tripal entity type but with a '_bundle' extension.
- $entity = entity_load($bundle_types->type . '_bundle', array($bundle_types->id), array(), $reset);
- return reset($entity);
- }
- /**
- * Allows the menu system to use a wildcard to fetch the entity.
- *
- * Make sure that the wildcard you choose in the tripal_entity entity
- * definition fits the function name here.
- *
- * @param $id
- * Integer specifying the tripal_entity id.
- * @param $reset
- * A boolean indicating that the internal cache should be reset.
- * @return
- * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
- *
- * @see tripal_entity_load_multiple()
- */
- function tripal_entity_load($id, $reset = FALSE) {
- // Get the type of entity by the ID.
- $entity_type = db_select('tripal_entity', 'td')
- ->fields('td', array('type'))
- ->condition('id', $id)
- ->execute()
- ->fetchField();
- // Load the entity.
- if ($entity_type) {
- $entity = entity_load($entity_type, array($id), array(), $reset);
- return reset($entity);
- }
- return FALSE;
- }
- /**
- *
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_field_edit_form is used for customizing the settings of
- * a field attached to an entity.
- */
- function tripal_entities_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
- // For entity fields added by Tripal Entities we don't want the
- // the end-user to change the cardinality and the required fields
- // such that record can't be saved in Chado.
- $dbs = tripal_entities_get_db_names_for_published_vocabularies();
- if (in_array($form['#instance']['entity_type'], $dbs)) {
- $form['field']['cardinality']['#access'] = FALSE;
- $form['instance']['required']['#access'] = FALSE;
- }
- // TODO: don't the the maximum length be larger than the field size.
- }
- /**
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_display_overview_form is used for formatting the display
- * or layout of fields attached to an entity.
- */
- function tripal_entities_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
- $entity_type = $form['#entity_type'];
- $bundle_name = $form['#bundle'];
- // Get the bundle record.
- $bundle = db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->condition('bundle', $bundle_name)
- ->execute()
- ->fetchObject();
- if (module_exists('ds')) {
- drupal_set_message('Tripal is not compatible with the Display Suite (ds) module. If you would like to use Tripal-style panels for the layout of your pages please disable the Display Suite module.', 'warning');
- }
- // Add a vertical tab fieldset at the bottom of the
- $form['overview_vert_tabs'] = array(
- '#type' => 'vertical_tabs'
- );
- $form['modes']['#group'] = 'overview_vert_tabs';
- $form['modes']['#weight'] = 1000;
- $form['tripal_entity_layout'] = array(
- '#type' => 'fieldset',
- '#title' => 'Panels',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'overview_vert_tabs'
- );
- $form['tripal_entity_layout']['panel_name'] = array(
- '#type' => 'textfield',
- '#title' => 'Add a new Panel',
- '#description' => t('You may add as many panels to your page layout as
- desired. Panels can be used to organize fields into categories....')
- );
- // Make sure our default panels are in the database.
- _tripal_entities_check_default_field_panels();
- // Now add each panel as a region.
- $form['fields']['#regions'] = array();
- $panels = db_select('tripal_panels', 'tp')
- ->fields('tp')
- ->condition('bundle_id', $bundle->id)
- ->orderBy('weight', 'ASC')
- ->orderBy('label', 'ASC')
- ->execute();
- $panel_options = array();
- while ($panel = $panels->fetchObject()) {
- $settings = unserialize($panel->settings);
- $form['fields']['#regions'][$panel->name] = array(
- 'title' => t($panel->label),
- 'message' => t($settings['message']),
- );
- $panel_options[$panel->name] = $panel->label;
- }
- // Set the table headers to add a new 'region' column.
- $form['fields']['#header'] = array(
- t('Field'),
- t('Weight'),
- t('Parent'),
- t('Label'),
- array('data' => t('Format'), 'colspan' => 3),
- t('Region'),
- );
- // Change the region callback for each field to place each field in the
- // proper "panel" region. Also, set the default region to be the base region.
- $fields = $form['fields'];
- foreach (element_children($fields) as $field_name) {
- $form['fields'][$field_name]['#region_callback'] = 'tripal_entities_field_ui_row_region';
- $form['fields'][$field_name]['region'] = array(
- '#type' => 'select',
- '#options' => $panel_options,
- '#default_value' => 'te_base',
- '#attributes' => array(
- 'class' => array('te-field-region'),
- )
- );
- }
- // Add validate and submit handlers. Rearrange the submit callbacks
- // so ours is first.
- $form['#validate'][] = 'tripal_entities_field_ui_validate';
- $submit = $form['#submit'];
- $form['#submit'] = array('tripal_entities_field_ui_submit');
- $form['#submit'] = array_merge($form['#submit'], $submit);
- }
- /**
- * A helper function for checking if the default panels are in the database.
- */
- function _tripal_entities_check_default_field_panels() {
- // Make sure we have records for our default regions: te_base and te_hidden.
- // First check if the base region is in the database. If not, add it.
- $te_base = db_select('tripal_panels', 'tp')
- ->fields('tp')
- ->condition('name', 'te_base')
- ->execute()
- ->fetchObject();
- if (!$te_base) {
- $settings = array(
- 'message' => 'Fields that should appear at the top of each page should be placed in the base panel. These fields are always present and help orient the user by at least indicating the necessary information to uniquely identiy the content.',
- );
- db_insert('tripal_panels')
- ->fields(array(
- 'bundle_id' => $bundle->id,
- 'name' => 'te_base',
- 'label' => 'Base Content',
- 'settings' => serialize($settings),
- 'weight' => -9999999
- ))
- ->execute();
- }
- // Next check if the hidden region is in the database. If not, add it.
- $te_base = db_select('tripal_panels', 'tp')
- ->fields('tp')
- ->condition('name', 'te_hidden')
- ->execute()
- ->fetchObject();
- if (!$te_base) {
- $settings = array(
- 'message' => 'Place field here to hide them from display.',
- );
- db_insert('tripal_panels')
- ->fields(array(
- 'bundle_id' => $bundle->id,
- 'name' => 'te_hidden',
- 'label' => 'Hidden',
- 'settings' => serialize($settings),
- 'weight' => 9999999
- ))
- ->execute();
- }
- }
- /**
- * Returns the region to which a row on the Field UI page belongs.
- *
- * @param $row
- * The current row that is being rendered in the Field UI page.
- */
- function tripal_entities_field_ui_row_region($row) {
- $default_panel = 'te_base';
- $panel = '';
- $field_name = $row['#parents'][1];
- $field = field_info_field($field_name);
- // First get the panel
- if (!$panel) {
- $panel = $default_panel;
- }
- // See if there is a record in the tripal_panel_fields for this field.
- return $default_panel;
- }
- /**
- * Validates the field UI form to ensure proper panel assignments.
- *
- * @param $form
- * @param $form_state
- */
- function tripal_entities_field_ui_validate($form, &$form_state) {
- }
- /**
- * Responds to a submit from the field UI form for saving panel assignments.
- *
- * @param $form
- * @param $form_state
- */
- function tripal_entities_field_ui_submit($form, &$form_state) {
- }
|