tripal_entities.admin.inc 25 KB

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