custom_tables.inc 11 KB

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