tripal_chado_views.api.inc 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. <?php
  2. /**
  3. * @file
  4. * API functions for Chado Views Integration
  5. */
  6. /**
  7. * Programatically enable view
  8. *
  9. * This should be used in a hook_menu definition as the callback to provide a link
  10. * to enable the view (first example). It can also be called directly if needed (second example).
  11. * @code
  12. // Create a URL that when the user navigates there, a given view will be enabled.
  13. // You will still need to provide a link to this menu item somewhere appropriate (ie: an admin landing page).
  14. function mymodule_menu() {
  15. $items = array();
  16. // Create one of these for each of your default views
  17. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/enable'] = array(
  18. 'title' => 'Enable <VIEW-HUMAN-READABLE-NAME>',
  19. 'page callback' => 'tripal_enable_view',
  20. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  21. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  22. 'type' => MENU_CALLBACK,
  23. );
  24. return $items;
  25. }
  26. // Call this function directly to disable a view
  27. // The example shows enabling your own default view when your module is enabled.
  28. // This might be useful if you disable your view when your module is disabled.
  29. function mymodule_enable() {
  30. $view_name = '<VIEW-MACHINE-NAME>';
  31. tripal_enable_view($view_name);
  32. }
  33. * @endcode
  34. *
  35. * @param $view_name
  36. * The machine-name of the view to be enabled
  37. * @param $redirect_link
  38. * The path to redirect to. FALSE if no redirect needed
  39. *
  40. * @ingroup tripal_views_api
  41. */
  42. function tripal_enable_view($view_name, $redirect_link = FALSE) {
  43. $status = variable_get('views_defaults', array());
  44. if (isset($status[$view_name])) {
  45. $status[$view_name] = FALSE;
  46. variable_set('views_defaults', $status);
  47. drupal_set_message("Successfully Enabled $view_name");
  48. }
  49. else {
  50. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to enable this view.",'notice');
  51. }
  52. if ($redirect_link) {
  53. drupal_goto($redirect_link);
  54. }
  55. }
  56. /**
  57. * Programatically disable view.
  58. *
  59. * This should be used in a hook_menu definition as the callback to provide a link
  60. * to disable the view (first example). It can also be called directly if needed (second example).
  61. * @code
  62. // Create a URL that when the user navigates there, a given view will be disabled.
  63. // You will still need to provide a link to this menu item somewhere appropriate.
  64. function mymodule_menu() {
  65. $items = array();
  66. // Create one of these for each of your default views
  67. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/disable'] = array(
  68. 'title' => 'Disable <VIEW-HUMAN-READABLE-NAME>',
  69. 'page callback' => 'tripal_disable_view',
  70. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  71. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  72. 'type' => MENU_CALLBACK,
  73. );
  74. return $items;
  75. }
  76. // Call this function directly to disable a view
  77. // The example shows disabling your own default view when your module is uninstalled
  78. function mymodule_uninstall() {
  79. $view_name = '<VIEW-MACHINE-NAME>';
  80. tripal_disable_view($view_name);
  81. }
  82. * @endcode
  83. *
  84. * @param $view_name
  85. * The machine-name of the view to be enabled
  86. * @param $redirect_link
  87. * The path to redirect to. FALSE if no redirect needed
  88. *
  89. * @ingroup tripal_views_api
  90. */
  91. function tripal_disable_view($view_name, $redirect_link = FALSE) {
  92. $status = variable_get('views_defaults', array());
  93. if (isset($status[$view_name])) {
  94. $status[$view_name] = TRUE;
  95. variable_set('views_defaults', $status);
  96. drupal_set_message("Disabled $view_name");
  97. }
  98. else {
  99. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to disable this view.",'notice');
  100. }
  101. if ($redirect_link) {
  102. drupal_goto($redirect_link);
  103. }
  104. }
  105. /**
  106. * Remove any drupal fields from a chado-based default view definition if chado is external.
  107. * This ensures compatibility with an external chado database.
  108. *
  109. * You should be calling this function in your hook_views_default_views(). This function
  110. * will only remove drupal tables if chado is external; thus you do not need to worry about
  111. * checking yourself. For example, the following is a good hook_views_default_views():
  112. * @code
  113. function mymodule_views_default_views() {
  114. $views = array();
  115. // NOTE: <VIEW-TYPE> describes the type of view:
  116. // - 'admin' for views used for administration of your module
  117. // - 'search' for views used to search data
  118. // - 'list' for views used primarily as data listings
  119. // <VIEW-HUMAN-READABLE-NAME>
  120. $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  121. $view = tripal_make_view_compatible_with_external($view);
  122. $views[$view->name] = $view;
  123. // <VIEW-HUMAN-READABLE-NAME>
  124. $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  125. $view = tripal_make_view_compatible_with_external($view);
  126. $views[$view->name] = $view;
  127. return $views;
  128. }
  129. function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  130. // PASTE VIEWS EXPORT CODE HERE
  131. return $view;
  132. }
  133. function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  134. // PASTE VIEWS EXPORT CODE HERE
  135. return $view;
  136. }
  137. * @endcode
  138. * Notice that the actual views export code is in a separate function. This makes your
  139. * hook_views_default_views() more readable.
  140. *
  141. * NOTE: Currently assumes all tables not in the tripal views integration tables are Drupal tables.
  142. *
  143. * @param $view
  144. * The default view definition object
  145. * @return
  146. * The default view with all relationships, fields, filters, sorts, arguments for
  147. * Drupal tables removed.
  148. */
  149. function tripal_make_view_compatible_with_external($view) {
  150. $remove_table = array();
  151. // First check that the base table for the view is a chado table
  152. // If it's not then don't do any filtering
  153. $setup_id = tripal_is_table_integrated($view->base_table);
  154. if (!$setup_id) {
  155. return $view;
  156. }
  157. // IF chado is external then remove all config relating to drupal tables
  158. $is_local = isset($GLOBALS["chado_is_local"]) && $GLOBALS["chado_is_local"];
  159. if (!$is_local) {
  160. // Iterate through all displays
  161. foreach ($view->display as $display_name => $display) {
  162. $display_options = $display->handler->display->display_options;
  163. $sections = array('fields', 'filters', 'sorts', 'relationships');
  164. foreach ($sections as $section) {
  165. $display_options[$section] = (isset($display_options[$section])) ? $display_options[$section] : array();
  166. foreach ($display_options[$section] as $key => $defn) {
  167. // If the table has not already been encountered; check if it's in tripal_views
  168. if (!isset($remove_table[ $defn['table'] ])) {
  169. // If the table is view then this is a general handler; thus keep
  170. if ($defn['table'] == 'views') {
  171. $remove_table[ $defn['table'] ] = FALSE;
  172. }
  173. // If this table is integrated then it is chado; thus keep
  174. $setup_id = tripal_is_table_integrated($defn['table']);
  175. if ($setup_id) {
  176. $remove_table[ $defn['table'] ] = FALSE;
  177. }
  178. else {
  179. $remove_table[ $defn['table'] ] = TRUE;
  180. }
  181. }
  182. // Based on the $remove_table array, unset this field if its from a drupal table
  183. if ($remove_table[ $defn['table'] ]) {
  184. unset($view->display[$display_name]->handler->display->display_options[$section][$key]);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. return $view;
  191. }
  192. /**
  193. * Retrieve the priority of the lightest priority for a given table.
  194. *
  195. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  196. * and -10 is of highest priority.
  197. *
  198. * @param $table_name
  199. * The name of the table to retrieve the setup ID for. This can be either a materialized
  200. * view or a chado table
  201. *
  202. * @return
  203. * returns the lowest priority. If the table has not been integrated, a priority of 10
  204. * is returned.
  205. *
  206. * @ingroup tripal_chado_views_api
  207. */
  208. function tripal_get_lightest_views_integration_priority($table_name) {
  209. // D7 TODO: Check DBTNG changes work
  210. $sql = "SELECT priority FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  211. $setup = db_query($sql, array(':table' => $table_name));
  212. $setup = $setup->fetchObject();
  213. if ($setup) {
  214. return $setup->priority;
  215. }
  216. else {
  217. // default priority is 10
  218. return 10;
  219. }
  220. }
  221. /**
  222. * Retrieve the views integration setup_id with the lightest priority for a given table
  223. *
  224. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  225. * and -10 is of highest priority.
  226. *
  227. * @param $table_name
  228. * The name of the table to retrieve the setup ID for. This can be either a materialized
  229. * view or a chado table
  230. *
  231. * @return
  232. * On success, the setup_id to use for integration of this table; otherwise FALSE
  233. *
  234. * @ingroup tripal_chado_views_api
  235. */
  236. function tripal_get_lightest_views_integration_setup($table_name) {
  237. // D7 TODO: Check DBTNG changes work
  238. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  239. $setup = db_query($sql, array(':table' => $table_name));
  240. $setup = $setup->fetchObject();
  241. if ($setup) {
  242. return $setup->setup_id;
  243. }
  244. else {
  245. return FALSE;
  246. }
  247. }
  248. /**
  249. * Retrieve the views integration setup_id with the given priority/table combination.
  250. *
  251. * @param $table_name
  252. * The name of the table to retrieve the setup ID for. This can be either a materialized
  253. * view or a chado table
  254. * @param $priority
  255. * The priority of the integration to retrieve the setup_id for
  256. *
  257. * @return
  258. * On success, the setup_id to use for integration of this table; otherwise FALSE
  259. *
  260. * @ingroup tripal_chado_views_api
  261. */
  262. function tripal_get_views_integration_setup_id($table_name, $priority) {
  263. // D7 TODO: Check DBTNG changes work
  264. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority ORDER BY priority ASC";
  265. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  266. $setup = $setup->fetchObject();
  267. if ($setup) {
  268. return $setup->setup_id;
  269. }
  270. else {
  271. return FALSE;
  272. }
  273. }
  274. /**
  275. * Check to see if this table already has an integration record with the given priority.
  276. *
  277. * @param $table_name
  278. * The name of the table to check for integration
  279. * @param $priority (optional)
  280. * The priority of record to check for
  281. *
  282. * @return
  283. * If the table is already integrated, the setup_id of the existing integration
  284. * record is returned (If priority is not specified this will be the lightest record);
  285. * Otherwise the table is not already integrated and FALSE is returned.
  286. *
  287. * @ingroup tripal_chado_views_api
  288. */
  289. function tripal_is_table_integrated($table_name, $priority = NULL) {
  290. if ($priority) {
  291. // D7 TODO: Check DBTNG changes work
  292. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority";
  293. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  294. $setup = $setup->fetchObject();
  295. }
  296. else {
  297. // D7 TODO: Check DBTNG changes work
  298. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  299. $setup = db_query($sql, array(':table' => $table_name));
  300. $setup = $setup->fetchObject();
  301. }
  302. if ($setup) {
  303. return $setup->setup_id;
  304. }
  305. else {
  306. return FALSE;
  307. }
  308. }
  309. /**
  310. * Checks if you are dealing with the lightest priority setup for a given table. This is a
  311. * good way to determine whether your modules integration is being used by views.
  312. *
  313. * @param $setup_id
  314. * The ID of the setup to check (is this setup the lightest one?)
  315. * @param $table_name
  316. * The name of the table associated with this setup
  317. *
  318. * @return TRUE is this is the lightest priority; FALSE otherwise
  319. *
  320. * @ingroup tripal_chado_views_api
  321. */
  322. function tripal_is_lightest_priority_setup($setup_id, $table_name) {
  323. $lightest_priority_setup_id = tripal_get_lightest_views_integration_setup($table_name);
  324. if ($lightest_priority_setup_id == $setup_id) {
  325. return TRUE;
  326. }
  327. else {
  328. return FALSE;
  329. }
  330. }
  331. /**
  332. * Rebuilds all the default integrations.
  333. *
  334. * This essentially clears the cache in case you mess up the integrations in your site.
  335. * This should not be used during module development since it really only rebuilds the
  336. * integrations described by all enabled modules in the site and if $delete_first is
  337. * TRUE it can delete custom integrations created by site administrators which will not
  338. * make your module popular.
  339. *
  340. * @param $delete_first
  341. * If TRUE then all integrations are first deleted.
  342. *
  343. * @ingroup tripal_chado_views_api
  344. */
  345. function tripal_rebuild_views_integrations($delete_first = FALSE) {
  346. if ($delete_first) {
  347. tripal_chado_views_delete_all_integrations();
  348. }
  349. tripal_chado_views_integrate_all_chado_tables();
  350. // TODO: the function above should have a return value from which we can
  351. // determine if the message below is approprite.
  352. drupal_set_message('Successfully rebuilt default Chado Views Integrations');
  353. }
  354. /**
  355. * Add views integration records into the tripal_views* tables.
  356. *
  357. * This is the programatic way to add your own integrations to the tripal views integrations
  358. * list. Keep in mind that the priority set in your $defn_array needs to be lighter than
  359. * any existing integrations to be used by views and that it should still be below 0
  360. * in order to allow site administrators to override it should they need to.
  361. *
  362. * @param $defn_array
  363. * An array describing the structure and fields of the table
  364. *
  365. * @return
  366. * True/False if completed successfully/not
  367. *
  368. * Example usage (in hook_install()):
  369. * @code
  370. $defn_array = array(
  371. 'table' => 'feature', //tablename or materialized view name
  372. 'name' => 'Sequence Features', // Human readable name
  373. 'type' => 'chado', //either chado or mview depending on tablename
  374. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  375. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  376. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  377. 'fields' => array(
  378. 'feature_id' => array(
  379. 'name' => 'feature_id', //field name in database
  380. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  381. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  382. 'type' => 'int', // the type of field
  383. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  384. 'field' => array(
  385. 'name' => 'chado_views_handler_numeric' //name of handler
  386. ),
  387. 'filter' => array( ... ),
  388. ...
  389. ),
  390. // Describe any joins involving this field.
  391. // Note: you can include both foreign keys (feature.type_id => cvterm.cvterm_id)
  392. // and referring tables (ie: feature.feature_id <= feature_relationship.subject_id)
  393. 'joins' => array(
  394. 'feature_relationship' => array( //table to join to.
  395. 'subject_id' => array( //field in above table (feature_relationship)
  396. 'table' => 'featureprop', //table to join to
  397. 'field' => 'feature_id', //field in above table (feature_relationship)
  398. 'handler' => 'views_join', //handler to use for joining
  399. 'relationship_handler' => 'views_handler_relationship', //handler to use when a relationship is added.
  400. 'relationship_only' => FALSE, //whether to join automatically (FALSE) or not (TRUE)
  401. ),
  402. ...
  403. ),
  404. ...
  405. ),
  406. )
  407. ),
  408. );
  409. tripal_add_views_integration($defn_array);
  410. * @endcode
  411. *
  412. * @ingroup tripal_chado_views_api
  413. */
  414. function tripal_add_views_integration($defn_array, $setup_id = FALSE) {
  415. $no_errors = TRUE;
  416. if (empty($defn_array['table'])) {
  417. tripal_report_error('tripal_views', TRIPAL_WARNING, 'Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array,TRUE)));
  418. $no_errors = FALSE;
  419. return $no_errors;
  420. }
  421. // First insert into tripal_views
  422. $view_record = array(
  423. 'table_name' => $defn_array['table'],
  424. 'name' => $defn_array['name'],
  425. 'comment' => $defn_array['description'],
  426. 'priority' => $defn_array['priority'],
  427. 'base_table' => $defn_array['base_table'],
  428. );
  429. if ($setup_id) {
  430. $view_record['setup_id'] = $setup_id;
  431. }
  432. if ($defn_array['type'] == 'mview') {
  433. $mview = db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table=:table", array(':table' => $defn_array['table']));
  434. $mview = $mview->fetchObject();
  435. $view_record['mview_id'] = $mview->mview_id;
  436. if (!$mview->mview_id) {
  437. return FALSE;
  438. }
  439. }
  440. if ($view_record['name']) { // && $view_record['comment']) { # SPF: commented out 9/24/2012 .. It's not required on the form
  441. if (isset($defn_array['additional_content'])) {
  442. // D7 TODO: Check DBTNG changes work
  443. $setup = db_query(
  444. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  445. array(':table' => $view_record['table_name'], ':priority' => $view_record['priority'])
  446. );
  447. $setup = $setup->fetchObject();
  448. if (empty($setup->setup_id)) {
  449. $status = drupal_write_record('tripal_views', $view_record);
  450. }
  451. else {
  452. $view_record['setup_id'] = $setup->setup_id;
  453. $status = drupal_write_record('tripal_views', $view_record, 'setup_id');
  454. }
  455. }
  456. else {
  457. $status = drupal_write_record('tripal_views', $view_record);
  458. }
  459. }
  460. else {
  461. $status = FALSE;
  462. drupal_set_message(t('Unable to integrate "%table" table due to a missing name field.', array('%table' => $defn_array['table'])), 'error');
  463. }
  464. if ($status) {
  465. // Need to update the tripal_views record so base_table can be false
  466. // this is a fix because drupal_write_record() puts in defaults if !isset()
  467. // and a variable is considered not set if it's null!
  468. // D7 TODO: Check DBTNG changes work
  469. db_query(
  470. "UPDATE {tripal_views} SET base_table=:base WHERE table_name=:table AND priority=:priority",
  471. array(
  472. ':base' => $defn_array['base_table'],
  473. ':table' => $defn_array['table'],
  474. ':priority' => $defn_array['priority']
  475. )
  476. );
  477. // Insert Field Definitions
  478. foreach ($defn_array['fields'] as $key => $field) {
  479. // Set some defaults.
  480. $field['name'] = (isset($field['name'])) ? $field['name'] : $key;
  481. $field['title'] = (isset($field['title'])) ? $field['title'] : $field['name'];
  482. $field['type'] = (isset($field['type'])) ? $field['type'] : 'text';
  483. $field['description'] = (isset($field['description'])) ? $field['description'] : $field['name'];
  484. $field['handlers'] = (isset($field['handlers'])) ? $field['handlers'] : array('field' => array('name' => 'views_handler_field'));
  485. // Build the field record.
  486. $field_record = array(
  487. 'setup_id' => $view_record['setup_id'],
  488. 'column_name' => $field['name'],
  489. 'name' => $field['title'],
  490. 'description' => $field['description'],
  491. 'type' => $field['type'],
  492. );
  493. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  494. if (isset($defn_array['additional_content'])) {
  495. // D7 TODO: Check DBTNG changes work
  496. $is_present = db_query(
  497. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  498. array(
  499. ':column' => $field_record['column_name'],
  500. ':setup' => $field_record['setup_id']
  501. )
  502. );
  503. $is_present = $is_present->fetchField();
  504. if (!$is_present) {
  505. $status = drupal_write_record('tripal_views_field', $field_record);
  506. }
  507. else {
  508. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  509. }
  510. }
  511. else {
  512. $status = drupal_write_record('tripal_views_field', $field_record);
  513. }
  514. }
  515. else {
  516. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  517. $status = FALSE;
  518. }
  519. if ($status) {
  520. // Insert Handler Definitions
  521. foreach ($field['handlers'] as $handler_type => $handler) {
  522. $handler_record = array(
  523. 'setup_id' => $view_record['setup_id'],
  524. 'column_name' => $field['name'],
  525. 'handler_type' => $handler_type,
  526. 'handler_name' => $handler['name'],
  527. 'arguments' => serialize($handler)
  528. );
  529. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  530. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  531. }
  532. else {
  533. $status = FALSE;
  534. }
  535. if (!$status) {
  536. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  537. $no_errors = FALSE;
  538. }
  539. }
  540. // Insert Joins
  541. // Note: The new defn_array structure accounts for 1+ joins to the same
  542. // table (ie: feature_relationship => feature : subject_id & object_id)
  543. // by making $field['joins'] an array of left_field keys.
  544. if (!is_array($field['joins'])) {
  545. $field['joins'] = array();
  546. }
  547. foreach ($field['joins'] as $joins) {
  548. // To keep backwards compatibility with the old defn_array which just
  549. // jumped right into the table definition allowing only a single join,
  550. // we need to check for old defn_arrays and transform them into the
  551. // new format.
  552. if (isset($joins['table'])) {
  553. $left_field = $joins['field'];
  554. $joins = array(
  555. $left_field => $joins
  556. );
  557. }
  558. // Loop on left fields
  559. foreach ($joins as $left_field => $join) {
  560. $join_record = array(
  561. 'setup_id' => $view_record['setup_id'],
  562. 'base_table' => $defn_array['table'],
  563. 'base_field' => $field['name'],
  564. 'left_table' => $join['table'],
  565. 'left_field' => $left_field,
  566. );
  567. $join_record['handler'] = (!empty($join['handler'])) ? $join['handler'] : 'views_join';
  568. $join_record['relationship_handler'] = (!empty($join['relationship_handler'])) ? $join['relationship_handler'] : 'views_handler_relationship';
  569. $join_record['relationship_only'] = (!empty($join['relationship_only'])) ? $join['relationship_only'] : 0;
  570. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $left_field) {
  571. $status = drupal_write_record('tripal_views_join', $join_record);
  572. }
  573. else {
  574. $status = FALSE;
  575. }
  576. if (!$status) {
  577. drupal_set_message(
  578. t(
  579. 'Unable to join %left_table.%left_field with %table.%field',
  580. array(
  581. '%left_table' => $join['table'],
  582. '%left_field' => $left_field,
  583. '%table' => $defn_array['table'],
  584. '%field' => $field['name']
  585. )
  586. ),
  587. 'error'
  588. );
  589. $no_errors = FALSE;
  590. }
  591. }
  592. }
  593. }
  594. else {
  595. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  596. $no_errors = FALSE;
  597. }
  598. }
  599. }
  600. else {
  601. drupal_set_message(t('Unable to set default tripal views integration'), 'error');
  602. $no_errors = FALSE;
  603. }
  604. return $no_errors;
  605. }
  606. /**
  607. * Export Views integration records.
  608. *
  609. * This is a great way to create your own integration since it returns an
  610. * already defined integration in array form that you can modify. After
  611. * modifications simply set the priority to something lighter (but still
  612. * below 0) than any existing integrations and use
  613. * tripal_add_views_integration() to add it to the list of integrations.
  614. *
  615. * @param $setup_id
  616. * The unique setup id of the tripal views integration
  617. *
  618. * @return
  619. * A views integration definition array as used by tripal_add_views_integration()
  620. *
  621. * @ingroup tripal_chado_views_api
  622. */
  623. function tripal_export_views_integration($setup_id) {
  624. // Main setup details
  625. // D7 TODO: Check DBTNG changes work
  626. $r = db_query("SELECT * FROM {tripal_views} WHERE setup_id=:setup", array(':setup' => $setup_id));
  627. $r = $r->fetchObject();
  628. $defn_array = array(
  629. 'table' => $r->table_name,
  630. 'name' => $r->name,
  631. 'type' => ($r->mview_id) ? 'mview' : 'chado',
  632. 'description' => $r->comment,
  633. 'priority' => $r->priority,
  634. 'base_table' => $r->base_table,
  635. 'fields' => array(),
  636. );
  637. // Add fields
  638. $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=:setup", array(':setup' => $setup_id));
  639. foreach ($resource as $r) {
  640. $defn_array['fields'][ $r->column_name ] = array(
  641. 'name' => $r->column_name,
  642. 'title' => $r->name,
  643. 'description' => $r->description,
  644. 'type' => $r->type,
  645. 'handlers' => array(),
  646. 'joins' => array()
  647. );
  648. }
  649. // Add handlers
  650. $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=:setup", array(':setup' => $setup_id));
  651. foreach ($resource as $r) {
  652. $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
  653. 'name' => $r->handler_name
  654. );
  655. }
  656. // Add joins
  657. $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=:setup", array(':setup' => $setup_id));
  658. foreach ($resource as $r) {
  659. $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ][ $r->left_field ] = array(
  660. 'table' => $r->left_table,
  661. 'field' => $r->left_field,
  662. 'handler' => $r->handler,
  663. );
  664. }
  665. return $defn_array;
  666. }
  667. /**
  668. * Removes a View Integration Entry when you only know the table the integration was
  669. * created for and the priority.
  670. *
  671. * This should only be used to remove integrations created by your own module (possibly
  672. * on uninstall of your module). To override existing integrations simply create your own
  673. * integration with a lighter priority using tripal_clone_views_integration() or
  674. * tripal_export_views_integration() to create a template.
  675. *
  676. * @param $identifies
  677. * An array of identifiers where the keys indicate what the identifier is. One of the
  678. * following compinations must be present:
  679. * 1) table_name & priority: the name of the table & the priority to remove a views
  680. * integration entry for
  681. * 2) setup_id: the setup_id of the entry to remove
  682. * @param $options
  683. * An array of options, currently none are supported
  684. *
  685. * @return
  686. * TRUE on Success; FALSE otherwise
  687. *
  688. * @ingroup tripal_chado_views_api
  689. */
  690. function tripal_remove_views_integration($identifiers, $options = array()) {
  691. // Remove the views integration using the table_name/priority combo
  692. if (isset($identifiers['table_name'])) {
  693. $table_name = $identifiers['table_name'];
  694. $priority = $identifiers['priority'];
  695. $views = db_query(
  696. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  697. array(
  698. ':table' => $table_name,
  699. ':priority' => $priority
  700. )
  701. );
  702. $views = $views->fetchObject();
  703. if ($views->setup_id) {
  704. $identifiers['setup_id'] = $views->setup_id;
  705. }
  706. }
  707. // Remove the views integration using the setup_id
  708. if (isset($identifiers['setup_id'])) {
  709. db_query('DELETE FROM {tripal_views} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  710. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  711. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  712. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  713. return TRUE;
  714. }
  715. return FALSE;
  716. }
  717. /**
  718. * Update an existing Views Intregration Entry.
  719. * This essentially removes and then re-adds the integration.
  720. *
  721. * @param $setup_id
  722. * The setup ID of the views integration entry to update
  723. * @param $defn_array
  724. * An array describing the structure and fields of the table as is used in
  725. * tripal_add_views_integration().
  726. *
  727. * @ingroup tripal_chado_views_api
  728. */
  729. function tripal_update_views_integration($setup_id, $defn_array) {
  730. tripal_remove_views_integration(array('setup_id' => $setup_id));
  731. $defn_array['additional_content'] = TRUE;
  732. tripal_add_views_integration($defn_array, $setup_id);
  733. }
  734. /**
  735. * Clone an integration. This is often a great way to create your own module-specific
  736. * integration while still benifiting from an existing (or even the lightest priority)
  737. * integration.
  738. *
  739. * @param $table_name
  740. * The table for which you'd like to clone an integration
  741. * @param $new_priority
  742. * The priority of the clone; this is the integration which will be created.
  743. * If no priority is supplied then one lighter then the $template_priority will be used.
  744. * @param $template_priority
  745. * The priority of the template to be used for the clone; this is an existing integration.
  746. * If no priority is supplied then the lightest priority will be used.
  747. *
  748. * @ingroup tripal_chado_views_api
  749. */
  750. function tripal_clone_views_integration($table_name, $new_priority = NULL, $template_priority = NULL) {
  751. if (empty($template_priority)) {
  752. $template_setup_id = tripal_get_lightest_views_integration_setup($table_name);
  753. }
  754. else {
  755. $template_setup_id = tripal_get_views_integration_setup_id($table_name, $template_priority);
  756. }
  757. $defn_array = tripal_export_views_integration($template_setup_id);
  758. if (empty($new_priority)) {
  759. $defn_array['priority'] = $new_priority;
  760. }
  761. else {
  762. $new_priority = $defn_array['priority'] - 1;
  763. $defn_array['priority'] = $defn_array['priority'] - 1;
  764. }
  765. tripal_add_views_integration($defn_array);
  766. $setup_id = db_query(
  767. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  768. array(
  769. ':table' => $table_name,
  770. ':priority' => $new_priority
  771. )
  772. );
  773. $setup_id = $setup_id->fetchField();
  774. if (empty($setup_id)) {
  775. tripal_report_error('tripal_views', TRIPAL_ERROR, 'Unable to clone the setup for %table in order to add the following field to the integration: %field.',
  776. array('%table' => $table_name, '%field' => print_r($field_array,TRUE)));
  777. return FALSE;
  778. }
  779. else {
  780. return $setup_id;
  781. }
  782. }
  783. /**
  784. * Adds the given field to an existing or cloned integration. In the case of a cloned
  785. * integration, the lightest integration is used as the template for the clone.
  786. *
  787. * NOTE: If that field already exists in the specified integration then it will first be
  788. * deleted and the new one added.
  789. *
  790. * @param $table_name
  791. * The name of the table the integration is for
  792. * @param $priority
  793. * The priority of the integration to use; pass NULL to use the lightest integration
  794. * @param $field
  795. * An array describing the field ot add; uses the same format as the $defn_array
  796. *
  797. * @return
  798. * TRUE if the field was added successfully; FALSE otherwise
  799. *
  800. * @ingroup tripal_chado_views_api
  801. */
  802. function tripal_add_field_to_views_integration($table_name, $priority, $field) {
  803. $no_errors = TRUE;
  804. // If no priority is supplied then add the field to the lightest integration
  805. if (empty($priority)) {
  806. $priority = tripal_get_lightest_views_integration_priority($table_name);
  807. }
  808. // First get the setup_id
  809. // D7 TODO: Check DBTNG changes work
  810. $setup_id = db_query(
  811. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  812. array(
  813. ':table' => $table_name,
  814. ':priority' => $priority
  815. )
  816. );
  817. $setup_id = $setup_id->fetchObject();
  818. // If there isn't an integration matching that table/priority combination
  819. // then clone the lightest priority integration
  820. if (empty($setup_id)) {
  821. $setup_id = tripal_clone_views_integration($table_name, $priority);
  822. }
  823. // Now delete any existing field
  824. db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column",
  825. array(':setup' => $setup_id, 'column' => $field['name'])
  826. );
  827. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column",
  828. array(':setup' => $setup_id, 'column' => $field['name'])
  829. );
  830. db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field",
  831. array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  832. );
  833. // Now we need to add/update the field
  834. $field_record = array(
  835. 'setup_id' => $setup_id,
  836. 'column_name' => $field['name'],
  837. 'name' => $field['title'],
  838. 'description' => $field['description'],
  839. 'type' => $field['type'],
  840. );
  841. if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  842. if ($defn_array['additional_content']) {
  843. // D7 TODO: Check DBTNG changes work
  844. $is = db_query(
  845. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  846. array(':column' => $field_record['column_name'], ':setup' => $field_record['setup_id'])
  847. );
  848. $is = $is->fetchObject();
  849. if (!$is->present) {
  850. $status = drupal_write_record('tripal_views_field', $field_record);
  851. }
  852. else {
  853. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  854. }
  855. }
  856. else {
  857. $status = drupal_write_record('tripal_views_field', $field_record);
  858. }
  859. }
  860. else {
  861. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  862. $status = FALSE;
  863. }
  864. if ($status) {
  865. // Insert Handler Definitions
  866. foreach ($field['handlers'] as $handler_type => $handler) {
  867. $handler_record = array(
  868. 'setup_id' => $setup_id,
  869. 'column_name' => $field['name'],
  870. 'handler_type' => $handler_type,
  871. 'handler_name' => $handler['name'],
  872. 'arguments' => serialize($handler)
  873. );
  874. if ($setup_id && $field['name'] && $handler_type && $handler['name'] && $handler) {
  875. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  876. }
  877. else {
  878. $status = FALSE;
  879. }
  880. if (!$status) {
  881. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  882. $no_errors = FALSE;
  883. }
  884. }
  885. // Insert Joins
  886. // Note: The new defn_array structure accounts for 1+ joins to the same
  887. // table (ie: feature_relationship => feature : subject_id & object_id)
  888. // by making $field['joins'] an array of left_field keys.
  889. if (!is_array($field['joins'])) {
  890. $field['joins'] = array();
  891. }
  892. foreach ($field['joins'] as $joins) {
  893. // To keep backwards compatibility with the old defn_array which just
  894. // jumped right into the table definition allowing only a single join,
  895. // we need to check for old defn_arrays and transform them into the
  896. // new format.
  897. if (isset($joins['table'])) {
  898. $left_field = $joins['field'];
  899. $joins = array(
  900. $left_field => $joins
  901. );
  902. }
  903. // Loop on left fields
  904. foreach ($joins as $left_field => $join) {
  905. $join_record = array(
  906. 'setup_id' => $setup_id,
  907. 'base_table' => $defn_array['table'],
  908. 'base_field' => $field['name'],
  909. 'left_table' => $join['table'],
  910. 'left_field' => $join['field'],
  911. );
  912. if (!empty($join['handler'])) {
  913. $join_record['handler'] = $join['handler'];
  914. }
  915. else {
  916. $join_record['handler'] = 'views_join';
  917. }
  918. if ($setup_id && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  919. $status = drupal_write_record('tripal_views_join', $join_record);
  920. }
  921. else {
  922. $status = FALSE;
  923. }
  924. if (!$status) {
  925. drupal_set_message(
  926. t(
  927. 'Unable to join %left_table.%left_field with %table.%field',
  928. array(
  929. '%left_table' => $join['table'],
  930. '%left_field' => $join['field'],
  931. '%table' => $defn_array['table'],
  932. '%field' => $field['name']
  933. )
  934. ),
  935. 'error'
  936. );
  937. $no_errors = FALSE;
  938. }
  939. }
  940. }
  941. }
  942. else {
  943. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  944. $no_errors = FALSE;
  945. }
  946. return $no_errors;
  947. }
  948. /**
  949. * Adds the given field to an existing or cloned integration. In the case of a cloned
  950. * integration, the lightest integration is used as the template for the clone.
  951. *
  952. * NOTE: If that field already exists in the specified integration then it will first be
  953. * deleted and the new one added.
  954. *
  955. * @param $table_name
  956. * The name of the table the integration is for
  957. * @param $priority
  958. * The priority of the integration to use; pass NULL to use the lightest integration
  959. * @param $field
  960. * An array describing the join to add. it should contain the following keys:
  961. * base_table, base_field, left_table, left_field, handler, relationship_handler,
  962. * relationship_only
  963. *
  964. * @return
  965. * TRUE if the field was added successfully; FALSE otherwise
  966. *
  967. * @ingroup tripal_chado_views_api
  968. */
  969. function tripal_add_join_to_views_integration($table_name, $priority, $join) {
  970. $no_errors = TRUE;
  971. // If no priority is supplied then add the field to the lightest integration
  972. if (empty($priority)) {
  973. $priority = tripal_get_lightest_views_integration_priority($table_name);
  974. }
  975. // First get the setup_id
  976. $setup_id = db_query(
  977. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  978. array(
  979. ':table' => $table_name,
  980. ':priority' => $priority
  981. )
  982. );
  983. $setup_id = $setup_id->fetchField();
  984. // If there isn't an integration matching that table/priority combination
  985. // then clone the lightest priority integration
  986. if (empty($setup_id)) {
  987. $setup_id = tripal_clone_views_integration($table_name, $priority);
  988. }
  989. // Add the setup_id to the join record passed in
  990. $join['setup_id'] = $setup_id;
  991. drupal_write_record('tripal_views_join', $join);
  992. }
  993. /**
  994. * Remove a join from an integration. This is usually done after cloning an existing
  995. * integration using tripal_clone_views_integration().
  996. *
  997. * @param $setup_id
  998. * The setup_id of the integration to delete the join from
  999. * @param $base_table
  1000. * The name of the base table involved the join
  1001. * @param $base_field
  1002. * The field from the base table involved in the join
  1003. * @param $left_table
  1004. * The name of the other table involved in the join
  1005. * @param $left_field
  1006. * The name of the field from the other table involved in the join
  1007. *
  1008. * @ingroup tripal_chado_views_api
  1009. */
  1010. function tripal_remove_join_from_views_integration($setup_id, $base_table, $base_field, $left_table, $left_field) {
  1011. db_query(
  1012. "DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:base-table AND base_field=:base-field AND left_table=:left-table AND left_field=:left-field",
  1013. array(
  1014. ':setup' => $setup_id,
  1015. ':base-table' => $base_table,
  1016. ':base-field' => $base_field,
  1017. ':left-table' => $left_table,
  1018. ':left-field' => $left_field
  1019. )
  1020. );
  1021. }