tripal_chado.mviews.inc 21 KB

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