tripal_entities.entity_form.inc 26 KB

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