custom_tables.inc 13 KB

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