tripal_chado.custom_tables.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions for creating, editing and deleting custom tables
  5. * on the Tripal website.
  6. *
  7. */
  8. /**
  9. * Provides a landing page for administrating custom tables.
  10. */
  11. function tripal_custom_table_admin_view() {
  12. $output = '';
  13. // set the breadcrumb
  14. $breadcrumb = array();
  15. $breadcrumb[] = l('Home', '<front>');
  16. $breadcrumb[] = l('Administration', 'admin');
  17. $breadcrumb[] = l('Tripal', 'admin/tripal');
  18. $breadcrumb[] = l('Chado Schema', 'admin/tripal/storage/chado');
  19. $breadcrumb[] = l('Custom Tables', 'admin/tripal/storage/chado/custom_tables');
  20. drupal_set_breadcrumb($breadcrumb);
  21. // Add the view
  22. $view = views_embed_view('tripal_admin_custom_table','default');
  23. if (isset($view)) {
  24. $output .= $view;
  25. }
  26. else {
  27. $output .= '<p>The Tripal Custom Table management system uses primarily views to provide an '
  28. . 'administrative interface. Currently one or more views needed for this '
  29. . 'administrative interface are disabled. <strong>Click each of the following links to '
  30. . 'enable the pertinent views</strong>:</p>';
  31. $output .= '<ul>';
  32. $output .= '<li>'.l('Custom Tables View', 'admin/tripal/storage/chado/custom_tables/views/tables/enable').'</li>';
  33. $output .= '</ul>';
  34. }
  35. return $output;
  36. }
  37. /**
  38. * Renders the tripal_custom_tables_form.
  39. *
  40. */
  41. function tripal_custom_table_new_page() {
  42. $form = drupal_get_form('tripal_custom_tables_form');
  43. return drupal_render($form);
  44. }
  45. /**
  46. * A template function which returns markup to display details for the custom table
  47. *
  48. * @param $table_id
  49. * The unique ID of the custom table
  50. *
  51. */
  52. function tripal_custom_table_view($table_id) {
  53. // get this custom_table details
  54. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
  55. $results = db_query($sql, array(':table_id' => $table_id));
  56. $custom_table = $results->fetchObject();
  57. // create a table with each row containig stats for
  58. // an individual job in the results set.
  59. $return_url = url("admin/tripal/storage/chado/custom_tables");
  60. $output = "<p><a href=\"$return_url\">" . t("Return to list of custom tables") . "</a></p>";
  61. $output .= "<br />";
  62. $output .= "<p>Details for <b>$custom_table->table_name</b>:</p>";
  63. $output .= "<br />";
  64. $output .= "<table class=\"border-table\">";
  65. if ($custom_table->table_name) {
  66. $output .= " <tr>" .
  67. " <th>Table Name</th>" .
  68. " <td>$custom_table->table_name</td>" .
  69. " </tr>";
  70. }
  71. if ($custom_table->schema) {
  72. $output .= " <tr>" .
  73. " <th>Table Field Definitions</th>" .
  74. " <td><pre>" . var_export(unserialize($custom_table->schema), 1) . "</pre></td>" .
  75. " </tr>";
  76. }
  77. // build the URLs using the url function so we can handle installations where
  78. // clean URLs are or are not used
  79. $delete_url = url("admin/tripal/storage/chado/custom_tables/delete/$custom_table->table_id");
  80. $edit_url = url("admin/tripal/storage/chado/custom_tables/edit/$custom_table->table_id");
  81. $output .= "<tr><th>Actions</th>" .
  82. "<td>" .
  83. " <a href='$edit_url'>Edit</a>, " .
  84. " <a href='$delete_url'>Delete</a></td></tr>";
  85. $output .= "</table>";
  86. return $output;
  87. }
  88. /**
  89. * A Form to Create/Edit a Custom table.
  90. *
  91. * @param $form_state
  92. * The current state of the form (Form API)
  93. * @param $table_id
  94. * The unique ID of the Custom table to Edit or NULL if creating a new table
  95. *
  96. * @return
  97. * A form array (Form API)
  98. *
  99. */
  100. function tripal_custom_tables_form($form, &$form_state = NULL, $table_id = NULL) {
  101. if (!$table_id) {
  102. $action = 'Add';
  103. }
  104. else {
  105. $action = 'Edit';
  106. }
  107. // get this requested table
  108. $default_schema = '';
  109. $default_force_drop = 0;
  110. if (strcmp($action, 'Edit')==0) {
  111. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id ";
  112. $results = db_query($sql, array(':table_id' => $table_id));
  113. $custom_table = $results->fetchObject();
  114. // if this is a materialized view then don't allow editing with this function
  115. if (property_exists($custom_table, 'mview_id') and $custom_table->mview_id) {
  116. drupal_set_message("This custom table is a materialized view. Please use the " . l('Materialized View', 'admin/tripal/storage/chado/mviews') . " interface to edit it.", 'error');
  117. drupal_goto("admin/tripal/storage/chado/custom_tables");
  118. return array();
  119. }
  120. // set the default values. If there is a value set in the
  121. // form_state then let's use that, otherwise, we'll pull
  122. // the values from the database
  123. if (array_key_exists('values', $form_state)) {
  124. $default_schema = $form_state['values']['schema'];
  125. $default_force_drop = $form_state['values']['force_drop'];
  126. }
  127. if (!$default_schema) {
  128. $default_schema = var_export(unserialize($custom_table->schema), 1);
  129. $default_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $default_schema);
  130. }
  131. }
  132. $form['return'] = array(
  133. '#type' => 'markup',
  134. '#markup' => "<p>" . l("Return to list of custom tables", "admin/tripal/storage/chado/custom_tables") . "</p>",
  135. );
  136. // Build the form
  137. $form['action'] = array(
  138. '#type' => 'value',
  139. '#value' => $action
  140. );
  141. $form['table_id'] = array(
  142. '#type' => 'value',
  143. '#value' => $table_id
  144. );
  145. $form['instructions'] = array(
  146. '#type' => 'fieldset',
  147. '#title' => 'Instructions',
  148. '#collapsible' => TRUE,
  149. '#collapsed' => TRUE,
  150. );
  151. $form['instructions']['text'] = array(
  152. '#type' => 'item',
  153. '#markup' => '<p>' . t('At times it is necessary to add a custom table
  154. to the Chado schema. These are not offically sanctioned tables but may
  155. be necessary for local data requirements. Avoid creating custom tables
  156. when possible as other GMOD tools may not recognize these tables nor
  157. the data in them. Linker tables or property tables are often a good
  158. candidate for a custom table. For example a table to link stocks and
  159. libraries (e.g. library_stock). Try to model linker or propery tables
  160. after existing tables. If the table already exists it will not be
  161. modified. To force dropping and recreation of the table
  162. click the checkbox below. Tables are defined using the ' .
  163. l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7',
  164. array('attributes' => array('target' => '_blank'))) . '</p>' .
  165. '<p>Please note that table names should be all lower-case.</p>'
  166. ),
  167. );
  168. $form['instructions']['example']= array(
  169. '#type' => 'item',
  170. '#markup' => "Example library_stock table: <pre>
  171. array (
  172. 'table' => 'library_stock',
  173. 'fields' => array (
  174. 'library_stock_id' => array(
  175. 'type' => 'serial',
  176. 'not null' => TRUE,
  177. ),
  178. 'library_id' => array(
  179. 'type' => 'int',
  180. 'not null' => TRUE,
  181. ),
  182. 'stock_id' => array(
  183. 'type' => 'int',
  184. 'not null' => TRUE,
  185. ),
  186. ),
  187. 'primary key' => array(
  188. 'library_stock_id'
  189. ),
  190. 'unique keys' => array(
  191. 'library_stock_c1' => array(
  192. 'library_id',
  193. 'stock_id'
  194. ),
  195. ),
  196. 'foreign keys' => array(
  197. 'library' => array(
  198. 'table' => 'library',
  199. 'columns' => array(
  200. 'library_id' => 'library_id',
  201. ),
  202. ),
  203. 'stock' => array(
  204. 'table' => 'stock',
  205. 'columns' => array(
  206. 'stock_id' => 'stock_id',
  207. ),
  208. ),
  209. ),
  210. )
  211. </pre>",
  212. );
  213. $form['force_drop']= array(
  214. '#type' => 'checkbox',
  215. '#title' => t('Re-create table'),
  216. '#description' => t('Check this box if your table already exists and you would like to drop it and recreate it.'),
  217. '#default_value' => $default_force_drop,
  218. );
  219. $form['schema']= array(
  220. '#type' => 'textarea',
  221. '#title' => t('Schema Array'),
  222. '#description' => t('Please enter the ' . l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7', array('attributes' => array('target' => '_blank'))) . ' compatible array that defines the table.'),
  223. '#required' => FALSE,
  224. '#default_value' => $default_schema,
  225. '#rows' => 25,
  226. );
  227. if ($action == 'Edit') {
  228. $value = 'Save';
  229. }
  230. if ($action == 'Add') {
  231. $value = 'Add';
  232. }
  233. $form['submit'] = array(
  234. '#type' => 'submit',
  235. '#value' => t($value),
  236. '#executes_submit_callback' => TRUE,
  237. );
  238. return $form;
  239. }
  240. /**
  241. * Implements hook_validate().
  242. * Validate the Create/Edit custom table form.
  243. *
  244. */
  245. function tripal_custom_tables_form_validate($form, &$form_state) {
  246. $action = $form_state['values']['action'];
  247. $table_id = $form_state['values']['table_id'];
  248. $schema = $form_state['values']['schema'];
  249. $force_drop = $form_state['values']['force_drop'];
  250. if (!$schema) {
  251. form_set_error($form_state['values']['schema'], t('Schema array field is required.'));
  252. }
  253. // make sure the array is valid
  254. $schema_array = array();
  255. if ($schema) {
  256. $success = preg_match('/^\s*array/', $schema);
  257. if (!$success) {
  258. form_set_error($form_state['values']['schema'],
  259. t("The schema array should begin with the word 'array'."));
  260. }
  261. else {
  262. $success = eval("\$schema_array = $schema;");
  263. if ($success === FALSE) {
  264. $error = error_get_last();
  265. form_set_error('schema', t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  266. }
  267. if (is_array($schema_array) and !array_key_exists('table', $schema_array)) {
  268. form_set_error('schema', t("The schema array must have key named 'table'"));
  269. }
  270. // validate the contents of the array
  271. $error = chado_validate_custom_table_schema($schema_array);
  272. if ($error) {
  273. form_set_error('schema', $error);
  274. }
  275. if ($action == 'Edit') {
  276. // see if the table name has changed. If so, then check to make sure
  277. // it doesn't already exists. We don't want to drop a table we didn't mean to
  278. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
  279. $results = db_query($sql, array(':table_id' => $table_id));
  280. $ct = $results->fetchObject();
  281. if ($ct->table_name != $schema_array['table']) {
  282. $exists = chado_table_exists($schema_array['table']);
  283. if ($exists) {
  284. form_set_error($form_state['values']['schema'],
  285. t("The table name already exists, please choose a different name."));
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Submit the Create/Edit Custom table form
  294. * Implements hook_form_submit().
  295. *
  296. */
  297. function tripal_custom_tables_form_submit($form, &$form_state) {
  298. $ret = array();
  299. $action = $form_state['values']['action'];
  300. $table_id = $form_state['values']['table_id'];
  301. $schema = $form_state['values']['schema'];
  302. $force_drop = $form_state['values']['force_drop'];
  303. $skip_creation = 1;
  304. if ($force_drop) {
  305. $skip_creation = 0;
  306. }
  307. // convert the schema into a PHP array
  308. $schema_arr = array();
  309. eval("\$schema_arr = $schema;");
  310. if (strcmp($action, 'Edit') == 0) {
  311. chado_edit_custom_table($table_id, $schema_arr['table'], $schema_arr, $skip_creation);
  312. }
  313. elseif (strcmp($action, 'Add') == 0) {
  314. chado_create_custom_table($schema_arr['table'], $schema_arr, $skip_creation);
  315. }
  316. else {
  317. drupal_set_message(t("No action performed."));
  318. }
  319. drupal_goto("admin/tripal/storage/chado/custom_tables");
  320. }
  321. /**
  322. * Just a simple form for confirming deletion of a custom table
  323. *
  324. */
  325. function tripal_custom_tables_delete_form($form, &$form_state, $table_id) {
  326. // get details about this table entry
  327. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
  328. $results = db_query($sql, array(':table_id' => $table_id));
  329. $entry = $results->fetchObject();
  330. // if this is a materialized view then don't allow editing with this function
  331. if ($entry->mview_id) {
  332. drupal_set_message("This custom table is a materialized view. Please use the " . l('Materialized View', 'admin/tripal/storage/chado/mviews') . " interface to delete it.", 'error');
  333. drupal_goto("admin/tripal/storage/chado/custom_tables");
  334. return array();
  335. }
  336. $form = array();
  337. $form['table_id'] = array(
  338. '#type' => 'value',
  339. '#value' => $table_id
  340. );
  341. $form['sure'] = array(
  342. '#type' => 'markup',
  343. '#markup' => '<p>Are you sure you want to delete the "' . $entry->table_name . '" custom table?</p>'
  344. );
  345. $form['submit'] = array(
  346. '#type' => 'submit',
  347. '#value' => 'Delete',
  348. );
  349. $form['cancel'] = array(
  350. '#type' => 'submit',
  351. '#value' => 'Cancel',
  352. );
  353. return $form;
  354. }
  355. /**
  356. * form submit hook for the tripal_custom_tables_delete_form form.
  357. *
  358. * @param $form
  359. * @param $form_state
  360. */
  361. function tripal_custom_tables_delete_form_submit($form, &$form_state) {
  362. $action = $form_state['clicked_button']['#value'];
  363. $table_id = $form_state['values']['table_id'];
  364. if (strcmp($action, 'Delete') == 0) {
  365. chado_delete_custom_table($table_id);
  366. }
  367. else {
  368. drupal_set_message(t("No action performed."));
  369. }
  370. drupal_goto("admin/tripal/storage/chado/custom_tables");
  371. }