tripal_core.mviews.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions for viewing and editing of Materialized Views
  5. * on a Tripal website.
  6. */
  7. /**
  8. * @defgroup tripal_mviews Tripal Materialized Views
  9. * @ingroup tripal_core
  10. * @{
  11. * Contains functions for viewing and editing of Materialized Views
  12. * on a Tripal website.
  13. * @}
  14. */
  15. /**
  16. * Provides a landing page for tripal jobs admin
  17. *
  18. * @ingroup tripal_mviews
  19. */
  20. function tripal_mview_admin_view() {
  21. $output = '';
  22. // set the breadcrumb
  23. $breadcrumb = array();
  24. $breadcrumb[] = l('Home', '<front>');
  25. $breadcrumb[] = l('Administration', 'admin');
  26. $breadcrumb[] = l('Tripal', 'admin/tripal');
  27. $breadcrumb[] = l('Chado Schema', 'admin/tripal/schema');
  28. $breadcrumb[] = l('Materialized Views', 'admin/tripal/schema/mviews');
  29. drupal_set_breadcrumb($breadcrumb);
  30. // Add the view
  31. $view = views_embed_view('tripal_core_admin_mviews','default');
  32. if (isset($view)) {
  33. $output .= $view;
  34. }
  35. else {
  36. $output .= '<p>The Tripal Materalized View management system uses primarily views to provide an '
  37. . 'administrative interface. Currently one or more views needed for this '
  38. . 'administrative interface are disabled. <strong>Click each of the following links to '
  39. . 'enable the pertinent views</strong>:</p>';
  40. $output .= '<ul>';
  41. $output .= '<li>'.l('MViews View', 'admin/tripal/schema/mviews/views/mviews/enable').'</li>';
  42. $output .= '</ul>';
  43. }
  44. return $output;
  45. }
  46. /**
  47. * A template function which returns markup to display details for the current materialized view
  48. *
  49. * @param $mview_id
  50. * The unique ID of the materialized view to render
  51. *
  52. * @ingroup tripal_mviews
  53. */
  54. function tripal_mview_report($mview_id) {
  55. // get this mview details
  56. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  57. $results = db_query($sql, array(':mview_id' => $mview_id));
  58. $mview = $results->fetchObject();
  59. $rows = array();
  60. // create a table with each row containig stats for
  61. // an individual job in the results set.
  62. $output = "<p>" . l("Return to table of materialized views", "admin/tripal/schema/mviews/") . "</p>";
  63. $output .= "<p>Details for <b>$mview->name</b>:</p>";
  64. // build the URLs using the url function so we can handle installations where
  65. // clean URLs are or are not used
  66. $update_url = url("admin/tripal/schema/mviews/action/update/$mview->mview_id");
  67. $delete_url = url("admin/tripal/schema/mviews/action/delete/$mview->mview_id");
  68. $edit_url = url("admin/tripal/schema/mviews/edit/$mview->mview_id");
  69. $rows[] = array('Actions', "<a href='$update_url'>Populate</a>, <a href='$edit_url'>Edit</a>, <a href='$delete_url'>Delete</a>");
  70. if ($mview->last_update > 0) {
  71. $update = format_date($mview->last_update);
  72. }
  73. else {
  74. $update = 'Not yet populated';
  75. }
  76. $rows[] = array('Last Update', $update);
  77. if ($mview->name) {
  78. $rows[] = array('View Name', $mview->name);
  79. }
  80. if ($mview->modulename) {
  81. $rows[] = array('Module Name', $mview->modulename);
  82. }
  83. if ($mview->mv_table) {
  84. $rows[] = array('Table Name', $mview->mv_table);
  85. }
  86. if ($mview->mv_specs) {
  87. $rows[] = array('Table Field Definitions', $mview->mv_specs);
  88. }
  89. if ($mview->query) {
  90. $rows[] = array('Query', "<textarea rows=\"15\" cols=\"120\" style=\"font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;\">" . $mview->query . "</textarea>");
  91. }
  92. if ($mview->indexed) {
  93. $rows[] = array('Indexed Fields', $mview->indexed);
  94. }
  95. if ($mview->special_index) {
  96. $rows[] = array('Special Indexed Fields', $mview->special_index);
  97. }
  98. if ($mview->mv_schema) {
  99. $rows[] = array('Drupal Schema API Definition', "<textarea rows=\"20\" cols=\"120\" style=\"font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;\">" . $mview->mv_schema . "</textarea>");
  100. }
  101. $header = array('Detail', 'Value');
  102. $table = array(
  103. 'header' => $header,
  104. 'rows' => $rows,
  105. 'attributes' => array('class' => 'tripal-data-table'),
  106. 'sticky' => FALSE,
  107. 'caption' => '',
  108. 'colgroups' => array(),
  109. 'empty' => 'There are no materialized views',
  110. );
  111. $table = theme_table($table);
  112. $output .= $table;
  113. return $output;
  114. }
  115. /**
  116. * A Form to Create/Edit a Materialized View
  117. *
  118. * @param $form_state
  119. * The current state of the form (Form API)
  120. * @param $mview_id
  121. * The unique ID of the Materialized View to Edit or NULL if creating a new materialized view
  122. *
  123. * @return
  124. * A form array (Form API)
  125. *
  126. * @ingroup tripal_mviews
  127. */
  128. function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
  129. if (!$mview_id) {
  130. $action = 'Add';
  131. }
  132. else {
  133. $action = 'Edit';
  134. }
  135. // set defaults for collapsed fieldsets
  136. $schema_collapsed = 0;
  137. $traditional_collapsed = 1;
  138. $default_name = '';
  139. $default_mv_table = '';
  140. $default_mv_specs = '';
  141. $default_indexed = '';
  142. $default_mvquery = '';
  143. $default_special_index = '';
  144. $default_comment = '';
  145. $default_modulename = '';
  146. $default_schema = '';
  147. // if the view is the older style legacy view then this value get's set to 1
  148. $is_legacy = 0;
  149. // get this requested view
  150. if (strcmp($action, 'Edit') == 0 ) {
  151. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  152. $mview = db_query($sql, array(':mview_id' => $mview_id))->fetchObject();
  153. // set the default values. If there is a value set in the
  154. // form_state then let's use that, otherwise, we'll pull
  155. // the values from the database
  156. if (array_key_exists('values', $form_state)) {
  157. $default_name = $form_state['values']['name'];
  158. $default_mv_table = $form_state['values']['mv_table'];
  159. $default_mv_specs = $form_state['values']['mv_specs'];
  160. $default_indexed = $form_state['values']['indexed'];
  161. $default_mvquery = $form_state['values']['mvquery'];
  162. $default_special_index = $form_state['values']['special_index'];
  163. $default_comment = $form_state['values']['comment'];
  164. $default_modulename = $form_state['values']['modulename'];
  165. }
  166. if (!$default_name) {
  167. $default_name = $mview->name;
  168. }
  169. if (!$default_mv_table) {
  170. $default_mv_table = $mview->mv_table;
  171. }
  172. if (!$default_mv_specs) {
  173. $default_mv_specs = $mview->mv_specs;
  174. }
  175. if (!$default_indexed) {
  176. $default_indexed = $mview->indexed;
  177. }
  178. if (!$default_mvquery) {
  179. $default_mvquery = $mview->query;
  180. }
  181. if (!$default_special_index) {
  182. $default_special_index = $mview->special_index;
  183. }
  184. if (!$default_comment) {
  185. $default_comment = $mview->comment;
  186. }
  187. if (!$default_schema) {
  188. $default_schema = $mview->mv_schema;
  189. }
  190. if (!$default_modulename) {
  191. $default_modulename = $mview->modulename ? $mview->modulename : 'tripal_core';
  192. }
  193. if ($mview->mv_specs) {
  194. $is_legacy = 1;
  195. }
  196. // the mv_table column of the tripal_mviews table always has the table
  197. // name even if it is a custom table. However, for the sake of the form,
  198. // we do not want this to show up as the mv_table is needed for the
  199. // traditional style input. We'll blank it out if we have a custom
  200. // table and it will get reset in the submit function using the
  201. // 'table' value from the schema array
  202. if ($default_schema) {
  203. $default_mv_table = '';
  204. }
  205. // set which fieldset is collapsed
  206. if (!$default_schema) {
  207. $schema_collapsed = 1;
  208. $traditional_collapsed = 0;
  209. }
  210. }
  211. // Build the form
  212. $form['action'] = array(
  213. '#type' => 'value',
  214. '#value' => $action
  215. );
  216. $form['is_legacy'] = array(
  217. '#type' => 'value',
  218. '#value' => $is_legacy
  219. );
  220. $form['mview_id'] = array(
  221. '#type' => 'value',
  222. '#value' => $mview_id
  223. );
  224. $form['modulename'] = array(
  225. '#type' => 'value',
  226. '#value' => $default_modulename,
  227. );
  228. $form['return_link'] = array(
  229. '#type' => 'item',
  230. '#description' => l("Return to table of materialized views", "admin/tripal/schema/mviews/"),
  231. );
  232. $form['name']= array(
  233. '#type' => 'textfield',
  234. '#title' => t('View Name'),
  235. '#description' => t('Please enter the name for this materialized view.'),
  236. '#required' => TRUE,
  237. '#default_value' => $default_name,
  238. );
  239. $form['comment']= array(
  240. '#type' => 'textarea',
  241. '#title' => t('MView Description'),
  242. '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
  243. '#required' => FALSE,
  244. '#default_value' => $default_comment,
  245. );
  246. // add a fieldset for the Drupal Schema API
  247. $form['schema'] = array(
  248. '#type' => 'fieldset',
  249. '#title' => 'Table Schema',
  250. '#description' => t('Use a ' . l("Drupal Schema API", "https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7", array('attributes' => array('target' => '_blank'))) .
  251. ' array to describe the table. See the bottom of this page for an example.'),
  252. '#collapsible' => 1,
  253. '#collapsed' => $schema_collapsed ,
  254. );
  255. $form['schema']['schema']= array(
  256. '#type' => 'textarea',
  257. '#title' => t('Schema Array'),
  258. '#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'))) .
  259. ' compatible array that defines the table. There must also be a "table" key with the name of the table as the value. See the example at the bottom of this page.'),
  260. '#required' => FALSE,
  261. '#default_value' => $default_schema,
  262. '#rows' => 25,
  263. '#attributes' => array(
  264. 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
  265. ),
  266. );
  267. // only let folks edit legacy MViews, not create new ones
  268. if ($is_legacy) {
  269. // add a fieldset for the Original Table Description fields
  270. $form['traditional'] = array(
  271. '#type' => 'fieldset',
  272. '#title' => 'Legacy MViews Setup',
  273. '#description' => t('Traditionally MViews were created by specifying PostgreSQL style ' .
  274. 'column types. This method can be used but is deprecated in favor of the ' .
  275. 'newer Drupal schema API method provided above. In rare cases where the Drupal Schema API ' .
  276. 'does not support a desired data type the Legacy Mviews should be used'),
  277. '#collapsible' => 1,
  278. '#collapsed' => $traditional_collapsed,
  279. );
  280. $form['traditional']['mv_table']= array(
  281. '#type' => 'textfield',
  282. '#title' => t('Table Name'),
  283. '#description' => t('Please enter the table name that this view will generate in the database. You can use the schema and table name for querying the view'),
  284. '#required' => FALSE,
  285. '#default_value' => $default_mv_table,
  286. );
  287. $form['traditional']['mv_specs']= array(
  288. '#type' => 'textarea',
  289. '#title' => t('Table Definition'),
  290. '#description' => t('Please enter the field definitions for this view. Each field should be separated by a comma or enter each field definition on each line.'),
  291. '#required' => FALSE,
  292. '#default_value' => $default_mv_specs,
  293. );
  294. $form['traditional']['indexed']= array(
  295. '#type' => 'textarea',
  296. '#title' => t('Indexed Fields'),
  297. '#description' => t('Please enter the field names (as provided in the table definition above) that will be indexed for this view. Separate by a comma or enter each field on a new line.'),
  298. '#required' => FALSE,
  299. '#default_value' => $default_indexed,
  300. );
  301. /**
  302. $form['traditional']['special_index']= array(
  303. '#type' => 'textarea',
  304. '#title' => t('View Name'),
  305. '#description' => t('Please enter the name for this materialized view.'),
  306. '#required' => TRUE,
  307. '#default_value' => $default_special_index,
  308. );
  309. */
  310. }
  311. $form['mvquery']= array(
  312. '#type' => 'textarea',
  313. '#title' => t('Query'),
  314. '#description' => t('Please enter the SQL statement used to populate the table.'),
  315. '#required' => TRUE,
  316. '#default_value' => $default_mvquery,
  317. '#rows' => 25,
  318. '#attributes' => array(
  319. 'style' => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;",
  320. ),
  321. );
  322. if ($action == 'Edit') {
  323. $value = 'Save';
  324. }
  325. if ($action == 'Add') {
  326. $value = 'Add';
  327. }
  328. $form['submit'] = array(
  329. '#type' => 'submit',
  330. '#value' => t($value),
  331. '#executes_submit_callback' => TRUE,
  332. );
  333. $form['example']= array(
  334. '#type' => 'item',
  335. '#description' => "<br>An example Schema API definition for a materialized view: <pre>
  336. array (
  337. 'description' => 'Stores the type and number of features per organism',
  338. 'table' => 'organism_feature_count',
  339. 'fields' => array (
  340. 'organism_id' => array (
  341. 'type' => 'int',
  342. 'not null' => true,
  343. ),
  344. 'genus' => array (
  345. 'type' => 'varchar',
  346. 'length' => '255',
  347. 'not null' => true,
  348. ),
  349. 'species' => array (
  350. 'type' => 'varchar',
  351. 'length' => '255',
  352. 'not null' => true,
  353. ),
  354. 'common_name' => array (
  355. 'type' => 'varchar',
  356. 'length' => '255',
  357. 'not null' => false,
  358. ),
  359. 'num_features' => array (
  360. 'type' => 'int',
  361. 'not null' => true,
  362. ),
  363. 'cvterm_id' => array (
  364. 'type' => 'int',
  365. 'not null' => true,
  366. ),
  367. 'feature_type' => array (
  368. 'type' => 'varchar',
  369. 'length' => '255',
  370. 'not null' => true,
  371. ),
  372. ),
  373. 'indexes' => array (
  374. 'organism_id_idx' => array ('organism_id'),
  375. 'cvterm_id_idx' => array ('cvterm_id'),
  376. 'feature_type_idx' => array ('feature_type'),
  377. ),
  378. )
  379. </pre>"
  380. );
  381. return $form;
  382. }
  383. /**
  384. * Validate the Create/Edit Materialized View Form
  385. * Implements hook_form_validate().
  386. *
  387. * @ingroup tripal_mviews
  388. */
  389. function tripal_mviews_form_validate($form, &$form_state) {
  390. $action = $form_state['values']['action'];
  391. $mview_id = $form_state['values']['mview_id'];
  392. $name = trim($form_state['values']['name']);
  393. $is_legacy = $form_state['values']['is_legacy'];
  394. $query = $form_state['values']['mvquery'];
  395. // if this is a legacy materialized view (no longer supported in Tripal v2.0
  396. // but code left just in case)
  397. if ($is_legacy) {
  398. $mv_table = trim($form_state['values']['mv_table']);
  399. $mv_specs = $form_state['values']['mv_specs'];
  400. $indexed = $form_state['values']['indexed'];
  401. $special_index = '';//$form_state['values']['special_index'];
  402. }
  403. else {
  404. $mv_table = '';
  405. $mv_specs = '';
  406. $indexed = '';
  407. $special_index = '';
  408. }
  409. $comment = trim($form_state['values']['comment']);
  410. $schema = $form_state['values']['schema'];
  411. // validate the contents of the array
  412. $schema_array = array();
  413. $success = eval("\$schema_array = $schema;");
  414. $error = chado_validate_custom_table_schema($schema_array);
  415. if ($error) {
  416. form_set_error('schema', $error);
  417. }
  418. // if both the schema and the older fields for the legacy view are populated then
  419. // this is an error and we need to let the user know.
  420. if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
  421. form_set_error($form_state['values']['schema'],
  422. t('You can create an MView using the Drupal Schema API method or the ' .
  423. 'traditional method but not both.'));
  424. }
  425. // if we don't have a schema and are missing fields for the legacy views then
  426. // inform the user.
  427. if (!$schema) {
  428. if (!$mv_specs) {
  429. form_set_error($form_state['values']['mv_specs'],
  430. t('The Table Definition field is required.'));
  431. }
  432. if (!$mv_table) {
  433. form_set_error($form_state['values']['mv_table'],
  434. t('The Table Name field is required.'));
  435. }
  436. }
  437. // make sure the array is valid
  438. if ($schema) {
  439. $success = eval("\$schema_array = $schema;");
  440. if ($success === FALSE) {
  441. $error = error_get_last();
  442. form_set_error($form_state['values']['schema'],
  443. t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  444. }
  445. if (!array_key_exists('table', $schema_array)) {
  446. form_set_error($form_state['values']['schema'],
  447. t("The schema array must have key named 'table'"));
  448. }
  449. // TODO: add in more validation checks of the array to help the user
  450. }
  451. }
  452. /**
  453. * Submit the Create/Edit Materialized View Form
  454. * Implements hook_form_submit().
  455. *
  456. * @ingroup tripal_mviews
  457. */
  458. function tripal_mviews_form_submit($form, &$form_state) {
  459. $ret = array();
  460. $action = $form_state['values']['action'];
  461. $mview_id = $form_state['values']['mview_id'];
  462. $name = trim($form_state['values']['name']);
  463. $is_legacy = $form_state['values']['is_legacy'];
  464. $query = $form_state['values']['mvquery'];
  465. $comment = trim($form_state['values']['comment']);
  466. $schema = $form_state['values']['schema'];
  467. $modulename = trim($form_state['values']['modulename']);
  468. $mv_table = '';
  469. $mv_specs = '';
  470. $indexed = '';
  471. $special_index = '';
  472. // if this is a legacy materialized view (no longer supported in Tripal v2.0
  473. // but code left just in case)
  474. if ($is_legacy) {
  475. $mv_table = $form_state['values']['mv_table'];
  476. $mv_specs = $form_state['values']['mv_specs'];
  477. $indexed = $form_state['values']['indexed'];
  478. $special_index = '';//$form_state['values']['special_index'];
  479. }
  480. if (!$modulename) {
  481. $modulename = 'tripal_core';
  482. }
  483. // if this is an edit action
  484. if (strcmp($action, 'Edit') == 0) {
  485. tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
  486. $indexed, $query, $special_index, $comment, $schema);
  487. }
  488. // else an add action
  489. elseif (strcmp($action, 'Add') == 0) {
  490. // convert the schema into a PHP array
  491. $schema_arr = array();
  492. eval("\$schema_arr = $schema;");
  493. tripal_add_mview($name, $modulename, $schema_arr, $query, $comment);
  494. drupal_goto("admin/tripal/schema/mviews");
  495. }
  496. else {
  497. drupal_set_message(t("No action performed."));
  498. }
  499. return '';
  500. }
  501. /**
  502. * Just a simple form for confirming deletion of a custom table
  503. *
  504. * @ingroup tripal_custom_tables
  505. */
  506. function tripal_mviews_delete_form($form, &$form_state, $mview_id) {
  507. // get details about this table entry
  508. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id";
  509. $results = db_query($sql, array(':mview_id' => $mview_id));
  510. $entry = $results->fetchObject();
  511. $form = array();
  512. $form['mview_id'] = array(
  513. '#type' => 'value',
  514. '#value' => $mview_id
  515. );
  516. $form['sure'] = array(
  517. '#type' => 'markup',
  518. '#markup' => '<p>Are you sure you want to delete the "' . $entry->name . '" materialized view?</p>'
  519. );
  520. $form['submit'] = array(
  521. '#type' => 'submit',
  522. '#value' => 'Delete',
  523. );
  524. $form['cancel'] = array(
  525. '#type' => 'submit',
  526. '#value' => 'Cancel',
  527. );
  528. return $form;
  529. }
  530. /**
  531. * form submit hook for the tripal_custom_tables_delete_form form.
  532. *
  533. * @param $form
  534. * @param $form_state
  535. */
  536. function tripal_mviews_delete_form_submit($form, &$form_state) {
  537. $action = $form_state['clicked_button']['#value'];
  538. $mview_id = $form_state['values']['mview_id'];
  539. if (strcmp($action, 'Delete') == 0) {
  540. tripal_delete_mview($mview_id);
  541. }
  542. else {
  543. drupal_set_message(t("No action performed."));
  544. }
  545. drupal_goto("admin/tripal/schema/mviews");
  546. }
  547. /**
  548. * A wrapper for the tripal_add_job_populate_mview() API function, which
  549. * then redirects back to the admin page for mviews.
  550. *
  551. * @param $mview_id
  552. */
  553. function tripal_mviews_add_populate_job($mview_id) {
  554. tripal_add_job_populate_mview($mview_id);
  555. drupal_goto("admin/tripal/schema/mviews");
  556. }