tripal_entities.entity_form.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. $vocab->cv_id = $values['cv_id'];
  266. }
  267. else {
  268. // Make sure the vocabulary is set to publish
  269. $values = array('publish' => 1);
  270. chado_update_record('tripal_vocabulary', $match, $values);
  271. $vocab = $vocab[0];
  272. }
  273. // Does this vocabulary have a record in the tripal_vocabulary_usage
  274. // table? If not then add one.
  275. $match = array('vocabulary_id' => $vocab->vocabulary_id);
  276. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $match);
  277. if (count($vocab_usage) == 0) {
  278. // Look to see if this vocabulary is used as a default for any table. If
  279. // so then we can use that to populate the tripal_vocabulary_usage table.
  280. $default = db_select('tripal_cv_defaults', 't')
  281. ->fields('t')
  282. ->condition('cv_id', $vocab->cv_id)
  283. ->execute()
  284. ->fetchObject();
  285. if ($default) {
  286. $values = array(
  287. 'vocabulary_id' => $vocab->vocabulary_id,
  288. 'data_table' => $default->table_name,
  289. 'type_table' => $default->table_name,
  290. 'field' => $default->field_name,
  291. );
  292. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  293. if (!$values) {
  294. form_set_error('', 'Could not add vocabulary to tripal_vocabulary_usage table.');
  295. return FALSE;
  296. }
  297. }
  298. // If there is no default table then we have an error, and we should
  299. // set a variable so that the form can help the user deal with the problem.
  300. else {
  301. $form_state['storage']['cvterm_has_default'] = FALSE;
  302. form_set_error('', t('There is no default mapping of this term\'s
  303. vocabulary to a table in Chado. Therefore, it is not possible to
  304. determine how to store data of this type.'));
  305. return FALSE;
  306. }
  307. $vocab_usage = new stdClass();
  308. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  309. $vocab_usage->data_table = $values['data_table'];
  310. $vocab_usage->type_table = $values['type_table'];
  311. $vocab_usage->field = $values['field'];
  312. }
  313. else {
  314. $vocab_usage = $vocab_usage[0];
  315. }
  316. // Now add the tripal_term record if it doesn't already exist.
  317. $match = array(
  318. 'vocabulary_id' => $vocab->vocabulary_id,
  319. 'cvterm_id' => $cvterm->cvterm_id,
  320. );
  321. $term = chado_select_record('tripal_term', array('*'), $match);
  322. if (count($term) == 0) {
  323. $values = array(
  324. 'vocabulary_id' => $vocab->vocabulary_id,
  325. 'cvterm_id' => $cvterm->cvterm_id,
  326. );
  327. $values = chado_insert_record('tripal_term', $values);
  328. if (!$values) {
  329. form_set_error('', 'Could not add term to tripal_term table..');
  330. return FALSE;
  331. }
  332. $term = new stdClass();
  333. $term->term_id = $values['term_id'];
  334. }
  335. else {
  336. $values = array('publish' => 1);
  337. chado_update_record('tripal_term', $match, $values);
  338. $term = $term[0];
  339. }
  340. // Finally, add the tripal_term_usage record if it doesn't already exist.
  341. $match = array('term_id' => $term->term_id);
  342. $options = array('has_record' => TRUE);
  343. if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
  344. $values = array(
  345. 'term_id' => $term->term_id,
  346. 'data_table' => $vocab_usage->data_table,
  347. 'type_table' => $vocab_usage->type_table,
  348. 'field' => $vocab_usage->field,
  349. );
  350. $values = chado_insert_record('tripal_term_usage', $values);
  351. if (!$values) {
  352. form_set_error('', 'Could not add term to tripal_term table..');
  353. return FALSE;
  354. }
  355. }
  356. // Clear the entity cache so that Drupal will read our
  357. // hook_entity_info() implementation which now will have the entities
  358. // described because we set the publish column to 1 in the tripal_term
  359. // table.
  360. global $language;
  361. $langcode = $language->language;
  362. cache_clear_all("entity_info:$langcode", 'cache');
  363. return TRUE;
  364. }
  365. /**
  366. * Implements hook_submit() for the tripal_entities_entity_form.
  367. */
  368. function tripal_entities_entity_form_submit($form, &$form_state) {
  369. if ($form_state['clicked_button']['#name'] =='cancel') {
  370. if (array_key_exists('id', $form_state['values'])) {
  371. $entity_id = $form_state['values']['entity_id'];
  372. $form_state['redirect'] = "data/$entity_id";
  373. }
  374. else {
  375. $form_state['redirect'] = "admin/structure/tripal_entity";
  376. }
  377. return;
  378. }
  379. if ($form_state['clicked_button']['#name'] =='select_cvterm') {
  380. // Check to see if the entity already exists. If not then create it.
  381. $cvterm = $form_state['storage']['terms'][0];
  382. $einfo = entity_get_info($cvterm->dbxref_id->db_id->name);
  383. tripal_entities_add_bundle($cvterm);
  384. $form_state['rebuild'] = TRUE;
  385. }
  386. if ($form_state['clicked_button']['#name'] =='update_data' or
  387. $form_state['clicked_button']['#name'] =='add_data') {
  388. // Use the Entity API to get the entity from the form state, then
  389. // attach the fields and save.
  390. $entity_type = $form_state['values']['type'];
  391. $entity = entity_ui_controller($entity_type)->entityFormSubmitBuildEntity($form, $form_state);
  392. $entity->save();
  393. $form_state['redirect'] = "data/$entity->id";
  394. }
  395. if ($form_state['clicked_button']['#name'] =='delete_data') {
  396. $entity_id = $form_state['values']['entity_id'];
  397. $form_state['redirect'] = 'data/' .$entity_id .'/delete';
  398. }
  399. }
  400. /**
  401. * Form callback: confirmation form for deleting a tripal_entity.
  402. *
  403. * @param $tripal_entity The
  404. * tripal_entity to delete
  405. *
  406. * @see confirm_form()
  407. */
  408. function tripal_entities_entity_delete_form($form, &$form_state, $entity) {
  409. $form_state['entity'] = $entity;
  410. $form['#submit'][] = 'tripal_entities_entity_delete_form_submit';
  411. $form = confirm_form($form,
  412. t('Click the delete button below to confirm deletion of the record titled: %title',
  413. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  414. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  415. return $form;
  416. }
  417. /**
  418. * Submit callback for tripal_entity_delete_form
  419. */
  420. function tripal_entities_entity_delete_form_submit($form, &$form_state) {
  421. $entity = $form_state['entity'];
  422. $entity_controller = new TripalEntityController($entity->type);
  423. if ($entity_controller->delete($entity)) {
  424. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  425. $form_state['redirect'] = 'admin/content/tripal_entitys';
  426. }
  427. else {
  428. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  429. }
  430. }
  431. /**
  432. * Implements hook_submit() for the tripal_entities_admin_publish_form.
  433. *
  434. */
  435. function tripal_entities_add_bundle($cvterm) {
  436. // Create the bundle name and entity type name.
  437. $type = $cvterm->dbxref_id->db_id->name;
  438. $bundle_name = $type . '_' . $cvterm->dbxref_id->accession;
  439. $entity_type_name = $cvterm->dbxref_id->db_id->name;
  440. // Check to see if this bundle exists. If not then create it
  441. $bundle = db_select('tripal_bundle', 't')
  442. ->fields('t')
  443. ->condition('type', $type)
  444. ->condition('bundle', $bundle_name)
  445. ->execute()
  446. ->fetchObject();
  447. if (!$bundle) {
  448. // The TripalBundle Entity manages the bundles we have available.
  449. // Therefore, we need to add a new entity for each bundle "type".
  450. $vals = array(
  451. 'label' => $bundle_name . ' (' . $cvterm->name . ')',
  452. 'type' => $entity_type_name,
  453. 'bundle' => $bundle_name,
  454. 'data' => serialize(array()),
  455. 'module' => 'tripal_entities'
  456. );
  457. $tripal_bundle = new TripalBundle($vals, $entity_type_name . '_bundle');
  458. $tripal_bundle->save();
  459. // Allow modules to now add fields to the bundle
  460. module_invoke_all('add_bundle_fields', $entity_type_name, $bundle_name, $cvterm);
  461. }
  462. }
  463. /**
  464. *
  465. * @param $table
  466. * @param $entity_type
  467. * @param $bundle_name
  468. */
  469. function tripal_entities_add_bundle_fields($entity_type_name, $bundle_name, $cvterm) {
  470. // Get the list of tables where this cvterm is used.
  471. $match = array('cvterm_id' => $cvterm->cvterm_id);
  472. $term = chado_select_record('tripal_term', array('*'), $match);
  473. $values = array('term_id' => $term[0]->term_id);
  474. $tables = chado_select_record('tripal_term_usage', array('*'), $values);
  475. // Iterate through the tables.
  476. foreach ($tables as $table) {
  477. $tablename = $table->data_table;
  478. $type_table = $table->type_table;
  479. $type_field = $table->field;
  480. // We only want to look at base tables.
  481. if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
  482. $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
  483. $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
  484. preg_match('/_cvterm$/', $tablename)) {
  485. continue;
  486. }
  487. // Iterate through the columns of the table and see if fields have been
  488. // created for each one. If not, then create them.
  489. $schema = chado_get_schema($tablename);
  490. $columns = $schema['fields'];
  491. foreach ($columns as $column_name => $details) {
  492. $field_name = $tablename . '__' . $column_name;
  493. $field = field_info_field($field_name);
  494. // Skip the primary key field.
  495. if ($column_name == $schema['primary key'][0]) {
  496. continue;
  497. }
  498. // Skip the type field.
  499. if ($tablename == $type_table and $column_name == $type_field) {
  500. continue;
  501. }
  502. // Determine if the field is required.
  503. $is_required = 0;
  504. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  505. $is_required = array_key_exists('default', $details) ? 0 : 1;
  506. }
  507. // Determine what type of field this should be.
  508. // Drupal data types are: https://www.drupal.org/node/159605.
  509. // Field types are here: https://www.drupal.org/node/1879542
  510. // Create an array with information about this field.
  511. $field_info = array(
  512. 'field_type' => '',
  513. 'widget_type' => '',
  514. 'field_settings' => array(),
  515. 'widget_settings' => array('display_label' => 1),
  516. 'description' => '',
  517. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  518. 'chado_table' => $tablename,
  519. 'chado_column' => $column_name
  520. );
  521. // Alter the field info array dependiing on the column details.
  522. switch($details['type']) {
  523. case 'char':
  524. $field_info['field_type'] = 'text';
  525. $field_info['widget_type'] = 'text_textfield';
  526. $field_info['field_settings']['max_length'] = $details['length'];
  527. break;
  528. case 'varchar':
  529. $field_info['field_type'] = 'text';
  530. $field_info['widget_type'] = 'text_textfield';
  531. $field_info['field_settings']['max_length'] = $details['length'];
  532. break;
  533. case 'text':
  534. $field_info['field_type'] = 'text';
  535. $field_info['widget_type'] = 'text_textarea';
  536. $field_info['field_settings']['max_length'] = 17179869184;
  537. break;
  538. case 'blob':
  539. // not sure how to support a blob field.
  540. continue;
  541. break;
  542. case 'int':
  543. $field_info['field_type'] = 'number_integer';
  544. $field_info['widget_type'] = 'number';
  545. break;
  546. case 'float':
  547. $field_info['field_type'] = 'number_float';
  548. $field_info['widget_type'] = 'number';
  549. $field_info['field_settings']['precision'] = 10;
  550. $field_info['field_settings']['scale'] = 2;
  551. $field_info['field_settings']['decimal_separator'] = '.';
  552. break;
  553. case 'numeric':
  554. $field_info['field_type'] = 'number_decimal';
  555. $field_info['widget_type'] = 'number';
  556. break;
  557. case 'serial':
  558. // Serial fields are most likely not needed as a field.
  559. break;
  560. case 'boolean':
  561. $field_info['field_type'] = 'list_boolean';
  562. $field_info['widget_type'] = 'options_onoff';
  563. $field_info['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  564. break;
  565. case 'datetime':
  566. // Use the Drupal Date and Date API to create the field/widget
  567. $field_info['field_type'] = 'datetime';
  568. $field_info['widget_type'] = 'date_select';
  569. $field_info['widget_settings']['increment'] = 1;
  570. $field_info['widget_settings']['tz_handling'] = 'none';
  571. // TODO: Add settings so that the minutes increment by 1.
  572. // And turn off the timezone, as the Chado field doesn't support it.
  573. break;
  574. }
  575. // If we don't have a field type then we don't need to create a field.
  576. if (!$field_info['field_type']) {
  577. // If we don't have a field type but it is required and doesn't have
  578. // a default value then we are in trouble.
  579. if ($is_required and !array_key_exists('default', $details)) {
  580. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  581. array('%table' => $tablename, '%field' => $column_name, '%type' => $details['type'])));
  582. }
  583. continue;
  584. }
  585. // If this field is a foreign key field then we will have a special custom
  586. // field provided by Tripal.
  587. $is_fk = FALSE;
  588. if (array_key_exists('foreign keys', $schema)) {
  589. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  590. if (array_key_exists($column_name, $fk_details['columns'])) {
  591. $is_fk = TRUE;
  592. }
  593. }
  594. }
  595. // Allow other modules to alter the field information array.
  596. drupal_alter('chado_field', $field_info);
  597. // If the field doesn't exist then create it.
  598. if (!$field) {
  599. $field = array(
  600. 'field_name' => $field_name,
  601. 'type' => $field_info['field_type'],
  602. 'cardinality' => 1,
  603. 'locked' => FALSE,
  604. 'storage' => array(
  605. 'type' => 'field_chado_storage'
  606. ),
  607. 'settings' => $field_info['field_settings'],
  608. );
  609. field_create_field($field);
  610. }
  611. // Attach the field to the bundle.
  612. $field_instance = array(
  613. 'field_name' => $field_name,
  614. 'label' => $field_info['label'],
  615. 'description' => $field_info['description'],
  616. 'widget' => array(
  617. 'type' => $field_info['widget_type'],
  618. 'settings' => $field_info['widget_settings'],
  619. ),
  620. 'entity_type' => $entity_type_name,
  621. 'required' => $is_required,
  622. 'settings' => $field_info['field_settings'],
  623. 'bundle' => $bundle_name,
  624. );
  625. field_create_instance($field_instance);
  626. }
  627. }
  628. }