custom_tables.inc

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

File

tripal_core/includes/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. * @ingroup tripal_core
  8. */
  9. /**
  10. * A template function which returns markup to display details for the custom table
  11. *
  12. * @param $table_id
  13. * The unique ID of the custom table
  14. *
  15. * @ingroup tripal_core
  16. */
  17. function tripal_custom_table_view($table_id) {
  18. // get this custom_table details
  19. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
  20. $custom_table = db_fetch_object(db_query($sql, $table_id));
  21. // create a table with each row containig stats for
  22. // an individual job in the results set.
  23. $return_url = url("admin/tripal/custom_tables/");
  24. $output .= "<p><a href=\"$return_url\">" . t("Return to list of custom tables") . "</a></p>";
  25. $output .= "<br />";
  26. $output .= "<p>Details for <b>$custom_table->table_name</b>:</p>";
  27. $output .= "<br />";
  28. $output .= "<table class=\"border-table\">";
  29. if ($custom_table->table_name) {
  30. $output .= " <tr>".
  31. " <th>Table Name</th>".
  32. " <td>$custom_table->table_name</td>".
  33. " </tr>";
  34. }
  35. if ($custom_table->schema) {
  36. $output .= " <tr>".
  37. " <th>Table Field Definitions</th>".
  38. " <td><pre>" . var_export(unserialize($custom_table->schema),1) . "</pre></td>".
  39. " </tr>";
  40. }
  41. // build the URLs using the url function so we can handle installations where
  42. // clean URLs are or are not used
  43. $delete_url = url("admin/tripal/custom_tables/action/delete/$custom_table->table_id");
  44. $edit_url = url("admin/tripal/custom_tables/edit/$custom_table->table_id");
  45. $output .= "<tr><th>Actions</th>".
  46. "<td>".
  47. " <a href='$edit_url'>Edit</a>, ".
  48. " <a href='$delete_url'>Delete</a></td></tr>";
  49. $output .= "</table>";
  50. return $output;
  51. }
  52. /**
  53. * A template function to render a listing of all Custom tables
  54. *
  55. * @ingroup tripal_core
  56. */
  57. function tripal_custom_tables_list() {
  58. $header = array('', 'Table Name', 'Description');
  59. $rows = array();
  60. $custom_tables = db_query("SELECT * FROM {tripal_custom_tables} ORDER BY table_name");
  61. while ($custom_table = db_fetch_object($custom_tables)) {
  62. $rows[] = array(
  63. l(t('View'), "admin/tripal/custom_tables/view/$custom_table->table_id") ." | ".
  64. l(t('Edit'), "admin/tripal/custom_tables/edit/$custom_table->table_id") ." | ".
  65. $custom_table->table_name,
  66. $custom_table->comment,
  67. l(t('Delete'), "admin/tripal/custom_tables/action/delete/$custom_table->table_id"),
  68. );
  69. }
  70. $rows[] = array(
  71. 'data' => array(
  72. array('data' => l(t('Create a new custom table.'), "admin/tripal/custom_tables/new"),
  73. 'colspan' => 6),
  74. )
  75. );
  76. $page = theme('table', $header, $rows);
  77. return $page;
  78. }
  79. /**
  80. * A Form to Create/Edit a Custom table
  81. *
  82. * @param $form_state
  83. * The current state of the form (Form API)
  84. * @param $table_id
  85. * The unique ID of the Custom table to Edit or NULL if creating a new table
  86. *
  87. * @return
  88. * A form array (Form API)
  89. *
  90. * @ingroup tripal_core
  91. */
  92. function tripal_custom_tables_form(&$form_state = NULL, $table_id = NULL) {
  93. if (!$table_id) {
  94. $action = 'Add';
  95. }
  96. else {
  97. $action = 'Edit';
  98. }
  99. // get this requested table
  100. if (strcmp($action, 'Edit')==0) {
  101. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d ";
  102. $custom_table = db_fetch_object(db_query($sql, $table_id));
  103. // set the default values. If there is a value set in the
  104. // form_state then let's use that, otherwise, we'll pull
  105. // the values from the database
  106. $default_schema = $form_state['values']['schema'];
  107. $default_force_drop = $form_state['values']['force_drop'];
  108. if (!$default_table_name) {
  109. $default_table = $custom_table->table_name;
  110. }
  111. if (!$default_schema) {
  112. $default_schema = var_export(unserialize($custom_table->schema),1);
  113. $default_schema = preg_replace('/=>\s+\n\s+array/','=> array', $default_schema);
  114. }
  115. }
  116. // Build the form
  117. $form['action'] = array(
  118. '#type' => 'value',
  119. '#value' => $action
  120. );
  121. $form['table_id'] = array(
  122. '#type' => 'value',
  123. '#value' => $table_id
  124. );
  125. $form['instructions']= array(
  126. '#type' => 'markup',
  127. '#value' => t('At times it is necessary to add a custom table to the Chado schema.
  128. These are not offically sanctioned tables but may be necessary for local data requirements.
  129. Avoid creating custom tables when possible as other GMOD tools may not recognize these tables
  130. nor the data in them. Linker tables or property tables are often a good candidate for
  131. a custom table. For example a table to link stocks and libraries (e.g. library_stock) would be
  132. a good custom table. Try to model linker or propery tables after existing tables. If the
  133. table already exists it will not be modified. To force dropping and recreation of the table
  134. click the checkbox below.
  135. '),
  136. );
  137. $form['force_drop']= array(
  138. '#type' => 'checkbox',
  139. '#title' => t('Re-create table'),
  140. '#description' => t('Check this box if your table already exists and you would like to drop it and recreate it.'),
  141. '#default_value' => $default_force_drop,
  142. );
  143. $form['schema']= array(
  144. '#type' => 'textarea',
  145. '#title' => t('Schema Array'),
  146. '#description' => t('Please enter the Drupal Schema API compatible array that defines the table.'),
  147. '#required' => FALSE,
  148. '#default_value' => $default_schema,
  149. '#rows' => 25,
  150. );
  151. if ($action == 'Edit') {
  152. $value = 'Save';
  153. }
  154. if ($action == 'Add') {
  155. $value = 'Add';
  156. }
  157. $form['submit'] = array(
  158. '#type' => 'submit',
  159. '#value' => t($value),
  160. '#executes_submit_callback' => TRUE,
  161. );
  162. $form['#redirect'] = 'admin/tripal/custom_tables';
  163. $form['example']= array(
  164. '#type' => 'markup',
  165. '#value' => "<br>Example library_stock table: <pre>
  166. array (
  167. 'table' => 'library_stock',
  168. 'fields' => array (
  169. 'library_stock_id' => array(
  170. 'type' => serial,
  171. 'not null' => TRUE,
  172. ),
  173. 'library_id' => array(
  174. 'type' => 'int',
  175. 'not null' => TRUE,
  176. ),
  177. 'stock_id' => array(
  178. 'type' => 'int',
  179. 'not null' => TRUE,
  180. ),
  181. ),
  182. 'primary key' => array(
  183. 'library_stock_id'
  184. ),
  185. 'unique keys' => array(
  186. 'library_stock_c1' => array(
  187. 'library_id',
  188. 'stock_id'
  189. ),
  190. ),
  191. 'foreign keys' => array(
  192. 'library' => array(
  193. 'table' => 'library',
  194. 'columns' => array(
  195. 'library_id' => 'library_id',
  196. ),
  197. ),
  198. 'stock' => array(
  199. 'table' => 'stock',
  200. 'columns' => array(
  201. 'stock_id' => 'stock_id',
  202. ),
  203. ),
  204. ),
  205. )
  206. </pre>",
  207. );
  208. return $form;
  209. }
  210. /**
  211. * Validate the Create/Edit custom table form
  212. * Implements hook_form_validate().
  213. *
  214. * @ingroup tripal_core
  215. */
  216. function tripal_custom_tables_form_validate($form, &$form_state) {
  217. $action = $form_state['values']['action'];
  218. $table_id = $form_state['values']['table_id'];
  219. $schema = $form_state['values']['schema'];
  220. $force_drop = $form_state['values']['force_drop'];
  221. if (!$schema) {
  222. form_set_error($form_state['values']['schema'],
  223. t('Schema array field is required.'));
  224. }
  225. // make sure the array is valid
  226. $schema_array = array();
  227. if ($schema) {
  228. $success = preg_match('/^\s*array/', $schema);
  229. if (!$success) {
  230. form_set_error($form_state['values']['schema'],
  231. t("The schema array should begin with the word 'array'."));
  232. }
  233. else {
  234. $success = eval("\$schema_array = $schema;");
  235. if ($success === FALSE) {
  236. $error = error_get_last();
  237. form_set_error($form_state['values']['schema'],
  238. t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  239. }
  240. if (is_array($schema_array) and !array_key_exists('table', $schema_array)) {
  241. form_set_error($form_state['values']['schema'],
  242. t("The schema array must have key named 'table'"));
  243. }
  244. if ($action == 'Edit') {
  245. // see if the table name has changed. If so, then check to make sure
  246. // it doesn't already exists. We don't want to drop a table we didn't mean to
  247. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
  248. $ct = db_fetch_object(db_query($sql, $table_id));
  249. if ($ct->table_name != $schema_array['table']) {
  250. $previous_db = tripal_db_set_active('chado');
  251. $exists = db_table_exists($schema_array['table']);
  252. tripal_db_set_active($previous_db);
  253. if ($exists) {
  254. form_set_error($form_state['values']['schema'],
  255. t("The table name already exists, please choose a different name."));
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /**
  263. * Submit the Create/Edit Custom table form
  264. * Implements hook_form_submit().
  265. *
  266. * @ingroup tripal_core
  267. */
  268. function tripal_custom_tables_form_submit($form, &$form_state) {
  269. $ret = array();
  270. $action = $form_state['values']['action'];
  271. $table_id = $form_state['values']['table_id'];
  272. $schema = $form_state['values']['schema'];
  273. $force_drop = $form_state['values']['force_drop'];
  274. $skip_creation = 1;
  275. if ($force_drop) {
  276. $skip_creation = 0;
  277. }
  278. // conver the schema into a PHP array
  279. $schema_arr = array();
  280. eval("\$schema_arr = $schema;");
  281. if (strcmp($action, 'Edit') == 0) {
  282. tripal_core_edit_custom_table($table_id, $schema_arr['table'], $schema_arr, $skip_creation);
  283. }
  284. elseif (strcmp($action, 'Add') == 0) {
  285. tripal_core_create_custom_table($ret, $schema_arr['table'], $schema_arr, $skip_creation);
  286. }
  287. else {
  288. drupal_set_message(t("No action performed."));
  289. }
  290. return '';
  291. }
  292. /**
  293. * Does the specified action for the specified custom table
  294. *
  295. * @param $op
  296. * The action to be taken. Currenly only delete is available
  297. * @param $table_id
  298. * The unique ID of the custom table for the action to be performed on
  299. * @param $redirect
  300. * TRUE/FALSE depending on whether you want to redirect the user to admin/tripal/custom_tables
  301. *
  302. * @ingroup tripal_core
  303. */
  304. function tripal_custom_tables_action($op, $table_id, $redirect = FALSE) {
  305. global $user;
  306. $args = array("$table_id");
  307. if (!$table_id) {
  308. return '';
  309. }
  310. // get this table details
  311. $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
  312. $custom_table = db_fetch_object(db_query($sql, $table_id));
  313. if ($op == 'delete') {
  314. // remove the entry from the tripal_custom tables table
  315. $sql = "DELETE FROM {tripal_custom_tables} ".
  316. "WHERE table_id = $table_id";
  317. db_query($sql);
  318. // drop the table from chado if it exists
  319. if (db_table_exists($custom_table->table_name)) {
  320. $success = chado_query("DROP TABLE %s", $custom_table->table_name);
  321. if($success){
  322. drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
  323. }
  324. }
  325. }
  326. // Redirect the user
  327. if ($redirect) {
  328. drupal_goto("admin/tripal/custom_tables");
  329. }
  330. }

Related topics