tripal_chado_views.api.inc 40 KB

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