tripal_entities.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. function tripal_entities_publish() {
  56. $form = drupal_get_form('tripal_entities_entity_form');
  57. $output = drupal_render($form);
  58. // set the breadcrumb
  59. $breadcrumb = array();
  60. $breadcrumb[] = l('Home', '<front>');
  61. $breadcrumb[] = l('Administration', 'admin');
  62. drupal_set_breadcrumb($breadcrumb);
  63. return $output;
  64. }
  65. /**
  66. *
  67. * @param unknown $form
  68. * @param unknown $form_state
  69. * @return multitype:
  70. */
  71. function tripal_entities_content_overview_form($form, &$form_state) {
  72. $form = array();
  73. $entities = db_select('tripal_entity', 'td')
  74. ->fields('td')
  75. ->orderBy('created', 'DESC')//ORDER BY created
  76. ->range(0,25)
  77. ->execute();
  78. $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
  79. $rows = array();
  80. while ($entity = $entities->fetchObject()) {
  81. $author = user_load($entity->uid);
  82. $rows[] = array(
  83. l($entity->title, 'data/' . $entity->id),
  84. $entity->type,
  85. $entity->bundle,
  86. l($author->name, 'user/' . $entity->uid),
  87. $entity->status == 1 ? 'published' : 'unpublished',
  88. format_date($entity->changed, 'short'),
  89. l('edit', 'data/' . $entity->id . '/edit') . '&nbsp;&nbsp;' .
  90. l('delete', 'data/' . $entity->id . '/delete')
  91. );
  92. }
  93. $table_vars = array(
  94. 'header' => $headers,
  95. 'rows' => $rows,
  96. 'attributes' => array(),
  97. 'sticky' => TRUE,
  98. 'caption' => '',
  99. 'colgroups' => array(),
  100. 'empty' => '',
  101. );
  102. $results_table = theme('table', $table_vars);
  103. $form['results'] = array(
  104. '#markup' => $results_table
  105. );
  106. return $form;
  107. }
  108. /**
  109. *
  110. * @param unknown $form
  111. * @param unknown $form_state
  112. * @return multitype:
  113. */
  114. function tripal_entities_admin_bundles_form($form, &$form_state) {
  115. $form = array();
  116. // Set the defaults.
  117. $cv_id = NULL;
  118. $term_name = NULL;
  119. // Set defaults using the form state.
  120. if (array_key_exists('values', $form_state)) {
  121. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
  122. $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
  123. }
  124. // Let the user select the vocabulary and tripal_entity but only if they haven't
  125. // already selected a tripal_entity.
  126. $sql = "
  127. SELECT CV.cv_id, CV.name
  128. FROM {cv} CV
  129. ORDER BY CV.name
  130. ";
  131. $vocabs = chado_query($sql);
  132. $cvs = array();
  133. while ($vocab = $vocabs->fetchObject()) {
  134. $cvs[$vocab->cv_id] = $vocab->name;
  135. }
  136. $form['cv_id'] = array(
  137. '#type' => 'select',
  138. '#title' => t('Vocabulary'),
  139. '#options' => $cvs,
  140. '#required' => FALSE,
  141. '#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.'),
  142. '#default_value' => $cv_id,
  143. '#ajax' => array(
  144. 'callback' => "tripal_entities_admin_bundles_form_ajax_callback",
  145. 'wrapper' => 'tripal_entities_admin_bundles_form',
  146. 'effect' => 'fade',
  147. 'method' => 'replace'
  148. )
  149. );
  150. $form['refresh_bundles'] = array(
  151. '#type' => 'submit',
  152. '#value' => t('Refresh Data Types'),
  153. '#name' => 'refresh_bundles',
  154. );
  155. $form['publish_new_data'] = array(
  156. '#type' => 'submit',
  157. '#value' => t('Publish New Data'),
  158. '#name' => 'publish_new_data',
  159. );
  160. $form['#prefix'] = '<div id="tripal_entities_admin_bundle_form">';
  161. $form['#suffix'] = '</div>';
  162. return $form;
  163. }
  164. /**
  165. * Submit a job to populate the entity tables
  166. * This operation makes available data types in the database publishable
  167. */
  168. function tripal_entities_admin_bundles_form_submit($form, $form_state) {
  169. global $user;
  170. if ($form_state['clicked_button']['#name'] == 'refresh_bundles') {
  171. tripal_add_job('Create publishable data types', 'tripal_entity', 'tripal_entities_populate_entity_tables', array(), $user->uid);
  172. }
  173. if ($form_state['clicked_button']['#name'] == 'publish_new_data') {
  174. }
  175. }
  176. /**
  177. *
  178. * @param unknown $form
  179. * @param unknown $form_state
  180. * @return multitype:
  181. */
  182. function tripal_entities_admin_publish_form($form, &$form_state) {
  183. $form = array();
  184. // Set the defaults.
  185. $cv_id = NULL;
  186. $term_name = NULL;
  187. // Set defaults using the form state.
  188. if (array_key_exists('values', $form_state)) {
  189. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
  190. $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
  191. }
  192. // Let the user select the vocabulary and tripal_entity but only if they haven't
  193. // already selected a tripal_entity.
  194. // $sql = "
  195. // SELECT CV.cv_id, CV.name
  196. // FROM {tripal_vocabulary} TET
  197. // INNER JOIN {cv} CV on CV.cv_id = TET.cv_id
  198. // ORDER BY CV.name
  199. // ";
  200. // $vocabs = chado_query($sql);
  201. // $cvs = array();
  202. // while ($vocab = $vocabs->fetchObject()) {
  203. // $cvs[$vocab->cv_id] = $vocab->name;
  204. // }
  205. $cvs = tripal_get_cv_select_options();
  206. $form['cv_id'] = array(
  207. '#type' => 'select',
  208. '#title' => t('Vocabulary'),
  209. '#options' => $cvs,
  210. '#required' => TRUE,
  211. '#description' => t('Select a vocabulary that contains the term you would like to set as publishable. Only vocabularies that are linked to data are shown.'),
  212. '#default_value' => $cv_id,
  213. '#ajax' => array(
  214. 'callback' => "tripal_entities_admin_publish_form_ajax_callback",
  215. 'wrapper' => 'tripal_entities_admin_publish_form',
  216. 'effect' => 'fade',
  217. 'method' => 'replace'
  218. )
  219. );
  220. // If we have a CV ID then we want to provide an autocomplete field
  221. if ($cv_id) {
  222. $form['cvterm_select']['term_name'] = array(
  223. '#title' => t('Data Type'),
  224. '#type' => 'textfield',
  225. '#description' => t("Please enter the name of the data type to set as publishable. The data type must be a valid term in the selected vocabulary. This field will autopopulate as you type to help find available data types."),
  226. '#required' => TRUE,
  227. '#default_value' => $term_name,
  228. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  229. );
  230. $form['cvterm_select']['select_button'] = array(
  231. '#type' => 'submit',
  232. '#value' => t('Publish Data'),
  233. '#name' => 'publish',
  234. );
  235. }
  236. $form['#prefix'] = '<div id="tripal_entities_admin_publish_form">';
  237. $form['#suffix'] = '</div>';
  238. return $form;
  239. }
  240. /**
  241. * An Ajax callback for the tripal_entities_admin_publish_form..
  242. */
  243. function tripal_entities_admin_publish_form_ajax_callback($form, $form_state) {
  244. // return the form so Drupal can update the content on the page
  245. return $form;
  246. }
  247. /**
  248. * Implements hook_validate() for the tripal_entities_admin_publish_form.
  249. *
  250. */
  251. function tripal_entities_admin_publish_form_validate($form, &$form_state) {
  252. $cv_id = $form_state['values']['cv_id'];
  253. $term_name = $form_state['values']['term_name'];
  254. // Make sure the term_name is a real term in the vocabulary.
  255. $type = tripal_get_cvterm(array(
  256. 'name' => $term_name,
  257. 'cv_id' => $cv_id
  258. ));
  259. if (!$type) {
  260. form_set_error('term_name', t("The data type is not a valid name for the selected vocabulary."));
  261. }
  262. // Make sure the term is used in the site:
  263. $values = array(
  264. 'cvterm_id' => $type->cvterm_id,
  265. );
  266. $bundles = chado_select_record('tripal_term', array('term_id'), $values);
  267. if (count($bundles) == 0) {
  268. form_set_error('term_name', t("The data type, %type, is not associated with data on this site and thus cannot be set as publishable.", array('%type' => $term_name)));
  269. }
  270. // Make sure the term is not already published.
  271. $values = array(
  272. 'cvterm_id' => $type->cvterm_id,
  273. 'publish' => 1,
  274. );
  275. $bundles = chado_select_record('tripal_term', array('term_id'), $values);
  276. if (count($bundles) > 0) {
  277. form_set_error('term_name', t("This data type is already set as publishable."));
  278. }
  279. }
  280. /**
  281. * Implements hook_submit() for the tripal_entities_admin_publish_form.
  282. *
  283. */
  284. function tripal_entities_admin_publish_form_submit($form, &$form_state) {
  285. $cv_id = $form_state['values']['cv_id'];
  286. $term_name = $form_state['values']['term_name'];
  287. // Get the data type using the $term_name and $cv_id.
  288. $cvterm = chado_generate_var('cvterm', array('cv_id' => $cv_id, 'name' => $term_name));
  289. // Start the transaction.
  290. $transaction = db_transaction();
  291. try {
  292. // We don't need to check if the vocabulary is used because the
  293. // form only shows those vocabs that are used.
  294. // Mark this entity as published.
  295. $match = array('cv_id' => $cv_id);
  296. $values = array('publish' => 1);
  297. $success = chado_update_record('tripal_vocabulary', $match, $values);
  298. if (!$success) {
  299. throw new Exception('Cannot set the vocabulary as publishable');
  300. }
  301. // We have already checked in the validate if the term already exists
  302. // as a bundle. So, if we're here we can enable it.
  303. $match = array('cvterm_id' => $cvterm->cvterm_id);
  304. $values = array('publish' => 1);
  305. $success = chado_update_record('tripal_term', $match, $values);
  306. if (!$success) {
  307. throw new Exception('Cannot set the data type as publishable');
  308. }
  309. // Create the bundle name and entity type name.
  310. $bundle_name = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  311. $entity_type_name = $cvterm->dbxref_id->db_id->name;
  312. // Clear the entity cache so that Drupal will read our
  313. // hook_entity_info() implementation which now will have the entities
  314. // described because we set the publish column to 1 in the tripal_term
  315. // table.
  316. global $language;
  317. $langcode = $language->language;
  318. cache_clear_all("entity_info:$langcode", 'cache');
  319. // The TripalBundle Entity manages the bundles we have available.
  320. // Therefore, we need to add a new entity for each bundle "type".
  321. $vals = array(
  322. 'label' => $bundle_name . ' (' . $cvterm->name . ')',
  323. 'type' => $entity_type_name,
  324. 'bundle' => $bundle_name,
  325. 'data' => serialize(array()),
  326. 'module' => 'tripal_entities'
  327. );
  328. $tripal_bundle = new TripalBundle($vals, $entity_type_name . '_bundle');
  329. $tripal_bundle->save();
  330. // Allow modules to now add fields to the bundle
  331. module_invoke_all('add_bundle_fields', $entity_type_name, $bundle_name, $cvterm);
  332. drupal_set_message(t('Data type, %type, is now set as publishable.', array('%type' => $term_name)));
  333. }
  334. catch (Exception $e) {
  335. $transaction->rollback();
  336. drupal_set_message('Failure publishing this data type: ' . $e->getMessage(), 'error');
  337. watchdog_exception('trp_entities', $e);
  338. }
  339. }
  340. /**
  341. * Implements hook_chado_field_alter.
  342. *
  343. * This function is used when new Chado fields are addd to an Entity. It
  344. * allows modules to customize the field, widget types and settings for
  345. * a field before it is created.
  346. *
  347. * @param $field
  348. */
  349. function hook_chado_field_alter(&$field) {
  350. // TODO: add example code for how to use this hook.
  351. }
  352. /**
  353. *
  354. * @param unknown $form
  355. * @param unknown $form_state
  356. * @return multitype:
  357. */
  358. function tripal_entities_admin_access_form($form, &$form_state) {
  359. $form = array();
  360. return $form;
  361. }