tripal_views_integration.inc 40 KB

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