ChadoDataUIController.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. /**
  3. * UI controller.
  4. */
  5. class ChadoDataUIController 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. $items[$this->path] = array(
  16. 'title' => 'Chado Data',
  17. 'description' => 'Add edit and update chado data.',
  18. 'page callback' => 'system_admin_menu_block_page',
  19. 'access arguments' => array('access administration pages'),
  20. 'file path' => drupal_get_path('module', 'system'),
  21. 'file' => 'system.admin.inc',
  22. );
  23. // Change the overview menu type for the list of models.
  24. $items[$this->path]['type'] = MENU_LOCAL_TASK;
  25. // Add an action link to the admin page for adding new data.
  26. $items[$this->path . '/add'] = array(
  27. 'title' => 'Add Chado Data',
  28. 'description' => 'Add a new chado data record',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('chado_data_form'),
  31. 'access callback' => 'chado_data_access',
  32. 'access arguments' => array('edit'),
  33. 'type' => MENU_LOCAL_ACTION,
  34. 'weight' => 20,
  35. );
  36. // Set a custom page for adding new chado data entities.
  37. $items['data/add'] = array(
  38. 'title' => 'Add Chado data',
  39. 'description' => 'Add a new chado data record',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('chado_data_form'),
  42. 'access callback' => 'chado_data_access',
  43. 'access arguments' => array('edit'),
  44. 'type' => MENU_NORMAL_ITEM,
  45. 'weight' => 20,
  46. );
  47. // Link for viewing a chado data type.
  48. $items['data/' . $wildcard] = array(
  49. 'title callback' => 'chado_data_title',
  50. 'title arguments' => array(1),
  51. 'page callback' => 'chado_data_view',
  52. 'page arguments' => array(1),
  53. 'access callback' => 'chado_data_access',
  54. 'access arguments' => array('view', 1),
  55. 'type' => MENU_CALLBACK,
  56. );
  57. // 'View' tab for an individual entity page.
  58. $items['data/' . $wildcard . '/view'] = array(
  59. 'title' => 'View',
  60. 'page callback' => 'chado_data_view',
  61. 'page arguments' => array(1),
  62. 'access callback' => 'chado_data_access',
  63. 'access arguments' => array('view', 1),
  64. 'type' => MENU_DEFAULT_LOCAL_TASK,
  65. 'weight' => -10,
  66. );
  67. // 'Edit' tab for an individual entity page.
  68. $items['data/' . $wildcard . '/edit'] = array(
  69. 'title' => 'Edit',
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('chado_data_form', 1),
  72. 'access callback' => 'chado_data_access',
  73. 'access arguments' => array('edit', 1),
  74. 'type' => MENU_LOCAL_TASK,
  75. );
  76. // Menu item for deleting chado data entities.
  77. $items['data/' . $wildcard . '/delete'] = array(
  78. 'title' => 'Delete',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('chado_data_delete_form', 1),
  81. 'access callback' => 'chado_data_access',
  82. 'access arguments' => array('edit', 1),
  83. 'type' => MENU_CALLBACK,
  84. 'weight' => 10,
  85. );
  86. return $items;
  87. }
  88. }
  89. /**
  90. * Determines whether the given user has access to a chado data entity.
  91. *
  92. * @param $op
  93. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  94. * or just 'edit' (being the same as 'create' or 'update').
  95. * @param $entity
  96. * Optionally a chado data entity or a chado data type to check access for.
  97. * If nothing is given, access for all types is determined.
  98. * @param $account
  99. * The user to check for. Leave it to NULL to check for the global user.
  100. * @return boolean
  101. * Whether access is allowed or not.
  102. */
  103. function chado_data_access($op, $entity = NULL, $account = NULL) {
  104. if (user_access('administer chado data', $account)) {
  105. return TRUE;
  106. }
  107. if (isset($entity) && $type_name = $entity->type) {
  108. $op = ($op == 'view') ? 'view' : 'edit';
  109. if (user_access("$op any $type_name data", $account)) {
  110. return TRUE;
  111. }
  112. }
  113. return FALSE;
  114. }
  115. /**
  116. *
  117. */
  118. function chado_data_form($form, &$form_state, $entity = NULL) {
  119. // Set the defaults.
  120. $cv_id = NULL;
  121. $term_name = NULL;
  122. $cvterm = NULL;
  123. // Set defaults if an entity was provided.
  124. if ($entity) {
  125. drupal_set_title('Edit ' . $entity->title);
  126. $entity_id = $entity->entity_id;
  127. $values = array('cvterm_id' => $entity->cvterm_id);
  128. $cvterm = chado_generate_var('cvterm', $values);
  129. $cv_id = $cvterm->cv_id->cv_id;
  130. $term_name = $cvterm->name;
  131. }
  132. // Set defaults using the form state.
  133. if (array_key_exists('values', $form_state)) {
  134. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
  135. $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
  136. // Get the cvterm that matches
  137. $values = array(
  138. 'cv_id' => $cv_id,
  139. 'name' => $term_name
  140. );
  141. $cvterm = chado_generate_var('cvterm', $values);
  142. }
  143. // Let the user select the vocabulary and chado_data but only if they haven't
  144. // already selected a chado_data.
  145. $cvs = tripal_get_cv_select_options();
  146. if (!$term_name) {
  147. $form['cv_id'] = array(
  148. '#type' => 'select',
  149. '#title' => t('Vocabulary'),
  150. '#options' => $cvs,
  151. '#required' => TRUE,
  152. '#description' => t('Select a vocabulary that contains the term for the type of data you want to add.'),
  153. '#default_value' => $cv_id,
  154. '#ajax' => array(
  155. 'callback' => "chado_data_form_ajax_callback",
  156. 'wrapper' => 'chado_data_form',
  157. 'effect' => 'fade',
  158. 'method' => 'replace'
  159. )
  160. );
  161. }
  162. // If we have a CV ID then we want to provide an autocomplete field
  163. if ($cv_id and !$term_name) {
  164. $form['cvterm_select']['term_name'] = array(
  165. '#title' => t('Record Type'),
  166. '#type' => 'textfield',
  167. '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."),
  168. '#required' => TRUE,
  169. '#default_value' => $term_name,
  170. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  171. );
  172. $form['cvterm_select']['select_button'] = array(
  173. '#type' => 'submit',
  174. '#value' => t('Use this term'),
  175. '#name' => 'select_cvterm',
  176. );
  177. }
  178. // Once the CV term is selected then provide the other fields.
  179. if ($cvterm) {
  180. $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  181. $form['cv_id'] = array(
  182. '#type' => 'hidden',
  183. '#value' => $cv_id,
  184. );
  185. $form['term_name'] = array(
  186. '#type' => 'hidden',
  187. '#value' => $term_name,
  188. );
  189. $form['cvterm_id'] = array(
  190. '#type' => 'hidden',
  191. '#value' => $cvterm->cvterm_id,
  192. );
  193. $form['type'] = array(
  194. '#type' => 'hidden',
  195. '#value' => $bundle_id,
  196. );
  197. $form['details'] = array(
  198. '#type' => 'fieldset',
  199. '#title' => 'Record Type',
  200. '#collapsable' => FALSE,
  201. '#weight' => -100,
  202. );
  203. $form['details']['cv_name_shown'] = array(
  204. '#type' => 'item',
  205. '#title' => 'Vocabulary',
  206. '#markup' => $cvterm->cv_id->name,
  207. );
  208. $form['details']['term_name_shown'] = array(
  209. '#type' => 'item',
  210. '#title' => 'Term',
  211. '#markup' => $cvterm->name,
  212. );
  213. /*
  214. // Create the Chado data type entity.
  215. $data_type_entity = chado_data_type_create(array(
  216. 'type' => $bundle_id,
  217. 'label' => $cvterm->name,
  218. 'module' => 'tripal_entities'
  219. ));
  220. $data_type_entity->save();
  221. // Drupal field types and settings:
  222. // https://www.drupal.org/node/1879542
  223. $field = array(
  224. 'field_name' => 'feature__name',
  225. 'type' => 'text',
  226. 'cardinality' => 1,
  227. 'storage' => array(
  228. 'type' => 'tripal_entities_storage'
  229. ),
  230. );
  231. field_create_field($field);
  232. $field_instance = array(
  233. 'field_name' => 'feature__name',
  234. 'label' => 'Name',
  235. 'widget' => array(
  236. 'type' => 'text_textfield'
  237. ),
  238. 'entity_type' => 'chado_data',
  239. 'required' => 'true',
  240. 'settings' => array(
  241. 'max_length' => 255
  242. ),
  243. 'bundle' => $bundle_id,
  244. );
  245. field_create_instance($field_instance);
  246. $field = array(
  247. 'field_name' => 'feature__uniquename',
  248. 'type' => 'text',
  249. 'cardinality' => 1,
  250. 'storage' => array(
  251. 'type' => 'tripal_entities_storage'
  252. ),
  253. );
  254. field_create_field($field);
  255. $field_instance = array(
  256. 'field_name' => 'feature__uniquename',
  257. 'label' => 'Unique Name',
  258. 'widget' => array(
  259. 'type' => 'text_textfield'
  260. ),
  261. 'entity_type' => 'chado_data',
  262. 'required' => 'true',
  263. 'settings' => array(
  264. 'max_length' => 255
  265. ),
  266. 'bundle' => $bundle_id,
  267. );
  268. field_create_instance($field_instance);
  269. $field = array(
  270. 'field_name' => 'feature__organism_id',
  271. 'type' => 'organism_id',
  272. 'cardinality' => 1,
  273. 'storage' => array(
  274. 'type' => 'tripal_entities_storage'
  275. ),
  276. );
  277. field_create_field($field);
  278. $field_instance = array(
  279. 'field_name' => 'feature__organism_id',
  280. 'label' => 'Organism',
  281. 'entity_type' => 'chado_data',
  282. 'required' => 'true',
  283. 'settings' => array(),
  284. 'bundle' => $bundle_id,
  285. );
  286. field_create_instance($field_instance);
  287. */
  288. // If the entity doesn't exist then create one.
  289. if (!$entity) {
  290. $entity = entity_get_controller('chado_data')->create(array('type' => $bundle_id));
  291. field_attach_form('chado_data', $entity, $form, $form_state);
  292. $form['submit'] = array(
  293. '#type' => 'submit',
  294. '#value' => t('Add a new ' . $cvterm->name),
  295. '#name' => 'add_data',
  296. '#weight' => 1000
  297. );
  298. }
  299. else {
  300. field_attach_form('chado_data', $entity, $form, $form_state);
  301. $form['submit'] = array(
  302. '#type' => 'submit',
  303. '#value' => t('Update'),
  304. '#name' => 'update_data',
  305. '#weight' => 1000
  306. );
  307. }
  308. // The entity object must be added to the $form_state in order for
  309. // the Entity API to work. It must have a key of the entity name.
  310. $form_state['chado_data'] = $entity;
  311. }
  312. $form['#prefix'] = '<div id="chado_data_form">';
  313. $form['#suffix'] = '</div>';
  314. return $form;
  315. }
  316. /**
  317. * An Ajax callback for the chado_data_form.
  318. */
  319. function chado_data_form_ajax_callback($form, $form_state) {
  320. // return the form so Drupal can update the content on the page
  321. return $form;
  322. }
  323. /**
  324. * Implements hook_validate() for the chado_data_form.
  325. */
  326. function chado_data_form_validate($form, &$form_state) {
  327. if ($form_state['clicked_button']['#name'] == 'add_data') {
  328. $chado_data = (object) $form_state['values'];
  329. field_attach_form_validate('chado_data', $chado_data, $form, $form_state);
  330. }
  331. }
  332. /**
  333. * Implements hook_submit() for the chado_data_form.
  334. *
  335. */
  336. function chado_data_form_submit($form, &$form_state) {
  337. if ($form_state['clicked_button']['#name'] == 'cancel') {
  338. if (array_key_exists('entity_id', $form_state['values'])){
  339. $entity = $form_state['values']['entity'];
  340. $form_state['redirect'] = "data/$entity->entity_id";
  341. }
  342. else {
  343. $form_state['redirect'] = "admin/structure/chado_data";
  344. }
  345. return;
  346. }
  347. if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
  348. // don't do anything, we just need to know what the term name is.
  349. $form_state['rebuild'] = TRUE;
  350. }
  351. if ($form_state['clicked_button']['#name'] == 'update_data' or
  352. $form_state['clicked_button']['#name'] == 'add_data') {
  353. // Use the Entity API to get the entity from the form state, then
  354. // attach the fields and save.
  355. $entity = entity_ui_controller('chado_data')->entityFormSubmitBuildEntity($form, $form_state);
  356. $entity->save();
  357. $form_state['redirect'] = "data/$entity->entity_id";
  358. }
  359. }
  360. /**
  361. * Form API submit callback for the delete button.
  362. *
  363. * @todo Remove hard-coded path
  364. */
  365. function chado_data_form_submit_delete(&$form, &$form_state) {
  366. $form_state['redirect'] = 'admin/content/chado_datas/chado_data/' . $form_state['chado_data']->chado_data_id . '/delete';
  367. }
  368. /**
  369. * Form callback: confirmation form for deleting a chado_data.
  370. *
  371. * @param $chado_data
  372. * The chado_data to delete
  373. *
  374. * @see confirm_form()
  375. */
  376. function chado_data_delete_form($form, &$form_state, $chado_data) {
  377. $form_state['chado_data'] = $chado_data;
  378. $form['#submit'][] = 'chado_data_delete_form_submit';
  379. $form = confirm_form($form,
  380. t('Are you sure you want to delete chado_data %name?', array('%name' => $chado_data->name)),
  381. 'admin/content/chado_datas/chado_data',
  382. '<p>' . t('This action cannot be undone.') . '</p>',
  383. t('Delete'),
  384. t('Cancel'),
  385. 'confirm'
  386. );
  387. return $form;
  388. }
  389. /**
  390. * Submit callback for chado_data_delete_form
  391. */
  392. function chado_data_delete_form_submit($form, &$form_state) {
  393. $chado_data = $form_state['chado_data'];
  394. chado_data_delete($chado_data);
  395. drupal_set_message(t('The chado_data %name has been deleted.', array('%name' => $chado_data->name)));
  396. watchdog('chado_data', 'Deleted chado_data %name.', array('%name' => $chado_data->name));
  397. $form_state['redirect'] = 'admin/content/chado_datas';
  398. }
  399. /**
  400. * Displays the list of available chado_data types for chado_data creation.
  401. *
  402. * @ingroup themeable
  403. */
  404. function theme_chado_data_add_list($variables) {
  405. $content = $variables['content'];
  406. $output = '';
  407. if ($content) {
  408. $output = '<dl class="chado_data-type-list">';
  409. foreach ($content as $item) {
  410. $output .= '<dt>' . l($item['title'], $item['href']) . '</dt>';
  411. $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
  412. }
  413. $output .= '</dl>';
  414. }
  415. else {
  416. if (user_access('administer chado_data types')) {
  417. $output = '<p>' . t('Chado Data Entities cannot be added because you have not created any chado_data types yet. Go to the <a href="@create-chado_data-type">chado_data type creation page</a> to add a new chado_data type.', array('@create-chado_data-type' => url('admin/structure/chado_data_types/add'))) . '</p>';
  418. }
  419. else {
  420. $output = '<p>' . t('No chado_data types have been created yet for you to use.') . '</p>';
  421. }
  422. }
  423. return $output;
  424. }
  425. /**
  426. * Sets the breadcrumb for administrative chado_data pages.
  427. */
  428. function chado_data_set_breadcrumb() {
  429. $breadcrumb = array(
  430. l(t('Home'), '<front>'),
  431. l(t('Administration'), 'admin'),
  432. l(t('Content'), 'admin/content'),
  433. l(t('Chado Data'), 'admin/content/chado_data'),
  434. );
  435. drupal_set_breadcrumb($breadcrumb);
  436. }
  437. /**
  438. * Menu callback to display an entity.
  439. *
  440. * As we load the entity for display, we're responsible for invoking a number
  441. * of hooks in their proper order.
  442. *
  443. * @see hook_entity_prepare_view()
  444. * @see hook_entity_view()
  445. * @see hook_entity_view_alter()
  446. */
  447. function chado_data_view($entity, $view_mode = 'full') {
  448. // Our entity type, for convenience.
  449. $entity_type = 'chado_data';
  450. // Start setting up the content.
  451. $entity->content = array(
  452. '#view_mode' => $view_mode,
  453. );
  454. // Build fields content - this is where the Field API really comes in to play.
  455. // The task has very little code here because it all gets taken care of by
  456. // field module. field_attach_prepare_view() lets the fields load any
  457. // data they need before viewing.
  458. field_attach_prepare_view($entity_type, array($entity->entity_id => $entity),
  459. $view_mode);
  460. // We call entity_prepare_view() so it can invoke hook_entity_prepare_view()
  461. // for us.
  462. entity_prepare_view($entity_type, array($entity->entity_id => $entity));
  463. // Now field_attach_view() generates the content for the fields.
  464. $entity->content += field_attach_view($entity_type, $entity, $view_mode);
  465. // OK, Field API done, now we can set up some of our own data.
  466. // $entity->content['created'] = array(
  467. // '#type' => 'item',
  468. // '#title' => t('Created date'),
  469. // '#markup' => format_date($entity->created),
  470. // );
  471. // Now to invoke some hooks. We need the language code for
  472. // hook_entity_view(), so let's get that.
  473. global $language;
  474. $langcode = $language->language;
  475. // And now invoke hook_entity_view().
  476. module_invoke_all('entity_view', $entity, $entity_type, $view_mode, $langcode);
  477. // Now invoke hook_entity_view_alter().
  478. drupal_alter(array('chado_data', 'entity_view'), $entity->content, $entity_type);
  479. // And finally return the content.
  480. return $entity->content;
  481. }
  482. /**
  483. * Menu title callback for showing individual entities
  484. */
  485. function chado_data_title(ChadoData $entity){
  486. return $entity->title;
  487. }