mviews.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. /**
  3. * @defgroup tripal_mviews_api Core Module Materalized Views API
  4. * @{
  5. * Provides an application programming interface (API) to manage materialized views in Chado.
  6. * The Perl-based chado comes with an interface for managing materialzed views. This
  7. * API provides an alternative Drupal-based method.
  8. * @}
  9. * @ingroup tripal_api
  10. */
  11. /**
  12. * Add a materialized view to the chado database to help speed data access. This
  13. * function supports the older style where postgres column specifications
  14. * are provided using the $mv_table, $mv_specs and $indexed variables. It also
  15. * supports the newer preferred method where the materialized view is described
  16. * using the Drupal Schema API array.
  17. *
  18. * @param $name
  19. * The name of the materialized view.
  20. * @param $modulename
  21. * The name of the module submitting the materialized view (e.g. 'tripal_library')
  22. * @param $mv_table
  23. * The name of the table to add to chado. This is the table that can be queried.
  24. * @param $mv_specs
  25. * The table definition
  26. * @param $indexed
  27. * The columns that are to be indexed
  28. * @param $query
  29. * The SQL query that loads the materialized view with data
  30. * @param $special_index
  31. * currently not used
  32. * @param $comment
  33. * A string containing a description of the materialized view
  34. * @param $mv_schema
  35. * If using the newer Schema API array to define the materialized view then
  36. * this variable should contain the array.
  37. *
  38. * @ingroup tripal_mviews_api
  39. */
  40. function tripal_add_mview ($name, $modulename, $mv_table, $mv_specs, $indexed,
  41. $query, $special_index, $comment=NULL, $mv_schema=NULL)
  42. {
  43. // get the table name from the schema array
  44. $schema_arr = array();
  45. if($mv_schema){
  46. // get the schema from the mv_specs and use it to add the custom table
  47. eval("\$schema_arr = $mv_schema;");
  48. $mv_table = $schema_arr['table'];
  49. }
  50. $record = new stdClass();
  51. $record->name = $name;
  52. $record->modulename = $modulename;
  53. $record->mv_table = $mv_table;
  54. $record->mv_specs = $mv_specs;
  55. $record->indexed = $indexed;
  56. $record->query = $query;
  57. $record->special_index = $special_index;
  58. $record->comment = $comment;
  59. $record->mv_schema = $mv_schema;
  60. // add the record to the tripal_mviews table and if successful
  61. // create the new materialized view in the chado schema
  62. if(drupal_write_record('tripal_mviews',$record)){
  63. // drop the table from chado if it exists
  64. $previous_db = tripal_db_set_active('chado'); // use chado database
  65. if (db_table_exists($mv_table)) {
  66. $sql = "DROP TABLE $mv_table";
  67. db_query($sql);
  68. }
  69. tripal_db_set_active($previous_db); // now use drupal database
  70. // now construct the indexes
  71. $index = '';
  72. if($indexed){
  73. // add to the array of values
  74. $vals = preg_split("/[\n,]+/",$indexed);
  75. $index = '';
  76. foreach ($vals as $field){
  77. $field = trim($field);
  78. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  79. }
  80. }
  81. // create the table differently depending on if it the traditional method
  82. // or the Drupal Schema API method
  83. if($mv_schema){
  84. if(!tripal_create_chado_table ($ret,$mv_table,$schema_arr)){
  85. drupal_set_message("Could not create the materialized view. Check Drupal error report logs.");
  86. } else {
  87. drupal_set_message(t("View '$name' created"));
  88. }
  89. } else {
  90. // add the table to the database
  91. $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
  92. $previous_db = tripal_db_set_active('chado'); // use chado database
  93. $results = db_query($sql);
  94. tripal_db_set_active($previous_db); // now use drupal database
  95. if($results){
  96. drupal_set_message(t("View '$name' created"));
  97. } else {
  98. drupal_set_message(t("Failed to create the materialized view table: '$mv_table'"));
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * Edits a materialized view to the chado database to help speed data access.This
  105. * function supports the older style where postgres column specifications
  106. * are provided using the $mv_table, $mv_specs and $indexed variables. It also
  107. * supports the newer preferred method where the materialized view is described
  108. * using the Drupal Schema API array.
  109. *
  110. * @param $mview_id
  111. * The mview_id of the materialized view to edit
  112. * @param $name
  113. * The name of the materialized view.
  114. * @param $modulename
  115. * The name of the module submitting the materialized view (e.g. 'tripal_library')
  116. * @param $mv_table
  117. * The name of the table to add to chado. This is the table that can be queried.
  118. * @param $mv_specs
  119. * The table definition
  120. * @param $indexed
  121. * The columns that are to be indexed
  122. * @param $query
  123. * The SQL query that loads the materialized view with data
  124. * @param $special_index
  125. * currently not used
  126. * @param $comment
  127. * A string containing a description of the materialized view
  128. * @param $mv_schema
  129. * If using the newer Schema API array to define the materialized view then
  130. * this variable should contain the array.
  131. *
  132. * @ingroup tripal_mviews_api
  133. */
  134. function tripal_edit_mview ($mview_id,$name,$modulename,$mv_table,$mv_specs,
  135. $indexed,$query,$special_index,$comment=NULL,$mv_schema=NULL)
  136. {
  137. // get the table name from the schema array
  138. $schema_arr = array();
  139. if($mv_schema){
  140. // get the schema from the mv_specs and use it to add the custom table
  141. eval("\$schema_arr = $mv_schema;");
  142. $mv_table = $schema_arr['table'];
  143. }
  144. $record = new stdClass();
  145. $record->mview_id = $mview_id;
  146. $record->name = $name;
  147. $record->modulename = $modulename;
  148. $record->mv_schema = $mv_schema;
  149. $record->mv_table = $mv_table;
  150. $record->mv_specs = $mv_specs;
  151. $record->indexed = $indexed;
  152. $record->query = $query;
  153. $record->special_index = $special_index;
  154. $record->last_update = 0;
  155. $record->status = '';
  156. $record->comment = $comment;
  157. // drop the table from chado if it exists
  158. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  159. $mview = db_fetch_object(db_query($sql));
  160. $previous_db = tripal_db_set_active('chado'); // use chado database
  161. if (db_table_exists($mview->mv_table)) {
  162. $sql = "DROP TABLE $mview->mv_table";
  163. db_query($sql);
  164. }
  165. tripal_db_set_active($previous_db); // now use drupal database
  166. // update the record to the tripal_mviews table and if successful
  167. // create the new materialized view in the chado schema
  168. if(drupal_write_record('tripal_mviews',$record,'mview_id')){
  169. // drop the table from chado if it exists
  170. $previous_db = tripal_db_set_active('chado'); // use chado database
  171. if (db_table_exists($mv_table)) {
  172. $sql = "DROP TABLE $mv_table";
  173. db_query($sql);
  174. }
  175. tripal_db_set_active($previous_db); // now use drupal database
  176. // now construct the indexes
  177. $index = '';
  178. if($indexed){
  179. // add to the array of values
  180. $vals = preg_split("/[\n,]+/",$indexed);
  181. $index = '';
  182. foreach ($vals as $field){
  183. $field = trim($field);
  184. $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
  185. }
  186. }
  187. // re-create the table differently depending on if it the traditional method
  188. // or the Drupal Schema API method
  189. if($mv_schema){
  190. if(!tripal_create_chado_table ($ret,$mv_table,$schema_arr)){
  191. drupal_set_message("Could not create the materialized view. Check Drupal error report logs.");
  192. } else {
  193. drupal_set_message(t("View '$name' created"));
  194. }
  195. } else {
  196. $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
  197. $previous_db = tripal_db_set_active('chado'); // use chado database
  198. $results = db_query($sql);
  199. tripal_db_set_active($previous_db); // now use drupal database
  200. if($results){
  201. drupal_set_message(t("View '$name' edited and saved. All results cleared. Please re-populate the view."));
  202. } else {
  203. drupal_set_message(t("Failed to create the materialized view table: '$mv_table'"));
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * Retrieve the materialized view_id given the name
  210. *
  211. * @param $view_name
  212. * The name of the materialized view
  213. *
  214. * @return
  215. * The unique identifier for the given view
  216. *
  217. * @ingroup tripal_mviews_api
  218. */
  219. function tripal_mviews_get_mview_id ($view_name){
  220. $sql = "SELECT * FROM {tripal_mviews} ".
  221. "WHERE name = '%s'";
  222. if(db_table_exists('tripal_mviews')){
  223. $mview = db_fetch_object(db_query($sql,$view_name));
  224. if($mview){
  225. return $mview->mview_id;
  226. }
  227. }
  228. return 0;
  229. }
  230. /**
  231. *
  232. *
  233. * @ingroup tripal_core
  234. */
  235. function tripal_mviews_action ($op,$mview_id,$redirect=0){
  236. global $user;
  237. $args = array("$mview_id");
  238. if(!$mview_id){
  239. return '';
  240. }
  241. // get this mview details
  242. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  243. $mview = db_fetch_object(db_query($sql));
  244. // add a job or perform the action based on the given operation
  245. if($op == 'update'){
  246. tripal_add_job("Populate materialized view '$mview->name'",'tripal_core',
  247. 'tripal_update_mview',$args,$user->uid);
  248. }
  249. if($op == 'delete'){
  250. // remove the mview from the tripal_mviews table
  251. $sql = "DELETE FROM {tripal_mviews} ".
  252. "WHERE mview_id = $mview_id";
  253. db_query($sql);
  254. // drop the table from chado if it exists
  255. $previous_db = tripal_db_set_active('chado'); // use chado database
  256. if (db_table_exists($mview->mv_table)) {
  257. $sql = "DROP TABLE $mview->mv_table";
  258. db_query($sql);
  259. }
  260. tripal_db_set_active($previous_db); // now use drupal database
  261. }
  262. if($redirect){
  263. drupal_goto("admin/tripal/mviews");
  264. }
  265. }
  266. /**
  267. * Update a Materialized View
  268. *
  269. * @param $mview_id
  270. * The unique identifier for the materialized view to be updated
  271. *
  272. * @return
  273. * True if successful, false otherwise
  274. *
  275. * @ingroup tripal_mviews_api
  276. */
  277. function tripal_update_mview ($mview_id){
  278. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
  279. $mview = db_fetch_object(db_query($sql,$mview_id));
  280. if($mview){
  281. $previous_db = tripal_db_set_active('chado'); // use chado database
  282. $results = db_query("DELETE FROM {$mview->mv_table}");
  283. $results = db_query("INSERT INTO {$mview->mv_table} ($mview->query)");
  284. tripal_db_set_active($previous_db); // now use drupal database
  285. if($results){
  286. $sql = "SELECT count(*) as cnt FROM {$mview->mv_table}";
  287. $count = db_fetch_object(db_query($sql));
  288. $record = new stdClass();
  289. $record->mview_id = $mview_id;
  290. $record->last_update = time();
  291. $record->status = "Populated with " . number_format($count->cnt) . " rows";
  292. drupal_write_record('tripal_mviews',$record,'mview_id');
  293. return 1;
  294. } else {
  295. # print and save the error message
  296. $record = new stdClass();
  297. $record->mview_id = $mview_id;
  298. $record->status = "ERROR populating. See Drupal's recent log entries for details.";
  299. print $record->status . "\n";
  300. drupal_write_record('tripal_mviews',$record,'mview_id');
  301. return 0;
  302. }
  303. }
  304. }
  305. /**
  306. *
  307. *
  308. * @ingroup tripal_mviews_api
  309. */
  310. function tripal_mview_report ($mview_id) {
  311. // get this mview details
  312. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id ";
  313. $mview = db_fetch_object(db_query($sql));
  314. // create a table with each row containig stats for
  315. // an individual job in the results set.
  316. $return_url = url("admin/tripal/mviews/");
  317. $output .= "<p><a href=\"$return_url\">Return to table of materialized views.</a></p>";
  318. $output .= "<br />";
  319. $output .= "<p>Details for <b>$mview->name</b>:</p>";
  320. $output .= "<br />";
  321. $output .= "<table class=\"border-table\">";
  322. if($mview->name){
  323. $output .= " <tr>".
  324. " <th>View Name</th>".
  325. " <td>$mview->name</td>".
  326. " </tr>";
  327. }
  328. if($mview->modulename){
  329. $output .= " <tr>".
  330. " <th>Module Name</th>".
  331. " <td>$mview->modulename</td>".
  332. " </tr>";
  333. }
  334. if($mview->mv_table){
  335. $output .= " <tr>".
  336. " <th>Table Name</th>".
  337. " <td>$mview->mv_table</td>".
  338. " </tr>";
  339. }
  340. if($mview->mv_specs){
  341. $output .= " <tr>".
  342. " <th>Table Field Definitions</th>".
  343. " <td>$mview->mv_specs</td>".
  344. " </tr>";
  345. }
  346. if($mview->query){
  347. $output .= " <tr>".
  348. " <th>Query</th>".
  349. " <td><pre>$mview->query</pre></td>".
  350. " </tr>";
  351. }
  352. if($mview->indexed){
  353. $output .= " <tr>".
  354. " <th>Indexed Fields</th>".
  355. " <td>$mview->indexed</td>".
  356. " </tr>";
  357. }
  358. if($mview->special_index){
  359. $output .= " <tr>".
  360. " <th>Special Indexed Fields</th>".
  361. " <td>$mview->speical_index</td>".
  362. " </tr>";
  363. }
  364. if($mview->last_update > 0){
  365. $update = format_date($mview->last_update);
  366. } else {
  367. $update = 'Not yet populated';
  368. }
  369. $output .= " <tr>".
  370. " <th>Last Update</th>".
  371. " <td>$update</td>".
  372. " </tr>";
  373. // build the URLs using the url function so we can handle installations where
  374. // clean URLs are or are not used
  375. $update_url = url("admin/tripal/mviews/action/update/$mview->mview_id");
  376. $delete_url = url("admin/tripal/mviews/action/delete/$mview->mview_id");
  377. $edit_url = url("admin/tripal/mviews/edit/$mview->mview_id");
  378. $output .= "<tr><th>Actions</th>".
  379. "<td> <a href='$update_url'>Populate</a>, ".
  380. " <a href='$edit_url'>Edit</a>, ".
  381. " <a href='$delete_url'>Delete</a></td></tr>";
  382. $output .= "</table>";
  383. return $output;
  384. }
  385. /**
  386. *
  387. *
  388. * @ingroup tripal_mviews_api
  389. */
  390. function tripal_mviews_report () {
  391. $header = array('','MView Name','Last Update','Status','Description','');
  392. $rows = array();
  393. $mviews = db_query("SELECT * FROM {tripal_mviews} ORDER BY name");
  394. while($mview = db_fetch_object($mviews)){
  395. if($mview->last_update > 0){
  396. $update = format_date($mview->last_update);
  397. } else {
  398. $update = 'Not yet populated';
  399. }
  400. $rows[] = array(
  401. l('View',"admin/tripal/mviews/report/$mview->mview_id") ." | ".
  402. l('Edit',"admin/tripal/mviews/edit/$mview->mview_id") ." | ".
  403. l('Populate',"admin/tripal/mviews/action/update/$mview->mview_id"),
  404. $mview->name,
  405. $update,
  406. $mview->status,
  407. $mview->comment,
  408. l('Delete',"admin/tripal/mviews/action/delete/$mview->mview_id"),
  409. );
  410. }
  411. $rows[] = array(
  412. 'data' => array(
  413. array('data' => l('Create a new materialized view.',"admin/tripal/mviews/new"),
  414. 'colspan' => 6),
  415. )
  416. );
  417. $page = theme('table', $header, $rows);
  418. return $page;
  419. }
  420. /**
  421. *
  422. *
  423. * @ingroup tripal_core
  424. */
  425. function tripal_mviews_form(&$form_state = NULL,$mview_id = NULL){
  426. if(!$mview_id){
  427. $action = 'Add';
  428. } else {
  429. $action = 'Edit';
  430. }
  431. // get this requested view
  432. if(strcmp($action,'Edit')==0){
  433. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = %d ";
  434. $mview = db_fetch_object(db_query($sql,$mview_id));
  435. # set the default values. If there is a value set in the
  436. # form_state then let's use that, otherwise, we'll pull
  437. # the values from the database
  438. $default_name = $form_state['values']['name'];
  439. $default_mv_table = $form_state['values']['mv_table'];
  440. $default_mv_specs = $form_state['values']['mv_specs'];
  441. $default_indexed = $form_state['values']['indexed'];
  442. $default_mvquery = $form_state['values']['mvquery'];
  443. $default_special_index = $form_state['values']['special_index'];
  444. $default_comment = $form_state['values']['cpmment'];
  445. if(!$default_name){
  446. $default_name = $mview->name;
  447. }
  448. if(!$default_mv_table){
  449. $default_mv_table = $mview->mv_table;
  450. }
  451. if(!$default_mv_specs){
  452. $default_mv_specs = $mview->mv_specs;
  453. }
  454. if(!$default_indexed){
  455. $default_indexed = $mview->indexed;
  456. }
  457. if(!$default_mvquery){
  458. $default_mvquery = $mview->query;
  459. }
  460. if(!$default_special_index){
  461. $default_special_index = $mview->special_index;
  462. }
  463. if(!$default_comment){
  464. $default_comment = $mview->comment;
  465. }
  466. if(!$default_schema){
  467. $default_schema = $mview->mv_schema;
  468. }
  469. // the mv_table column of the tripal_mviews table always has the table
  470. // name even if it is a custom table. However, for the sake of the form,
  471. // we do not want this to show up as the mv_table is needed for the
  472. // traditional style input. We'll blank it out if we have a custom
  473. // table and it will get reset in the submit function using the
  474. // 'table' value from the schema array
  475. if($default_schema){
  476. $default_mv_table = '';
  477. }
  478. // set which fieldset is collapsed
  479. $schema_collapsed = 0;
  480. $traditional_collapsed = 1;
  481. if(!$default_schema){
  482. $schema_collapsed = 1;
  483. $traditional_collapsed = 0;
  484. }
  485. }
  486. // Build the form
  487. $form['action'] = array(
  488. '#type' => 'value',
  489. '#value' => $action
  490. );
  491. $form['mview_id'] = array(
  492. '#type' => 'value',
  493. '#value' => $mview_id
  494. );
  495. $form['name']= array(
  496. '#type' => 'textfield',
  497. '#title' => t('View Name'),
  498. '#description' => t('Please enter the name for this materialized view.'),
  499. '#required' => TRUE,
  500. '#default_value' => $default_name,
  501. );
  502. $form['comment']= array(
  503. '#type' => 'textarea',
  504. '#title' => t('MView Description'),
  505. '#description' => t('Optional. Please provide a description of the purpose for this materialized vieww.'),
  506. '#required' => FALSE,
  507. '#default_value' => $default_comment,
  508. );
  509. // add a fieldset for the Drupal Schema API
  510. $form['schema'] = array(
  511. '#type' => 'fieldset',
  512. '#title' => 'Drupal Schema API Setup',
  513. '#description' => t('Use the Drupal Schema API array to describe a table. The benefit is that it '.
  514. 'can be fully integrated with Tripal Views. Tripal supports an extended '.
  515. 'array format to allow for descriptoin of foreign key relationships.'),
  516. '#collapsible' => 1,
  517. '#collapsed' => $schema_collapsed ,
  518. );
  519. $form['schema']['schema']= array(
  520. '#type' => 'textarea',
  521. '#title' => t('Schema Array'),
  522. '#description' => t('Please enter the Drupal Schema API compatible array that defines the table.'),
  523. '#required' => FALSE,
  524. '#default_value' => $default_schema,
  525. '#rows' => 25,
  526. );
  527. // add a fieldset for the Original Table Description fields
  528. $form['traditional'] = array(
  529. '#type' => 'fieldset',
  530. '#title' => 'Traditional MViews Setup',
  531. '#description' => t('Traidtionally with Tripal MViews were created by specifying PostgreSQL style '.
  532. 'column types. This method can be used but is deprecated in favor of the '.
  533. 'newer Drupal schema API method provided above.'),
  534. '#collapsible' => 1,
  535. '#collapsed' => $traditional_collapsed,
  536. );
  537. $form['traditional']['mv_table']= array(
  538. '#type' => 'textfield',
  539. '#title' => t('Table Name'),
  540. '#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'),
  541. '#required' => FALSE,
  542. '#default_value' => $default_mv_table,
  543. );
  544. $form['traditional']['mv_specs']= array(
  545. '#type' => 'textarea',
  546. '#title' => t('Table Definition'),
  547. '#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.'),
  548. '#required' => FALSE,
  549. '#default_value' => $default_mv_specs,
  550. );
  551. $form['traditional']['indexed']= array(
  552. '#type' => 'textarea',
  553. '#title' => t('Indexed Fields'),
  554. '#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.'),
  555. '#required' => FALSE,
  556. '#default_value' => $default_indexed,
  557. );
  558. /**
  559. $form['traditional']['special_index']= array(
  560. '#type' => 'textarea',
  561. '#title' => t('View Name'),
  562. '#description' => t('Please enter the name for this materialized view.'),
  563. '#required' => TRUE,
  564. '#default_value' => $default_special_index,
  565. );
  566. */
  567. $form['mvquery']= array(
  568. '#type' => 'textarea',
  569. '#title' => t('Query'),
  570. '#description' => t('Please enter the SQL statement used to populate the table.'),
  571. '#required' => TRUE,
  572. '#default_value' => $default_mvquery,
  573. '#rows' => 25,
  574. );
  575. if($action == 'Edit'){
  576. $value = 'Save';
  577. }
  578. if($action == 'Add'){
  579. $value = 'Add';
  580. }
  581. $form['submit'] = array (
  582. '#type' => 'submit',
  583. '#value' => t($value),
  584. '#weight' => 9,
  585. '#executes_submit_callback' => TRUE,
  586. );
  587. $form['#redirect'] = 'admin/tripal/mviews';
  588. return $form;
  589. }
  590. /**
  591. *
  592. *
  593. * @ingroup tripal_core
  594. */
  595. function tripal_mviews_form_validate($form, &$form_state){
  596. $action = $form_state['values']['action'];
  597. $mview_id = $form_state['values']['mview_id'];
  598. $name = $form_state['values']['name'];
  599. $mv_table = $form_state['values']['mv_table'];
  600. $mv_specs = $form_state['values']['mv_specs'];
  601. $indexed = $form_state['values']['indexed'];
  602. $query = $form_state['values']['mvquery'];
  603. $special_index = $form_state['values']['special_index'];
  604. $comment = $form_state['values']['comment'];
  605. $schema = $form_state['values']['schema'];
  606. if($schema and ($mv_table or $mv_specs or $indexed or $special_index)){
  607. form_set_error($form_state['values']['schema'],
  608. t('You can create an MView using the Drupal Schema API method or the '.
  609. 'traditional method but not both.'));
  610. }
  611. if(!$schema){
  612. if(!$mv_specs){
  613. form_set_error($form_state['values']['mv_specs'],
  614. t('The Table Definition field is required.'));
  615. }
  616. if(!$mv_table){
  617. form_set_error($form_state['values']['mv_table'],
  618. t('The Table Name field is required.'));
  619. }
  620. }
  621. // make sure the array is valid
  622. if($schema){
  623. $success = eval("\$schema_array = $schema;");
  624. if ($success === FALSE){
  625. $error = error_get_last();
  626. form_set_error($form_state['values']['schema'],
  627. t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
  628. }
  629. if(!array_key_exists('table',$schema_array)){
  630. form_set_error($form_state['values']['schema'],
  631. t("The schema array must have key named 'table'"));
  632. }
  633. // TODO: add in more validation checks of the array to help the user
  634. }
  635. }
  636. /**
  637. *
  638. *
  639. * @ingroup tripal_core
  640. */
  641. function tripal_mviews_form_submit($form, &$form_state){
  642. $ret = array();
  643. $action = $form_state['values']['action'];
  644. $mview_id = $form_state['values']['mview_id'];
  645. $name = $form_state['values']['name'];
  646. $mv_table = $form_state['values']['mv_table'];
  647. $mv_specs = $form_state['values']['mv_specs'];
  648. $indexed = $form_state['values']['indexed'];
  649. $query = $form_state['values']['mvquery'];
  650. $special_index = $form_state['values']['special_index'];
  651. $comment = $form_state['values']['comment'];
  652. $schema = $form_state['values']['schema'];
  653. if(strcmp($action,'Edit')==0){
  654. tripal_edit_mview($mview_id,$name, 'tripal_core',$mv_table, $mv_specs,
  655. $indexed,$query,$special_index,$comment,$schema);
  656. }
  657. else if(strcmp($action,'Add')==0){
  658. tripal_add_mview ($name, 'tripal_core',$mv_table, $mv_specs,
  659. $indexed,$query,$special_index,$comment,$schema);
  660. }
  661. else {
  662. drupal_set_message("No action performed.");
  663. }
  664. return '';
  665. }