tripal_views_integration.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. <?php
  2. /**
  3. * Purpose: Provide Guidance to new Tripal Admin
  4. *
  5. * @return
  6. * HTML Formatted text
  7. *
  8. * @ingroup tripal_views_integration
  9. */
  10. function tripal_views_description_page() {
  11. $text .= '<h3>Tripal Views Quick Links:</h3>';
  12. $text .= "<ul>
  13. <li><a href=\"".url("admin/tripal/views/integration/mviews") . "\">List of integrated tables</a></li>
  14. <li><a href=\"".url("admin/tripal/views/integration/mviews/new"). "\">Integrate a new table</a></li>
  15. </ul>";
  16. $text .= '<h3>Views Integration Description:</h3>';
  17. $text .= '<p>Tripal Views provides an interface for integrating <a href="http://drupal.org/project/views">Drupal Views</a>
  18. with Tripal materialized views. This will allow site administrators to create custom queries for the materialized views
  19. and in turn provide custom content pages, custom blocks and custom search forms. The forms allow a site administrator
  20. to select a materialized view and associate other Chado tables on which the view can join. Usage of this module requires
  21. a good understanding of foreign-key relationships in Chado.
  22. </p>';
  23. $text .= '<h3>Setup Instructions:</h3>';
  24. $text .= '<p>After installation of the Tripal core module. The following tasks should be performed
  25. <ol>
  26. <li><b>Set Permissions</b>: To allow access to site administrators for this module, simply
  27. <a href="'.url('admin/user/permissions').'">assign permissions</a> to the appropriate user roles for the
  28. permission type "manage tripal_views_integration". </li>
  29. </ol>
  30. </p>';
  31. $text .= '<h3>Usage Instructions:</h3>';
  32. $text .= "<p>To use Tripal Views integration follow these steps:
  33. <ol>
  34. <li><b>Identify or create a materialized view:</b> Using the <a href=\"".url("admin/tripal/views/mviews") . "\">
  35. Tripal materialized View</a> interface, identify the view you would like to integrate or create a new one.</li>
  36. <li><b>Setup the Views Integration</b>: Navigate to the <a href=\"".url("admin/tripal/views/integration/mviews/new") . "\">
  37. Tripal views integration setup page</a> to integrate the selected materialized view. Provide a user friendly name
  38. and description to help you remember the purpose for integrating the view. Next, select the view you want to integrate
  39. from the provided select box. If your materialized view has fields that can join with other Chado tables, you may
  40. provide those relationships in the provided form. Finally, if your fields require a special handler for display, you
  41. may select it from the drop down provided</li>
  42. <li><b>Create custom pages/block/search form</b>: After saving setup information from step 2 above, you will be redirected to the
  43. Drupal Views interface</a> where you can create a custom page, block or search form.</li>
  44. <li><b>Review your integrated views</b>: A page providing a
  45. <a href=\"".url("admin/tripal/views/integration/mviews/list") . "\">list of all integrated views</a> is provided. You may
  46. view this page to see all integrated views, but also to remove any unwanted integrations.</li>
  47. </ol>
  48. </p>";
  49. return $text;
  50. }
  51. /**
  52. * Purpose: Generates a themable table containing the list of integrated tables
  53. * The look-and-feel of the table can be altered by overriding the theme for
  54. * tables.
  55. *
  56. * @return
  57. * a themed HTML table
  58. *
  59. * @ingroup tripal_views_integration
  60. */
  61. function tripal_views_integration_setup_list(){
  62. $header = array('', 'Drupal Views Type Name', 'Table Name', 'Is Mview', 'Priority', 'Comment','');
  63. $rows = array();
  64. // get the list of materialized views
  65. $tviews = db_query('SELECT * FROM {tripal_views} ORDER BY table_name, priority');
  66. while($tview = db_fetch_object($tviews)){
  67. $rows[] = array(
  68. l('Edit',"admin/tripal/views/integration/edit/".$tview->setup_id) ,
  69. $tview->name,
  70. $tview->table_name,
  71. ($tview->mview_id) ? 'Yes' : 'No',
  72. $tview->priority,
  73. $tview->comment,
  74. l('Delete',"admin/tripal/views/integration/delete/".$tview->setup_id),
  75. );
  76. }
  77. $rows[] = array(
  78. 'data' => array(
  79. array('data' => l('Add a new entry',"admin/tripal/views/integration/new") . " | " .
  80. l("Create View",'admin/build/views/add'),
  81. 'colspan' => 7),
  82. )
  83. );
  84. return theme('table', $header, $rows);
  85. }
  86. /**
  87. * Purpose: Deletes integration of a table with the Views module. This
  88. * function is meant to be called from a menu item. After completion it
  89. * redirects the user to the views intergation page.
  90. *
  91. * @param $setup_id
  92. * the unique setup id for the integrated table
  93. *
  94. * @ingroup tripal_views_integration
  95. */
  96. function tripal_views_integration_delete($setup_id){
  97. tripal_views_integration_remove_entry_by_setup_id ($setup_id);
  98. drupal_set_message("Record Deleted");
  99. drupal_goto('admin/tripal/views/integration');
  100. }
  101. /**
  102. * Purpose: defines the web form used for specifing the base table, joins and
  103. * handlers when integrating a table with views. This form is used for both
  104. * creating a new record and editing an existing record.
  105. *
  106. * @param &$form_state
  107. * The form state which is passed automatically by drupal
  108. *
  109. * @param $setup_id
  110. * The unique setup for an integrated table. This value is only set when
  111. * the form is used for updating an existing record.
  112. *
  113. * @return
  114. * A proper Drupal form associative array.
  115. *
  116. * @ingroup tripal_views_integration
  117. */
  118. function tripal_views_integration_form(&$form_state, $setup_id = NULL){
  119. $form = array();
  120. $data = array();
  121. $form['#cache'] = TRUE;
  122. // ahah_helper requires us to register the form with it's module
  123. ahah_helper_register($form, $form_state);
  124. // if a setup_id is provided then we want to get the form defaults
  125. $setup_obj = array();
  126. if(isset($setup_id)){
  127. // get the deafult setup values
  128. $sql = "SELECT * FROM {tripal_views} WHERE setup_id = %d";
  129. $setup_obj = db_fetch_object(db_query($sql,$setup_id));
  130. $mview_id = $setup_obj->mview_id;
  131. $table_name = $setup_obj->table_name;
  132. $form_state['storage']['mview_id'] = $mview_id;
  133. $form_state['storage']['table_name'] = $table_name;
  134. // get the default field name/description
  135. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
  136. $query = db_query($sql,$setup_id);
  137. $default_fields = array();
  138. while ($field = db_fetch_object($query)) {
  139. $default_fields[$field->column_name]['name'] = $field->name;
  140. $default_fields[$field->column_name]['description'] = $field->description;
  141. }
  142. // get the default join settings and handlers
  143. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  144. $query = db_query($sql,$setup_id);
  145. $default_joins = array();
  146. while ($join = db_fetch_object($query)){
  147. $default_joins[$join->base_field]['left_table'] = $join->left_table;
  148. $default_joins[$join->base_field]['left_field'] = $join->left_field;
  149. }
  150. // get the default handlers
  151. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d";
  152. $query = db_query($sql,$setup_id);
  153. $default_handlers = array();
  154. while ($handler = db_fetch_object($query)){
  155. $default_handlers[$handler->column_name][$handler->handler_type]['handler_name'] = $handler->handler_name;
  156. $default_handlers[$handler->column_name][$handler->handler_type]['arguments'] = $handler->arguments;
  157. }
  158. // add in the setup_id for the form so we know this is an update not an insert
  159. $form['setup_id'] = array(
  160. '#type' => 'hidden',
  161. '#value' => $setup_id,
  162. );
  163. }
  164. // add a fieldset for the MView & Chado table selectors
  165. $form['base_table_type'] = array(
  166. '#type' => 'fieldset',
  167. '#title' => 'Base Table',
  168. '#description' => 'Please select either a materialized view or a Chado table for integration with '.
  169. 'Drupal Views. In Drupal Views terminology, the selected table becomes the "base table". '.
  170. 'After you select a table from either list, the fields from that table will appear below '.
  171. 'and you can specify other tables to join with and handlers.',
  172. );
  173. // build the form element that lists the materialized views
  174. $query = db_query("SELECT mview_id,name FROM {tripal_mviews} ORDER BY name");
  175. $mview_tables = array();
  176. $mview_tables['0'] = 'Select';
  177. while ($mview = db_fetch_object($query)){
  178. $mview_tables[$mview->mview_id] = $mview->name;
  179. }
  180. $form['base_table_type']['mview_id'] = array(
  181. '#title' => t('Materialized View'),
  182. '#type' => 'select',
  183. '#options' => $mview_tables,
  184. '#description' => 'Which materialized view to use.',
  185. '#default_value' => $setup_obj->mview_id,
  186. '#ahah' => array(
  187. 'path' => ahah_helper_path(array('view_setup_table')),
  188. 'wrapper' => 'table-rows-div',
  189. 'effect' => 'fade',
  190. 'event' => 'change',
  191. 'method' => 'replace',
  192. ),
  193. );
  194. // build the form element for the Chado tables
  195. $chado_tables = tripal_core_get_chado_tables();
  196. $chado_tables = array_merge(array('Select',), $chado_tables);
  197. $form['base_table_type']['table_name'] = array(
  198. '#title' => t('Chado Table'),
  199. '#type' => 'select',
  200. '#options' => $chado_tables,
  201. '#description' => 'Which Chado table to use.',
  202. '#default_value' => (!$setup_obj->mview_id) ? $setup_obj->table_name : '',
  203. '#ahah' => array(
  204. 'path' => ahah_helper_path(array('view_setup_table')),
  205. 'wrapper' => 'table-rows-div',
  206. 'effect' => 'fade',
  207. 'event' => 'change',
  208. 'method' => 'replace',
  209. ),
  210. );
  211. $form['views_type'] = array(
  212. '#type' => 'fieldset',
  213. '#title' => 'View Type',
  214. '#description' => 'Here you can provide the "type" of View you want to create.',
  215. );
  216. // field for the name of the
  217. $form['views_type']['row_name'] = array(
  218. '#title' => t('View Type Name'),
  219. '#type' => 'textfield',
  220. '#default_value' => $setup_obj->name,
  221. '#size' => 60,
  222. '#maxlength' => 128,
  223. '#description' => 'Provide the view type name. This is the name that will appear in '.
  224. 'the Drupal Views interface when adding a new view. The view type name '.
  225. 'must be unique.',
  226. '#required' => TRUE,
  227. );
  228. if(isset($setup_id)){
  229. $form['row_name']['#attributes'] = array('readonly' => 'readonly');
  230. }
  231. $priorities = array();
  232. foreach (range(-10,10) as $v) {
  233. $priorities[$v] = (string) $v;
  234. }
  235. $form['views_type']['row_priority'] = array(
  236. '#type' => 'select',
  237. '#title' => t('Priority'),
  238. '#description' => t('The level of priority your Views integration has in relation to the '
  239. .'default core and module definitions. The views integration definition with the '
  240. .'lightest priority will be used. For example, if there is a definition created by '
  241. .'core with a priority of 10 and another by a custom module of 5 and yours is -1 then '
  242. .'you definition will be used for that table because -1 is lighter then both 5 and 10.'),
  243. '#options' => $priorities,
  244. '#default_value' => (isset($setup_obj->priority)) ? $setup_obj->priority : -1,
  245. );
  246. $form['views_type']['row_description'] = array(
  247. '#title' => t('Comment'),
  248. '#type' => 'textarea',
  249. '#description' => '(Optional). Provide any details regarding this setup you would like. This '.
  250. 'description will appear when selecting a type for a new Drupal View',
  251. '#required' => FALSE,
  252. );
  253. // we need a div block where the table fields will get put when the
  254. // AHAH callback is made
  255. $form['view_setup_table'] = array(
  256. '#type' => 'item',
  257. '#prefix' => '<div id="table-rows-div">',
  258. '#suffix' => '</div>',
  259. );
  260. // add the fieldset for the table fields, but only if the $mview_id or $table_name
  261. // is set. The only times these values are set is if we're editing an existing
  262. // record or if the AHAH callback is being made.
  263. if ($form_state['storage']['mview_id'] or $form_state['storage']['table_name']){
  264. $mview_id = $form_state['storage']['mview_id'];
  265. $table_name = $form_state['storage']['table_name'];
  266. $form['view_setup_table'] = array(
  267. '#type' => 'fieldset',
  268. '#title' => 'Join Selection',
  269. '#prefix' => '<div id="fieldset-table-rows-wrapper">',
  270. '#suffix' => '</div>',
  271. );
  272. // get the columns in this materialized view. They are separated by commas
  273. // where the first word is the column name and the rest is the type
  274. $columns = array();
  275. if($mview_id){
  276. $sql = "SELECT mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
  277. $mview = db_fetch_object(db_query($sql,$mview_id));
  278. $columns = explode(",",$mview->mv_specs);
  279. } else {
  280. $table_desc = module_invoke_all('chado_'.$table_name.'_schema');
  281. $fields = $table_desc['fields'];
  282. // iterate through the columns and build the format
  283. // compatible with the code below. The column name is first followed
  284. // by the type with a separating space
  285. foreach($fields as $column => $attrs){
  286. $columns[] = "$column ".$attrs['type'];
  287. }
  288. }
  289. $i=1;
  290. $form['view_setup_table']["instructions"] = array(
  291. '#type' => 'markup',
  292. '#value' => "Select an optional table to which the fields of the ".
  293. "materialized view can join. If a field does not need to ".
  294. "join you may leave the selection blank.",
  295. );
  296. $data['field_types'] = array();
  297. // get the list of chado tables to join on
  298. $chado_join_tables = tripal_core_get_chado_tables();
  299. $chado_join_tables = array_merge(array('Select a Join Table',), $chado_join_tables);
  300. // get list of all handlers
  301. $all_handlers = tripal_views_integration_discover_handlers();
  302. $handlers_fields = array(0 => "Select a field handler");
  303. $handlers_filters = array(0 => "Select a filter handler");
  304. $handlers_sort = array(0 => "Select a sort handler");
  305. $handlers_argument = array(0 => "Select an argument handler");
  306. $handlers_join = array(0 => "Select a join handler");
  307. $handlers_rel = array(0 => "Select a relationship handler");
  308. foreach($all_handlers as $handler){
  309. if(preg_match("/views_handler_field/",$handler)){
  310. $handlers_fields[$handler] = $handler;
  311. }
  312. if(preg_match("/views_handler_filter/",$handler)){
  313. $handlers_filters[$handler] = $handler;
  314. }
  315. if(preg_match("/views_handler_sort/",$handler)){
  316. $handlers_sort[$handler] = $handler;
  317. }
  318. if(preg_match("/views_handler_argument/",$handler)){
  319. $handlers_argument[$handler] = $handler;
  320. }
  321. if(preg_match("/_join/",$handler)){
  322. $handlers_join[$handler] = $handler;
  323. }
  324. if(preg_match("/views_handler_relationship/",$handler)){
  325. $handlers_rel[$handler] = $handler;
  326. }
  327. }
  328. // generate a unique $table_id for keeping track of the table
  329. if($mview_id){
  330. $table_id = $mview_id;
  331. } else {
  332. $table_id = $table_name;
  333. }
  334. // Per Row (Fields) --------------
  335. // now iterate through the columns of the materialized view or
  336. // chado table and generate the join and handler fields
  337. foreach ($columns as $column){
  338. $column = trim($column); // trim trailing and leading spaces
  339. preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  340. $column_name = $matches[1];
  341. $column_type = $matches[2];
  342. $form['view_setup_table']["$table_id-$i"] = array(
  343. '#type' => 'markup',
  344. '#prefix' => "<div class=\"fields-new-row\">",
  345. '#suffix' => "</div>"
  346. );
  347. // COLUMN I
  348. $form['view_setup_table']["$table_id-$i"]["fields_name_$table_id-$i"] = array(
  349. '#type' => 'markup',
  350. '#prefix' => "<div class=\"column-one\">",
  351. '#value' => "<span class=\"column-name\">$column_name</span>".
  352. "<br><span class=\"column-type\">$column_type</span>",
  353. '#suffix' => "</div>",
  354. );
  355. $data['field_types'][$column_name] = $column_type;
  356. // COLUMN II
  357. $form['view_setup_table']["$table_id-$i"]['column-2'] = array(
  358. '#type' => 'markup',
  359. '#prefix' => "<div class=\"column-two\">",
  360. '#suffix' => "</div>"
  361. );
  362. // set the default values for the human-readable name and description
  363. $default_name = '';
  364. $default_descrip = '';
  365. if (isset($setup_id) && !isset($form_state['storage']["fields_readable_name_$table_id-$i"])){
  366. $default_name = $default_fields[$column_name]['name'];
  367. $default_descrip = $default_fields[$column_name]['description'];
  368. } else {
  369. $default_name = $form_state['storage']["fields_readable_name_$table_id-$i"];
  370. $default_descrip = $form_state['storage']["fields_description_$table_id-$i"];
  371. }
  372. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_readable_name_$table_id-$i"] = array(
  373. '#type' => 'textfield',
  374. '#title' => 'Human-Readable Name',
  375. '#description' => 'This is the name of the field in the Views UI',
  376. '#required' => TRUE,
  377. '#default_value' => $default_name,
  378. );
  379. $form['view_setup_table']["$table_id-$i"]['column-2']["fields_description_$table_id-$i"] = array(
  380. '#type' => 'textarea',
  381. '#title' => 'Short Description',
  382. '#description' => 'This is the field help in the Views UI',
  383. '#required' => TRUE,
  384. '#cols' => 42,
  385. '#rows' => 3,
  386. '#default_value' => $default_descrip,
  387. );
  388. // COLUMN III
  389. $form['view_setup_table']["$table_id-$i"]['column-3'] = array(
  390. '#type' => 'markup',
  391. '#prefix' => "<div class=\"column-three\">",
  392. '#suffix' => "</div>"
  393. );
  394. // set the default values for the join table and columns
  395. $default_join_table = 0;
  396. $default_join_field = 0;
  397. if(isset($setup_id) && !isset($form_state['storage']["fields_join_$table_id-$i"])){
  398. $default_join_table = $default_joins[$column_name]['left_table'];
  399. $default_join_field = $default_joins[$column_name]['left_field'];
  400. $form_state['storage']["fields_join_$table_id-$i"] = $default_join_table;
  401. $form_state['storage']["fields_join_column_$table_id-$i"] = $default_join_field;
  402. }
  403. else{
  404. $default_join_table = $form_state['storage']["fields_join_$table_id-$i"];
  405. $default_join_field = $form_state['storage']["fields_join_column_$table_id-$i"];
  406. }
  407. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_$table_id-$i"] = array(
  408. '#type' => 'select',
  409. '#prefix' => "<div class=\"fields-column-join\">",
  410. '#suffix' => "</div>",
  411. '#options' => $chado_join_tables,
  412. '#required' => FALSE,
  413. '#default_value' => $default_join_table,
  414. '#ahah' => array(
  415. 'path' => ahah_helper_path(array("view_setup_table","$table_id-$i",'column-3',"fields_join_column_$table_id-$i")),
  416. 'wrapper' => "fields-column-join-column-$table_id-$i",
  417. 'effect' => 'fade',
  418. 'event' => 'change',
  419. 'method' => 'replace',
  420. ),
  421. );
  422. $columns = array();
  423. if($default_join_table){
  424. $table_desc = module_invoke_all('chado_'.$default_join_table.'_schema');
  425. foreach ($table_desc['fields'] as $column => $def){
  426. $columns[$column] = $column;
  427. }
  428. } else {
  429. $columns = array('Select Join Column');
  430. }
  431. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_column_$table_id-$i"] = array(
  432. '#type' => 'select',
  433. '#prefix' => " <div id=\"fields-column-join-column-$table_id-$i\" class=\"fields-column-join-column\">",
  434. '#suffix' => "</div>",
  435. '#options' => $columns,
  436. '#required' => FALSE,
  437. '#default_value' => $default_join_field
  438. );
  439. $default_join_handler = 0;
  440. if(isset($setup_id) && !isset($form_state['storage']["fields_join_handler_$table_id-$i"])){
  441. $default_join_handler = $default_handlers[$column_name]['join']['handler_name'];
  442. $form_state['storage']["fields_join_handler_$table_id-$i"]=$default_join_handler;
  443. }
  444. else {
  445. $default_join_handler = $form_state['storage']["fields_join_handler_$table_id-$i"];
  446. }
  447. $form['view_setup_table']["$table_id-$i"]['column-3']["fields_join_handler_$table_id-$i"] = array(
  448. '#type' => 'select',
  449. '#prefix' => "<div class=\"fields-join-handler\">",
  450. '#suffix' => "</div>",
  451. '#options' => $handlers_join,
  452. '#required' => FALSE,
  453. '#default_value' => $default_join_handler,
  454. );
  455. // COLUMN 4
  456. $form['view_setup_table']["$table_id-$i"]['column-4'] = array(
  457. '#type' => 'markup',
  458. '#prefix' => "<div class=\"column-four\">",
  459. '#suffix' => "</div>"
  460. );
  461. // create the handler fields
  462. $default_field_handler = 0;
  463. if(isset($setup_id) && !isset($form_state['storage']["fields_field_handler_$table_id-$i"])){
  464. $default_field_handler = $default_handlers[$column_name]['field']['handler_name'];
  465. $form_state['storage']["fields_field_handler_$table_id-$i"] = $default_field_handler;
  466. }
  467. else {
  468. $default_field_handler = $form_state['storage']["fields_field_handler_$table_id-$i"];
  469. if(!$default_field_handler){
  470. if($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial'){
  471. $default_field_handler = 'chado_views_handler_field_numeric';
  472. }
  473. elseif(preg_match("/character varying/",$column_type) or $column_type == 'char' or $column_type == 'text'){
  474. $default_field_handler = 'chado_views_handler_field';
  475. }
  476. elseif($column_type == 'boolean'){
  477. $default_field_handler = 'chado_views_handler_field_boolean';
  478. }
  479. elseif($column_type == 'float'){
  480. $default_field_handler = 'chado_views_handler_field_numeric';
  481. }
  482. elseif($column_type == 'datetime'){
  483. $default_field_handler = 'chado_views_handler_field_date';
  484. }
  485. }
  486. }
  487. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_field_handler_$table_id-$i"] = array(
  488. '#type' => 'select',
  489. '#prefix' => "<div class=\"fields-field-handler\">",
  490. '#suffix' => "</div>",
  491. '#options' => $handlers_fields,
  492. '#required' => FALSE,
  493. '#default_value' => $default_field_handler,
  494. );
  495. $default_filter_handler = 0;
  496. if(isset($setup_id) && !isset($form_state['storage']["fields_filter_handler_$table_id-$i"])){
  497. $default_filter_handler = $default_handlers[$column_name]['filter']['handler_name'];
  498. $form_state['storage']["fields_filter_handler_$table_id-$i"]= $default_filter_handler;
  499. }
  500. else {
  501. $default_filter_handler = $form_state['storage']["fields_filter_handler_$table_id-$i"];
  502. if(!$default_filter_handler){
  503. if($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial'){
  504. $default_filter_handler = 'chado_views_handler_filter_numeric';
  505. }
  506. elseif(preg_match("/^character varying/",$column_type) or $column_type == 'char' or $column_type == 'text'){
  507. $default_filter_handler = 'chado_views_handler_filter_string';
  508. }
  509. elseif($column_type == 'boolean'){
  510. $default_filter_handler = 'chado_views_handler_filter_boolean';
  511. }
  512. elseif($column_type == 'float'){
  513. $default_filter_handler = 'chado_views_handler_filter_float';
  514. }
  515. elseif($column_type == 'datetime'){
  516. $default_filter_handler = 'chado_views_handler_filter_date';
  517. }
  518. }
  519. }
  520. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_filter_handler_$table_id-$i"] = array(
  521. '#type' => 'select',
  522. '#prefix' => "<div class=\"fields-filter-handler\">",
  523. '#suffix' => "</div>",
  524. '#options' => $handlers_filters,
  525. '#required' => FALSE,
  526. '#default_value' => $default_filter_handler,
  527. );
  528. $default_sort_handler = 0;
  529. if(isset($setup_id) && !isset($form_state['storage']["fields_sort_handler_$table_id-$i"])){
  530. $default_sort_handler = $default_handlers[$column_name]['sort']['handler_name'];
  531. $form_state['storage']["fields_sort_handler_$table_id-$i"] = $default_sort_handler;
  532. }
  533. else {
  534. $default_sort_handler = $form_state['storage']["fields_sort_handler_$table_id-$i"];
  535. if(!$default_sort_handler){
  536. if($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial'){
  537. $default_sort_handler = 'chado_views_handler_sort';
  538. }
  539. elseif(preg_match("/character varying/",$column_type) or $column_type == 'char' or $column_type == 'text'){
  540. $default_sort_handler = 'chado_views_handler_sort';
  541. }
  542. elseif($column_type == 'boolean'){
  543. $default_sort_handler = 'chado_views_handler_sort';
  544. }
  545. elseif($column_type == 'float'){
  546. $default_sort_handler = 'chado_views_handler_sort';
  547. }
  548. elseif($column_type == 'datetime'){
  549. $default_sort_handler = 'chado_views_handler_sort_date';
  550. }
  551. }
  552. }
  553. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_sort_handler_$table_id-$i"] = array(
  554. '#type' => 'select',
  555. '#prefix' => "<div class=\"fields-sort-handler\">",
  556. '#suffix' => "</div>",
  557. '#options' => $handlers_sort,
  558. '#required' => FALSE,
  559. '#default_value' => $default_sort_handler,
  560. );
  561. $default_argument_handler = 0;
  562. if(isset($setup_id) && !isset($form_state['storage']["fields_argument_handler_$table_id-$i"])){
  563. $default_argument_handler = $default_handlers[$column_name]['argument']['handler_name'];
  564. $form_state['storage']["fields_argument_handler_$table_id-$i"]=$default_argument_handler ;
  565. }
  566. else {
  567. $default_argument_handler = $form_state['storage']["fields_argument_handler_$table_id-$i"];
  568. if(!$default_argument_handler){
  569. if($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial'){
  570. $default_argument_handler = 'views_handler_argument_numeric';
  571. }
  572. elseif(preg_match("/character varying/",$column_type) or $column_type == 'char' or $column_type == 'text'){
  573. $default_argument_handler = 'views_handler_argument_string';
  574. }
  575. elseif($column_type == 'boolean'){
  576. $default_argument_handler = 'views_handler_argument_numeric';
  577. }
  578. elseif($column_type == 'float'){
  579. $default_argument_handler = 'views_handler_argument_numeric';
  580. }
  581. elseif($column_type == 'datetime'){
  582. $default_argument_handler = 'views_handler_argument_date';
  583. }
  584. }
  585. }
  586. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_argument_handler_$table_id-$i"] = array(
  587. '#type' => 'select',
  588. '#prefix' => "<div class=\"fields-argument-handler\">",
  589. '#suffix' => "</div>",
  590. '#options' => $handlers_argument,
  591. '#required' => FALSE,
  592. '#default_value' => $default_argument_handler,
  593. );
  594. $default_relationship_handler = 0;
  595. if(isset($setup_id) && !isset($form_state['storage']["fields_relationship_handler_$table_id-$i"])){
  596. $default_relationship_handler = $default_handlers[$column_name]['relationship']['handler_name'];
  597. $form_state['storage']["fields_relationship_handler_$table_id-$i"]=$default_relationship_handler;
  598. }
  599. else {
  600. $default_relationship_handler = $form_state['storage']["fields_relationship_handler_$table_id-$i"];
  601. if(!$default_relationship_handler){
  602. if($column_type == 'integer' or $column_type == 'int' or $column_type == 'serial'){
  603. $default_relationship_handler = 'views_handler_relationship';
  604. }
  605. elseif(preg_match("/character varying/",$column_type) or $column_type == 'char' or $column_type == 'text'){
  606. $default_relationship_handler = 'views_handler_relationship';
  607. }
  608. elseif($column_type == 'boolean'){
  609. $default_relationship_handler = 'views_handler_relationship';
  610. }
  611. elseif($column_type == 'float'){
  612. $default_relationship_handler = 'views_handler_relationship';
  613. }
  614. elseif($column_type == 'datetime'){
  615. $default_relationship_handler = 'views_handler_relationship';
  616. }
  617. }
  618. }
  619. $form['view_setup_table']["$table_id-$i"]['column-4']["fields_relationship_handler_$table_id-$i"] = array(
  620. '#type' => 'select',
  621. '#prefix' => "<div class=\"fields-relationship-handler\">",
  622. '#suffix' => "</div>",
  623. '#options' => $handlers_rel,
  624. '#required' => FALSE,
  625. '#default_value' => $default_relationship_handler,
  626. );
  627. $i++;
  628. }
  629. $form['view_setup_table']['save'] = array(
  630. '#type' => 'submit',
  631. '#value' => t('Save'),
  632. );
  633. $data['row_count'] = $i - 1;
  634. }
  635. //use this to put values into $form_state['values']
  636. $form['data'] = array();
  637. //need to find out if storing $form['data'][$key]['#value'] = $value <- is an issue
  638. //since it will give me errors if i try to stare an array instead of $value
  639. //and yet $value can be an array ie "field_types"
  640. foreach ($data as $key => $value) {
  641. $form['data'][$key] = array(
  642. '#type' => 'hidden',
  643. '#value' => $value,
  644. );
  645. }
  646. $form['#redirect'] = 'admin/tripal/views/integration';
  647. return $form;
  648. }
  649. /**
  650. * Purpose: validates the tripal_views_integration_form after submission
  651. *
  652. * @param $form
  653. * The form object which is passed automatically by drupal
  654. *
  655. * @param &$form_state
  656. * The form state pbject which is passed automatically by drupal
  657. *
  658. * @ingroup tripal_views_integration
  659. */
  660. function tripal_views_integration_form_validate($form, &$form_state){
  661. $name_array = explode(" ", $form_state['values']['row_name']);
  662. $mview_id = $form_state['values']['mview_id'];
  663. $table_name = $form_state['values']['table_name'];
  664. // if(count($name_array) > 1){
  665. // form_set_error($form_state['values']['row_name'], 'The View type name must be a single word only.');
  666. // }
  667. if($mview_id and $table_name){
  668. form_set_error($form_state['values']['mview_id'], 'Please select either a materialized view or a Chado table but not both');
  669. }
  670. if(!$mview_id and !$table_name){
  671. form_set_error($form_state['values']['mview_id'], 'Please select either a materialized view or a Chado table');
  672. }
  673. // TODO: do we need to require that a handler be set for each field and each type of handler?
  674. }
  675. /**
  676. * Purpose: inserts or updates the record in the tripal views integration
  677. * tables. This function is only called if validation is passed.
  678. *
  679. * @param $form
  680. * The form object which is passed automatically by drupal
  681. *
  682. * @param &$form_state
  683. * The form state pbject which is passed automatically by drupal
  684. *
  685. * @ingroup tripal_views_integration
  686. */
  687. function tripal_views_integration_form_submit($form, &$form_state){
  688. $name = $form_state['values']['row_name'];
  689. $mview_id = $form_state['values']['mview_id'];
  690. $table_name = $form_state['values']['table_name'];
  691. $setup_id = $form_state['values']['setup_id'];
  692. $priority = $form_state['values']['row_priority'];
  693. $comment = $form_state['values']['row_description'];
  694. // get details about this mview
  695. if ($mview_id) {
  696. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id";
  697. $mview = db_fetch_object(db_query($sql));
  698. $table_name = $mview->mv_table;
  699. $table_id = $mview_id;
  700. $type = 'mview';
  701. } else {
  702. $type = 'chado';
  703. $table_id = $table_name;
  704. }
  705. // If this is for a materialized view then we want to add/update that record
  706. $tripal_views_record = array();
  707. if($mview_id){
  708. // get details about this mview
  709. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = $mview_id";
  710. $mview = db_fetch_object(db_query($sql));
  711. // build the record for insert/update
  712. $tripal_views_record = array(
  713. 'mview_id' => $mview_id,
  714. 'table_name' => $mview->mv_table,
  715. 'name' => $name,
  716. 'priority' => $priority,
  717. 'comment' => $comment,
  718. );
  719. }
  720. // if a chado table then...
  721. else {
  722. // build the record for insert/update
  723. $tripal_views_record = array(
  724. 'table_name' => $table_name,
  725. 'name' => $name,
  726. 'priority' => $priority,
  727. 'comment' => $comment,
  728. );
  729. }
  730. // perform the insert or update
  731. if(!$setup_id){ // this is an insert
  732. if(!drupal_write_record('tripal_views', $tripal_views_record)){
  733. drupal_set_message("Failed to add record.");
  734. return;
  735. }
  736. } else { // this is an update
  737. $tripal_views_record['setup_id'] = $setup_id;
  738. if(!drupal_write_record('tripal_views', $tripal_views_record,array('setup_id'))){
  739. drupal_set_message("Failed to update record.");
  740. return;
  741. }
  742. }
  743. // if this is an update then clean out the existing joins and handlers so we can add new ones
  744. if($setup_id){
  745. db_query("DELETE FROM {tripal_views_field} WHERE setup_id = %d",$setup_id);
  746. db_query("DELETE FROM {tripal_views_join} WHERE setup_id = %d",$setup_id);
  747. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id = %d",$setup_id);
  748. }
  749. // iterate through the columns of the form and add
  750. // the joins if provided, and the handlers
  751. $i = 1;
  752. foreach ($form_state['values']['field_types'] as $key => $value){
  753. // add the field definition
  754. $view_field_record = array(
  755. 'setup_id' => $tripal_views_record['setup_id'],
  756. 'column_name' => $key,
  757. 'name' => $form_state['values']["fields_readable_name_$table_id-$i"],
  758. 'description' => $form_state['values']["fields_description_$table_id-$i"],
  759. 'type' => $value,
  760. );
  761. drupal_write_record('tripal_views_field', $view_field_record);
  762. // first add the join if it exists
  763. $left_table = $form_state['values']["fields_join_$table_id-$i"];
  764. $left_column = $form_state['values']["fields_join_column_$table_id-$i"];
  765. if($left_column){
  766. $view_join_record = array(
  767. 'setup_id' => $tripal_views_record['setup_id'],
  768. 'base_table' => $mview->mv_table,
  769. 'base_field' => $key,
  770. 'left_table' => $left_table,
  771. 'left_field' => $left_column,
  772. );
  773. // write the new joins to the database
  774. drupal_write_record('tripal_views_join', $view_join_record);
  775. }
  776. // add the hanlders
  777. $handlers = array('filter','field','sort','argument','join','relationship');
  778. foreach($handlers as $handler){
  779. $handler_name = $form_state['values']["fields_".$handler."_handler_$table_id-$i"];
  780. if($handler_name){
  781. $handler_record = array(
  782. 'setup_id' => $tripal_views_record['setup_id'],
  783. 'column_name' => $key,
  784. 'handler_type' => $handler,
  785. 'handler_name' => $handler_name,
  786. );
  787. drupal_write_record('tripal_views_handlers', $handler_record);
  788. }
  789. }
  790. $i++;
  791. }
  792. if($setup_id){
  793. drupal_set_message('Record Updated');
  794. } else {
  795. drupal_set_message('Record Added');
  796. }
  797. // now clear all the caches so that Drupal views picks up our chages
  798. views_invalidate_cache();
  799. }
  800. /**
  801. * Purpose: this function queries all modules currently enabled on the site
  802. * looking for custom handlers and returns a list of all available handerls.
  803. * The base View handlers are also included.
  804. *
  805. * @return
  806. * Returns an array of handler names
  807. *
  808. * @ingroup tripal_views_integration
  809. */
  810. function tripal_views_integration_discover_handlers() {
  811. $handlers = array();
  812. // Get handlers from all modules.
  813. foreach (module_implements('views_handlers') as $module) {
  814. $function = $module . '_views_handlers';
  815. $result = $function();
  816. if (!is_array($result)) {
  817. continue;
  818. }
  819. foreach ($result['handlers'] as $handler => $parent){
  820. $handlers[] = $handler;
  821. }
  822. }
  823. // these handlers are hard coded because I could not
  824. // get the views_handlers() function to be called
  825. // in the code above. However, we will be creating
  826. // Chado wrappers for many of these and once that work
  827. // is done these will no longer be needed.
  828. // argument handlers
  829. $handlers[] = 'views_handler_argument';
  830. $handlers[] = 'views_handler_argument_numeric';
  831. $handlers[] = 'views_handler_argument_formula';
  832. $handlers[] = 'views_handler_argument_date';
  833. $handlers[] = 'views_handler_argument_string';
  834. $handlers[] = 'views_handler_argument_many_to_one';
  835. $handlers[] = 'views_handler_argument_null';
  836. // field handlers
  837. $handlers[] = 'views_handler_field';
  838. $handlers[] = 'views_handler_field_date';
  839. $handlers[] = 'views_handler_field_boolean';
  840. $handlers[] = 'views_handler_field_markup';
  841. $handlers[] = 'views_handler_field_xss';
  842. $handlers[] = 'views_handler_field_url';
  843. $handlers[] = 'views_handler_field_file_size';
  844. $handlers[] = 'views_handler_field_prerender_list';
  845. $handlers[] = 'views_handler_field_numeric';
  846. $handlers[] = 'views_handler_field_custom';
  847. $handlers[] = 'views_handler_field_counter';
  848. // filter handlers
  849. $handlers[] = 'views_handler_filter';
  850. $handlers[] = 'views_handler_filter_equality';
  851. $handlers[] = 'views_handler_filter_string';
  852. $handlers[] = 'views_handler_filter_boolean_operator';
  853. $handlers[] = 'views_handler_filter_boolean_operator_string';
  854. $handlers[] = 'views_handler_filter_in_operator';
  855. $handlers[] = 'views_handler_filter_numeric';
  856. $handlers[] = 'views_handler_filter_float';
  857. $handlers[] = 'views_handler_filter_date';
  858. $handlers[] = 'views_handler_filter_many_to_one';
  859. // relationship handlers
  860. $handlers[] = 'views_handler_relationship';
  861. // sort handlers
  862. $handlers[] = 'views_handler_sort';
  863. $handlers[] = 'views_handler_sort_formula';
  864. $handlers[] = 'views_handler_sort_date';
  865. $handlers[] = 'views_handler_sort_menu_hierarchy';
  866. $handlers[] = 'views_handler_sort_random';
  867. // join handler
  868. $handlers[] = 'views_join';
  869. return $handlers;
  870. }