tripal_views.api.inc 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. <?php
  2. /**
  3. * @file
  4. * API functions for Tripal Views Integration
  5. */
  6. /**
  7. * @defgroup tripal_views_api Tripal Views Module API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides functions for Tripal Views Integrations
  11. * @}
  12. */
  13. /**
  14. * Retrieve the priority of the lightest priority for a given table
  15. *
  16. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  17. * and -10 is of highest priority.
  18. *
  19. * @param $table_name
  20. * The name of the table to retrieve the setup ID for. This can be either a materialized
  21. * view or a chado table
  22. *
  23. * @return
  24. * returns the lowest priority. If the table has not been integrated, a priority of 10
  25. * is returned.
  26. *
  27. * @ingroup tripal_views_api
  28. */
  29. function tripal_views_get_table_lightest_priority($table_name) {
  30. // D7 TODO: Check DBTNG changes work
  31. $sql = "SELECT priority FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  32. $setup = db_query($sql, array(':table' => $table_name));
  33. $setup = $setup->fetchObject();
  34. if ($setup) {
  35. return $setup->priority;
  36. }
  37. else {
  38. // default priority is 10
  39. return 10;
  40. }
  41. }
  42. /**
  43. * Retrieve the views integration setup with the lightest priority for a given table
  44. *
  45. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  46. * and -10 is of highest priority.
  47. *
  48. * @param $table_name
  49. * The name of the table to retrieve the setup ID for. This can be either a materialized
  50. * view or a chado table
  51. *
  52. * @return
  53. * On success, the setup_id to use for integration of this table; otherwise FALSE
  54. *
  55. * @ingroup tripal_views_api
  56. */
  57. function tripal_views_get_lightest_priority_setup($table_name) {
  58. // D7 TODO: Check DBTNG changes work
  59. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  60. $setup = db_query($sql, array(':table' => $table_name));
  61. $setup = $setup->fetchObject();
  62. if ($setup) {
  63. return $setup->setup_id;
  64. }
  65. else {
  66. return FALSE;
  67. }
  68. }
  69. /**
  70. * Retrieve the views integration setup with the given priority/table combination
  71. *
  72. * @param $table_name
  73. * The name of the table to retrieve the setup ID for. This can be either a materialized
  74. * view or a chado table
  75. * @param $priority
  76. * The priority of the integration to retrieve the setup_id for
  77. *
  78. * @return
  79. * On success, the setup_id to use for integration of this table; otherwise FALSE
  80. *
  81. * @ingroup tripal_views_api
  82. */
  83. function tripal_views_get_setup_id($table_name, $priority) {
  84. // D7 TODO: Check DBTNG changes work
  85. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority ORDER BY priority ASC";
  86. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  87. $setup = $setup->fetchObject();
  88. if ($setup) {
  89. return $setup->setup_id;
  90. }
  91. else {
  92. return FALSE;
  93. }
  94. }
  95. /**
  96. * Check to see if this table already has an integration record with the given priority
  97. *
  98. * @param $table_name
  99. * The name of the table to check for integration
  100. * @param $priority (optional)
  101. * The priority of record to check for
  102. *
  103. * @return
  104. * If the table is already integrated, the setup_id of the existing integration
  105. * record is returned (If priority is not specified this will be the lightest record);
  106. * Otherwise the table is not already integrated and FALSE is returned.
  107. *
  108. * @ingroup tripal_views_api
  109. */
  110. function tripal_views_is_integrated($table_name, $priority = NULL) {
  111. if ($priority) {
  112. // D7 TODO: Check DBTNG changes work
  113. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority";
  114. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  115. $setup = $setup->fetchObject();
  116. }
  117. else {
  118. // D7 TODO: Check DBTNG changes work
  119. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  120. $setup = db_query($sql, array(':table' => $table_name));
  121. $setup = $setup->fetchObject();
  122. }
  123. if ($setup) {
  124. return $setup->setup_id;
  125. }
  126. else {
  127. return FALSE;
  128. }
  129. }
  130. /**
  131. * Checks if you are dealing with the lightest priority setup for a given table
  132. *
  133. * @param $setup_id
  134. * The ID of the setup to check (is this setup the lightest one?)
  135. * @param $table_name
  136. * The name of the table associated with this setup
  137. *
  138. * @return TRUE is this is the lightest priority; FALSE otherwise
  139. *
  140. * @ingroup tripal_views_api
  141. */
  142. function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
  143. $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  144. if ($lightest_priority_setup_id == $setup_id) {
  145. return TRUE;
  146. }
  147. else {
  148. return FALSE;
  149. }
  150. }
  151. /**
  152. * Programatically enable view
  153. *
  154. * @param $view_name
  155. * The machine-name of the view to be enabled
  156. * @param $redirect_link
  157. * The path to redirect to. FALSE if no redirect needed
  158. */
  159. function tripal_views_admin_enable_view($view_name, $redirect_link = FALSE) {
  160. $status = variable_get('views_defaults', array());
  161. if (isset($status[$view_name])) {
  162. $status[$view_name] = FALSE;
  163. variable_set('views_defaults', $status);
  164. drupal_set_message("Successfully Enabled $view_name");
  165. }
  166. else {
  167. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to enable this view.",'error');
  168. }
  169. if ($redirect_link) {
  170. drupal_goto($redirect_link);
  171. }
  172. }
  173. /**
  174. * Programatically disable view
  175. *
  176. * @param $view_name
  177. * The machine-name of the view to be enabled
  178. * @param $redirect_link
  179. * The path to redirect to. FALSE if no redirect needed
  180. */
  181. function tripal_views_admin_disable_view($view_name, $redirect_link = FALSE) {
  182. $status = variable_get('views_defaults', array());
  183. if (isset($status[$view_name])) {
  184. $status[$view_name] = TRUE;
  185. variable_set('views_defaults', $status);
  186. drupal_set_message("Disabled $view_name");
  187. }
  188. else {
  189. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to disable this view.",'error');
  190. }
  191. if ($redirect_link) {
  192. drupal_goto($redirect_link);
  193. }
  194. }
  195. /**
  196. * Purpose: Deletes ALL Tripal Views Integrations.
  197. */
  198. function tripal_views_delete_all_integrations() {
  199. db_query("DELETE FROM {tripal_views}");
  200. db_query("DELETE FROM {tripal_views_field}");
  201. db_query("DELETE FROM {tripal_views_handlers}");
  202. db_query("DELETE FROM {tripal_views_join}");
  203. drupal_set_message("Successfully deleted all views integration.");
  204. }
  205. /**
  206. * Rebuilds all the default integrations
  207. *
  208. * @param $delete_first
  209. * If TRUE then all integrations are first deleted.
  210. */
  211. function tripal_views_rebuild_views_integrations($delete_first = FALSE) {
  212. if ($delete_first) {
  213. tripal_views_delete_all_integrations();
  214. }
  215. tripal_views_integrate_all_chado_tables();
  216. drupal_set_message('Successfully rebuilt default Tripal Views Integrations');
  217. }
  218. /**
  219. * Add views integration records into the tripal_views* tables
  220. *
  221. * @param $defn_array
  222. * An array describing the structure and fields of the table
  223. *
  224. * @return
  225. * True/False if completed successfully/not
  226. *
  227. * Example usage (in hook_install()):
  228. * @code
  229. $defn_array = array(
  230. 'table' => 'feature', //tablename or materialized view name
  231. 'name' => 'Sequence Features', // Human readable name
  232. 'type' => 'chado', //either chado or mview depending on tablename
  233. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  234. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  235. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  236. 'fields' => array(
  237. 'feature_id' => array(
  238. 'name' => 'feature_id', //field name in database
  239. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  240. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  241. 'type' => 'int', // the type of field
  242. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  243. 'field' => array(
  244. 'name' => 'chado_views_handler_numeric' //name of handler
  245. ),
  246. 'filter' => array( ... ),
  247. ...
  248. ),
  249. 'join' => array( //describe a table that joins to this one via this field
  250. 'table' => 'featureprop', //table to join to
  251. 'field' => 'feature_id', //field in above table (featureprop)
  252. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  253. ),
  254. )
  255. ),
  256. );
  257. tripal_views_integration_add_entry($defn_array);
  258. * @endcode
  259. *
  260. * @ingroup tripal_views_api
  261. */
  262. function tripal_views_integration_add_entry($defn_array) {
  263. $no_errors = TRUE;
  264. if (empty($defn_array['table'])) {
  265. watchdog('tripal_views','Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array,TRUE)), WATCHDOG_WARNING);
  266. $no_errors = FALSE;
  267. return $no_errors;
  268. }
  269. // First insert into tripal_views
  270. $view_record = array(
  271. 'table_name' => $defn_array['table'],
  272. 'name' => $defn_array['name'],
  273. 'comment' => $defn_array['description'],
  274. 'priority' => $defn_array['priority'],
  275. 'base_table' => $defn_array['base_table'],
  276. );
  277. if ($defn_array['type'] == 'mview') {
  278. // D7 TODO: Check DBTNG changes work
  279. $mview = db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table=:table", array(':table' => $defn_array['table']));
  280. $mview = $mview->fetchObject();
  281. $view_record['mview_id'] = $mview->mview_id;
  282. if (!$mview->mview_id) {
  283. return FALSE;
  284. }
  285. }
  286. if ($view_record['name']) { // && $view_record['comment']) { # SPF: commented out 9/24/2012 .. It's not required on the form
  287. if (isset($defn_array['additional_content'])) {
  288. // D7 TODO: Check DBTNG changes work
  289. $setup = db_query(
  290. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  291. array(':table' => $view_record['table_name'], ':priority' => $view_record['priority'])
  292. );
  293. $setup = $setup->fetchObject();
  294. if (empty($setup->setup_id)) {
  295. $status = drupal_write_record('tripal_views', $view_record);
  296. }
  297. else {
  298. $view_record['setup_id'] = $setup->setup_id;
  299. $status = drupal_write_record('tripal_views', $view_record, 'setup_id');
  300. }
  301. }
  302. else {
  303. $status = drupal_write_record('tripal_views', $view_record);
  304. }
  305. }
  306. else {
  307. $status = FALSE;
  308. drupal_set_message(t('Unable to integrate "%table" table due to a missing name field.', array('%table' => $defn_array['table'])), 'error');
  309. }
  310. if ($status) {
  311. // Need to update the tripal_views record so base_table can be false
  312. // this is a fix because drupal_write_record() puts in defaults if !isset()
  313. // and a variable is considered not set if it's null!
  314. // D7 TODO: Check DBTNG changes work
  315. db_query(
  316. "UPDATE {tripal_views} SET base_table=:base WHERE table_name=:table AND priority=:priority",
  317. array(
  318. ':base' => $defn_array['base_table'],
  319. ':table' => $defn_array['table'],
  320. ':priority' => $defn_array['priority']
  321. )
  322. );
  323. // Insert Field Definitions
  324. foreach ($defn_array['fields'] as $field) {
  325. $field_record = array(
  326. 'setup_id' => $view_record['setup_id'],
  327. 'column_name' => $field['name'],
  328. 'name' => $field['title'],
  329. 'description' => $field['description'],
  330. 'type' => $field['type'],
  331. );
  332. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  333. if (isset($defn_array['additional_content'])) {
  334. // D7 TODO: Check DBTNG changes work
  335. $is = db_query(
  336. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  337. array(
  338. ':column' => $field_record['column_name'],
  339. ':setup' => $field_record['setup_id']
  340. )
  341. );
  342. $is = $is->fetchObject();
  343. if (!$is->present) {
  344. $status = drupal_write_record('tripal_views_field', $field_record);
  345. }
  346. else {
  347. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  348. }
  349. }
  350. else {
  351. $status = drupal_write_record('tripal_views_field', $field_record);
  352. }
  353. }
  354. else {
  355. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  356. $status = FALSE;
  357. }
  358. if ($status) {
  359. // Insert Handler Definitions
  360. foreach ($field['handlers'] as $handler_type => $handler) {
  361. $handler_record = array(
  362. 'setup_id' => $view_record['setup_id'],
  363. 'column_name' => $field['name'],
  364. 'handler_type' => $handler_type,
  365. 'handler_name' => $handler['name'],
  366. 'arguments' => serialize($handler)
  367. );
  368. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  369. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  370. }
  371. else {
  372. $status = FALSE;
  373. }
  374. if (!$status) {
  375. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  376. $no_errors = FALSE;
  377. }
  378. }
  379. // Insert Joins
  380. if (!is_array($field['joins'])) {
  381. $field['joins'] = array();
  382. }
  383. foreach ($field['joins'] as $join) {
  384. $join_record = array(
  385. 'setup_id' => $view_record['setup_id'],
  386. 'base_table' => $defn_array['table'],
  387. 'base_field' => $field['name'],
  388. 'left_table' => $join['table'],
  389. 'left_field' => $join['field'],
  390. );
  391. $join_record['handler'] = (!empty($join['handler'])) ? $join['handler'] : 'views_join';
  392. $join_record['relationship_handler'] = (!empty($join['relationship_handler'])) ? $join['relationship_handler'] : 'views_handler_relationship';
  393. $join_record['relationship_only'] = (!empty($join['relationship_only'])) ? $join['relationship_only'] : 0;
  394. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  395. $status = drupal_write_record('tripal_views_join', $join_record);
  396. }
  397. else {
  398. $status = FALSE;
  399. }
  400. if (!$status) {
  401. drupal_set_message(
  402. t(
  403. 'Unable to join %left_table.%left_field with %table.%field',
  404. array(
  405. '%left_table' => $join['table'],
  406. '%left_field' => $join['field'],
  407. '%table' => $defn_array['table'],
  408. '%field' => $field['name']
  409. )
  410. ),
  411. 'error'
  412. );
  413. $no_errors = FALSE;
  414. }
  415. }
  416. }
  417. else {
  418. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  419. $no_errors = FALSE;
  420. }
  421. }
  422. }
  423. else {
  424. drupal_set_message(t('Unable to set default tripal views integration'), 'error');
  425. $no_errors = FALSE;
  426. }
  427. return $no_errors;
  428. }
  429. /**
  430. * Export Views integration records
  431. *
  432. * @param $setup_id
  433. * The unique setup id of the tripal views integration
  434. *
  435. * @return
  436. * A views integration definition array as used by tripal_views_integration_add_entry()
  437. *
  438. * @ingroup tripal_views_api
  439. */
  440. function tripal_views_integration_export_entry($setup_id) {
  441. // Main setup details
  442. // D7 TODO: Check DBTNG changes work
  443. $r = db_query("SELECT * FROM {tripal_views} WHERE setup_id=:setup", array(':setup' => $setup_id));
  444. $r = $r->fetchObject();
  445. $defn_array = array(
  446. 'table' => $r->table_name,
  447. 'name' => $r->name,
  448. 'type' => ($r->mview_id) ? 'mview' : 'chado',
  449. 'description' => $r->comment,
  450. 'priority' => $r->priority,
  451. 'base_table' => $r->base_table,
  452. 'fields' => array(),
  453. );
  454. // Add fields
  455. // D7 TODO: Check DBTNG changes work
  456. $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=:setup", array(':setup' => $setup_id));
  457. foreach ($resource as $r) {
  458. $defn_array['fields'][ $r->column_name ] = array(
  459. 'name' => $r->column_name,
  460. 'title' => $r->name,
  461. 'description' => $r->description,
  462. 'type' => $r->type,
  463. 'handlers' => array(),
  464. 'joins' => array()
  465. );
  466. }
  467. // Add handlers
  468. // D7 TODO: Check DBTNG changes work
  469. $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=:setup", array(':setup' => $setup_id));
  470. foreach ($resource as $r) {
  471. $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
  472. 'name' => $r->handler_name
  473. );
  474. }
  475. // Add joins
  476. // D7 TODO: Check DBTNG changes work
  477. $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=:setup", array(':setup' => $setup_id));
  478. foreach ($resource as $r) {
  479. $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ] = array(
  480. 'table' => $r->left_table,
  481. 'field' => $r->left_field,
  482. 'handler' => $r->handler,
  483. );
  484. }
  485. return $defn_array;
  486. }
  487. /**
  488. * Removes a View Integration Entry
  489. *
  490. * @param $table_name
  491. * The name of the table to remove a views integration entry for
  492. * @param $priority
  493. * The priority of the of views integration entry
  494. *
  495. * @return
  496. * TRUE on Success; FALSE otherwise
  497. *
  498. * @ingroup tripal_views_api
  499. */
  500. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  501. // D7 TODO: Check DBTNG changes work
  502. $views = db_query(
  503. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  504. array(
  505. ':table' => $table_name,
  506. ':priority' => $priority
  507. )
  508. );
  509. $views = $views->fetchObject();
  510. if ($views->setup_id) {
  511. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  512. return TRUE;
  513. }
  514. else {
  515. return FALSE;
  516. }
  517. }
  518. /**
  519. * Removes a View Integration Entry
  520. *
  521. * @param $setup_id
  522. * The setup ID of the views integration entry to remove
  523. *
  524. * @ingroup tripal_views_api
  525. */
  526. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  527. // D7 TODO: Check DBTNG changes work
  528. db_query('DELETE FROM {tripal_views} WHERE setup_id=:setup', array(':setup' => $setup_id));
  529. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=:setup', array(':setup' => $setup_id));
  530. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup', array(':setup' => $setup_id));
  531. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=:setup', array(':setup' => $setup_id));
  532. }
  533. /**
  534. * Integrate all chado tables in the schema api. This integration only occurs
  535. * once and sets all Chado tables to a priority of 10
  536. *
  537. * @ingroup tripal_views_api
  538. */
  539. function tripal_views_integrate_all_chado_tables() {
  540. $tables = tripal_core_get_chado_tables(TRUE);
  541. $base_tables = array(
  542. 'acquisition', 'analysis', 'assay', 'biomaterial', 'contact', 'cv', 'cvterm',
  543. 'db', 'dbxref', 'environment', 'expression', 'feature', 'featuremap', 'genotype',
  544. 'library', 'nd_experiment', 'nd_geolocation', 'nd_protocol', 'nd_reagent',
  545. 'organism', 'phendesc', 'phenotype', 'phenstatement', 'phylonode', 'phylotree',
  546. 'project', 'protocol', 'pub', 'stock', 'study', 'synonym'
  547. );
  548. foreach ($tables as $tablename) {
  549. $priority = 10;
  550. if (!tripal_views_is_integrated($tablename, $priority)) {
  551. if (in_array($tablename, $base_tables)) {
  552. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
  553. }
  554. else {
  555. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
  556. }
  557. if ($table_integration_array) {
  558. tripal_views_integration_add_entry($table_integration_array);
  559. }
  560. }
  561. }
  562. }
  563. /**
  564. * Returns the array needed to integrate a given chado table with views
  565. *
  566. * @param $tablename
  567. * The table to generate the tripal views integration array for
  568. * @return
  569. * The tripal views integration array which is the parameter for
  570. * tripal_views_integration_add_entry($defn_array)
  571. *
  572. * @ingroup tripal_views_api
  573. */
  574. function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE, $priority = 9) {
  575. // Get the schema for this table (via the chado schema api)
  576. $schema = tripal_core_get_chado_table_schema($table_name);
  577. // Base definition array
  578. $defn_array = array(
  579. 'table' => $table_name,
  580. 'type' => 'chado',
  581. 'name' => 'Chado ' . ucwords(str_replace('_', ' ', $table_name)),
  582. 'description' => (!empty($schema['description'])) ? $schema['description'] : ' ',
  583. 'priority' => $priority,
  584. 'base_table' => $base_table,
  585. 'fields' => array(),
  586. );
  587. // Add fields
  588. if (!isset($schema['fields'])) {
  589. watchdog('tripal_views', 'There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_NOTICE);
  590. return FALSE;
  591. }
  592. foreach ($schema['fields'] as $field_name => $field_schema) {
  593. // Base field definition
  594. if (!empty($field_name) && !empty($field_schema['type'])) {
  595. $defn_array['fields'][$field_name] = array(
  596. 'name' => $field_name,
  597. 'title' => ucwords(str_replace('_', ' ', $field_name)),
  598. 'type' => $field_schema['type'],
  599. 'description' => (isset($field_schema['description'])) ? $field_schema['description'] : ucwords(str_replace('_', ' ', $field_name)),
  600. 'handlers' => array(),
  601. 'joins' => array()
  602. );
  603. // Add handlers based on type
  604. if (preg_match('/^int/', $field_schema['type'])) {
  605. $defn_array['fields'][$field_name]['handlers'] = array(
  606. /** D6
  607. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  608. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  609. 'sort' => array('name' => 'chado_views_handler_sort'),
  610. */
  611. 'field' => array('name' => 'views_handler_field_numeric'),
  612. 'filter' => array('name' => 'views_handler_filter_numeric'),
  613. 'sort' => array('name' => 'views_handler_sort'),
  614. );
  615. }
  616. elseif (preg_match('/^serial/', $field_schema['type'])) {
  617. $defn_array['fields'][$field_name]['handlers'] = array(
  618. /** D6
  619. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  620. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  621. 'sort' => array('name' => 'chado_views_handler_sort'),
  622. */
  623. 'field' => array('name' => 'views_handler_field_numeric'),
  624. 'filter' => array('name' => 'views_handler_filter_numeric'),
  625. 'sort' => array('name' => 'views_handler_sort'),
  626. );
  627. $defn_array['fields'][$field_name]['type'] = 'int';
  628. }
  629. elseif (preg_match('/^varchar/', $field_schema['type'])) {
  630. $defn_array['fields'][$field_name]['handlers'] = array(
  631. /** D6
  632. 'field' => array('name' => 'chado_views_handler_field'),
  633. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  634. 'sort' => array('name' => 'chado_views_handler_sort'),
  635. */
  636. 'field' => array('name' => 'views_handler_field'),
  637. 'filter' => array('name' => 'views_handler_filter_string'),
  638. 'sort' => array('name' => 'views_handler_sort'),
  639. );
  640. }
  641. elseif (preg_match('/^text/', $field_schema['type'])) {
  642. $defn_array['fields'][$field_name]['handlers'] = array(
  643. /** D6
  644. 'field' => array('name' => 'chado_views_handler_field'),
  645. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  646. 'sort' => array('name' => 'chado_views_handler_sort'),
  647. */
  648. 'field' => array('name' => 'views_handler_field'),
  649. 'filter' => array('name' => 'views_handler_filter_string'),
  650. 'sort' => array('name' => 'views_handler_sort'),
  651. );
  652. }
  653. elseif (preg_match('/^boolean/', $field_schema['type'])) {
  654. $defn_array['fields'][$field_name]['handlers'] = array(
  655. /**
  656. 'field' => array('name' => 'chado_views_handler_field_boolean'),
  657. 'filter' => array('name' => 'chado_views_handler_filter_boolean_operator'),
  658. 'sort' => array('name' => 'chado_views_handler_sort'),
  659. */
  660. 'field' => array('name' => 'views_handler_field_boolean'),
  661. 'filter' => array('name' => 'views_handler_filter_boolean_operator'),
  662. 'sort' => array('name' => 'views_handler_sort'),
  663. );
  664. }
  665. elseif (preg_match('/^datetime/', $field_schema['type'])) {
  666. $defn_array['fields'][$field_name]['handlers'] = array(
  667. /** D6
  668. 'field' => array('name' => 'chado_views_handler_field_date'),
  669. 'filter' => array('name' => 'chado_views_handler_filter_date'),
  670. 'sort' => array('name' => 'views_handler_sort_date'),
  671. */
  672. 'field' => array('name' => 'views_handler_field_date'),
  673. 'filter' => array('name' => 'views_handler_filter_date'),
  674. 'sort' => array('name' => 'views_handler_sort_date'),
  675. );
  676. }
  677. else {
  678. $defn_array['fields'][$field_name]['handlers'] = array(
  679. /** D6
  680. 'field' => array('name' => 'chado_views_handler_field'),
  681. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  682. 'sort' => array('name' => 'chado_views_handler_sort'),
  683. */
  684. 'field' => array('name' => 'views_handler_field'),
  685. 'filter' => array('name' => 'views_handler_filter_string'),
  686. 'sort' => array('name' => 'views_handler_sort'),
  687. );
  688. }
  689. // Specify specialty handlers
  690. if ($field_name == 'type_id' OR $field_name == 'cvterm_id') {
  691. $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_cvterm';
  692. }
  693. if (preg_match('/name/',$field_name)) {
  694. $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
  695. }
  696. }
  697. }
  698. // Add Joins & Relationships for foreign keys to fields
  699. if (!isset($schema['foreign keys'])) {
  700. $schema['foreign keys'] = array();
  701. }
  702. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  703. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  704. // Join
  705. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  706. 'table' => $foreign_key_schema['table'],
  707. 'field' => $right_field,
  708. /**D6 'handler' => 'views_handler_join_chado_aggregator'*/
  709. 'handler' => 'views_handler_join',
  710. 'relationship_handler' => 'views_handler_relationship',
  711. );
  712. }
  713. }
  714. // Add in reverse relationships
  715. if (isset($schema['referring_tables'])) {
  716. foreach ($schema['referring_tables'] as $referring_table) {
  717. // D7 @todo: fix referring_tables in schema to list the keys like foreign keys does
  718. $referring_schema = tripal_core_get_chado_table_schema($referring_table);
  719. $referring_schema_fk_columns = $referring_schema['foreign keys'][$table_name]['columns'];
  720. foreach ($referring_schema_fk_columns as $left_field => $right_field) {
  721. $defn_array['fields'][$right_field]['joins'][ $referring_table ] = array(
  722. 'table' => $referring_table,
  723. 'field' => $left_field,
  724. 'relationship_handler' => 'views_handler_relationship',
  725. 'relationship_only' => 1
  726. );
  727. }
  728. }
  729. }
  730. return $defn_array;
  731. }
  732. /**
  733. * Clone an integration
  734. *
  735. * @param $table_name
  736. * The table for which you'd like to clone an integration
  737. * @param $new_priority
  738. * The priority of the clone; this is the integration which will be created.
  739. * If no priority is supplied then one lighter then the $template_priority will be used.
  740. * @param $template_priority
  741. * The priority of the template to be used for the clone; this is an existing integration.
  742. * If no priority is supplied then the lightest priority will be used.
  743. *
  744. * @ingroup tripal_views_api
  745. */
  746. function tripal_views_clone_integration($table_name, $new_priority = NULL, $template_priority = NULL) {
  747. if (empty($template_priority)) {
  748. $template_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  749. }
  750. else {
  751. $template_setup_id = tripal_views_get_setup_id($table_name, $template_priority);
  752. }
  753. $defn_array = tripal_views_integration_export_entry($template_setup_id);
  754. if (empty($new_priority)) {
  755. $defn_array['priority'] = $new_priority;
  756. }
  757. else {
  758. $new_priority = $defn_array['priority'] - 1;
  759. $defn_array['priority'] = $defn_array['priority'] - 1;
  760. }
  761. // D7 TODO: Check DBTNG changes work
  762. tripal_views_integration_add_entry($defn_array);
  763. $setup_id = db_query(
  764. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  765. array(
  766. ':table' => $table_name,
  767. ':priority' => $new_priority
  768. )
  769. );
  770. $setup_id = $setup_id->fetchObject();
  771. if (empty($setup_id)) {
  772. watchdog('tripal_views','Unable to clone the setup for %table in order to add the following field to the integration: %field.',
  773. array('%table' => $table_name, '%field' => print_r($field_array,TRUE)),WATCHDOG_ERROR);
  774. return FALSE;
  775. }
  776. else {
  777. return $setup_id;
  778. }
  779. }
  780. /**
  781. * Adds the given field to an existing or cloned integration. In the case of a cloned
  782. * integration, the lightest integration is used as the template for the clone.
  783. *
  784. * NOTE: If that field already exists in the specified integration then it will first be
  785. * deleted and the new one added.
  786. *
  787. * @param $table_name
  788. * The name of the table the integration is for
  789. * @param $priority
  790. * The priority of the integration to use; pass NULL to use the lightest integration
  791. * @param $field
  792. * An array describing the field ot add; uses the same format as the $defn_array
  793. *
  794. * @return
  795. * TRUE if the field was added successfully; FALSE otherwise
  796. *
  797. * @ingroup tripal_views_api
  798. */
  799. function tripal_views_add_field_to_integration($table_name, $priority, $field) {
  800. $no_errors = TRUE;
  801. // If no priority is supplied then add the field to the lightest integration
  802. if (empty($priority)) {
  803. $priority = tripal_views_get_table_lightest_priority($table_name);
  804. }
  805. // First get the setup_id
  806. // D7 TODO: Check DBTNG changes work
  807. $setup_id = db_query(
  808. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  809. array(
  810. ':table' => $table_name,
  811. ':priority' => $priority
  812. )
  813. );
  814. $setup_id = $setup_id->fetchObject();
  815. // If there isn't an integration matching that table/priority combination
  816. // then clone the lightest priority integration
  817. if (empty($setup_id)) {
  818. $setup_id = tripal_views_clone_integration($table_name, $priority);
  819. }
  820. // Now delete any existing field
  821. db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column",
  822. array(':setup' => $setup_id, 'column' => $field['name'])
  823. );
  824. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column",
  825. array(':setup' => $setup_id, 'column' => $field['name'])
  826. );
  827. db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field",
  828. array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  829. );
  830. // Now we need to add/update the field
  831. $field_record = array(
  832. 'setup_id' => $setup_id,
  833. 'column_name' => $field['name'],
  834. 'name' => $field['title'],
  835. 'description' => $field['description'],
  836. 'type' => $field['type'],
  837. );
  838. if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  839. if ($defn_array['additional_content']) {
  840. // D7 TODO: Check DBTNG changes work
  841. $is = db_query(
  842. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  843. array(':column' => $field_record['column_name'], ':setup' => $field_record['setup_id'])
  844. );
  845. $is = $is->fetchObject();
  846. if (!$is->present) {
  847. $status = drupal_write_record('tripal_views_field', $field_record);
  848. }
  849. else {
  850. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  851. }
  852. }
  853. else {
  854. $status = drupal_write_record('tripal_views_field', $field_record);
  855. }
  856. }
  857. else {
  858. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  859. $status = FALSE;
  860. }
  861. if ($status) {
  862. // Insert Handler Definitions
  863. foreach ($field['handlers'] as $handler_type => $handler) {
  864. $handler_record = array(
  865. 'setup_id' => $setup_id,
  866. 'column_name' => $field['name'],
  867. 'handler_type' => $handler_type,
  868. 'handler_name' => $handler['name'],
  869. 'arguments' => serialize($handler)
  870. );
  871. if ($setup_id && $field['name'] && $handler_type && $handler['name'] && $handler) {
  872. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  873. }
  874. else {
  875. $status = FALSE;
  876. }
  877. if (!$status) {
  878. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  879. $no_errors = FALSE;
  880. }
  881. }
  882. // Insert Joins
  883. if (!is_array($field['joins'])) {
  884. $field['joins'] = array();
  885. }
  886. foreach ($field['joins'] as $join) {
  887. $join_record = array(
  888. 'setup_id' => $setup_id,
  889. 'base_table' => $defn_array['table'],
  890. 'base_field' => $field['name'],
  891. 'left_table' => $join['table'],
  892. 'left_field' => $join['field'],
  893. );
  894. if (!empty($join['handler'])) {
  895. $join_record['handler'] = $join['handler'];
  896. }
  897. else {
  898. $join_record['handler'] = 'views_join';
  899. }
  900. if ($setup_id && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  901. $status = drupal_write_record('tripal_views_join', $join_record);
  902. }
  903. else {
  904. $status = FALSE;
  905. }
  906. if (!$status) {
  907. drupal_set_message(
  908. t(
  909. 'Unable to join %left_table.%left_field with %table.%field',
  910. array(
  911. '%left_table' => $join['table'],
  912. '%left_field' => $join['field'],
  913. '%table' => $defn_array['table'],
  914. '%field' => $field['name']
  915. )
  916. ),
  917. 'error'
  918. );
  919. $no_errors = FALSE;
  920. }
  921. }
  922. }
  923. else {
  924. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  925. $no_errors = FALSE;
  926. }
  927. return $no_errors;
  928. }
  929. /**
  930. * Remove a join from an integration
  931. *
  932. * @param $setup_id
  933. * The setup_id of the integration to delete the join from
  934. * @param $base_table
  935. * The name of the base table involved the join
  936. * @param $base_field
  937. * The field from the base table involved in the join
  938. * @param $left_table
  939. * The name of the other table involved in the join
  940. * @param $left_field
  941. * The name of the field from the other table involved in the join
  942. *
  943. * @ingroup tripal_views_api
  944. */
  945. function tripal_views_remove_join_from_integration($setup_id, $base_table, $base_field, $left_table, $left_field) {
  946. db_query(
  947. "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",
  948. array(
  949. ':setup' => $setup_id,
  950. ':base-table' => $base_table,
  951. ':base-field' => $base_field,
  952. ':left-table' => $left_table,
  953. ':left-field' => $left_field
  954. )
  955. );
  956. }