TripalEntityUIController.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <?php
  2. /**
  3. * UI controller.
  4. */
  5. class TripalEntityUIController extends EntityDefaultUIController {
  6. /**
  7. * Overrides hook_menu() defaults. Main reason for doing this is that
  8. * parent class hook_menu() is optimized for entity type administration.
  9. */
  10. public function hook_menu() {
  11. $items = array();
  12. // Set this on the object so classes that extend hook_menu() can use it.
  13. $this->id_count = count(explode('/', $this->path));
  14. $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
  15. $id_count = count(explode('/', $this->path));
  16. // The content menu.
  17. $items[$this->path] = array(
  18. 'title' => 'Tripal Content',
  19. 'page callback' => 'tripal_content_view',
  20. 'file' => 'includes/tripal.admin.inc',
  21. 'file path' => drupal_get_path('module', 'tripal'),
  22. 'access arguments' => array('access tripal content overview'),
  23. 'type' => MENU_LOCAL_TASK,
  24. 'weight' => -9
  25. );
  26. $items['bio_data/add'] = array(
  27. 'title' => 'Add Tripal Content',
  28. 'page callback' => 'tripal_add_page',
  29. 'access callback' => '_tripal_entity_add_access',
  30. );
  31. // Add a menu item for creating each bundle
  32. $bundles = array_keys($this->entityInfo['bundles']);
  33. foreach ($bundles as $bundle_name) {
  34. $matches = array();
  35. if (preg_match('/^bio_data_(.*?)$/', $bundle_name, $matches)) {
  36. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  37. if (!$bundle) {
  38. throw new Exception(t("Cannot find bundle that matches: %bundle_name",
  39. array('%bundle_name' => $bundle_name)));
  40. }
  41. $term_id = $matches[1];
  42. // Get the term for this bundle
  43. $term = entity_load('TripalTerm', array('id' => $term_id));
  44. $term = reset($term);
  45. $default_description = $term->definition ? $term->definition : '';
  46. // Set a custom page for adding new tripal data entities.
  47. $items['bio_data/add/' . $term->id] = array(
  48. 'title' => ucfirst($bundle->label),
  49. 'description' => tripal_get_bundle_variable('description', $bundle->id, $default_description),
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('tripal_entity_form', 2),
  52. 'access arguments' => array('create bio_data_' . $term->id),
  53. );
  54. }
  55. }
  56. // Link for viewing a tripal data type.
  57. $items['bio_data/' . $wildcard] = array(
  58. 'title callback' => 'tripal_entity_title',
  59. 'title arguments' => array(1),
  60. 'page callback' => 'tripal_view_entity',
  61. 'page arguments' => array(1),
  62. 'access callback' => 'tripal_entity_access',
  63. 'access arguments' => array('view', 1),
  64. 'type' => MENU_CALLBACK,
  65. );
  66. // 'View' tab for an individual entity page.
  67. $items['bio_data/' . $wildcard . '/view'] = array(
  68. 'title' => 'View',
  69. 'page callback' => 'tripal_view_entity',
  70. 'page arguments' => array(1),
  71. 'access callback' => 'tripal_entity_access',
  72. 'access arguments' => array('view', 1),
  73. 'type' => MENU_DEFAULT_LOCAL_TASK,
  74. 'weight' => -10,
  75. );
  76. // 'Edit' tab for an individual entity page.
  77. $items['bio_data/' . $wildcard . '/edit'] = array(
  78. 'title' => 'Edit',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('tripal_entity_form', NULL, 1),
  81. 'access callback' => 'tripal_entity_access',
  82. 'access arguments' => array('edit', 1),
  83. 'type' => MENU_LOCAL_TASK,
  84. 'weight' => -8,
  85. );
  86. // Menu item for deleting tripal data entities.
  87. $items['bio_data/' . $wildcard . '/delete'] = array(
  88. 'title' => 'Delete',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('tripal_entity_delete_form', 1),
  91. 'access callback' => 'tripal_entity_access',
  92. 'access arguments' => array('delete', 1),
  93. 'type' => MENU_CALLBACK,
  94. 'weight' => 10,
  95. );
  96. return $items;
  97. }
  98. }
  99. /**
  100. * Menu callback to display an entity.
  101. *
  102. * As we load the entity for display, we're responsible for invoking a number
  103. * of hooks in their proper order.
  104. *
  105. * @see hook_entity_prepare_view()
  106. * @see hook_entity_view()
  107. * @see hook_entity_view_alter()
  108. */
  109. function tripal_view_entity($entity, $view_mode = 'full') {
  110. $content = '';
  111. $controller = entity_get_controller($entity->type);
  112. $content = $controller->view(array($entity->id => $entity));
  113. drupal_set_title($entity->title);
  114. if ($entity->status == 0) {
  115. drupal_set_message('This page is currently unpublished', 'warning');
  116. }
  117. return $content;
  118. }
  119. /**
  120. * Provide a data listing for tripal entites (ie: biological data).
  121. *
  122. * This function is a callback in a menu item which is set in the
  123. * TripalEntityUIController class.
  124. */
  125. function tripal_content_view() {
  126. // Retrieve our data listing form and render it.
  127. $form = drupal_get_form('tripal_content_overview_form');
  128. $output = drupal_render($form);
  129. // Set the breadcrumb.
  130. $breadcrumb = array();
  131. $breadcrumb[] = l('Home', '<front>');
  132. $breadcrumb[] = l('Administration', 'admin');
  133. drupal_set_breadcrumb($breadcrumb);
  134. return $output;
  135. }
  136. /**
  137. * Display a listing of Tripal entities.
  138. *
  139. * @TODO Filters and bulk operations needed to be added to this form.
  140. *
  141. * @param array $form
  142. * @param array $form_state
  143. * @return
  144. * A form array describing this listing to the Form API.
  145. */
  146. function tripal_content_overview_form($form, &$form_state) {
  147. global $user;
  148. // Set form defaults. The $_SESSION contains the last known selection
  149. // by this user. That should be overridden if the $_GET variable contains
  150. // values.
  151. $etype = '';
  152. $status = '';
  153. if (array_key_exists('tripal_content_overview', $_SESSION)) {
  154. $etype = $_SESSION['tripal_content_overview']['type'];
  155. $status = $_SESSION['tripal_content_overview']['status'];
  156. }
  157. $etype = array_key_exists('type', $_GET) ? $_GET['type'] : $etype;
  158. $status = array_key_exists('status', $_GET) ? $_GET['status'] : $status;
  159. $headers = array(
  160. 'title' => array(
  161. 'data' => t('Title'),
  162. 'type' => 'property',
  163. 'specifier' => 'title'
  164. ),
  165. 'Type',
  166. 'Term',
  167. 'Author',
  168. 'status' => array(
  169. 'data' => t('Status'),
  170. 'type' => 'property',
  171. 'specifier' => 'status'
  172. ),
  173. 'changed' => array(
  174. 'data' => t('Updated'),
  175. 'type' => 'property',
  176. 'specifier' => 'changed',
  177. 'sort' => 'desc',
  178. ),
  179. 'Operations',
  180. );
  181. $rows = array();
  182. // Get the Options arrays used by the fields below. We need to build them
  183. // here because we will use them both for creating the item list below
  184. // and for the fields.
  185. $etypes = db_select('tripal_bundle', 'tb')
  186. ->fields('tb', array('name', 'label'))
  187. ->execute()
  188. ->fetchAllKeyed();
  189. $etypes = array('0' => 'any') + $etypes;
  190. $statuses = array(
  191. '0' => 'any',
  192. 'status-1' => 'published',
  193. 'status-0' => 'not published'
  194. );
  195. // Build the list of existing filters.
  196. $items = array();
  197. if ($etype) {
  198. $items[] = 'where <strong>type</strong> is <strong>' . $etypes[$etype] . '</strong>';
  199. }
  200. if ($status) {
  201. $items[] = 'where <strong>status</strong> is <strong>' . $statuses[$status] . '</strong>';
  202. }
  203. // Add 'and' to the beginning of every filter after the first
  204. if (count($items) > 1) {
  205. for ($i = 1; $i < count($items); $i++) {
  206. $items[$i] = 'and ' . $items[$i];
  207. }
  208. }
  209. $list = theme_item_list(array(
  210. 'items' => $items,
  211. 'title' => '',
  212. 'type' => 'ul',
  213. 'attributes' => array(),
  214. ));
  215. // Set the title to be informative (defaults to content for some reason).
  216. drupal_set_title('Tripal Content');
  217. $form['filter'] = array(
  218. '#type' => 'fieldset',
  219. '#title' => 'Show only items where',
  220. '#collapsible' => FALSE,
  221. '#collapsed' => FALSE,
  222. '#prefix' => '<div class="exposed-filters">',
  223. '#suffix' => '</div>',
  224. );
  225. if (count($items) > 0) {
  226. $form['filter']['filters'] = array(
  227. '#type' => 'item',
  228. '#markup' => $list,
  229. );
  230. }
  231. if (!$status) {
  232. $form['filter']['status'] = array(
  233. '#type' => 'select',
  234. '#title' => 'Status',
  235. '#options' => $statuses,
  236. );
  237. }
  238. else {
  239. $form['filter']['status'] = array(
  240. '#type' => 'value',
  241. '#value' => $status,
  242. );
  243. }
  244. if (!$etype) {
  245. $form['filter']['type'] = array(
  246. '#type' => 'select',
  247. '#title' => 'Content Type',
  248. '#options' => $etypes,
  249. );
  250. }
  251. else {
  252. $form['filter']['type'] = array(
  253. '#type' => 'value',
  254. '#value' => $etype,
  255. );
  256. }
  257. $form['filter']['buttons'] = array(
  258. '#type' => 'value',
  259. '#prefix' => '<div id="edit-actions" class="container-inline form-actions form-wrapper">',
  260. '#suffix' => '</div>',
  261. );
  262. if (count($items) > 0) {
  263. if (count($items) < 2) {
  264. $form['filter']['buttons']['refinebtn'] = array(
  265. '#type' => 'submit',
  266. '#value' => 'Refine',
  267. '#name' => 'refine',
  268. );
  269. }
  270. $form['filter']['buttons']['filterbtn'] = array(
  271. '#type' => 'submit',
  272. '#value' => 'Reset',
  273. '#name' => 'reset',
  274. );
  275. }
  276. else {
  277. $form['filter']['buttons']['filterbtn'] = array(
  278. '#type' => 'submit',
  279. '#value' => 'Filter',
  280. '#name' => 'filter',
  281. );
  282. }
  283. $query = new EntityFieldQuery();
  284. $query->entityCondition('entity_type', 'TripalEntity');
  285. if ($etype) {
  286. $query->entityCondition('bundle', $etype);
  287. }
  288. //$query->propertyOrderBy('created', 'DESC');
  289. // Find out the total number of records and determine what page we're on, and
  290. // initialize the pager.
  291. $cquery = clone $query;
  292. $cquery->count();
  293. $num_records = $cquery->execute();
  294. // Calculate the range and create a pager.
  295. $query->pager(25);
  296. $query->tableSort($headers);
  297. $results = $query->execute();
  298. $pager = theme('pager');
  299. // For each entity retrieved add a row to the data listing.
  300. //while ($entity = $entities->fetchObject()) {
  301. if (!isset($results['TripalEntity'])) $results['TripalEntity'] = array();
  302. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  303. $vocabulary = '';
  304. $term_name = '';
  305. // We don't need all of the attached fields for an entity so, we'll
  306. // not use the entity_load() function. Instead just pull it from the
  307. // database table.
  308. $equery = db_select('tripal_entity', 'TE');
  309. $equery->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
  310. $equery->fields('TB', array('label'));
  311. $equery->fields('TE');
  312. $equery->condition('TE.id', $entity_id);
  313. $entity = $equery->execute()->fetchObject();
  314. if (!$entity) {
  315. continue;
  316. }
  317. // Get the term
  318. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  319. $term = reset($term);
  320. if ($term) {
  321. $term_name = $term->name;
  322. $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
  323. $vocab = reset($vocab);
  324. $vocabulary = $vocab->vocabulary;
  325. }
  326. // Retrieve details about the user who created this data.
  327. $author = user_load($entity->uid);
  328. // Build the action links
  329. $links = '';
  330. if (entity_access('edit', 'TripalEntity', $entity, $user)) {
  331. $links .= '&nbsp;&nbsp;' . l('edit', 'bio_data/' . $entity->id . '/edit');
  332. }
  333. if (entity_access('delete', 'TripalEntity', $entity, $user)) {
  334. $links .= '&nbsp;&nbsp;' . l('delete', 'bio_data/' . $entity->id . '/delete');
  335. }
  336. // Add information to the table.
  337. $rows[] = array(
  338. l($entity->title, 'bio_data/' . $entity->id),
  339. $entity->label,
  340. $vocabulary . ':' . $term_name,
  341. l($author->name, 'user/' . $entity->uid),
  342. $entity->status == 1 ? 'published' : 'unpublished',
  343. format_date($entity->changed, 'short'),
  344. $links,
  345. );
  346. }
  347. // If there are no entites created yet then add a message to the table to
  348. // provide guidance to administrators.
  349. if (empty($rows)) {
  350. $rows[] = array(
  351. array(
  352. 'data' => t('No content can be found.'),
  353. 'colspan' => 7
  354. )
  355. );
  356. }
  357. // Render the data listing.
  358. $table_vars = array(
  359. 'header' => $headers,
  360. 'rows' => $rows,
  361. 'attributes' => array(),
  362. 'sticky' => TRUE,
  363. 'caption' => '',
  364. 'colgroups' => array(),
  365. 'empty' => '',
  366. );
  367. $output = "<div><strong>Found $num_records records</strong></div>" . $pager . theme('table', $table_vars) . $pager;
  368. $form['results'] = array(
  369. '#type' => 'markup',
  370. '#markup' => $output,
  371. );
  372. return $form;
  373. }
  374. /**
  375. *
  376. */
  377. function tripal_content_overview_form_validate($form, &$form_state) {
  378. }
  379. /**
  380. *
  381. */
  382. function tripal_content_overview_form_submit($form, &$form_state) {
  383. // Always just rebuild the form on submit. that will update the
  384. // result table using the filters provided.
  385. $form_state['rebuild'] = TRUE;
  386. // Save the current user filter settings.
  387. if ($form_state['clicked_button']['#name'] == 'filter' or
  388. $form_state['clicked_button']['#name'] == 'refine') {
  389. $_SESSION['tripal_content_overview']['type'] = array_key_exists('type', $form_state['values']) ? $form_state['values']['type'] : '';
  390. $_SESSION['tripal_content_overview']['status'] = array_key_exists('status', $form_state['values']) ? $form_state['values']['status'] : '';
  391. }
  392. if ($form_state['clicked_button']['#name'] == 'reset') {
  393. unset($_SESSION['tripal_content_overview']);
  394. }
  395. }
  396. /**
  397. *
  398. */
  399. function tripal_entity_form($form, &$form_state, $term_id = '', $entity = NULL) {
  400. global $user;
  401. $bundle_name = 'bio_data_' . $term_id;
  402. // Add a vertical tabs element
  403. $form['entity_form_vtabs'] = array(
  404. '#type' => 'vertical_tabs',
  405. '#weight' => 999,
  406. );
  407. // If the entity doesn't exist then create one.
  408. if (!$entity) {
  409. $entity = entity_get_controller('TripalEntity')->create(array(
  410. 'bundle' => $bundle_name,
  411. 'term_id' => $term_id
  412. ));
  413. field_attach_form('TripalEntity', $entity, $form, $form_state);
  414. $form['add_button'] = array(
  415. '#type' => 'submit',
  416. '#value' => t('Save'),
  417. '#name' => 'add_data',
  418. '#weight' => 1000
  419. );
  420. }
  421. else {
  422. field_attach_form('TripalEntity', $entity, $form, $form_state);
  423. $form['update_button'] = array(
  424. '#type' => 'submit',
  425. '#value' => t('Update'),
  426. '#name' => 'update_data',
  427. '#weight' => 1000
  428. );
  429. // Put the delete button on the far-right so that it's harder
  430. // to accidentally click it.
  431. if (entity_access('delete', 'TripalEntity', $entity, $user)) {
  432. $form['delete_button'] = array(
  433. '#type' => 'submit',
  434. '#value' => t('Delete'),
  435. '#name' => 'delete_data',
  436. '#weight' => 1002,
  437. '#attributes' => array('style' => 'float: right')
  438. );
  439. }
  440. }
  441. $form['additional_settings'] = array(
  442. '#type' => 'vertical_tabs',
  443. '#weight' => 99,
  444. );
  445. // Node author information for administrators
  446. $form['author'] = array(
  447. '#type' => 'fieldset',
  448. '#access' => user_access('administer nodes'),
  449. '#title' => t('Authoring information'),
  450. '#collapsible' => TRUE,
  451. '#collapsed' => TRUE,
  452. '#group' => 'additional_settings',
  453. '#attributes' => array(
  454. 'class' => array('TripalEntity-form-author'),
  455. ),
  456. '#attached' => array(
  457. 'js' => array(
  458. drupal_get_path('module', 'node') . '/node.js',
  459. array(
  460. 'type' => 'setting',
  461. 'data' => array('anonymous' => variable_get('anonymous', t('Anonymous'))),
  462. ),
  463. ),
  464. ),
  465. '#weight' => 90,
  466. );
  467. $account = user_load($entity->uid);
  468. $form['author']['author_name'] = array(
  469. '#type' => 'textfield',
  470. '#title' => t('Authored by'),
  471. '#maxlength' => 60,
  472. '#autocomplete_path' => 'user/autocomplete',
  473. '#default_value' => !empty($account->name) ? $account->name : '',
  474. '#weight' => -1,
  475. '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  476. );
  477. $form['author']['author_date'] = array(
  478. '#type' => 'textfield',
  479. '#title' => t('Authored on'),
  480. '#maxlength' => 25,
  481. '#description' => t('Format: %time. The date format is YYYY-MM-DD and ' .
  482. '%timezone is the time zone offset from UTC. Leave blank to use the ' .
  483. 'time of form submission.',
  484. array(
  485. '%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'),
  486. '%timezone' => !empty($entity->created) ? date('O', $entity->created) : format_date($entity->created, 'custom', 'O'))
  487. ),
  488. '#default_value' => !empty($entity->created) ? date('Y-m-d H:i:s O', $entity->created) : '',
  489. );
  490. // Node options for administrators
  491. $form['options'] = array(
  492. '#type' => 'fieldset',
  493. '#access' => user_access('administer nodes'),
  494. '#title' => t('Publishing options'),
  495. '#collapsible' => TRUE,
  496. '#collapsed' => TRUE,
  497. '#group' => 'additional_settings',
  498. '#attributes' => array(
  499. 'class' => array('node-form-options'),
  500. ),
  501. '#attached' => array(
  502. 'js' => array(drupal_get_path('module', 'node') . '/node.js'),
  503. ),
  504. '#weight' => 95,
  505. );
  506. $form['options']['status'] = array(
  507. '#type' => 'checkbox',
  508. '#title' => t('Published'),
  509. '#default_value' => $entity->status,
  510. );
  511. $form['cancel_button'] = array(
  512. '#type' => 'submit',
  513. '#value' => t('Cancel'),
  514. '#name' => 'cancel_data',
  515. '#weight' => 1001,
  516. '#limit_validation_errors' => array(array('')),
  517. );
  518. // The entity object must be added to the $form_state in order for
  519. // the Entity API to work. It must have a key of the entity name.
  520. $form_state['TripalEntity'] = $entity;
  521. $form['#prefix'] = "<div id='$bundle_name-entity-form'>";
  522. $form['#suffix'] = "</div>";
  523. $form['#submit'][] = 'tripal_entity_form_submit';
  524. return $form;
  525. }
  526. /**
  527. * An Ajax callback for the tripal_entity_form.
  528. */
  529. function tripal_entity_form_ajax_callback($form, $form_state) {
  530. // return the form so Drupal can update the content on the page
  531. return $form;
  532. }
  533. /**
  534. * Implements hook_validate() for the tripal_entity_form.
  535. */
  536. function tripal_entity_form_validate($form, &$form_state) {
  537. // If the user is cancelling or deleting the entity then don't validate.
  538. if (array_key_exists('clicked_button', $form_state) and
  539. ($form_state['clicked_button']['#name'] == 'cancel_data' or
  540. $form_state['clicked_button']['#name'] == 'delete_data')) {
  541. return;
  542. }
  543. // For adds and updates, perform validation.
  544. $entity = $form_state['TripalEntity'];
  545. field_attach_form_validate('TripalEntity', $entity, $form, $form_state);
  546. $username = $form_state['values']['author_name'];
  547. $user = user_load_by_name($username);
  548. if (!$user) {
  549. form_set_error('author_name', 'Please provide a valid author name.');
  550. }
  551. try {
  552. $create_date = new DateTime($form_state['values']['author_date']);
  553. }
  554. catch (Exception $e) {
  555. form_set_error('author_date', 'Please provide a valid authored on date.');
  556. }
  557. }
  558. /**
  559. * Implements hook_submit() for the tripal_entity_form.
  560. */
  561. function tripal_entity_form_submit($form, &$form_state) {
  562. $entity = $form_state['TripalEntity'];
  563. if ($form_state['clicked_button']['#name'] =='cancel_data') {
  564. if (property_exists($entity, 'id')) {
  565. $form_state['redirect'] = "bio_data/" . $entity->id;
  566. }
  567. else {
  568. $form_state['redirect'] = 'bio_data/add';
  569. }
  570. return;
  571. }
  572. if ($form_state['clicked_button']['#name'] =='delete_data') {
  573. $form_state['redirect'] = 'bio_data/' . $entity->id .'/delete';
  574. return;
  575. }
  576. $username = $form_state['values']['author_name'];
  577. $user = user_load_by_name($username);
  578. $entity->uid = $user->uid;
  579. $create_date = $form_state['values']['author_date'];
  580. $entity->created = $create_date;
  581. $published = $form_state['values']['status'];
  582. $entity->status = $published;
  583. // Allow the fields to perform actions prior to submit.
  584. $instances = field_info_instances('TripalEntity', $entity->bundle);
  585. $langcode = 'und';
  586. foreach ($instances as $field_name => $instance) {
  587. $entity_type = $instance['entity_type'];
  588. if($entity_type == 'TripalEntity') {
  589. foreach ($form[$field_name][$langcode] as $delta => $field_form) {
  590. if (!preg_match('/^\d+$/', $delta)) {
  591. continue;
  592. }
  593. $widget_type = $instance['widget']['type'];
  594. if (tripal_load_include_field_class($widget_type)) {
  595. $field = $field_form['#field'];
  596. $widget = new $widget_type($field, $instance);
  597. $widget->submit($form, $form_state, $entity_type, $entity, $langcode, $delta);
  598. }
  599. }
  600. }
  601. }
  602. $entityform = entity_ui_controller('TripalEntity')->entityFormSubmitBuildEntity($form, $form_state);
  603. if ($entityform->save()) {
  604. $form_state['redirect'] = "bio_data/" . $entity->id;
  605. }
  606. else {
  607. drupal_set_message('Cannot save entity', 'error');
  608. }
  609. }
  610. /**
  611. * Provides a list of TripalEntity types (bundles) for the user to add.
  612. *
  613. * This function is a callback in a menu item which is set in the
  614. * TripalEntityUIController class.
  615. */
  616. function tripal_add_page() {
  617. $item = menu_get_item();
  618. $content = system_admin_menu_block($item);
  619. // Bypass the node/add listing if only one content type is available.
  620. if (count($content) == 1) {
  621. $item = array_shift($content);
  622. drupal_goto($item['href']);
  623. }
  624. return theme('tripal_add_list', array('content' => $content));
  625. }
  626. /**
  627. * Returns HTML for a list of available node types for node creation.
  628. *
  629. * @param $variables
  630. * An associative array containing:
  631. * - content: An array of content types.
  632. *
  633. * @ingroup themeable
  634. */
  635. function theme_tripal_add_list($variables) {
  636. $content = $variables['content'];
  637. $output = '';
  638. if ($content) {
  639. $output = '<dl class="node-type-list">';
  640. foreach ($content as $item) {
  641. $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
  642. $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
  643. }
  644. $output .= '</dl>';
  645. }
  646. else {
  647. $output = tripal_set_message(
  648. 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".'),
  649. TRIPAL_INFO,
  650. array('return_html' => TRUE)
  651. );
  652. $output .= '<p>' . t('You have not created any biological data types yet. ' .
  653. 'Go to the <a href="@create-content">data type creation page</a> to ' .
  654. 'add a new data type.',
  655. array('@create-content' => url('admin/structure/bio_data/add'))) . '</p>';
  656. }
  657. return $output;
  658. }
  659. /**
  660. * Form callback: confirmation form for deleting a tripal_entity.
  661. *
  662. * @param $tripal_entity The
  663. * tripal_entity to delete
  664. *
  665. * @see confirm_form()
  666. */
  667. function tripal_entity_delete_form($form, &$form_state, $entity) {
  668. $form_state['entity'] = $entity;
  669. $form['#submit'][] = 'tripal_entity_delete_form_submit';
  670. $form = confirm_form($form,
  671. t('Click the delete button below to confirm deletion of the record titled: %title',
  672. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  673. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  674. return $form;
  675. }
  676. /**
  677. * Submit callback for tripal_entity_delete_form
  678. */
  679. function tripal_entity_delete_form_submit($form, &$form_state) {
  680. $entity = $form_state['entity'];
  681. if (!entity_access('delete', 'TripalEntity', $entity, $user)) {
  682. drupal_set_message(t('You do not have permission to delete this content.'), "error");
  683. $form_state['redirect'] = 'admin/content/bio_data';
  684. return;
  685. }
  686. $entity_controller = new TripalEntityController($entity->type);
  687. if ($entity_controller->delete(array($entity->id))) {
  688. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  689. $form_state['redirect'] = 'admin/content/bio_data';
  690. }
  691. else {
  692. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  693. }
  694. }
  695. /**
  696. * A helper function for checking if a user can add Tripal Content.
  697. *
  698. * This function is a callback for the bio_data/add menu path.
  699. */
  700. function _tripal_entity_add_access() {
  701. global $user;
  702. $types = tripal_get_content_types();
  703. foreach ($types as $type) {
  704. if (user_access('create ' . $type->name, $user)) {
  705. return TRUE;
  706. }
  707. }
  708. return FALSE;
  709. }