123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800 |
- <?php
- class TripalEntityUIController extends EntityDefaultUIController {
-
- public function hook_menu() {
- $items = array();
-
- $this->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));
-
- $items[$this->path] = array(
- 'title' => 'Tripal Content',
- 'page callback' => 'tripal_content_view',
- 'file' => 'includes/tripal.admin.inc',
- 'file path' => drupal_get_path('module', 'tripal'),
- 'access arguments' => array('access tripal content overview'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => -9
- );
- $items['bio_data/add'] = array(
- 'title' => 'Add Tripal Content',
- 'page callback' => 'tripal_add_page',
- 'access callback' => '_tripal_entity_add_access',
- );
-
- $bundles = array_keys($this->entityInfo['bundles']);
- foreach ($bundles as $bundle_name) {
- $matches = array();
- if (preg_match('/^bio_data_(.*?)$/', $bundle_name, $matches)) {
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
- if (!$bundle) {
- throw new Exception(t("Cannot find bundle that matches: %bundle_name",
- array('%bundle_name' => $bundle_name)));
- }
- $term_id = $matches[1];
-
- $term = entity_load('TripalTerm', array('id' => $term_id));
- $term = reset($term);
- $default_description = $term->definition ? $term->definition : '';
-
- $items['bio_data/add/' . $term->id] = array(
- 'title' => ucfirst($bundle->label),
- 'description' => tripal_get_bundle_variable('description', $bundle->id, $default_description),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_entity_form', 2),
- 'access arguments' => array('create bio_data_' . $term->id),
- );
- }
- }
-
- $items['bio_data/' . $wildcard] = array(
- 'title callback' => 'tripal_entity_title',
- 'title arguments' => array(1),
- 'page callback' => 'tripal_view_entity',
- 'page arguments' => array(1),
- 'access callback' => 'tripal_entity_access',
- 'access arguments' => array('view', 1),
- 'type' => MENU_CALLBACK,
- );
-
- $items['bio_data/' . $wildcard . '/view'] = array(
- 'title' => 'View',
- 'page callback' => 'tripal_view_entity',
- 'page arguments' => array(1),
- 'access callback' => 'tripal_entity_access',
- 'access arguments' => array('view', 1),
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => -10,
- );
-
- $items['bio_data/' . $wildcard . '/edit'] = array(
- 'title' => 'Edit',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_entity_form', NULL, 1),
- 'access callback' => 'tripal_entity_access',
- 'access arguments' => array('edit', 1),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => -8,
- );
-
- $items['bio_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('delete', 1),
- 'type' => MENU_CALLBACK,
- 'weight' => 10,
- );
- return $items;
- }
- }
- function tripal_view_entity($entity, $view_mode = 'full') {
- $content = '';
- $controller = entity_get_controller($entity->type);
- $content = $controller->view(array($entity->id => $entity));
- drupal_set_title($entity->title);
- if ($entity->status == 0) {
- drupal_set_message('This page is currently unpublished', 'warning');
- }
- return $content;
- }
-
- function tripal_content_view() {
-
- $form = drupal_get_form('tripal_content_overview_form');
- $output = drupal_render($form);
-
- $breadcrumb = array();
- $breadcrumb[] = l('Home', '<front>');
- $breadcrumb[] = l('Administration', 'admin');
- drupal_set_breadcrumb($breadcrumb);
- return $output;
- }
-
- function tripal_content_overview_form($form, &$form_state) {
- global $user;
-
-
-
- $etype = '';
- $status = '';
- if (array_key_exists('tripal_content_overview', $_SESSION)) {
- $etype = $_SESSION['tripal_content_overview']['type'];
- $status = $_SESSION['tripal_content_overview']['status'];
- }
- $etype = array_key_exists('type', $_GET) ? $_GET['type'] : $etype;
- $status = array_key_exists('status', $_GET) ? $_GET['status'] : $status;
- $headers = array(
- 'title' => array(
- 'data' => t('Title'),
- 'type' => 'property',
- 'specifier' => 'title'
- ),
- 'Type',
- 'Term',
- 'Author',
- 'status' => array(
- 'data' => t('Status'),
- 'type' => 'property',
- 'specifier' => 'status'
- ),
- 'changed' => array(
- 'data' => t('Updated'),
- 'type' => 'property',
- 'specifier' => 'changed',
- 'sort' => 'desc',
- ),
- 'Operations',
- );
- $rows = array();
-
-
-
- $etypes = db_select('tripal_bundle', 'tb')
- ->fields('tb', array('name', 'label'))
- ->execute()
- ->fetchAllKeyed();
- $etypes = array('0' => 'any') + $etypes;
- $statuses = array(
- '0' => 'any',
- 'status-1' => 'published',
- 'status-0' => 'not published'
- );
-
- $items = array();
- if ($etype) {
- $items[] = 'where <strong>type</strong> is <strong>' . $etypes[$etype] . '</strong>';
- }
- if ($status) {
- $items[] = 'where <strong>status</strong> is <strong>' . $statuses[$status] . '</strong>';
- }
-
- if (count($items) > 1) {
- for ($i = 1; $i < count($items); $i++) {
- $items[$i] = 'and ' . $items[$i];
- }
- }
- $list = theme_item_list(array(
- 'items' => $items,
- 'title' => '',
- 'type' => 'ul',
- 'attributes' => array(),
- ));
-
- drupal_set_title('Tripal Content');
- $form['filter'] = array(
- '#type' => 'fieldset',
- '#title' => 'Show only items where',
- '#collapsible' => FALSE,
- '#collapsed' => FALSE,
- '#prefix' => '<div class="exposed-filters">',
- '#suffix' => '</div>',
- );
- if (count($items) > 0) {
- $form['filter']['filters'] = array(
- '#type' => 'item',
- '#markup' => $list,
- );
- }
- if (!$status) {
- $form['filter']['status'] = array(
- '#type' => 'select',
- '#title' => 'Status',
- '#options' => $statuses,
- );
- }
- else {
- $form['filter']['status'] = array(
- '#type' => 'value',
- '#value' => $status,
- );
- }
- if (!$etype) {
- $form['filter']['type'] = array(
- '#type' => 'select',
- '#title' => 'Content Type',
- '#options' => $etypes,
- );
- }
- else {
- $form['filter']['type'] = array(
- '#type' => 'value',
- '#value' => $etype,
- );
- }
- $form['filter']['buttons'] = array(
- '#type' => 'value',
- '#prefix' => '<div id="edit-actions" class="container-inline form-actions form-wrapper">',
- '#suffix' => '</div>',
- );
- if (count($items) > 0) {
- if (count($items) < 2) {
- $form['filter']['buttons']['refinebtn'] = array(
- '#type' => 'submit',
- '#value' => 'Refine',
- '#name' => 'refine',
- );
- }
- $form['filter']['buttons']['filterbtn'] = array(
- '#type' => 'submit',
- '#value' => 'Reset',
- '#name' => 'reset',
- );
- }
- else {
- $form['filter']['buttons']['filterbtn'] = array(
- '#type' => 'submit',
- '#value' => 'Filter',
- '#name' => 'filter',
- );
- }
- $query = new EntityFieldQuery();
- $query->entityCondition('entity_type', 'TripalEntity');
- if ($etype) {
- $query->entityCondition('bundle', $etype);
- }
- if ($status) {
- if ($status == 'status-1') {
- $query->propertyCondition('status', 1);
- }
- if ($status == 'status-0') {
- $query->propertyCondition('status', 0);
- }
- }
- $query->propertyOrderBy('label', 'ASC');
-
-
- $cquery = clone $query;
- $cquery->count();
- $num_records = $cquery->execute();
-
- $query->pager(25);
- $query->tableSort($headers);
- $results = $query->execute();
- $pager = theme('pager');
-
- if (!isset($results['TripalEntity'])) {
- $results['TripalEntity'] = array();
- }
- foreach ($results['TripalEntity'] as $entity_id => $stub) {
- $vocabulary = '';
- $term_name = '';
-
-
-
- $equery = db_select('tripal_entity', 'TE');
- $equery->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
- $equery->fields('TB', array('label'));
- $equery->fields('TE');
- $equery->condition('TE.id', $entity_id);
- $entity = $equery->execute()->fetchObject();
- if (!$entity) {
- continue;
- }
-
- $term = entity_load('TripalTerm', array('id' => $entity->term_id));
- $term = reset($term);
- if ($term) {
- $term_name = $term->name;
- $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
- $vocab = reset($vocab);
- $vocabulary = $vocab->vocabulary;
- }
-
- $author = user_load($entity->uid);
-
- $links = '';
- if (entity_access('edit', 'TripalEntity', $entity, $user)) {
- $links .= ' ' . l('edit', 'bio_data/' . $entity->id . '/edit');
- }
- if (entity_access('delete', 'TripalEntity', $entity, $user)) {
- $links .= ' ' . l('delete', 'bio_data/' . $entity->id . '/delete');
- }
-
- $rows[] = array(
- l($entity->title, 'bio_data/' . $entity->id),
- $entity->label,
- $vocabulary . ':' . $term_name,
- l($author->name, 'user/' . $entity->uid),
- $entity->status == 1 ? 'published' : 'unpublished',
- format_date($entity->changed, 'short'),
- $links,
- );
- }
-
-
- if (empty($rows)) {
- $rows[] = array(
- array(
- 'data' => t('No content can be found.'),
- 'colspan' => 7
- )
- );
- }
-
- $table_vars = array(
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => array(),
- 'sticky' => TRUE,
- 'caption' => '',
- 'colgroups' => array(),
- 'empty' => '',
- );
- $output = "<div><strong>Found $num_records records</strong></div>" . $pager . theme('table', $table_vars) . $pager;
- $form['results'] = array(
- '#type' => 'markup',
- '#markup' => $output,
- );
- return $form;
- }
-
- function tripal_content_overview_form_validate($form, &$form_state) {
- }
-
- function tripal_content_overview_form_submit($form, &$form_state) {
-
-
- $form_state['rebuild'] = TRUE;
-
- if ($form_state['clicked_button']['#name'] == 'filter' or
- $form_state['clicked_button']['#name'] == 'refine') {
- $_SESSION['tripal_content_overview']['type'] = array_key_exists('type', $form_state['values']) ? $form_state['values']['type'] : '';
- $_SESSION['tripal_content_overview']['status'] = array_key_exists('status', $form_state['values']) ? $form_state['values']['status'] : '';
- }
- if ($form_state['clicked_button']['#name'] == 'reset') {
- unset($_SESSION['tripal_content_overview']);
- }
- }
-
- function tripal_entity_form($form, &$form_state, $term_id = '', $entity = NULL) {
- global $user;
- $bundle_name = 'bio_data_' . $term_id;
-
- $form['entity_form_vtabs'] = array(
- '#type' => 'vertical_tabs',
- '#weight' => 999,
- );
-
- if (!$entity) {
- $entity = entity_get_controller('TripalEntity')->create(array(
- 'bundle' => $bundle_name,
- 'term_id' => $term_id
- ));
- field_attach_form('TripalEntity', $entity, $form, $form_state);
- $form['add_button'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- '#name' => 'add_data',
- '#weight' => 1000
- );
- }
- else {
- field_attach_form('TripalEntity', $entity, $form, $form_state);
- $form['update_button'] = array(
- '#type' => 'submit',
- '#value' => t('Update'),
- '#name' => 'update_data',
- '#weight' => 1000
- );
-
-
- if (entity_access('delete', 'TripalEntity', $entity, $user)) {
- $form['delete_button'] = array(
- '#type' => 'submit',
- '#value' => t('Delete'),
- '#name' => 'delete_data',
- '#weight' => 1002,
- '#attributes' => array('style' => 'float: right')
- );
- }
- }
- $form['additional_settings'] = array(
- '#type' => 'vertical_tabs',
- '#weight' => 99,
- );
-
- $form['author'] = array(
- '#type' => 'fieldset',
- '#access' => user_access('administer nodes'),
- '#title' => t('Authoring information'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'additional_settings',
- '#attributes' => array(
- 'class' => array('TripalEntity-form-author'),
- ),
- '#attached' => array(
- 'js' => array(
- drupal_get_path('module', 'node') . '/node.js',
- array(
- 'type' => 'setting',
- 'data' => array('anonymous' => variable_get('anonymous', t('Anonymous'))),
- ),
- ),
- ),
- '#weight' => 90,
- );
- $account = user_load($entity->uid);
- $form['author']['author_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Authored by'),
- '#maxlength' => 60,
- '#autocomplete_path' => 'user/autocomplete',
- '#default_value' => !empty($account->name) ? $account->name : '',
- '#weight' => -1,
- '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
- );
- $form['author']['author_date'] = array(
- '#type' => 'textfield',
- '#title' => t('Authored on'),
- '#maxlength' => 25,
- '#description' => t('Format: %time. The date format is YYYY-MM-DD and ' .
- '%timezone is the time zone offset from UTC. Leave blank to use the ' .
- 'time of form submission.',
- array(
- '%time' => !empty($entity->created) ? date('Y-m-d H:i:s O', $entity->created) : format_date($entity->created, 'custom', 'Y-m-d H:i:s O'),
- '%timezone' => !empty($entity->created) ? date('O', $entity->created) : format_date($entity->created, 'custom', 'O'))
- ),
- '#default_value' => !empty($entity->created) ? date('Y-m-d H:i:s O', $entity->created) : '',
- );
-
- $form['options'] = array(
- '#type' => 'fieldset',
- '#access' => user_access('administer nodes'),
- '#title' => t('Publishing options'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'additional_settings',
- '#attributes' => array(
- 'class' => array('node-form-options'),
- ),
- '#attached' => array(
- 'js' => array(drupal_get_path('module', 'node') . '/node.js'),
- ),
- '#weight' => 95,
- );
- $form['options']['status'] = array(
- '#type' => 'checkbox',
- '#title' => t('Published'),
- '#default_value' => $entity->status,
- );
- $form['cancel_button'] = array(
- '#type' => 'submit',
- '#value' => t('Cancel'),
- '#name' => 'cancel_data',
- '#weight' => 1001,
- '#limit_validation_errors' => array(array('')),
- );
-
-
- $form_state['TripalEntity'] = $entity;
- $form['#prefix'] = "<div id='$bundle_name-entity-form'>";
- $form['#suffix'] = "</div>";
- $form['#submit'][] = 'tripal_entity_form_submit';
- return $form;
- }
-
- function tripal_entity_form_ajax_callback($form, $form_state) {
-
- return $form;
- }
-
- function tripal_entity_form_validate($form, &$form_state) {
-
- if (array_key_exists('clicked_button', $form_state) and
- ($form_state['clicked_button']['#name'] == 'cancel_data' or
- $form_state['clicked_button']['#name'] == 'delete_data')) {
- return;
- }
-
- $entity = $form_state['TripalEntity'];
- field_attach_form_validate('TripalEntity', $entity, $form, $form_state);
- $username = $form_state['values']['author_name'];
- $user = user_load_by_name($username);
- if (!$user) {
- form_set_error('author_name', 'Please provide a valid author name.');
- }
- try {
- $create_date = new DateTime($form_state['values']['author_date']);
- }
- catch (Exception $e) {
- form_set_error('author_date', 'Please provide a valid authored on date.');
- }
- }
-
- function tripal_entity_form_submit($form, &$form_state) {
- $entity = $form_state['TripalEntity'];
- if ($form_state['clicked_button']['#name'] =='cancel_data') {
- if (property_exists($entity, 'id')) {
- $form_state['redirect'] = "bio_data/" . $entity->id;
- }
- else {
- $form_state['redirect'] = 'bio_data/add';
- }
- return;
- }
- if ($form_state['clicked_button']['#name'] =='delete_data') {
- $form_state['redirect'] = 'bio_data/' . $entity->id .'/delete';
- return;
- }
- $username = $form_state['values']['author_name'];
- $user = user_load_by_name($username);
- $entity->uid = $user->uid;
- $create_date = $form_state['values']['author_date'];
- $entity->created = $create_date;
- $published = $form_state['values']['status'];
- $entity->status = $published;
-
- $instances = field_info_instances('TripalEntity', $entity->bundle);
- $langcode = 'und';
- foreach ($instances as $field_name => $instance) {
- $entity_type = $instance['entity_type'];
- if($entity_type == 'TripalEntity' and array_key_exists($field_name, $form)) {
- foreach ($form[$field_name][$langcode] as $delta => $field_form) {
- if (!preg_match('/^\d+$/', $delta)) {
- continue;
- }
- $widget_type = $instance['widget']['type'];
- if (tripal_load_include_field_class($widget_type)) {
- $field = $field_form['#field'];
- $widget = new $widget_type($field, $instance);
- $widget->submit($form, $form_state, $entity_type, $entity, $langcode, $delta);
- }
- }
- }
- }
- $entityform = entity_ui_controller('TripalEntity')->entityFormSubmitBuildEntity($form, $form_state);
- if ($entityform->save()) {
- $form_state['redirect'] = "bio_data/" . $entity->id;
- }
- else {
- drupal_set_message('Cannot save entity', 'error');
- }
- }
-
- function tripal_add_page() {
- $item = menu_get_item();
- $content = system_admin_menu_block($item);
-
- if (count($content) == 1) {
- $item = array_shift($content);
- drupal_goto($item['href']);
- }
- return theme('tripal_add_list', array('content' => $content));
- }
-
- function theme_tripal_add_list($variables) {
- $content = $variables['content'];
- $output = '';
- if ($content) {
- $output = '<dl class="node-type-list">';
- foreach ($content as $item) {
- $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
- $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
- }
- $output .= '</dl>';
- }
- else {
- $output = tripal_set_message(
- t('This page is for adding Tripal content to your site. However, before you can add data you have to specify what types of data your site will support. For example, if you want to add genes to be displayed to users, you must first create a data type "gene".'),
- TRIPAL_INFO,
- array('return_html' => TRUE)
- );
- $output .= '<p>' . t('You have not created any biological data types yet. ' .
- 'Go to the <a href="@create-content">data type creation page</a> to ' .
- 'add a new data type.',
- array('@create-content' => url('admin/structure/bio_data/add'))) . '</p>';
- }
- return $output;
- }
-
- function tripal_entity_delete_form($form, &$form_state, $entity) {
- $form_state['entity'] = $entity;
- $form['#submit'][] = 'tripal_entity_delete_form_submit';
- $form = confirm_form($form,
- t('Click the delete button below to confirm deletion of the record titled: %title',
- array('%title' => $entity->title)), 'admin/content/tripal_entity',
- '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
- return $form;
- }
-
- function tripal_entity_delete_form_submit($form, &$form_state) {
- global $user;
- $entity = $form_state['entity'];
- if (!entity_access('delete', 'TripalEntity', $entity, $user)) {
- drupal_set_message(t('You do not have permission to delete this content.'), "error");
- $form_state['redirect'] = 'admin/content/bio_data';
- return;
- }
- $entity_controller = new TripalEntityController($entity->type);
- if ($entity_controller->delete(array($entity->id))) {
- drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
- $form_state['redirect'] = 'admin/content/bio_data';
- }
- else {
- drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
- }
- }
-
- function _tripal_entity_add_access() {
- global $user;
- $types = tripal_get_content_types();
- foreach ($types as $type) {
- if (user_access('create ' . $type->name, $user)) {
- return TRUE;
- }
- }
- return FALSE;
- }
|