tripal_core.custom_tables.inc

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

File

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

Related topics