tripal_chado_views.api.inc 39 KB

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