tripal_chado.custom_tables.inc

Contains functions for creating, editing and deleting custom tables on the Tripal website.

File

tripal_chado/includes/tripal_chado.custom_tables.inc
View source
  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. // set the breadcrumb
  102. $breadcrumb = array();
  103. $breadcrumb[] = l('Home', '<front>');
  104. $breadcrumb[] = l('Administration', 'admin');
  105. $breadcrumb[] = l('Tripal', 'admin/tripal');
  106. $breadcrumb[] = l('Chado Schema', 'admin/tripal/storage/chado');
  107. $breadcrumb[] = l('Custom Tables', 'admin/tripal/storage/chado/custom_tables');
  108. drupal_set_breadcrumb($breadcrumb);
  109. if (!$table_id) {
  110. $action = 'Add';
  111. }
  112. else {
  113. $action = 'Edit';
  114. }
  115. // get this requested table
  116. $default_schema = '';
  117. $default_force_drop = 0;
  118. if (strcmp($action, 'Edit')==0) {
  119. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id ";
  120. $results = db_query($sql, array(':table_id' => $table_id));
  121. $custom_table = $results->fetchObject();
  122. // if this is a materialized view then don't allow editing with this function
  123. if (property_exists($custom_table, 'mview_id') and $custom_table->mview_id) {
  124. 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');
  125. drupal_goto("admin/tripal/storage/chado/custom_tables");
  126. return array();
  127. }
  128. // set the default values. If there is a value set in the
  129. // form_state then let's use that, otherwise, we'll pull
  130. // the values from the database
  131. if (array_key_exists('values', $form_state)) {
  132. $default_schema = $form_state['values']['schema'];
  133. $default_force_drop = $form_state['values']['force_drop'];
  134. }
  135. if (!$default_schema) {
  136. $default_schema = var_export(unserialize($custom_table->schema), 1);
  137. $default_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $default_schema);
  138. }
  139. }
  140. // Build the form
  141. $form['action'] = array(
  142. '#type' => 'value',
  143. '#value' => $action
  144. );
  145. $form['table_id'] = array(
  146. '#type' => 'value',
  147. '#value' => $table_id
  148. );
  149. $form['instructions'] = array(
  150. '#type' => 'fieldset',
  151. '#title' => 'Instructions',
  152. '#collapsible' => TRUE,
  153. '#collapsed' => TRUE,
  154. );
  155. $form['instructions']['text'] = array(
  156. '#type' => 'item',
  157. '#markup' => '<p>' . t('At times it is necessary to add a custom table
  158. to the Chado schema. These are not offically sanctioned tables but may
  159. be necessary for local data requirements. Avoid creating custom tables
  160. when possible as other GMOD tools may not recognize these tables nor
  161. the data in them. Linker tables or property tables are often a good
  162. candidate for a custom table. For example a table to link stocks and
  163. libraries (e.g. library_stock). Try to model linker or propery tables
  164. after existing tables. If the table already exists it will not be
  165. modified. To force dropping and recreation of the table
  166. click the checkbox below. Tables are defined using the ' .
  167. l('Drupal Schema API', 'https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7',
  168. array('attributes' => array('target' => '_blank'))) . '</p>' .
  169. '<p>Please note that table names should be all lower-case.</p>'
  170. ),
  171. );
  172. $form['instructions']['example']= array(
  173. '#type' => 'item',
  174. '#markup' => "Example library_stock table: <pre>
  175. array (
  176. 'table' => 'library_stock',
  177. 'fields' => array (
  178. 'library_stock_id' => array(
  179. 'type' => 'serial',
  180. 'not null' => TRUE,
  181. ),
  182. 'library_id' => array(
  183. 'type' => 'int',
  184. 'not null' => TRUE,
  185. ),
  186. 'stock_id' => array(
  187. 'type' => 'int',
  188. 'not null' => TRUE,
  189. ),
  190. ),
  191. 'primary key' => array(
  192. 'library_stock_id'
  193. ),
  194. 'unique keys' => array(
  195. 'library_stock_c1' => array(
  196. 'library_id',
  197. 'stock_id'
  198. ),
  199. ),
  200. 'foreign keys' => array(
  201. 'library' => array(
  202. 'table' => 'library',
  203. 'columns' => array(
  204. 'library_id' => 'library_id',
  205. ),
  206. ),
  207. 'stock' => array(
  208. 'table' => 'stock',
  209. 'columns' => array(
  210. 'stock_id' => 'stock_id',
  211. ),
  212. ),
  213. ),
  214. )
  215. </pre>",
  216. );
  217. $form['force_drop']= array(
  218. '#type' => 'checkbox',
  219. '#title' => t('Re-create table'),
  220. '#description' => t('Check this box if your table already exists and you would like to drop it and recreate it.'),
  221. '#default_value' => $default_force_drop,
  222. );
  223. $form['schema']= array(
  224. '#type' => 'textarea',
  225. '#title' => t('Schema Array'),
  226. '#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.'),
  227. '#required' => FALSE,
  228. '#default_value' => $default_schema,
  229. '#rows' => 25,
  230. );
  231. if ($action == 'Edit') {
  232. $value = 'Save';
  233. }
  234. if ($action == 'Add') {
  235. $value = 'Add';
  236. }
  237. $form['submit'] = array(
  238. '#type' => 'submit',
  239. '#value' => t($value),
  240. '#executes_submit_callback' => TRUE,
  241. );
  242. $form['cancel'] = array(
  243. '#type' => 'markup',
  244. '#markup' => l('Cancel', 'admin/tripal/storage/chado/custom_tables'),
  245. );
  246. return $form;
  247. }
  248. /**
  249. * Implements hook_validate().
  250. * Validate the Create/Edit custom table form.
  251. *
  252. */
  253. function tripal_custom_tables_form_validate($form, &$form_state) {
  254. $action = $form_state['values']['action'];
  255. $table_id = $form_state['values']['table_id'];
  256. $schema = $form_state['values']['schema'];
  257. $force_drop = $form_state['values']['force_drop'];
  258. if (!$schema) {
  259. form_set_error($form_state['values']['schema'], t('Schema array field is required.'));
  260. }
  261. // make sure the array is valid
  262. $schema_array = array();
  263. if ($schema) {
  264. $success = preg_match('/^\s*array/', $schema);
  265. if (!$success) {
  266. form_set_error($form_state['values']['schema'],
  267. t("The schema array should begin with the word 'array'."));
  268. }
  269. else {
  270. $success = eval("\$schema_array = $schema;");
  271. if ($success === FALSE) {
  272. $error = error_get_last();
  273. form_set_error('schema', t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  274. }
  275. if (is_array($schema_array) and !array_key_exists('table', $schema_array)) {
  276. form_set_error('schema', t("The schema array must have key named 'table'"));
  277. }
  278. // validate the contents of the array
  279. $error = chado_validate_custom_table_schema($schema_array);
  280. if ($error) {
  281. form_set_error('schema', $error);
  282. }
  283. if ($action == 'Edit') {
  284. // see if the table name has changed. If so, then check to make sure
  285. // it doesn't already exists. We don't want to drop a table we didn't mean to
  286. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
  287. $results = db_query($sql, array(':table_id' => $table_id));
  288. $ct = $results->fetchObject();
  289. if ($ct->table_name != $schema_array['table']) {
  290. $exists = chado_table_exists($schema_array['table']);
  291. if ($exists) {
  292. form_set_error($form_state['values']['schema'],
  293. t("The table name already exists, please choose a different name."));
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. /**
  301. * Submit the Create/Edit Custom table form
  302. * Implements hook_form_submit().
  303. *
  304. */
  305. function tripal_custom_tables_form_submit($form, &$form_state) {
  306. $ret = array();
  307. $action = $form_state['values']['action'];
  308. $table_id = $form_state['values']['table_id'];
  309. $schema = $form_state['values']['schema'];
  310. $force_drop = $form_state['values']['force_drop'];
  311. $skip_creation = 1;
  312. if ($force_drop) {
  313. $skip_creation = 0;
  314. }
  315. // convert the schema into a PHP array
  316. $schema_arr = array();
  317. eval("\$schema_arr = $schema;");
  318. if (strcmp($action, 'Edit') == 0) {
  319. chado_edit_custom_table($table_id, $schema_arr['table'], $schema_arr, $skip_creation);
  320. }
  321. elseif (strcmp($action, 'Add') == 0) {
  322. chado_create_custom_table($schema_arr['table'], $schema_arr, $skip_creation);
  323. }
  324. else {
  325. drupal_set_message(t("No action performed."));
  326. }
  327. drupal_goto("admin/tripal/storage/chado/custom_tables");
  328. }
  329. /**
  330. * Just a simple form for confirming deletion of a custom table
  331. *
  332. */
  333. function tripal_custom_tables_delete_form($form, &$form_state, $table_id) {
  334. // get details about this table entry
  335. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
  336. $results = db_query($sql, array(':table_id' => $table_id));
  337. $entry = $results->fetchObject();
  338. // if this is a materialized view then don't allow editing with this function
  339. if ($entry->mview_id) {
  340. 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');
  341. drupal_goto("admin/tripal/storage/chado/custom_tables");
  342. return array();
  343. }
  344. $form = array();
  345. $form['table_id'] = array(
  346. '#type' => 'value',
  347. '#value' => $table_id
  348. );
  349. $form['sure'] = array(
  350. '#type' => 'markup',
  351. '#markup' => '<p>Are you sure you want to delete the "' . $entry->table_name . '" custom table?</p>'
  352. );
  353. $form['submit'] = array(
  354. '#type' => 'submit',
  355. '#value' => 'Delete',
  356. );
  357. $form['cancel'] = array(
  358. '#type' => 'submit',
  359. '#value' => 'Cancel',
  360. );
  361. return $form;
  362. }
  363. /**
  364. * form submit hook for the tripal_custom_tables_delete_form form.
  365. *
  366. * @param $form
  367. * @param $form_state
  368. */
  369. function tripal_custom_tables_delete_form_submit($form, &$form_state) {
  370. $action = $form_state['clicked_button']['#value'];
  371. $table_id = $form_state['values']['table_id'];
  372. if (strcmp($action, 'Delete') == 0) {
  373. chado_delete_custom_table($table_id);
  374. }
  375. else {
  376. drupal_set_message(t("No action performed."));
  377. }
  378. drupal_goto("admin/tripal/storage/chado/custom_tables");
  379. }