TripalEntityUIController.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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/%'] = 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/%/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. $items['bio_data/' . $wildcard . '/reload'] = array(
  87. 'title' => 'Reload',
  88. 'page callback' => 'tripal_entity_reload',
  89. 'page arguments' => array(1),
  90. 'access arguments' => array('administer tripal'),
  91. 'type' => MENU_LOCAL_TASK,
  92. 'weight' => 10,
  93. );
  94. // Menu item for deleting tripal data entities.
  95. $items['bio_data/' . $wildcard . '/delete'] = array(
  96. 'title' => 'Delete',
  97. 'page callback' => 'drupal_get_form',
  98. 'page arguments' => array('tripal_entity_delete_form', 1),
  99. 'access callback' => 'tripal_entity_access',
  100. 'access arguments' => array('delete', 1),
  101. 'type' => MENU_CALLBACK,
  102. 'weight' => 10,
  103. );
  104. return $items;
  105. }
  106. }
  107. /**
  108. * Menu callback to display an entity.
  109. *
  110. * As we load the entity for display, we're responsible for invoking a number
  111. * of hooks in their proper order.
  112. *
  113. * @see hook_entity_prepare_view()
  114. * @see hook_entity_view()
  115. * @see hook_entity_view_alter()
  116. */
  117. function tripal_view_entity($entity, $view_mode = 'full') {
  118. if(!is_object($entity)) {
  119. $id = intval($entity);
  120. if($id === 0) {
  121. return drupal_not_found();
  122. }
  123. $entities = tripal_load_entity('TripalEntity', [$id]);
  124. if(empty($entities)) {
  125. return drupal_not_found();
  126. }
  127. $entity = reset($entities);
  128. }
  129. $controller = entity_get_controller($entity->type);
  130. $content = $controller->view(array($entity->id => $entity));
  131. drupal_set_title($entity->title);
  132. if ($entity->status == 0) {
  133. drupal_set_message('This page is currently unpublished', 'warning');
  134. }
  135. return $content;
  136. }
  137. /**
  138. * Provide a data listing for tripal entites (ie: biological data).
  139. *
  140. * This function is a callback in a menu item which is set in the
  141. * TripalEntityUIController class.
  142. */
  143. function tripal_content_view() {
  144. // Retrieve our data listing form and render it.
  145. $form = drupal_get_form('tripal_content_overview_form');
  146. $output = drupal_render($form);
  147. // Set the breadcrumb.
  148. $breadcrumb = array();
  149. $breadcrumb[] = l('Home', '<front>');
  150. $breadcrumb[] = l('Administration', 'admin');
  151. drupal_set_breadcrumb($breadcrumb);
  152. return $output;
  153. }
  154. /**
  155. * Display a listing of Tripal entities.
  156. *
  157. * @TODO Filters and bulk operations needed to be added to this form.
  158. *
  159. * @param array $form
  160. * @param array $form_state
  161. * @return
  162. * A form array describing this listing to the Form API.
  163. */
  164. function tripal_content_overview_form($form, &$form_state) {
  165. global $user;
  166. // Set form defaults. The $_SESSION contains the last known selection
  167. // by this user. That should be overridden if the $_GET variable contains
  168. // values.
  169. $etype = '';
  170. $status = '';
  171. if (array_key_exists('tripal_content_overview', $_SESSION)) {
  172. $etype = $_SESSION['tripal_content_overview']['type'];
  173. $status = $_SESSION['tripal_content_overview']['status'];
  174. }
  175. $etype = array_key_exists('type', $_GET) ? $_GET['type'] : $etype;
  176. $status = array_key_exists('status', $_GET) ? $_GET['status'] : $status;
  177. $headers = array(
  178. 'title' => array(
  179. 'data' => t('Title'),
  180. 'type' => 'property',
  181. 'specifier' => 'title'
  182. ),
  183. 'Type',
  184. 'Term',
  185. 'Author',
  186. 'status' => array(
  187. 'data' => t('Status'),
  188. 'type' => 'property',
  189. 'specifier' => 'status'
  190. ),
  191. 'changed' => array(
  192. 'data' => t('Updated'),
  193. 'type' => 'property',
  194. 'specifier' => 'changed',
  195. 'sort' => 'desc',
  196. ),
  197. 'Operations',
  198. );
  199. $rows = array();
  200. // Get the Options arrays used by the fields below. We need to build them
  201. // here because we will use them both for creating the item list below
  202. // and for the fields.
  203. $etypes = db_select('tripal_bundle', 'tb')
  204. ->fields('tb', array('name', 'label'))
  205. ->execute()
  206. ->fetchAllKeyed();
  207. $etypes = array('0' => 'any') + $etypes;
  208. $statuses = array(
  209. '0' => 'any',
  210. 'status-1' => 'published',
  211. 'status-0' => 'not published'
  212. );
  213. // Build the list of existing filters.
  214. $items = array();
  215. if ($etype) {
  216. $items[] = 'where <strong>type</strong> is <strong>' . $etypes[$etype] . '</strong>';
  217. }
  218. if ($status) {
  219. $items[] = 'where <strong>status</strong> is <strong>' . $statuses[$status] . '</strong>';
  220. }
  221. // Add 'and' to the beginning of every filter after the first
  222. if (count($items) > 1) {
  223. for ($i = 1; $i < count($items); $i++) {
  224. $items[$i] = 'and ' . $items[$i];
  225. }
  226. }
  227. $list = theme_item_list(array(
  228. 'items' => $items,
  229. 'title' => '',
  230. 'type' => 'ul',
  231. 'attributes' => array(),
  232. ));
  233. // Set the title to be informative (defaults to content for some reason).
  234. drupal_set_title('Tripal Content');
  235. $form['filter'] = array(
  236. '#type' => 'fieldset',
  237. '#title' => 'Show only items where',
  238. '#collapsible' => FALSE,
  239. '#collapsed' => FALSE,
  240. '#prefix' => '<div class="exposed-filters">',
  241. '#suffix' => '</div>',
  242. );
  243. if (count($items) > 0) {
  244. $form['filter']['filters'] = array(
  245. '#type' => 'item',
  246. '#markup' => $list,
  247. );
  248. }
  249. if (!$status) {
  250. $form['filter']['status'] = array(
  251. '#type' => 'select',
  252. '#title' => 'Status',
  253. '#options' => $statuses,
  254. );
  255. }
  256. else {
  257. $form['filter']['status'] = array(
  258. '#type' => 'value',
  259. '#value' => $status,
  260. );
  261. }
  262. if (!$etype) {
  263. $form['filter']['type'] = array(
  264. '#type' => 'select',
  265. '#title' => 'Content Type',
  266. '#options' => $etypes,
  267. );
  268. }
  269. else {
  270. $form['filter']['type'] = array(
  271. '#type' => 'value',
  272. '#value' => $etype,
  273. );
  274. }
  275. $form['filter']['buttons'] = array(
  276. '#type' => 'value',
  277. '#prefix' => '<div id="edit-actions" class="container-inline form-actions form-wrapper">',
  278. '#suffix' => '</div>',
  279. );
  280. if (count($items) > 0) {
  281. if (count($items) < 2) {
  282. $form['filter']['buttons']['refinebtn'] = array(
  283. '#type' => 'submit',
  284. '#value' => 'Refine',
  285. '#name' => 'refine',
  286. );
  287. }
  288. $form['filter']['buttons']['filterbtn'] = array(
  289. '#type' => 'submit',
  290. '#value' => 'Reset',
  291. '#name' => 'reset',
  292. );
  293. }
  294. else {
  295. $form['filter']['buttons']['filterbtn'] = array(
  296. '#type' => 'submit',
  297. '#value' => 'Filter',
  298. '#name' => 'filter',
  299. );
  300. }
  301. $query = new EntityFieldQuery();
  302. $query->entityCondition('entity_type', 'TripalEntity');
  303. if ($etype) {
  304. $query->entityCondition('bundle', $etype);
  305. }
  306. if ($status) {
  307. if ($status == 'status-1') {
  308. $query->propertyCondition('status', 1);
  309. }
  310. if ($status == 'status-0') {
  311. $query->propertyCondition('status', 0);
  312. }
  313. }
  314. // Find out the total number of records and determine what page we're on, and
  315. // initialize the pager.
  316. $cquery = clone $query;
  317. $cquery->count();
  318. $num_records = $cquery->execute();
  319. // Calculate the range and create a pager.
  320. $query->pager(25);
  321. $query->tableSort($headers);
  322. $results = $query->execute();
  323. $pager = theme('pager');
  324. // For each entity retrieved add a row to the data listing.
  325. if (!isset($results['TripalEntity'])) {
  326. $results['TripalEntity'] = array();
  327. }
  328. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  329. $vocabulary = '';
  330. $term_name = '';
  331. // We don't need all of the attached fields for an entity so, we'll
  332. // not use the entity_load() function. Instead just pull it from the
  333. // database table.
  334. $equery = db_select('tripal_entity', 'TE');
  335. $equery->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
  336. $equery->fields('TB', array('label'));
  337. $equery->fields('TE');
  338. $equery->condition('TE.id', $entity_id);
  339. $entity = $equery->execute()->fetchObject();
  340. if (!$entity) {
  341. continue;
  342. }
  343. // Get the term
  344. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  345. $term = reset($term);
  346. if ($term) {
  347. $term_name = $term->name;
  348. $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
  349. $vocab = reset($vocab);
  350. $vocabulary = $vocab->vocabulary;
  351. }
  352. // Retrieve details about the user who created this data.
  353. $author = user_load($entity->uid);
  354. // Build the action links
  355. $links = '';
  356. if (entity_access('edit', 'TripalEntity', $entity, $user)) {
  357. $links .= '&nbsp;&nbsp;' . l('edit', 'bio_data/' . $entity->id . '/edit');
  358. }
  359. if (entity_access('delete', 'TripalEntity', $entity, $user)) {
  360. $links .= '&nbsp;&nbsp;' . l('delete', 'bio_data/' . $entity->id . '/delete');
  361. }
  362. // Add information to the table.
  363. $rows[] = array(
  364. l($entity->title, 'bio_data/' . $entity->id),
  365. $entity->label,
  366. $vocabulary . ':' . $term_name,
  367. l($author->name, 'user/' . $entity->uid),
  368. $entity->status == 1 ? 'published' : 'unpublished',
  369. format_date($entity->changed, 'short'),
  370. $links,
  371. );
  372. }
  373. // If there are no entites created yet then add a message to the table to
  374. // provide guidance to administrators.
  375. if (empty($rows)) {
  376. $rows[] = array(
  377. array(
  378. 'data' => t('No content can be found.'),
  379. 'colspan' => 7
  380. )
  381. );
  382. }
  383. // Render the data listing.
  384. $table_vars = array(
  385. 'header' => $headers,
  386. 'rows' => $rows,
  387. 'attributes' => array(),
  388. 'sticky' => TRUE,
  389. 'caption' => '',
  390. 'colgroups' => array(),
  391. 'empty' => '',
  392. );
  393. $output = "<div><strong>Found $num_records records</strong></div>" . $pager . theme('table', $table_vars) . $pager;
  394. $form['results'] = array(
  395. '#type' => 'markup',
  396. '#markup' => $output,
  397. );
  398. return $form;
  399. }
  400. /**
  401. *
  402. */
  403. function tripal_content_overview_form_validate($form, &$form_state) {
  404. }
  405. /**
  406. *
  407. */
  408. function tripal_content_overview_form_submit($form, &$form_state) {
  409. // Always just rebuild the form on submit. that will update the
  410. // result table using the filters provided.
  411. $form_state['rebuild'] = TRUE;
  412. // Save the current user filter settings.
  413. if ($form_state['clicked_button']['#name'] == 'filter' or
  414. $form_state['clicked_button']['#name'] == 'refine') {
  415. $_SESSION['tripal_content_overview']['type'] = array_key_exists('type', $form_state['values']) ? $form_state['values']['type'] : '';
  416. $_SESSION['tripal_content_overview']['status'] = array_key_exists('status', $form_state['values']) ? $form_state['values']['status'] : '';
  417. }
  418. if ($form_state['clicked_button']['#name'] == 'reset') {
  419. unset($_SESSION['tripal_content_overview']);
  420. }
  421. }
  422. /**
  423. * Clears the cache of a given entity to force a reload.
  424. *
  425. * @param $entity_id
  426. * The entity_id of the entity to reload.
  427. */
  428. function tripal_entity_reload($entity) {
  429. $entity_id = $entity->id;
  430. $cid = 'field:TripalEntity:' . $entity_id . ':';
  431. cache_clear_all($cid, 'cache_field', TRUE);
  432. $sql = "SELECT count(*) FROM cache_field WHERE cid like :cid";
  433. $count = db_query($sql, [':cid' => $cid . '%'])->fetchField();
  434. if (!isset($count) or $count > 0) {
  435. drupal_set_message('Failed to clear the cache for this entity.');
  436. }
  437. else {
  438. drupal_set_message('Cache cleared, entity reloaded');
  439. }
  440. drupal_goto('bio_data/' . $entity_id);
  441. }
  442. /**
  443. *
  444. */
  445. function tripal_entity_form($form, &$form_state, $term_id = '', $entity = NULL) {
  446. global $user;
  447. $bundle_name = 'bio_data_' . $term_id;
  448. // Add a vertical tabs element
  449. $form['entity_form_vtabs'] = array(
  450. '#type' => 'vertical_tabs',
  451. '#weight' => 999,
  452. );
  453. // If the entity doesn't exist then create one.
  454. if (!$entity) {
  455. $entity = entity_get_controller('TripalEntity')->create(array(
  456. 'bundle' => $bundle_name,
  457. 'term_id' => $term_id
  458. ));
  459. field_attach_form('TripalEntity', $entity, $form, $form_state);
  460. $form['add_button'] = array(
  461. '#type' => 'submit',
  462. '#value' => t('Save'),
  463. '#name' => 'add_data',
  464. '#weight' => 1000
  465. );
  466. }
  467. else {
  468. field_attach_form('TripalEntity', $entity, $form, $form_state);
  469. $form['update_button'] = array(
  470. '#type' => 'submit',
  471. '#value' => t('Update'),
  472. '#name' => 'update_data',
  473. '#weight' => 1000
  474. );
  475. // Put the delete button on the far-right so that it's harder
  476. // to accidentally click it.
  477. if (entity_access('delete', 'TripalEntity', $entity, $user)) {
  478. $form['delete_button'] = array(
  479. '#type' => 'submit',
  480. '#value' => t('Delete'),
  481. '#name' => 'delete_data',
  482. '#weight' => 1002,
  483. '#attributes' => array('style' => 'float: right')
  484. );
  485. }
  486. }
  487. $form['additional_settings'] = array(
  488. '#type' => 'vertical_tabs',
  489. '#weight' => 99,
  490. );
  491. // Node author information for administrators
  492. $form['author'] = array(
  493. '#type' => 'fieldset',
  494. '#access' => user_access('administer nodes'),
  495. '#title' => t('Authoring information'),
  496. '#collapsible' => TRUE,
  497. '#collapsed' => TRUE,
  498. '#group' => 'additional_settings',
  499. '#attributes' => array(
  500. 'class' => array('TripalEntity-form-author'),
  501. ),
  502. '#attached' => array(
  503. 'js' => array(
  504. drupal_get_path('module', 'node') . '/node.js',
  505. array(
  506. 'type' => 'setting',
  507. 'data' => array('anonymous' => variable_get('anonymous', t('Anonymous'))),
  508. ),
  509. ),
  510. ),
  511. '#weight' => 90,
  512. );
  513. $account = user_load($entity->uid);
  514. $form['author']['author_name'] = array(
  515. '#type' => 'textfield',
  516. '#title' => t('Authored by'),
  517. '#maxlength' => 60,
  518. '#autocomplete_path' => 'user/autocomplete',
  519. '#default_value' => !empty($account->name) ? $account->name : '',
  520. '#weight' => -1,
  521. '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  522. );
  523. $form['author']['author_date'] = array(
  524. '#type' => 'textfield',
  525. '#title' => t('Authored on'),
  526. '#maxlength' => 25,
  527. '#description' => t('Format: %time. The date format is YYYY-MM-DD and ' .
  528. '%timezone is the time zone offset from UTC. Leave blank to use the ' .
  529. 'time of form submission.',
  530. array(
  531. '%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'),
  532. '%timezone' => !empty($entity->created) ? date('O', $entity->created) : format_date($entity->created, 'custom', 'O'))
  533. ),
  534. '#default_value' => !empty($entity->created) ? date('Y-m-d H:i:s O', $entity->created) : '',
  535. );
  536. // Node options for administrators
  537. $form['options'] = array(
  538. '#type' => 'fieldset',
  539. '#access' => user_access('administer nodes'),
  540. '#title' => t('Publishing options'),
  541. '#collapsible' => TRUE,
  542. '#collapsed' => TRUE,
  543. '#group' => 'additional_settings',
  544. '#attributes' => array(
  545. 'class' => array('node-form-options'),
  546. ),
  547. '#attached' => array(
  548. 'js' => array(drupal_get_path('module', 'node') . '/node.js'),
  549. ),
  550. '#weight' => 95,
  551. );
  552. $form['options']['status'] = array(
  553. '#type' => 'checkbox',
  554. '#title' => t('Published'),
  555. '#default_value' => $entity->status,
  556. );
  557. $form['cancel_button'] = array(
  558. '#type' => 'submit',
  559. '#value' => t('Cancel'),
  560. '#name' => 'cancel_data',
  561. '#weight' => 1001,
  562. '#limit_validation_errors' => array(array('')),
  563. );
  564. // The entity object must be added to the $form_state in order for
  565. // the Entity API to work. It must have a key of the entity name.
  566. $form_state['TripalEntity'] = $entity;
  567. $form['#prefix'] = "<div id='$bundle_name-entity-form'>";
  568. $form['#suffix'] = "</div>";
  569. $form['#submit'][] = 'tripal_entity_form_submit';
  570. return $form;
  571. }
  572. /**
  573. * An Ajax callback for the tripal_entity_form.
  574. */
  575. function tripal_entity_form_ajax_callback($form, $form_state) {
  576. // return the form so Drupal can update the content on the page
  577. return $form;
  578. }
  579. /**
  580. * Implements hook_validate() for the tripal_entity_form.
  581. */
  582. function tripal_entity_form_validate($form, &$form_state) {
  583. // If the user is cancelling or deleting the entity then don't validate.
  584. if (array_key_exists('clicked_button', $form_state) and
  585. ($form_state['clicked_button']['#name'] == 'cancel_data' or
  586. $form_state['clicked_button']['#name'] == 'delete_data')) {
  587. return;
  588. }
  589. // For adds and updates, perform validation.
  590. $entity = $form_state['TripalEntity'];
  591. field_attach_form_validate('TripalEntity', $entity, $form, $form_state);
  592. $username = $form_state['values']['author_name'];
  593. $user = user_load_by_name($username);
  594. if (!$user) {
  595. form_set_error('author_name', 'Please provide a valid author name.');
  596. }
  597. try {
  598. $create_date = new DateTime($form_state['values']['author_date']);
  599. }
  600. catch (Exception $e) {
  601. form_set_error('author_date', 'Please provide a valid authored on date.');
  602. }
  603. }
  604. /**
  605. * Implements hook_submit() for the tripal_entity_form.
  606. */
  607. function tripal_entity_form_submit($form, &$form_state) {
  608. $entity = $form_state['TripalEntity'];
  609. if ($form_state['clicked_button']['#name'] =='cancel_data') {
  610. if (property_exists($entity, 'id')) {
  611. $form_state['redirect'] = "bio_data/" . $entity->id;
  612. }
  613. else {
  614. $form_state['redirect'] = 'bio_data/add';
  615. }
  616. return;
  617. }
  618. if ($form_state['clicked_button']['#name'] =='delete_data') {
  619. $form_state['redirect'] = 'bio_data/' . $entity->id .'/delete';
  620. return;
  621. }
  622. $username = $form_state['values']['author_name'];
  623. $user = user_load_by_name($username);
  624. $entity->uid = $user->uid;
  625. $create_date = $form_state['values']['author_date'];
  626. $entity->created = $create_date;
  627. $published = $form_state['values']['status'];
  628. $entity->status = $published;
  629. // Allow the fields to perform actions prior to submit.
  630. $instances = field_info_instances('TripalEntity', $entity->bundle);
  631. $langcode = 'und';
  632. foreach ($instances as $field_name => $instance) {
  633. $entity_type = $instance['entity_type'];
  634. if($entity_type == 'TripalEntity' and array_key_exists($field_name, $form)) {
  635. foreach ($form[$field_name][$langcode] as $delta => $field_form) {
  636. if (!preg_match('/^\d+$/', $delta)) {
  637. continue;
  638. }
  639. $widget_type = $instance['widget']['type'];
  640. if (tripal_load_include_field_class($widget_type)) {
  641. $field = $field_form['#field'];
  642. $widget = new $widget_type($field, $instance);
  643. $widget->submit($form, $form_state, $entity_type, $entity, $langcode, $delta);
  644. }
  645. }
  646. }
  647. }
  648. $entityform = entity_ui_controller('TripalEntity')->entityFormSubmitBuildEntity($form, $form_state);
  649. if ($entityform->save()) {
  650. $form_state['redirect'] = "bio_data/" . $entity->id;
  651. }
  652. else {
  653. drupal_set_message('Cannot save entity', 'error');
  654. }
  655. }
  656. /**
  657. * Provides a list of TripalEntity types (bundles) for the user to add.
  658. *
  659. * This function is a callback in a menu item which is set in the
  660. * TripalEntityUIController class.
  661. */
  662. function tripal_add_page() {
  663. // The content array to be returned.
  664. $content = [];
  665. $content['instructions'] = [
  666. '#type' => 'markup',
  667. '#markup' => 'This page provides links for creating pages for Tripal ' .
  668. 'supported content types. These content types are organized by categories. ' .
  669. 'Please note, however, that the categorization is the most common use ' .
  670. 'for a given type. Some types may be useful in other "categories" as well.',
  671. '#weight' => -15,
  672. ];
  673. // Get the list of categories.
  674. $select = "
  675. SELECT TBV.value as category
  676. FROM tripal_bundle_variables TBV
  677. INNER JOIN tripal_variables TV on TV.variable_id = TBV.variable_id
  678. WHERE TV.name = 'bundle_category'
  679. ";
  680. $categories = db_query($select);
  681. // Build the fieldsets for the categories.
  682. $fieldsets = [];
  683. $category_weight = 1;
  684. while ($category = $categories->fetchField()) {
  685. $machine_name = preg_replace('/[^\w]/', '_', $category);
  686. $fieldsets[$machine_name . '_fieldset'] = [
  687. '#type' => 'fieldset',
  688. '#title' => $category,
  689. '#collapsed' => TRUE,
  690. '#collapsible' => TRUE,
  691. '#attributes' => array(
  692. 'class' => array('collapsible', 'tripal-content-list'),
  693. ),
  694. '#attached' => array(
  695. 'js' => array('misc/collapse.js', 'misc/form.js')
  696. ),
  697. '#weight' => $category == 'General' ? -10 : $category_weight++,
  698. ];
  699. }
  700. // Create the "other" fieldset, and set it's weight to 100 so it goes to
  701. // the bottom.
  702. $fieldsets['Other_fieldset'] = [
  703. '#type' => 'fieldset',
  704. '#title' => 'Other',
  705. '#weight' => 100,
  706. '#collapsed' => TRUE,
  707. '#collapsible' => TRUE,
  708. '#attributes' => array(
  709. 'class' => array('collapsible'),
  710. ),
  711. '#attached' => array(
  712. 'js' => array('misc/collapse.js', 'misc/form.js')
  713. ),
  714. ];
  715. // Get the list of bundles and iterate through them.
  716. $select = "SELECT id, name, label FROM tripal_bundle ORDER BY label";
  717. $bundles = db_query($select);
  718. while ($bundle = $bundles->fetchObject()) {
  719. // Lookup the bundle category.
  720. $sql = "
  721. SELECT TBV.value as category
  722. FROM tripal_bundle TB
  723. INNER JOIN tripal_bundle_variables TBV on TB.id = TBV.bundle_id
  724. INNER JOIN tripal_variables TV on TV.variable_id = TBV.variable_id
  725. WHERE TV.name = 'bundle_category' and TB.id = :id;
  726. ";
  727. $category = db_query($sql, [':id' => $bundle->id])->fetchField();
  728. if (!$category) {
  729. $category = 'Other';
  730. }
  731. $machine_name = preg_replace('/[^\w]/', '_', $category);
  732. $bundle = tripal_load_bundle_entity(['id' => $bundle->id]);
  733. if (!$bundle) {
  734. continue;
  735. }
  736. if (user_access('create ' . $bundle->name)) {
  737. if (!array_key_exists($machine_name . '_fieldset', $content)) {
  738. $content[$machine_name . '_fieldset'] = $fieldsets[$machine_name . '_fieldset'];
  739. }
  740. $content[$machine_name . '_fieldset'][$bundle->name] = [
  741. '#type' => 'item',
  742. '#markup' => l($bundle->label, 'bio_data/add/' . $bundle->id),
  743. '#description' => $bundle->term->definition
  744. ];
  745. }
  746. }
  747. // Now iterate through the fieldsets and set their weight
  748. return $content;
  749. }
  750. /**
  751. * Returns HTML for a list of available node types for node creation.
  752. *
  753. * @param $variables
  754. * An associative array containing:
  755. * - content: An array of content types.
  756. *
  757. * @ingroup themeable
  758. */
  759. function theme_tripal_add_list($variables) {
  760. $content = $variables['content'];
  761. $output = '';
  762. if ($content) {
  763. $output = '<dl class="node-type-list">';
  764. foreach ($content as $item) {
  765. $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
  766. $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
  767. }
  768. $output .= '</dl>';
  769. }
  770. else {
  771. $output = tripal_set_message(
  772. 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".'),
  773. TRIPAL_INFO,
  774. array('return_html' => TRUE)
  775. );
  776. $output .= '<p>' . t('You have not created any biological data types yet. ' .
  777. 'Go to the <a href="@create-content">data type creation page</a> to ' .
  778. 'add a new data type.',
  779. array('@create-content' => url('admin/structure/bio_data/add'))) . '</p>';
  780. }
  781. return $output;
  782. }
  783. /**
  784. * Form callback: confirmation form for deleting a tripal_entity.
  785. *
  786. * @param $tripal_entity The
  787. * tripal_entity to delete
  788. *
  789. * @see confirm_form()
  790. */
  791. function tripal_entity_delete_form($form, &$form_state, $entity) {
  792. $form_state['entity'] = $entity;
  793. $form['#submit'][] = 'tripal_entity_delete_form_submit';
  794. $form = confirm_form($form,
  795. t('Click the delete button below to confirm deletion of the record titled: %title',
  796. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  797. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  798. return $form;
  799. }
  800. /**
  801. * Submit callback for tripal_entity_delete_form
  802. */
  803. function tripal_entity_delete_form_submit($form, &$form_state) {
  804. global $user;
  805. $entity = $form_state['entity'];
  806. if (!entity_access('delete', 'TripalEntity', $entity, $user)) {
  807. drupal_set_message(t('You do not have permission to delete this content.'), "error");
  808. $form_state['redirect'] = 'admin/content/bio_data';
  809. return;
  810. }
  811. $entity_controller = new TripalEntityController($entity->type);
  812. if ($entity_controller->delete(array($entity->id))) {
  813. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  814. $form_state['redirect'] = 'admin/content/bio_data';
  815. }
  816. else {
  817. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  818. }
  819. }
  820. /**
  821. * A helper function for checking if a user can add Tripal Content.
  822. *
  823. * This function is a callback for the bio_data/add menu path.
  824. */
  825. function _tripal_entity_add_access() {
  826. global $user;
  827. $types = tripal_get_content_types();
  828. foreach ($types as $type) {
  829. if (user_access('create ' . $type->name, $user)) {
  830. return TRUE;
  831. }
  832. }
  833. return FALSE;
  834. }