tripal_views.api.inc 33 KB

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