custom_tables.inc 11 KB

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