tripal_entities.entity_form.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_entities_entity_form($form, &$form_state, $term_name = '', $entity = NULL) {
  6. // Set the defaults.
  7. $cv_id = NULL;
  8. $cvterm = NULL;
  9. $do_sync = 0;
  10. $terms = array();
  11. $num_terms = 0;
  12. // Set defaults if an entity was provided.
  13. if ($entity) {
  14. drupal_set_title('Edit ' . $entity->title);
  15. $id = $entity->id;
  16. $values = array('cvterm_id' => $entity->cvterm_id);
  17. $cvterm = chado_generate_var('cvterm', $values);
  18. $cv_id = $cvterm->cv_id->cv_id;
  19. $term_name = $cvterm->name;
  20. $terms[] = $cvterm;
  21. $num_terms = 1;
  22. }
  23. // Set defaults using the form state.
  24. if (array_key_exists('storage', $form_state) and
  25. array_key_exists('terms', $form_state['storage'])) {
  26. $terms = $form_state['storage']['terms'];
  27. $num_terms = count($terms);
  28. }
  29. // If we have a term name but the $form_state['storage']['terms'] isn't set
  30. // then the term_name is being passed in, and we should try to get the
  31. // terms.
  32. else if ($term_name) {
  33. $match = array(
  34. 'name' => $term_name,
  35. );
  36. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  37. $form_state['storage']['terms'] = $terms;
  38. $num_terms = count($terms);
  39. // Make sure the term is set as published.
  40. if ($num_terms == 1) {
  41. tripal_entities_add_term_usage($terms[0], $form_state);
  42. }
  43. $einfo = entity_get_info($terms[0]->dbxref_id->db_id->name);
  44. tripal_entities_add_bundle($terms[0]);
  45. }
  46. if (array_key_exists('values', $form_state) and
  47. array_key_exists('term_name', $form_state['values'])) {
  48. $term_name = $form_state['values']['term_name'];
  49. }
  50. if (array_key_exists('values', $form_state) and
  51. array_key_exists('do_sync', $form_state['values'])) {
  52. $do_sync = $form_state['values']['do_sync'];
  53. }
  54. // If no term has been selected yet then provide the auto complete field.
  55. if ($num_terms != 1) {
  56. $cvterms = tripal_entities_get_published_terms_as_select_options($cv_id);
  57. $form['term_name'] = array(
  58. '#title' => t('Data Type'),
  59. '#type' => 'textfield',
  60. '#description' => t("Please enter the type of data that you want to publish. As you type, suggestions will be provided."),
  61. '#required' => TRUE,
  62. '#default_value' => $term_name,
  63. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  64. );
  65. // If we are not editing an existing entity then provide a radio button
  66. // to allow the user to add a new record or to sync existing records.
  67. if (!$entity) {
  68. $action = array(0 => t('Publish a new record'), 1 => t('Publish one or more existing records'));
  69. $form['do_sync'] = array(
  70. '#type' => 'radios',
  71. '#title' => t('Publish Action'),
  72. '#default_value' => $do_sync,
  73. '#options' => $action,
  74. );
  75. }
  76. }
  77. // If the term belongs to more than one vocabulary then add additional fields
  78. // to let the user select the vocabulary.
  79. if ($num_terms > 1) {
  80. $cvs = array();
  81. foreach ($terms as $term) {
  82. $cvs[$term->cv_id->cv_id] = $term->cv_id->name;
  83. }
  84. $form['cv)id'] = array(
  85. '#type' => 'radios',
  86. '#title' => t('Select the appropriate vocabulary'),
  87. '#options' => $cvs,
  88. '#description' => t('The term belongs to more than one vocabulary. Please
  89. indicate the proper vocabulary for the term.')
  90. );
  91. }
  92. // Add in the button for the cases of no terms or too many.
  93. if ($num_terms != 1) {
  94. $form['select_button'] = array(
  95. '#type' => 'submit',
  96. '#value' => t('Use this term'),
  97. '#name' => 'select_cvterm'
  98. );
  99. }
  100. // If we only have one cvterm then the user has provided a unique
  101. // term and we can either allow them to add a new entry or sync existing
  102. // records.
  103. if ($num_terms == 1){
  104. $cvterm = $terms[0];
  105. $bundle_id = $cvterm->dbxref_id->db_id->name .'_' .$cvterm->dbxref_id->accession;
  106. $form['cv_id'] = array(
  107. '#type' => 'hidden',
  108. '#value' => $cvterm->cv_id->cv_id
  109. );
  110. $form['type'] = array(
  111. '#type' => 'hidden',
  112. '#value' => $cvterm->dbxref_id->db_id->name
  113. );
  114. $form['term_name'] = array(
  115. '#type' => 'hidden',
  116. '#value' => $cvterm->name
  117. );
  118. $form['cvterm_id'] = array(
  119. '#type' => 'hidden',
  120. '#value' => $cvterm->cvterm_id
  121. );
  122. $form['bundle'] = array(
  123. '#type' => 'hidden',
  124. '#value' => $bundle_id
  125. );
  126. $form['details'] = array(
  127. '#type' => 'item',
  128. '#title' => 'Record Type',
  129. '#markup' => '(' . $cvterm->cv_id->name . ') ' . $cvterm->name,
  130. '#weight' => -1000,
  131. );
  132. if(!$do_sync) {
  133. tripal_entities_entity_form_add_new($bundle_id, $cvterm, $form, $form_state, $entity);
  134. }
  135. else {
  136. tripal_entities_entity_form_add_sync($bundle_id, $cvterm, $form, $form_state, $entity);
  137. }
  138. // Add a prefix and a suffix around this form so that fields may
  139. // replace the entire form on an ajax call if they want.
  140. $form['#prefix'] = "<div id='$bundle_id-entity-form'>";
  141. $form['#suffix'] = "</div>";
  142. }
  143. return $form;
  144. }
  145. /**
  146. * Adds fields to the tripal_entities_entity_form for syncing an entity.
  147. */
  148. function tripal_entities_entity_form_add_sync($bundle_id, $cvterm, &$form, &$form_state, $entity) {
  149. $form['todo'] = array(
  150. '#type' => 'item',
  151. '#markup' => t('TODO: Add a form for allowing the user to filter by fields'),
  152. );
  153. }
  154. /**
  155. * Adds fields to the tripal_entities_entity_form for editing/adding an entity.
  156. */
  157. function tripal_entities_entity_form_add_new($bundle_id, $cvterm, &$form, &$form_state, $entity) {
  158. // Add a vertical tabs element
  159. $form['ev_tabs'] = array(
  160. '#type' => 'vertical_tabs',
  161. '#weight' => 999,
  162. );
  163. // If the entity doesn't exist then create one.
  164. if (!$entity) {
  165. $entity = entity_get_controller($cvterm->dbxref_id->db_id->name)->create(array('bundle' => $bundle_id));
  166. field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state);
  167. $form['add_button'] = array(
  168. '#type' => 'submit',
  169. '#value' => t('Save'),
  170. '#name' => 'add_data',
  171. '#weight' => 1000
  172. );
  173. }
  174. else {
  175. field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state);
  176. $form['entity_id'] = array(
  177. '#type' => 'hidden',
  178. '#value' => $entity->id
  179. );
  180. $form['update_button'] = array(
  181. '#type' => 'submit',
  182. '#value' => t('Update'),
  183. '#name' => 'update_data',
  184. '#weight' => 1000
  185. );
  186. $form['delete_button'] = array(
  187. '#type' => 'submit',
  188. '#value' => t('Delete'),
  189. '#name' => 'delete_data',
  190. '#weight' => 1001
  191. );
  192. }
  193. // The entity object must be added to the $form_state in order for
  194. // the Entity API to work. It must have a key of the entity name.
  195. $form_state[$cvterm->dbxref_id->db_id->name] = $entity;
  196. }
  197. /**
  198. * An Ajax callback for the tripal_entities_entity_form.
  199. */
  200. function tripal_entities_entity_form_ajax_callback($form, $form_state) {
  201. // return the form so Drupal can update the content on the page
  202. return $form;
  203. }
  204. /**
  205. * Implements hook_validate() for the tripal_entities_entity_form.
  206. */
  207. function tripal_entities_entity_form_validate($form, &$form_state) {
  208. // Check if this term and vocabulary is in the tripal_vocabulary usage tables.
  209. // If not then add it.
  210. if (array_key_exists('clicked_button', $form_state) and
  211. $form_state['clicked_button']['#name'] =='select_cvterm') {
  212. // First, make sure the term is unique. If not then we can't check it.
  213. $term_name = $form_state['values']['term_name'];
  214. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  215. $cvterm = NULL;
  216. // If a term and cv_id are provided then we can look for the term using
  217. // both and we should find a unique term. If only ther term is provided
  218. // we can still look for a unique term but there must only be one.
  219. if ($term_name and !$cv_id) {
  220. $match = array(
  221. 'name' => $term_name,
  222. );
  223. }
  224. else {
  225. $match = array(
  226. 'name' => $term_name,
  227. 'cv_id' => $cv_id,
  228. );
  229. }
  230. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  231. // Add the cvterm to the storage element so we don't have to keep
  232. // looking it up in the submit function or on a rebuild of the form.
  233. $form_state['storage']['terms'] = $terms;
  234. // If we only have one term then we found a unique match and we can do
  235. // some further checking.
  236. if (count($terms) == 1) {
  237. $cvterm = $terms[0];
  238. // Make sure the term is set as published.
  239. tripal_entities_add_term_usage($cvterm, $form_state);
  240. }
  241. // If we do not have any terms then the term provided by the user does not
  242. // exists and we need to provide an error message.
  243. if (count($terms) == 0) {
  244. form_set_error('term_name', t('The term does not exist in this database.'));
  245. }
  246. // If we have more than one term then we need to set an error so that the
  247. // form can provide a list of vocabularies to select from.
  248. if (count($terms) > 1) {
  249. form_set_error('', t('The term is not unique. A list of vocabularies has been provided where this term is present. Please select the appropriate one.'));
  250. }
  251. }
  252. if (array_key_exists('clicked_button', $form_state) and
  253. $form_state['clicked_button']['#name'] =='add_data') {
  254. $tripal_entity = (object) $form_state['values'];
  255. $entity_type = $form_state['values']['type'];
  256. field_attach_form_validate($entity_type, $tripal_entity, $form, $form_state);
  257. }
  258. }
  259. /**
  260. * Adds a vocabulary and term to the Tripal term usage tables.
  261. *
  262. * This function is meant to be called only by the
  263. * tripal_entities_entity_form_validate() function. This code is
  264. * separated to simplify that function. Therefore, if errors occur with adding
  265. * of terms then the form_set_error() is called.
  266. *
  267. * @param $cvterm
  268. */
  269. function tripal_entities_add_term_usage($cvterm, &$form_state) {
  270. // Before creating the entity we mut add records to the tripal_vocabulary
  271. // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
  272. $match = array('cv_id' => $cvterm->cv_id->cv_id);
  273. $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
  274. if (count($vocab) == 0) {
  275. $values = array(
  276. 'cv_id' => $cvterm->cv_id->cv_id,
  277. 'db_id' => $cvterm->dbxref_id->db_id->db_id,
  278. 'publish' => 1,
  279. );
  280. $values = chado_insert_record('tripal_vocabulary', $values);
  281. if (!$values) {
  282. form_set_error('', 'Could not add vocabulary to tripal_vocabluary table.');
  283. return FALSE;
  284. }
  285. // Convert the values array into an object.
  286. $vocab = new stdClass();
  287. $vocab->vocabulary_id = $values['vocabulary_id'];
  288. $vocab->cv_id = $values['cv_id'];
  289. }
  290. else {
  291. // Make sure the vocabulary is set to publish
  292. $values = array('publish' => 1);
  293. chado_update_record('tripal_vocabulary', $match, $values);
  294. $vocab = $vocab[0];
  295. }
  296. // Does this vocabulary have a record in the tripal_vocabulary_usage
  297. // table? If not then add one.
  298. $match = array('vocabulary_id' => $vocab->vocabulary_id);
  299. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $match);
  300. if (count($vocab_usage) == 0) {
  301. // Look to see if this vocabulary is used as a default for any table. If
  302. // so then we can use that to populate the tripal_vocabulary_usage table.
  303. $default = db_select('tripal_cv_defaults', 't')
  304. ->fields('t')
  305. ->condition('cv_id', $vocab->cv_id)
  306. ->execute()
  307. ->fetchObject();
  308. if ($default) {
  309. $values = array(
  310. 'vocabulary_id' => $vocab->vocabulary_id,
  311. 'data_table' => $default->table_name,
  312. 'type_table' => $default->table_name,
  313. 'field' => $default->field_name,
  314. );
  315. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  316. if (!$values) {
  317. form_set_error('', 'Could not add vocabulary to tripal_vocabulary_usage table.');
  318. return FALSE;
  319. }
  320. }
  321. // If there is no default table then we have an error, and we should
  322. // set a variable so that the form can help the user deal with the problem.
  323. else {
  324. $form_state['storage']['cvterm_has_default'] = FALSE;
  325. form_set_error('', t('There is no default mapping of this term\'s
  326. vocabulary to a table in Chado. Therefore, it is not possible to
  327. determine how to store data of this type.'));
  328. return FALSE;
  329. }
  330. $vocab_usage = new stdClass();
  331. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  332. $vocab_usage->data_table = $values['data_table'];
  333. $vocab_usage->type_table = $values['type_table'];
  334. $vocab_usage->field = $values['field'];
  335. }
  336. else {
  337. $vocab_usage = $vocab_usage[0];
  338. }
  339. // Now add the tripal_term record if it doesn't already exist.
  340. $match = array(
  341. 'vocabulary_id' => $vocab->vocabulary_id,
  342. 'cvterm_id' => $cvterm->cvterm_id,
  343. );
  344. $term = chado_select_record('tripal_term', array('*'), $match);
  345. if (count($term) == 0) {
  346. $values = array(
  347. 'vocabulary_id' => $vocab->vocabulary_id,
  348. 'cvterm_id' => $cvterm->cvterm_id,
  349. );
  350. $values = chado_insert_record('tripal_term', $values);
  351. if (!$values) {
  352. form_set_error('', 'Could not add term to tripal_term table..');
  353. return FALSE;
  354. }
  355. $term = new stdClass();
  356. $term->term_id = $values['term_id'];
  357. }
  358. else {
  359. $values = array('publish' => 1);
  360. chado_update_record('tripal_term', $match, $values);
  361. $term = $term[0];
  362. }
  363. // Finally, add the tripal_term_usage record if it doesn't already exist.
  364. $match = array('term_id' => $term->term_id);
  365. $options = array('has_record' => TRUE);
  366. if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
  367. $values = array(
  368. 'term_id' => $term->term_id,
  369. 'data_table' => $vocab_usage->data_table,
  370. 'type_table' => $vocab_usage->type_table,
  371. 'field' => $vocab_usage->field,
  372. );
  373. $values = chado_insert_record('tripal_term_usage', $values);
  374. if (!$values) {
  375. form_set_error('', 'Could not add term to tripal_term table..');
  376. return FALSE;
  377. }
  378. }
  379. // Clear the entity cache so that Drupal will read our
  380. // hook_entity_info() implementation which now will have the entities
  381. // described because we set the publish column to 1 in the tripal_term
  382. // table.
  383. global $language;
  384. $langcode = $language->language;
  385. cache_clear_all("entity_info:$langcode", 'cache');
  386. return TRUE;
  387. }
  388. /**
  389. * Implements hook_submit() for the tripal_entities_entity_form.
  390. */
  391. function tripal_entities_entity_form_submit($form, &$form_state) {
  392. if ($form_state['clicked_button']['#name'] =='cancel') {
  393. if (array_key_exists('id', $form_state['values'])) {
  394. $entity_id = $form_state['values']['entity_id'];
  395. $form_state['redirect'] = "data/$entity_id";
  396. }
  397. else {
  398. $form_state['redirect'] = "admin/structure/tripal_entity";
  399. }
  400. return;
  401. }
  402. if ($form_state['clicked_button']['#name'] =='select_cvterm') {
  403. // Check to see if the entity already exists. If not then create it.
  404. $cvterm = $form_state['storage']['terms'][0];
  405. $einfo = entity_get_info($cvterm->dbxref_id->db_id->name);
  406. tripal_entities_add_bundle($cvterm);
  407. $form_state['rebuild'] = TRUE;
  408. }
  409. if ($form_state['clicked_button']['#name'] =='update_data' or
  410. $form_state['clicked_button']['#name'] =='add_data') {
  411. // Use the Entity API to get the entity from the form state, then
  412. // attach the fields and save.
  413. $entity_type = $form_state['values']['type'];
  414. $entity = entity_ui_controller($entity_type)->entityFormSubmitBuildEntity($form, $form_state);
  415. $entity->save();
  416. $form_state['redirect'] = "data/$entity->id";
  417. }
  418. if ($form_state['clicked_button']['#name'] =='delete_data') {
  419. $entity_id = $form_state['values']['entity_id'];
  420. $form_state['redirect'] = 'data/' .$entity_id .'/delete';
  421. }
  422. }
  423. /**
  424. * Form callback: confirmation form for deleting a tripal_entity.
  425. *
  426. * @param $tripal_entity The
  427. * tripal_entity to delete
  428. *
  429. * @see confirm_form()
  430. */
  431. function tripal_entities_entity_delete_form($form, &$form_state, $entity) {
  432. $form_state['entity'] = $entity;
  433. $form['#submit'][] = 'tripal_entities_entity_delete_form_submit';
  434. $form = confirm_form($form,
  435. t('Click the delete button below to confirm deletion of the record titled: %title',
  436. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  437. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  438. return $form;
  439. }
  440. /**
  441. * Submit callback for tripal_entity_delete_form
  442. */
  443. function tripal_entities_entity_delete_form_submit($form, &$form_state) {
  444. $entity = $form_state['entity'];
  445. $entity_controller = new TripalEntityController($entity->type);
  446. if ($entity_controller->delete($entity)) {
  447. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  448. $form_state['redirect'] = 'admin/content/tripal_entitys';
  449. }
  450. else {
  451. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  452. }
  453. }
  454. /**
  455. * Implements hook_submit() for the tripal_entities_admin_publish_form.
  456. *
  457. */
  458. function tripal_entities_add_bundle($cvterm) {
  459. // Create the bundle name and entity type name.
  460. $type = $cvterm->dbxref_id->db_id->name;
  461. $bundle_name = $type . '_' . $cvterm->dbxref_id->accession;
  462. $entity_type_name = $cvterm->dbxref_id->db_id->name;
  463. // Check to see if this bundle exists. If not then create it
  464. $bundle = db_select('tripal_bundle', 't')
  465. ->fields('t')
  466. ->condition('type', $type)
  467. ->condition('bundle', $bundle_name)
  468. ->execute()
  469. ->fetchObject();
  470. if (!$bundle) {
  471. // The TripalBundle Entity manages the bundles we have available.
  472. // Therefore, we need to add a new entity for each bundle "type".
  473. $vals = array(
  474. 'label' => $bundle_name . ' (' . $cvterm->name . ')',
  475. 'type' => $entity_type_name,
  476. 'bundle' => $bundle_name,
  477. 'data' => serialize(array()),
  478. 'module' => 'tripal_entities'
  479. );
  480. $tripal_bundle = new TripalBundle($vals, $entity_type_name . '_bundle');
  481. $tripal_bundle->save();
  482. }
  483. // Allow modules to now add fields to the bundle
  484. module_invoke_all('add_bundle_fields', $entity_type_name, $bundle_name, $cvterm);
  485. }
  486. /**
  487. * Implements hook_add_bundle_fields().
  488. *
  489. * @param $entity_type_name
  490. * @param $bundle_name
  491. * @param $cvterm
  492. */
  493. function tripal_entities_add_bundle_fields($entity_type_name, $bundle_name, $cvterm) {
  494. // Adds the fields for the base table to the entity.
  495. tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_name, $cvterm);
  496. // Check to see if there are any kv-property tables associated to this
  497. // base table. If so, add the fields for that type of table.
  498. tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_name, 'featureprop');
  499. }
  500. /**
  501. * Adds the fields for a kv-property table fields
  502. *
  503. * @param $entity_type_name
  504. * @param $bundle_name
  505. * @param $kv_table
  506. */
  507. function tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_name, $kv_table) {
  508. // First add a generic property field so that users can add new proeprty types.
  509. $field_name = $kv_table;
  510. // Initialize the field array.
  511. $field_info = array(
  512. 'field_type' => 'kvproperty_adder',
  513. 'widget_type' => 'tripal_fields_kvproperty_adder_widget',
  514. 'field_settings' => array(),
  515. 'widget_settings' => array('display_label' => 1),
  516. 'description' => '',
  517. 'label' => 'Additional Properties',
  518. 'is_required' => 0,
  519. );
  520. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  521. }
  522. /**
  523. * Adds the fields for the base table to the entity.
  524. */
  525. function tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_name, $cvterm) {
  526. // Get the list of tables where this cvterm is used.
  527. $match = array('cvterm_id' => $cvterm->cvterm_id);
  528. $term = chado_select_record('tripal_term', array('*'), $match);
  529. $values = array('term_id' => $term[0]->term_id);
  530. $tables = chado_select_record('tripal_term_usage', array('*'), $values);
  531. // Iterate through the tables.
  532. foreach ($tables as $table) {
  533. $table_name = $table->data_table;
  534. $type_table = $table->type_table;
  535. $type_field = $table->field;
  536. // We only want to look at base tables.
  537. if ($table_name == 'cvterm_dbxref' || $table_name == 'cvterm_relationship' ||
  538. $table_name == 'cvtermpath' || $table_name == 'cvtermprop' || $table_name == 'chadoprop' ||
  539. $table_name == 'cvtermsynonym' || preg_match('/_relationship$/', $table_name) ||
  540. preg_match('/_cvterm$/', $table_name)) {
  541. continue;
  542. }
  543. // Iterate through the columns of the table and see if fields have been
  544. // created for each one. If not, then create them.
  545. $schema = chado_get_schema($table_name);
  546. $columns = $schema['fields'];
  547. foreach ($columns as $column_name => $details) {
  548. $field_name = $table_name . '__' . $column_name;
  549. // Skip the primary key field.
  550. if ($column_name == $schema['primary key'][0]) {
  551. continue;
  552. }
  553. // Skip the type field.
  554. if ($table_name == $type_table and $column_name == $type_field) {
  555. continue;
  556. }
  557. // Get the field defaults for this column.
  558. $field_info = tripal_entities_get_table_column_field_default($table_name, $schema, $column_name);
  559. // Determine if the field is required.
  560. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  561. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  562. }
  563. // If we don't have a field type then we don't need to create a field.
  564. if (!$field_info['field_type']) {
  565. // If we don't have a field type but it is required and doesn't have
  566. // a default value then we are in trouble.
  567. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  568. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  569. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  570. }
  571. continue;
  572. }
  573. // If this field is a foreign key field then we will have a special custom
  574. // field provided by Tripal.
  575. $is_fk = FALSE;
  576. if (array_key_exists('foreign keys', $schema)) {
  577. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  578. if (array_key_exists($column_name, $fk_details['columns'])) {
  579. $is_fk = TRUE;
  580. }
  581. }
  582. }
  583. // Add the field to the bundle.
  584. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  585. }
  586. }
  587. }
  588. /**
  589. * Returns a $field_info array for a field based on a databaes column.
  590. *
  591. */
  592. function tripal_entities_get_table_column_field_default($table_name, $schema, $column_name) {
  593. $details = $schema['fields'][$column_name];
  594. // Create an array with information about this field.
  595. $field_info = array(
  596. 'field_type' => '',
  597. 'widget_type' => '',
  598. 'field_settings' => array(
  599. 'chado_table' => $table_name,
  600. 'chado_column' => $column_name,
  601. ),
  602. 'widget_settings' => array('display_label' => 1),
  603. 'description' => '',
  604. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  605. 'is_required' => 0,
  606. );
  607. // Alter the field info array depending on the column details.
  608. switch($details['type']) {
  609. case 'char':
  610. $field_info['field_type'] = 'text';
  611. $field_info['widget_type'] = 'text_textfield';
  612. $field_info['field_settings']['max_length'] = $details['length'];
  613. break;
  614. case 'varchar':
  615. $field_info['field_type'] = 'text';
  616. $field_info['widget_type'] = 'text_textfield';
  617. $field_info['field_settings']['max_length'] = $details['length'];
  618. break;
  619. case 'text':
  620. $field_info['field_type'] = 'text';
  621. $field_info['widget_type'] = 'text_textarea';
  622. $field_info['field_settings']['max_length'] = 17179869184;
  623. break;
  624. case 'blob':
  625. // not sure how to support a blob field.
  626. continue;
  627. break;
  628. case 'int':
  629. $field_info['field_type'] = 'number_integer';
  630. $field_info['widget_type'] = 'number';
  631. break;
  632. case 'float':
  633. $field_info['field_type'] = 'number_float';
  634. $field_info['widget_type'] = 'number';
  635. $field_info['field_settings']['precision'] = 10;
  636. $field_info['field_settings']['scale'] = 2;
  637. $field_info['field_settings']['decimal_separator'] = '.';
  638. break;
  639. case 'numeric':
  640. $field_info['field_type'] = 'number_decimal';
  641. $field_info['widget_type'] = 'number';
  642. break;
  643. case 'serial':
  644. // Serial fields are most likely not needed as a field.
  645. break;
  646. case 'boolean':
  647. $field_info['field_type'] = 'list_boolean';
  648. $field_info['widget_type'] = 'options_onoff';
  649. $field_info['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  650. break;
  651. case 'datetime':
  652. // Use the Drupal Date and Date API to create the field/widget
  653. $field_info['field_type'] = 'datetime';
  654. $field_info['widget_type'] = 'date_select';
  655. $field_info['widget_settings']['increment'] = 1;
  656. $field_info['widget_settings']['tz_handling'] = 'none';
  657. $field_info['widget_settings']['collapsible'] = TRUE;
  658. // TODO: Add settings so that the minutes increment by 1.
  659. // And turn off the timezone, as the Chado field doesn't support it.
  660. break;
  661. }
  662. return $field_info;
  663. }