id_count = count(explode('/', $this->path)); $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object'; $id_count = count(explode('/', $this->path)); // The content menu. $items[$this->path] = array( 'title' => 'Biological Data', 'page callback' => 'tripal_entities_content_view', 'file' => 'includes/tripal_entities.admin.inc', 'file path' => drupal_get_path('module', 'tripal_entities'), 'access arguments' => array('administer tripal data'), 'type' => MENU_LOCAL_TASK, ); // Change the add page menu to multiple types of entities $items[$this->path . '/add'] = array( 'title' => 'Add new biological data', 'description' => 'Add new biological data', 'page callback' => 'drupal_get_form', 'page arguments' => array('tripal_entity_form'), 'access callback' => 'tripal_entity_access', 'access arguments' => array('administer tripal data'), 'type' => MENU_LOCAL_ACTION, 'weight' => 20, ); // Set a custom page for adding new tripal data entities. $items['data/add'] = array( 'title' => 'Add Tripal data', 'description' => 'Add a new tripal data record', 'page callback' => 'drupal_get_form', 'page arguments' => array('tripal_entity_form'), 'access callback' => 'tripal_entity_access', 'access arguments' => array('edit'), 'type' => MENU_NORMAL_ITEM, 'weight' => 20, ); // Link for viewing a tripal data type. $items['data/' . $wildcard] = array( 'title callback' => 'tripal_entity_title', 'title arguments' => array(1), 'page callback' => 'tripal_entity_view', 'page arguments' => array(1), 'access callback' => 'tripal_entity_access', 'access arguments' => array('view', 1), 'type' => MENU_CALLBACK, ); // 'View' tab for an individual entity page. $items['data/' . $wildcard . '/view'] = array( 'title' => 'View', 'page callback' => 'tripal_entity_view', 'page arguments' => array(1), 'access callback' => 'tripal_entity_access', 'access arguments' => array('view', 1), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); // 'Edit' tab for an individual entity page. $items['data/' . $wildcard . '/edit'] = array( 'title' => 'Edit', 'page callback' => 'drupal_get_form', 'page arguments' => array('tripal_entity_form', 1), 'access callback' => 'tripal_entity_access', 'access arguments' => array('edit', 1), 'type' => MENU_LOCAL_TASK, ); // Menu item for deleting tripal data entities. $items['data/' . $wildcard . '/delete'] = array( 'title' => 'Delete', 'page callback' => 'drupal_get_form', 'page arguments' => array('tripal_entity_delete_form', 1), 'access callback' => 'tripal_entity_access', 'access arguments' => array('edit', 1), 'type' => MENU_CALLBACK, 'weight' => 10, ); return $items; } } /** * Determines whether the given user has access to a tripal data entity. * * @param $op * The operation being performed. One of 'view', 'update', 'create', 'delete' * or just 'edit' (being the same as 'create' or 'update'). * @param $entity * Optionally a tripal data entity or a tripal data type to check access for. * If nothing is given, access for all types is determined. * @param $account * The user to check for. Leave it to NULL to check for the global user. * @return boolean * Whether access is allowed or not. */ function tripal_entity_access($op, $entity = NULL, $account = NULL) { if (user_access('administer tripal data', $account)) { return TRUE; } if (isset($entity) && $type_name = $entity->type) { $op = ($op == 'view') ? 'view' : 'edit'; if (user_access("$op any $type_name data", $account)) { return TRUE; } } return FALSE; } /** * */ function tripal_entity_form($form, &$form_state, $entity = NULL) { // Set the defaults. $cv_id = NULL; $term_name = NULL; $cvterm = NULL; // Set defaults if an entity was provided. if ($entity) { drupal_set_title('Edit ' . $entity->title); $id = $entity->id; $values = array('cvterm_id' => $entity->cvterm_id); $cvterm = chado_generate_var('cvterm', $values); $cv_id = $cvterm->cv_id->cv_id; $term_name = $cvterm->name; } // Set defaults using the form state. 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; // Get the cvterm that matches $values = array( 'cv_id' => $cv_id, 'name' => $term_name ); $cvterm = chado_generate_var('cvterm', $values); } // Let the user select the vocabulary defaut and tripal_entity but only if they haven't // already selected a tripal_entity. $cvs = tripal_entities_get_published_vocabularies_as_select_options(); if (!$term_name) { $form['cv_id'] = array( '#type' => 'select', '#title' => t('Published 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_entity_form_ajax_callback", 'wrapper' => 'tripal_entity_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) { $cvterms = tripal_entities_get_published_terms_as_select_options ($cv_id); $form['cvterm_select']['term_name'] = array( '#title' => t('Published term'), '#type' => 'select', '#options' => $cvterms, '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."), '#required' => TRUE, ); $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 ($cvterm) { $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession; $form['cv_id'] = array( '#type' => 'hidden', '#value' => $cv_id, ); $form['type'] = array( '#type' => 'hidden', '#value' => $cvterm->dbxref_id->db_id->name, ); $form['term_name'] = array( '#type' => 'hidden', '#value' => $term_name, ); $form['cvterm_id'] = array( '#type' => 'hidden', '#value' => $cvterm->cvterm_id, ); $form['bundle'] = array( '#type' => 'hidden', '#value' => $bundle_id, ); $form['details'] = array( '#type' => 'fieldset', '#title' => 'Record Type', '#collapsable' => FALSE, '#weight' => -100, ); $form['details']['cv_name_shown'] = array( '#type' => 'item', '#title' => 'Vocabulary', '#markup' => $cvterm->cv_id->name, ); $form['details']['term_name_shown'] = array( '#type' => 'item', '#title' => 'Term', '#markup' => $cvterm->name, ); // If the entity doesn't exist then create one. if (!$entity) { $entity = entity_get_controller($cvterm->dbxref_id->db_id->name)->create(array('bundle' => $bundle_id)); field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state); $form['add_button'] = array( '#type' => 'submit', '#value' => t('Add a new ' . $cvterm->name), '#name' => 'add_data', '#weight' => 1000 ); } else { field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state); $form['entity_id'] = array( '#type' => 'hidden', '#value' => $entity->id, ); $form['update_button'] = array( '#type' => 'submit', '#value' => t('Update'), '#name' => 'update_data', '#weight' => 1000 ); $form['delete_button'] = array( '#type' => 'submit', '#value' => t('Delete'), '#name' => 'delete_data', '#weight' => 1001, ); } // The entity object must be added to the $form_state in order for // the Entity API to work. It must have a key of the entity name. $form_state[$cvterm->dbxref_id->db_id->name] = $entity; } $form['#prefix'] = '
' . t('This action cannot be undone.') . '
', t('Delete'), t('Cancel'), 'confirm' ); return $form; } /** * Submit callback for tripal_entity_delete_form */ function tripal_entity_delete_form_submit($form, &$form_state) { $entity = $form_state['entity']; $entity_controller = new TripalEntityController($entity->type); if($entity_controller->delete($entity)) { drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title))); $form_state['redirect'] = 'admin/content/tripal_entitys'; } else { drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error"); } } /** * Menu callback to display an entity. * * As we load the entity for display, we're responsible for invoking a number * of hooks in their proper order. * * @see hook_entity_prepare_view() * @see hook_entity_view() * @see hook_entity_view_alter() */ function tripal_entity_view($entity, $view_mode = 'full') { if ($entity) { $controller = entity_get_controller($entity->type); $content = $controller->view(array($entity->id => $entity)); drupal_set_title($entity->title); return $content; } } /** * Menu title callback for showing individual entities */ function tripal_entity_title($entity){ if ($entity) { return $entity->title; } }