tripal_entities.admin.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * Launchpad for feature administration.
  4. *
  5. * @ingroup tripal_feature
  6. */
  7. function tripal_entities_admin_view() {
  8. $form = drupal_get_form('tripal_entities_admin_bundles_form');
  9. $output = drupal_render($form) . "<br>[ Image Place Holder for Data Type Summary ]<br>";
  10. // set the breadcrumb
  11. $breadcrumb = array();
  12. $breadcrumb[] = l('Home', '<front>');
  13. $breadcrumb[] = l('Administration', 'admin');
  14. $breadcrumb[] = l('Tripal', 'admin/tripal');
  15. $breadcrumb[] = l('Biological Data', 'admin/tripal/bundles');
  16. drupal_set_breadcrumb($breadcrumb);
  17. /* // Add the view
  18. $view = views_embed_view('tripal_feature_admin_features','default');
  19. if (isset($view)) {
  20. $output .= $view;
  21. }
  22. else {
  23. $output .= '<p>The Feature module uses primarily views to provide an '
  24. . 'administrative interface. Currently one or more views needed for this '
  25. . 'administrative interface are disabled. <strong>Click each of the following links to '
  26. . 'enable the pertinent views</strong>:</p>';
  27. $output .= '<ul>';
  28. $output .= '<li>'.l('Features View', 'admin/tripal/chado/tripal_feature/views/features/enable').'</li>';
  29. $output .= '</ul>';
  30. }
  31. // Add a summary chart.
  32. //-----------------------------------
  33. $output .= theme('tripal_feature_bar_chart_type_organism_summary');
  34. drupal_add_js('
  35. Drupal.behaviors.tripalFeature_moveAdminSummaryChart = {
  36. attach: function (context, settings) {
  37. jQuery("#tripal-feature-admin-summary").insertBefore( jQuery(".view-filters") );
  38. }};
  39. ', 'inline'); */
  40. return $output;
  41. }
  42. /**
  43. *
  44. */
  45. function tripal_entities_content_view() {
  46. $form = drupal_get_form('tripal_entities_content_overview_form');
  47. $output = drupal_render($form);
  48. // set the breadcrumb
  49. $breadcrumb = array();
  50. $breadcrumb[] = l('Home', '<front>');
  51. $breadcrumb[] = l('Administration', 'admin');
  52. drupal_set_breadcrumb($breadcrumb);
  53. return $output;
  54. }
  55. /**
  56. *
  57. * @param unknown $form
  58. * @param unknown $form_state
  59. * @return multitype:
  60. */
  61. function tripal_entities_content_overview_form($form, &$form_state) {
  62. $form = array();
  63. $entities = db_select('tripal_entity', 'td')
  64. ->fields('td')
  65. ->orderBy('created', 'DESC')//ORDER BY created
  66. ->range(0,25)
  67. ->execute();
  68. $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
  69. $rows = array();
  70. while ($entity = $entities->fetchObject()) {
  71. $author = user_load($entity->uid);
  72. $rows[] = array(
  73. l($entity->title, 'BioData/' . $entity->id),
  74. $entity->type,
  75. $entity->bundle,
  76. l($author->name, 'user/' . $entity->uid),
  77. $entity->status == 1 ? 'published' : 'unpublished',
  78. format_date($entity->changed, 'short'),
  79. l('edit', 'BioData/' . $entity->id . '/edit') . '&nbsp;&nbsp;' .
  80. l('delete', 'BioData/' . $entity->id . '/delete')
  81. );
  82. }
  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. $results_table = theme('table', $table_vars);
  93. $form['results'] = array(
  94. '#markup' => $results_table
  95. );
  96. return $form;
  97. }
  98. /**
  99. *
  100. * @param unknown $form
  101. * @param unknown $form_state
  102. * @return multitype:
  103. */
  104. function tripal_entities_admin_bundles_form($form, &$form_state) {
  105. $form = array();
  106. // Set the defaults.
  107. $cv_id = NULL;
  108. $term_name = NULL;
  109. // Set defaults using the form state.
  110. if (array_key_exists('values', $form_state)) {
  111. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
  112. $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
  113. }
  114. // Let the user select the vocabulary and tripal_entity but only if they haven't
  115. // already selected a tripal_entity.
  116. $sql = "
  117. SELECT CV.cv_id, CV.name
  118. FROM {cv} CV
  119. ORDER BY CV.name
  120. ";
  121. $vocabs = chado_query($sql);
  122. $cvs = array();
  123. while ($vocab = $vocabs->fetchObject()) {
  124. $cvs[$vocab->cv_id] = $vocab->name;
  125. }
  126. $form['cv_id'] = array(
  127. '#type' => 'select',
  128. '#title' => t('Vocabulary'),
  129. '#options' => $cvs,
  130. '#required' => FALSE,
  131. '#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.'),
  132. '#default_value' => $cv_id,
  133. '#ajax' => array(
  134. 'callback' => "tripal_entities_admin_bundles_form_ajax_callback",
  135. 'wrapper' => 'tripal_entities_admin_bundles_form',
  136. 'effect' => 'fade',
  137. 'method' => 'replace'
  138. )
  139. );
  140. $form['refresh_bundles'] = array(
  141. '#type' => 'submit',
  142. '#value' => t('Refresh Data Types'),
  143. '#name' => 'refresh_bundles',
  144. );
  145. $form['publish_new_data'] = array(
  146. '#type' => 'submit',
  147. '#value' => t('Publish New Data'),
  148. '#name' => 'publish_new_data',
  149. );
  150. $form['#prefix'] = '<div id="tripal_entities_admin_bundle_form">';
  151. $form['#suffix'] = '</div>';
  152. return $form;
  153. }
  154. /**
  155. * Submit a job to populate the entity tables
  156. * This operation makes available data types in the database publishable
  157. */
  158. function tripal_entities_admin_bundles_form_submit($form, $form_state) {
  159. global $user;
  160. if ($form_state['clicked_button']['#name'] == 'refresh_bundles') {
  161. tripal_add_job('Create publishable data types', 'tripal_entity', 'tripal_entities_populate_entity_tables', array(), $user->uid);
  162. }
  163. if ($form_state['clicked_button']['#name'] == 'publish_new_data') {
  164. }
  165. }
  166. /**
  167. *
  168. * @param unknown $form
  169. * @param unknown $form_state
  170. * @return multitype:
  171. */
  172. function tripal_entities_admin_add_type_form($form, &$form_state) {
  173. $term_name = '';
  174. $num_terms = 0;
  175. $cv_id = '';
  176. // Set defaults using the form state.
  177. if (array_key_exists('storage', $form_state) and
  178. array_key_exists('terms', $form_state['storage'])) {
  179. $terms = $form_state['storage']['terms'];
  180. $num_terms = count($terms);
  181. }
  182. if (array_key_exists('values', $form_state) and
  183. array_key_exists('term_name', $form_state['values'])) {
  184. $term_name = $form_state['values']['term_name'];
  185. }
  186. // If no term has been selected yet then provide the auto complete field.
  187. if ($num_terms == 0) {
  188. $form['term_name'] = array(
  189. '#title' => t('Biological Data Type'),
  190. '#type' => 'textfield',
  191. '#description' => t("Please enter the type of data that you want to add.
  192. Once added, priviledged users can add new records of the selected
  193. type. As you type, suggestions will be provided."),
  194. '#required' => TRUE,
  195. '#default_value' => $term_name,
  196. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  197. );
  198. }
  199. // If the term belongs to more than one vocabulary then add additional fields
  200. // to let the user select the vocabulary.
  201. if ($num_terms > 1) {
  202. $cvs = array();
  203. foreach ($terms as $term) {
  204. $cvs[$term->cv_id->cv_id] = $term->cv_id->name;
  205. }
  206. $form['cv_id'] = array(
  207. '#type' => 'radios',
  208. '#title' => t('Select the appropriate vocabulary'),
  209. '#options' => $cvs,
  210. '#description' => t('The term belongs to more than one vocabulary. Please
  211. indicate the proper vocabulary for the term.')
  212. );
  213. }
  214. // Add in the button for the cases of no terms or too many.
  215. if ($num_terms != 1) {
  216. $form['select_button'] = array(
  217. '#type' => 'submit',
  218. '#value' => t('Use this term'),
  219. '#name' => 'select_cvterm'
  220. );
  221. }
  222. return $form;
  223. }
  224. /**
  225. * Implements hook_validate() for the tripal_entities_admin_publish_form.
  226. *
  227. */
  228. function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
  229. // Check if this term and vocabulary is in the tripal_vocabulary usage tables.
  230. // If not then add it.
  231. if (array_key_exists('clicked_button', $form_state) and
  232. $form_state['clicked_button']['#name'] =='select_cvterm') {
  233. // First, make sure the term is unique. If not then we can't check it.
  234. $term_name = $form_state['values']['term_name'];
  235. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  236. $cvterm = NULL;
  237. // If a term and cv_id are provided then we can look for the term using
  238. // both and we should find a unique term. If only ther term is provided
  239. // we can still look for a unique term but there must only be one.
  240. if ($term_name and !$cv_id) {
  241. $match = array(
  242. 'name' => $term_name,
  243. );
  244. }
  245. else {
  246. $match = array(
  247. 'name' => $term_name,
  248. 'cv_id' => $cv_id,
  249. );
  250. }
  251. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  252. // Add the cvterm to the storage element so we don't have to keep
  253. // looking it up in the submit function or on a rebuild of the form.
  254. $form_state['storage']['terms'] = $terms;
  255. // If we only have one term then we found a unique match and we can do
  256. // some further checking.
  257. if (count($terms) == 1) {
  258. $cvterm = $terms[0];
  259. // Make sure the term is set as published.
  260. tripal_entities_add_term_usage($cvterm, $form_state);
  261. }
  262. // If we do not have any terms then the term provided by the user does not
  263. // exists and we need to provide an error message.
  264. if (count($terms) == 0) {
  265. form_set_error('term_name', t('The term does not exist in this database.'));
  266. }
  267. // If we have more than one term then we need to set an error so that the
  268. // form can provide a list of vocabularies to select from.
  269. if (count($terms) > 1) {
  270. 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.'));
  271. }
  272. }
  273. }
  274. /**
  275. * Implements hook_submit() for the tripal_entities_admin_publish_form.
  276. *
  277. */
  278. function tripal_entities_admin_add_type_form_submit($form, &$form_state) {
  279. if ($form_state['clicked_button']['#name'] =='select_cvterm') {
  280. $cvterm = $form_state['storage']['terms'][0];
  281. $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
  282. // Before we try to add this type, check to see if it already exists
  283. // as a bundle.
  284. $einfo = entity_get_info('BioData');
  285. if (!in_array($bundle_id, array_keys($einfo['bundles']))) {
  286. tripal_entities_add_term_usage($cvterm, $form_state);
  287. tripal_entities_add_bundle($cvterm);
  288. drupal_set_message('New biological data type created. Fields are added automatically to this type.');
  289. $form_state['redirect'] = "admin/structure/BioData";
  290. }
  291. else {
  292. drupal_set_message('This type already exists.', 'warning');
  293. }
  294. }
  295. }
  296. /**
  297. * Adds a vocabulary and term to the Tripal term usage tables.
  298. *
  299. * This function is meant to be called only by the
  300. * tripal_entities_entity_form_validate() function. This code is
  301. * separated to simplify that function. Therefore, if errors occur with adding
  302. * of terms then the form_set_error() is called.
  303. *
  304. * @param $cvterm
  305. */
  306. function tripal_entities_add_term_usage($cvterm, &$form_state) {
  307. // Before creating the entity we mut add records to the tripal_vocabulary
  308. // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
  309. $match = array('cv_id' => $cvterm->cv_id->cv_id);
  310. $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
  311. if (count($vocab) == 0) {
  312. $values = array(
  313. 'cv_id' => $cvterm->cv_id->cv_id,
  314. 'db_id' => $cvterm->dbxref_id->db_id->db_id,
  315. 'publish' => 1,
  316. );
  317. $values = chado_insert_record('tripal_vocabulary', $values);
  318. if (!$values) {
  319. form_set_error('', 'Could not add vocabulary to tripal_vocabluary table.');
  320. return FALSE;
  321. }
  322. // Convert the values array into an object.
  323. $vocab = new stdClass();
  324. $vocab->vocabulary_id = $values['vocabulary_id'];
  325. $vocab->cv_id = $values['cv_id'];
  326. }
  327. else {
  328. // Make sure the vocabulary is set to publish
  329. $values = array('publish' => 1);
  330. chado_update_record('tripal_vocabulary', $match, $values);
  331. $vocab = $vocab[0];
  332. }
  333. // Does this vocabulary have a record in the tripal_vocabulary_usage
  334. // table? If not then add one.
  335. $match = array('vocabulary_id' => $vocab->vocabulary_id);
  336. $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $match);
  337. if (count($vocab_usage) == 0) {
  338. // Look to see if this vocabulary is used as a default for any table. If
  339. // so then we can use that to populate the tripal_vocabulary_usage table.
  340. $default = db_select('tripal_cv_defaults', 't')
  341. ->fields('t')
  342. ->condition('cv_id', $vocab->cv_id)
  343. ->execute()
  344. ->fetchObject();
  345. if ($default) {
  346. $values = array(
  347. 'vocabulary_id' => $vocab->vocabulary_id,
  348. 'data_table' => $default->table_name,
  349. 'type_table' => $default->table_name,
  350. 'field' => $default->field_name,
  351. );
  352. $values = chado_insert_record('tripal_vocabulary_usage', $values);
  353. if (!$values) {
  354. form_set_error('', 'Could not add vocabulary to tripal_vocabulary_usage table.');
  355. return FALSE;
  356. }
  357. }
  358. // If there is no default table then we have an error, and we should
  359. // set a variable so that the form can help the user deal with the problem.
  360. else {
  361. $form_state['storage']['cvterm_has_default'] = FALSE;
  362. form_set_error('', t('There is no default mapping of this term\'s
  363. vocabulary to a table in Chado. Therefore, it is not possible to
  364. determine how to store data of this type.'));
  365. return FALSE;
  366. }
  367. $vocab_usage = new stdClass();
  368. $vocab_usage->vocabulary_id = $values['vocabulary_id'];
  369. $vocab_usage->data_table = $values['data_table'];
  370. $vocab_usage->type_table = $values['type_table'];
  371. $vocab_usage->field = $values['field'];
  372. }
  373. else {
  374. $vocab_usage = $vocab_usage[0];
  375. }
  376. // Now add the tripal_term record if it doesn't already exist.
  377. $match = array(
  378. 'vocabulary_id' => $vocab->vocabulary_id,
  379. 'cvterm_id' => $cvterm->cvterm_id,
  380. );
  381. $term = chado_select_record('tripal_term', array('*'), $match);
  382. if (count($term) == 0) {
  383. $values = array(
  384. 'vocabulary_id' => $vocab->vocabulary_id,
  385. 'cvterm_id' => $cvterm->cvterm_id,
  386. );
  387. $values = chado_insert_record('tripal_term', $values);
  388. if (!$values) {
  389. form_set_error('', 'Could not add term to tripal_term table..');
  390. return FALSE;
  391. }
  392. $term = new stdClass();
  393. $term->term_id = $values['term_id'];
  394. }
  395. else {
  396. $values = array('publish' => 1);
  397. chado_update_record('tripal_term', $match, $values);
  398. $term = $term[0];
  399. }
  400. // Finally, add the tripal_term_usage record if it doesn't already exist.
  401. $match = array('term_id' => $term->term_id);
  402. $options = array('has_record' => TRUE);
  403. if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
  404. $values = array(
  405. 'term_id' => $term->term_id,
  406. 'data_table' => $vocab_usage->data_table,
  407. 'type_table' => $vocab_usage->type_table,
  408. 'field' => $vocab_usage->field,
  409. );
  410. $values = chado_insert_record('tripal_term_usage', $values);
  411. if (!$values) {
  412. form_set_error('', 'Could not add term to tripal_term table..');
  413. return FALSE;
  414. }
  415. }
  416. // Clear the entity cache so that Drupal will read our
  417. // hook_entity_info() implementation which now will have the entities
  418. // described because we set the publish column to 1 in the tripal_term
  419. // table.
  420. global $language;
  421. $langcode = $language->language;
  422. cache_clear_all("entity_info:$langcode", 'cache');
  423. return TRUE;
  424. }
  425. /**
  426. * Implements hook_chado_field_alter.
  427. *
  428. * This function is used when new Chado fields are addd to an Entity. It
  429. * allows modules to customize the field, widget types and settings for
  430. * a field before it is created.
  431. *
  432. * @param $field
  433. */
  434. function hook_chado_field_alter(&$field) {
  435. // TODO: add example code for how to use this hook.
  436. }
  437. /**
  438. *
  439. * @param unknown $form
  440. * @param unknown $form_state
  441. * @return multitype:
  442. */
  443. function tripal_entities_admin_access_form($form, &$form_state) {
  444. $form = array();
  445. return $form;
  446. }