tripal_entities.entity_form.inc 25 KB

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