tripal_entities.entity_form.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_entities_add_page() {
  6. $item = menu_get_item();
  7. $content = system_admin_menu_block($item);
  8. // Bypass the node/add listing if only one content type is available.
  9. if (count($content) == 1) {
  10. $item = array_shift($content);
  11. drupal_goto($item['href']);
  12. }
  13. return theme('tripal_entities_add_list', array('content' => $content));
  14. }
  15. /**
  16. * Returns HTML for a list of available node types for node creation.
  17. *
  18. * @param $variables
  19. * An associative array containing:
  20. * - content: An array of content types.
  21. *
  22. * @ingroup themeable
  23. */
  24. function theme_tripal_entities_add_list($variables) {
  25. $content = $variables['content'];
  26. $output = '';
  27. if ($content) {
  28. $output = '<dl class="node-type-list">';
  29. foreach ($content as $item) {
  30. $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
  31. $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
  32. }
  33. $output .= '</dl>';
  34. }
  35. else {
  36. $output = '<p>' . t('You have not created any biological types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.', array('@create-content' => url('admin/structure/BioData/add'))) . '</p>';
  37. }
  38. return $output;
  39. }
  40. /**
  41. *
  42. */
  43. function tripal_entities_entity_form($form, &$form_state, $dbxref_id = '', $entity = NULL) {
  44. $bundle_id = 'dbxref_' . $dbxref_id;
  45. // Add a vertical tabs element
  46. $form['ev_tabs'] = array(
  47. '#type' => 'vertical_tabs',
  48. '#weight' => 999,
  49. );
  50. // If the entity doesn't exist then create one.
  51. if (!$entity) {
  52. $entity = entity_get_controller('BioData')->create(array('bundle' => $bundle_id));
  53. field_attach_form('BioData', $entity, $form, $form_state);
  54. $form['add_button'] = array(
  55. '#type' => 'submit',
  56. '#value' => t('Save'),
  57. '#name' => 'add_data',
  58. '#weight' => 1000
  59. );
  60. }
  61. else {
  62. field_attach_form('BioData', $entity, $form, $form_state);
  63. $form['update_button'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Update'),
  66. '#name' => 'update_data',
  67. '#weight' => 1000
  68. );
  69. $form['delete_button'] = array(
  70. '#type' => 'submit',
  71. '#value' => t('Delete'),
  72. '#name' => 'delete_data',
  73. '#weight' => 1001
  74. );
  75. }
  76. // The entity object must be added to the $form_state in order for
  77. // the Entity API to work. It must have a key of the entity name.
  78. $form_state['BioData'] = $entity;
  79. $form['#prefix'] = "<div id='$bundle_id-entity-form'>";
  80. $form['#suffix'] = "</div>";
  81. return $form;
  82. }
  83. /**
  84. * An Ajax callback for the tripal_entities_entity_form.
  85. */
  86. function tripal_entities_entity_form_ajax_callback($form, $form_state) {
  87. // return the form so Drupal can update the content on the page
  88. return $form;
  89. }
  90. /**
  91. * Implements hook_validate() for the tripal_entities_entity_form.
  92. */
  93. function tripal_entities_entity_form_validate($form, &$form_state) {
  94. if (array_key_exists('clicked_button', $form_state) and
  95. $form_state['clicked_button']['#name'] =='add_data') {
  96. $entity = $form_state['BioData'];
  97. field_attach_form_validate('BioData', $entity, $form, $form_state);
  98. }
  99. }
  100. /**
  101. * Implements hook_submit() for the tripal_entities_entity_form.
  102. */
  103. function tripal_entities_entity_form_submit($form, &$form_state) {
  104. $entity = $form_state['BioData'];
  105. if ($form_state['clicked_button']['#name'] =='cancel') {
  106. $form_state['redirect'] = "BioData/" . $entity->id;
  107. }
  108. if ($form_state['clicked_button']['#name'] =='update_data' or
  109. $form_state['clicked_button']['#name'] =='add_data') {
  110. $entityform = entity_ui_controller('BioData')->entityFormSubmitBuildEntity($form, $form_state);
  111. if ($entityform->save()) {
  112. $form_state['redirect'] = "BioData/" . $entity->id;
  113. }
  114. else {
  115. drupal_set_message('Cannot save entity', 'error');
  116. }
  117. }
  118. if ($form_state['clicked_button']['#name'] =='delete_data') {
  119. $form_state['redirect'] = 'BioData/' . $entity->id .'/delete';
  120. }
  121. }
  122. /**
  123. * Form callback: confirmation form for deleting a tripal_entity.
  124. *
  125. * @param $tripal_entity The
  126. * tripal_entity to delete
  127. *
  128. * @see confirm_form()
  129. */
  130. function tripal_entities_entity_delete_form($form, &$form_state, $entity) {
  131. $form_state['entity'] = $entity;
  132. $form['#submit'][] = 'tripal_entities_entity_delete_form_submit';
  133. $form = confirm_form($form,
  134. t('Click the delete button below to confirm deletion of the record titled: %title',
  135. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  136. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  137. return $form;
  138. }
  139. /**
  140. * Submit callback for tripal_entity_delete_form
  141. */
  142. function tripal_entities_entity_delete_form_submit($form, &$form_state) {
  143. $entity = $form_state['entity'];
  144. $entity_controller = new TripalEntityController($entity->type);
  145. if ($entity_controller->delete($entity)) {
  146. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  147. $form_state['redirect'] = 'admin/content/tripal_entitys';
  148. }
  149. else {
  150. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  151. }
  152. }
  153. /**
  154. * Implements hook_submit() for the tripal_entities_admin_publish_form.
  155. *
  156. */
  157. function tripal_entities_add_bundle($cvterm) {
  158. // Create the bundle name and entity type name. The bundle name is the
  159. // dbxref ID. This isn't very human readable, but the alternative is to
  160. // use the accession which may not always be alpha-numeric.
  161. $bundle_name = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
  162. // Check to see if this bundle exists. If not then create it
  163. $bundle = db_select('tripal_bundle', 't')
  164. ->fields('t')
  165. ->condition('type', 'BioData')
  166. ->condition('bundle', $bundle_name)
  167. ->execute()
  168. ->fetchObject();
  169. if (!$bundle) {
  170. // The TripalBundle Entity manages the bundles we have available.
  171. // Therefore, we need to add a new entity for each bundle "type".
  172. $vals = array(
  173. 'label' => $cvterm->name,
  174. 'type' => 'BioData',
  175. 'bundle' => $bundle_name,
  176. 'data' => serialize(array()),
  177. 'module' => 'tripal_entities'
  178. );
  179. $tripal_bundle = new TripalBundle($vals, 'BioData_bundles');
  180. $tripal_bundle->save();
  181. }
  182. // Allow modules to now add fields to the bundle
  183. module_invoke_all('add_bundle_fields', 'BioData', $bundle_name, $cvterm);
  184. }
  185. /**
  186. * Implements hook_add_bundle_fields().
  187. *
  188. * @param $entity_type_name
  189. * @param $bundle_name
  190. * @param $cvterm
  191. */
  192. function tripal_entities_add_bundle_fields($entity_type_name, $bundle_name, $cvterm) {
  193. // Adds the fields for the base table to the entity.
  194. tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_name, $cvterm);
  195. // Check to see if there are any kv-property tables associated to this
  196. // base table. If so, add the fields for that type of table.
  197. tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_name, 'featureprop');
  198. }
  199. /**
  200. * Adds the fields for a kv-property table fields
  201. *
  202. * @param $entity_type_name
  203. * @param $bundle_name
  204. * @param $kv_table
  205. */
  206. function tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_name, $kv_table) {
  207. // First add a generic property field so that users can add new proeprty types.
  208. $field_name = $kv_table;
  209. // Initialize the field array.
  210. $field_info = array(
  211. 'field_type' => 'kvproperty_adder',
  212. 'widget_type' => 'tripal_fields_kvproperty_adder_widget',
  213. 'field_settings' => array(),
  214. 'widget_settings' => array('display_label' => 1),
  215. 'description' => '',
  216. 'label' => 'Additional Properties',
  217. 'is_required' => 0,
  218. );
  219. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  220. }
  221. /**
  222. * Adds the fields for the base table to the entity.
  223. */
  224. function tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_name, $cvterm) {
  225. // Get the list of tables where this cvterm is used.
  226. $match = array('cvterm_id' => $cvterm->cvterm_id);
  227. $term = chado_select_record('tripal_term', array('*'), $match);
  228. $values = array('term_id' => $term[0]->term_id);
  229. $tables = chado_select_record('tripal_term_usage', array('*'), $values);
  230. // Iterate through the tables.
  231. foreach ($tables as $table) {
  232. $table_name = $table->data_table;
  233. $type_table = $table->type_table;
  234. $type_field = $table->field;
  235. // We only want to look at base tables.
  236. if ($table_name == 'cvterm_dbxref' || $table_name == 'cvterm_relationship' ||
  237. $table_name == 'cvtermpath' || $table_name == 'cvtermprop' || $table_name == 'chadoprop' ||
  238. $table_name == 'cvtermsynonym' || preg_match('/_relationship$/', $table_name) ||
  239. preg_match('/_cvterm$/', $table_name)) {
  240. continue;
  241. }
  242. // Iterate through the columns of the table and see if fields have been
  243. // created for each one. If not, then create them.
  244. $schema = chado_get_schema($table_name);
  245. $columns = $schema['fields'];
  246. foreach ($columns as $column_name => $details) {
  247. $field_name = $table_name . '__' . $column_name;
  248. // Skip the primary key field.
  249. if ($column_name == $schema['primary key'][0]) {
  250. continue;
  251. }
  252. // Skip the type field.
  253. if ($table_name == $type_table and $column_name == $type_field) {
  254. continue;
  255. }
  256. // Get the field defaults for this column.
  257. $field_info = tripal_entities_get_table_column_field_default($table_name, $schema, $column_name);
  258. // Determine if the field is required.
  259. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  260. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  261. }
  262. // If we don't have a field type then we don't need to create a field.
  263. if (!$field_info['field_type']) {
  264. // If we don't have a field type but it is required and doesn't have
  265. // a default value then we are in trouble.
  266. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  267. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  268. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  269. }
  270. continue;
  271. }
  272. // If this field is a foreign key field then we will have a special custom
  273. // field provided by Tripal.
  274. $is_fk = FALSE;
  275. if (array_key_exists('foreign keys', $schema)) {
  276. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  277. if (array_key_exists($column_name, $fk_details['columns'])) {
  278. $is_fk = TRUE;
  279. }
  280. }
  281. }
  282. // Add the field to the bundle.
  283. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  284. }
  285. }
  286. }
  287. /**
  288. * Returns a $field_info array for a field based on a databaes column.
  289. *
  290. */
  291. function tripal_entities_get_table_column_field_default($table_name, $schema, $column_name) {
  292. $details = $schema['fields'][$column_name];
  293. // Create an array with information about this field.
  294. $field_info = array(
  295. 'field_type' => '',
  296. 'widget_type' => '',
  297. 'field_settings' => array(
  298. 'chado_table' => $table_name,
  299. 'chado_column' => $column_name,
  300. ),
  301. 'widget_settings' => array('display_label' => 1),
  302. 'description' => '',
  303. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  304. 'is_required' => 0,
  305. );
  306. // Alter the field info array depending on the column details.
  307. switch($details['type']) {
  308. case 'char':
  309. $field_info['field_type'] = 'text';
  310. $field_info['widget_type'] = 'text_textfield';
  311. $field_info['field_settings']['max_length'] = $details['length'];
  312. break;
  313. case 'varchar':
  314. $field_info['field_type'] = 'text';
  315. $field_info['widget_type'] = 'text_textfield';
  316. $field_info['field_settings']['max_length'] = $details['length'];
  317. break;
  318. case 'text':
  319. $field_info['field_type'] = 'text';
  320. $field_info['widget_type'] = 'text_textarea';
  321. $field_info['field_settings']['max_length'] = 17179869184;
  322. break;
  323. case 'blob':
  324. // not sure how to support a blob field.
  325. continue;
  326. break;
  327. case 'int':
  328. $field_info['field_type'] = 'number_integer';
  329. $field_info['widget_type'] = 'number';
  330. break;
  331. case 'float':
  332. $field_info['field_type'] = 'number_float';
  333. $field_info['widget_type'] = 'number';
  334. $field_info['field_settings']['precision'] = 10;
  335. $field_info['field_settings']['scale'] = 2;
  336. $field_info['field_settings']['decimal_separator'] = '.';
  337. break;
  338. case 'numeric':
  339. $field_info['field_type'] = 'number_decimal';
  340. $field_info['widget_type'] = 'number';
  341. break;
  342. case 'serial':
  343. // Serial fields are most likely not needed as a field.
  344. break;
  345. case 'boolean':
  346. $field_info['field_type'] = 'list_boolean';
  347. $field_info['widget_type'] = 'options_onoff';
  348. $field_info['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  349. break;
  350. case 'datetime':
  351. // Use the Drupal Date and Date API to create the field/widget
  352. $field_info['field_type'] = 'datetime';
  353. $field_info['widget_type'] = 'date_select';
  354. $field_info['widget_settings']['increment'] = 1;
  355. $field_info['widget_settings']['tz_handling'] = 'none';
  356. $field_info['widget_settings']['collapsible'] = TRUE;
  357. // TODO: Add settings so that the minutes increment by 1.
  358. // And turn off the timezone, as the Chado field doesn't support it.
  359. break;
  360. }
  361. return $field_info;
  362. }