tripal_views_integration.inc 43 KB

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