TripalBundleUIController.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * UI controller.
  7. */
  8. class TripalBundleUIController extends EntityDefaultUIController {
  9. public function __construct($entity_type, $entity_info) {
  10. parent::__construct($entity_type, $entity_info);
  11. }
  12. /**
  13. * Overrides hook_menu() defaults.
  14. */
  15. public function hook_menu() {
  16. $items = parent::hook_menu();
  17. // Alter the Admin > Structure > Tripal Content Types menu item.
  18. $items[$this->path]['description'] = 'Manage biological content types that are
  19. added using Tripal.';
  20. // We don't want to let the user import new Tripal data types.
  21. unset($items[$this->path . '/import']);
  22. // Add an action link to Admin > Structure > Tripal Content Types for adding types.
  23. $items[$this->path . '/add'] = array(
  24. 'title' => 'Add Tripal Content Type',
  25. 'description' => 'Add new biological content',
  26. 'page callback' => 'drupal_get_form',
  27. 'page arguments' => array('tripal_entities_admin_add_type_form'),
  28. 'access arguments' => array('administer tripal data types'),
  29. 'file' => 'includes/tripal_entities.admin.inc',
  30. 'file path' => drupal_get_path('module', 'tripal_entities'),
  31. 'type' => MENU_LOCAL_ACTION,
  32. 'weight' => 2
  33. );
  34. return $items;
  35. }
  36. /**
  37. * Allows us to change the forms created by the parent class.
  38. */
  39. function hook_forms() {
  40. $forms = parent::hook_forms();
  41. // The edit form for the entity type by default expects a function,
  42. // named after the entity type but we can't dynamically create these
  43. // functions. We'll use a single form for all entity types.
  44. $forms[$this->entityType . '_form'] = array(
  45. 'callback' => 'tripal_entities_tripal_bundle_form',
  46. 'callback arguments' => array($this->entityType)
  47. );
  48. return $forms;
  49. }
  50. }
  51. /**
  52. * Tripal content type edit form.
  53. *
  54. * @param $form
  55. * The default form array. Usually empty.
  56. * @param $form_state
  57. * Build information for the form including the entity type and submitted values.
  58. * @param $entityDataType
  59. * A string indicating the entity type. This will always be TripalBundle.
  60. */
  61. function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType) {
  62. $entity_type = $form_state['build_info']['args'][0];
  63. $chado_basetable = $entity_type->label;
  64. $cvterm = chado_generate_var('cvterm', array('name' => $entity_type->label));
  65. // Add a validate and submit handler to save the data in this form.
  66. $form['#validate'] = array('tripal_entities_tripal_bundle_form_validate');
  67. $form['#submit'] = array('tripal_entities_tripal_bundle_form_submit');
  68. // @TODO: Move this into a css file.
  69. $form['#attached']['css'] = array(
  70. array(
  71. 'data' => '.form-item select, .form-item input { width:40%; }',
  72. 'type' => 'inline',
  73. ),
  74. );
  75. //dpm($form_state, 'form state');
  76. //dpm($entity_type, 'entity type');
  77. //dpm($cvterm, 'cvterm');
  78. $form['vocab'] = array(
  79. '#type' => 'select',
  80. '#title' => t('Vocabulary'),
  81. '#options' => array($cvterm->cv_id->name),
  82. '#description' => t('The vocabulary the following term is part of.'),
  83. '#default_value' => $cvterm->cv_id->name,
  84. '#disabled' => TRUE
  85. );
  86. $form['term'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Term'),
  89. '#description' => t('The term the content type is based on.'),
  90. '#default_value' => $cvterm->name,
  91. '#disabled' => TRUE
  92. );
  93. $form['additional_settings'] = array(
  94. '#type' => 'vertical_tabs',
  95. '#weight' => 99,
  96. );
  97. // Set Title Format.
  98. //-------------------------
  99. $title_format = chado_node_get_unique_constraint_format($chado_basetable);
  100. $form['set_titles'] = array(
  101. '#type' => 'fieldset',
  102. '#title' => t('Page Title options'),
  103. '#collapsible' => TRUE,
  104. '#collapsed' => FALSE,
  105. '#tree' => TRUE,
  106. '#group' => 'additional_settings',
  107. );
  108. $form['set_titles']['explanation'] = array(
  109. '#type' => 'item',
  110. '#markup' => t('<p>The format below is used to determine the title displayed on content
  111. pages. This ensures all content of this type is consistent while still allowing you
  112. to indicate which data you want represented in the title (ie: which data would most
  113. identify your content).</p>
  114. <p>Keep in mind that it might be confusing to users if more than
  115. one page has the same title. We recommend you <strong>choose a combination of tokens that
  116. will uniquely identify your content</strong>.</p>'),
  117. );
  118. $form['set_titles']['title_format'] = array(
  119. '#type' => 'textarea',
  120. '#title' => t('Page Title Format'),
  121. '#description' => t('You may rearrange elements in this text box to customize the page
  122. titles. The available tokens are listed below. You can separate or include any text
  123. between the tokens.'),
  124. '#default_value' => $title_format,
  125. '#rows' => 1
  126. );
  127. $form['set_titles']['token_display'] = array(
  128. '#type' => 'fieldset',
  129. '#title' => t('Available Tokens'),
  130. '#description' => t('Copy the token and paste it into the "Custom Page Title" text field above.'),
  131. '#collapsible' => TRUE,
  132. '#collapsed' => TRUE
  133. );
  134. $tokens = array();
  135. if (empty($tokens)) {
  136. $tokens = chado_node_generate_tokens($chado_basetable);
  137. }
  138. $form['set_titles']['tokens'] = array(
  139. '#type' => 'hidden',
  140. '#value' => serialize($tokens)
  141. );
  142. $token_list = chado_node_format_tokens($tokens);
  143. $form['set_titles']['token_display']['content'] = array(
  144. '#type' => 'item',
  145. '#markup' => $token_list
  146. );
  147. // Submit Buttons
  148. //-------------------------
  149. $form['save'] = array(
  150. '#type' => 'submit',
  151. '#value' => t('Save Content Type'),
  152. '#weight' => 100
  153. );
  154. $form['delete'] = array(
  155. '#type' => 'submit',
  156. '#value' => t('Delete Content Type'),
  157. '#weight' => 101
  158. );
  159. return $form;
  160. }
  161. /**
  162. * Validate: Tripal content type edit form.
  163. */
  164. function tripal_entities_tripal_bundle_form_validate($form, $form_state) {
  165. $tokens_available = unserialize($form_state['values']['set_titles']['tokens']);
  166. if (preg_match_all('/(\[\w+\.\w+\])/', $form_state['values']['set_titles']['title_format'], $matches)) {
  167. // The matches of the first and only pattern will be our tokens.
  168. $tokens_used = $matches[1];
  169. // Determine if any of the tokens used were not in the original list of available tokens.
  170. $tokens_missing = array_diff($tokens_used, array_keys($tokens_available));
  171. if ($tokens_missing) {
  172. $msg = t('You must only use tokens listed under available tokens. You used the following incorrect tokens: %tokens',
  173. array('%tokens' => implode(', ', $tokens_missing)));
  174. form_set_error('set_titles][title_format', $msg);
  175. }
  176. }
  177. else {
  178. $msg = t('You should use at least one token in your title format or the title for all %type pages will be the same.',
  179. array('%type' => $form_state['build_info']['args'][0]->label));
  180. form_set_error('set_titles][title_format', $msg);
  181. }
  182. }
  183. /**
  184. * Submit: Tripal content type edit form.
  185. */
  186. function tripal_entities_tripal_bundle_form_submit($form, $form_state) {
  187. //dpm($form_state, 'form state in submit');
  188. }
  189. /**
  190. * Access callback for the entity API.
  191. */
  192. function tripal_bundle_access($op, $type = NULL, $account = NULL) {
  193. return user_access('administer tripal data types', $account);
  194. }
  195. /**
  196. * Generates the tripal data type editing form.
  197. */
  198. function tripal_bundle_form($form, &$form_state, $tripal_bundle, $op = 'edit') {
  199. if ($op == 'clone') {
  200. $tripal_bundle->label .= ' (cloned)';
  201. $tripal_bundle->type = '';
  202. }
  203. $form['label'] = array(
  204. '#title' => t('Label'),
  205. '#type' => 'textfield',
  206. '#default_value' => $tripal_bundle->label,
  207. '#description' => t('The human-readable name of this tripal data type.'),
  208. '#required' => TRUE,
  209. '#size' => 30,
  210. );
  211. return $form;
  212. }
  213. /**
  214. * Form API submit callback for the type form.
  215. */
  216. function tripal_bundle_form_submit(&$form, &$form_state) {
  217. $tripal_bundle = entity_ui_form_submit_build_entity($form, $form_state);
  218. $tripal_bundle->save();
  219. $form_state['redirect'] = $this->path;
  220. }
  221. /**
  222. * Form API submit callback for the delete button.
  223. */
  224. function tripal_bundle_form_submit_delete(&$form, &$form_state) {
  225. $form_state['redirect'] = $this->path;
  226. }