tripal_entities.entity_form.inc 24 KB

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