ChadoDataUIController.inc 17 KB

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