tripal_views_integration.inc 39 KB

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