tripal_views.api.inc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. $base_tables = array(
  474. 'acquisition', 'analysis', 'assay', 'biomaterial', 'contact', 'cv', 'cvterm',
  475. 'db', 'dbxref', 'environment', 'expression', 'feature', 'featuremap', 'genotype',
  476. 'library', 'nd_experiment', 'nd_geolocation', 'nd_protocol', 'nd_reagent',
  477. 'organism', 'phendesc', 'phenotype', 'phenstatement', 'phylonode', 'phylotree',
  478. 'project', 'protocol', 'pub', 'stock', 'study', 'synonym'
  479. );
  480. foreach ($tables as $tablename) {
  481. $priority = 10;
  482. if (!tripal_views_is_integrated($tablename, $priority)) {
  483. if (in_array($tablename, $base_tables)) {
  484. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
  485. }
  486. else {
  487. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
  488. }
  489. if ($table_integration_array) {
  490. tripal_views_integration_add_entry($table_integration_array);
  491. }
  492. }
  493. }
  494. }
  495. /**
  496. * Returns the array needed to integrate a given chado table with views
  497. *
  498. * @param $tablename
  499. * The table to generate the tripal views integration array for
  500. * @return
  501. * The tripal views integration array which is the parameter for
  502. * tripal_views_integration_add_entry($defn_array)
  503. *
  504. * @ingroup tripal_views_api
  505. */
  506. function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE, $priority = 9) {
  507. // Get the schema for this table (via the chado schema api)
  508. $schema = tripal_core_get_chado_table_schema($table_name);
  509. // Base definition array
  510. $defn_array = array(
  511. 'table' => $table_name,
  512. 'type' => 'chado',
  513. 'name' => 'Chado ' . ucwords(str_replace('_', ' ', $table_name)),
  514. 'description' => (!empty($schema['description'])) ? $schema['description'] : ' ',
  515. 'priority' => $priority,
  516. 'base_table' => $base_table,
  517. 'fields' => array(),
  518. );
  519. // Add fields
  520. if (!isset($schema['fields'])) {
  521. watchdog('tripal_views', 'There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_NOTICE);
  522. return FALSE;
  523. }
  524. foreach ($schema['fields'] as $field_name => $field_schema) {
  525. // Base field definition
  526. if (!empty($field_name) && !empty($field_schema['type'])) {
  527. $defn_array['fields'][$field_name] = array(
  528. 'name' => $field_name,
  529. 'title' => ucwords(str_replace('_', ' ', $field_name)),
  530. 'type' => $field_schema['type'],
  531. 'description' => ($field_schema['description']) ? $field_schema['description'] : ucwords(str_replace('_', ' ', $field_name)),
  532. 'handlers' => array(),
  533. 'joins' => array()
  534. );
  535. // Add handlers based on type
  536. if (preg_match('/^int/', $field_schema['type'])) {
  537. $defn_array['fields'][$field_name]['handlers'] = array(
  538. /** D6
  539. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  540. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  541. 'sort' => array('name' => 'chado_views_handler_sort'),
  542. */
  543. 'field' => array('name' => 'views_handler_field_numeric'),
  544. 'filter' => array('name' => 'views_handler_filter_numeric'),
  545. 'sort' => array('name' => 'views_handler_sort'),
  546. );
  547. }
  548. elseif (preg_match('/^serial/', $field_schema['type'])) {
  549. $defn_array['fields'][$field_name]['handlers'] = array(
  550. /** D6
  551. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  552. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  553. 'sort' => array('name' => 'chado_views_handler_sort'),
  554. */
  555. 'field' => array('name' => 'views_handler_field_numeric'),
  556. 'filter' => array('name' => 'views_handler_filter_numeric'),
  557. 'sort' => array('name' => 'views_handler_sort'),
  558. );
  559. $defn_array['fields'][$field_name]['type'] = 'int';
  560. }
  561. elseif (preg_match('/^varchar/', $field_schema['type'])) {
  562. $defn_array['fields'][$field_name]['handlers'] = array(
  563. /** D6
  564. 'field' => array('name' => 'chado_views_handler_field'),
  565. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  566. 'sort' => array('name' => 'chado_views_handler_sort'),
  567. */
  568. 'field' => array('name' => 'views_handler_field'),
  569. 'filter' => array('name' => 'views_handler_filter_string'),
  570. 'sort' => array('name' => 'views_handler_sort'),
  571. );
  572. }
  573. elseif (preg_match('/^text/', $field_schema['type'])) {
  574. $defn_array['fields'][$field_name]['handlers'] = array(
  575. /** D6
  576. 'field' => array('name' => 'chado_views_handler_field'),
  577. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  578. 'sort' => array('name' => 'chado_views_handler_sort'),
  579. */
  580. 'field' => array('name' => 'views_handler_field'),
  581. 'filter' => array('name' => 'views_handler_filter_string'),
  582. 'sort' => array('name' => 'views_handler_sort'),
  583. );
  584. }
  585. elseif (preg_match('/^boolean/', $field_schema['type'])) {
  586. $defn_array['fields'][$field_name]['handlers'] = array(
  587. /**
  588. 'field' => array('name' => 'chado_views_handler_field_boolean'),
  589. 'filter' => array('name' => 'chado_views_handler_filter_boolean_operator'),
  590. 'sort' => array('name' => 'chado_views_handler_sort'),
  591. */
  592. 'field' => array('name' => 'views_handler_field_boolean'),
  593. 'filter' => array('name' => 'views_handler_filter_boolean_operator'),
  594. 'sort' => array('name' => 'views_handler_sort'),
  595. );
  596. }
  597. elseif (preg_match('/^datetime/', $field_schema['type'])) {
  598. $defn_array['fields'][$field_name]['handlers'] = array(
  599. /** D6
  600. 'field' => array('name' => 'chado_views_handler_field_date'),
  601. 'filter' => array('name' => 'chado_views_handler_filter_date'),
  602. 'sort' => array('name' => 'views_handler_sort_date'),
  603. */
  604. 'field' => array('name' => 'views_handler_field_date'),
  605. 'filter' => array('name' => 'views_handler_filter_date'),
  606. 'sort' => array('name' => 'views_handler_sort_date'),
  607. );
  608. }
  609. else {
  610. $defn_array['fields'][$field_name]['handlers'] = array(
  611. /** D6
  612. 'field' => array('name' => 'chado_views_handler_field'),
  613. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  614. 'sort' => array('name' => 'chado_views_handler_sort'),
  615. */
  616. 'field' => array('name' => 'views_handler_field'),
  617. 'filter' => array('name' => 'views_handler_filter_string'),
  618. 'sort' => array('name' => 'views_handler_sort'),
  619. );
  620. }
  621. // Specify specialty handlers
  622. if ($field_name == 'type_id' OR $field_name == 'cvterm_id') {
  623. //$defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_cvterm';
  624. }
  625. }
  626. }
  627. // Add Joins & Relationship Handlers to fields
  628. if (!isset($schema['foreign keys'])) {
  629. $schema['foreign keys'] = array();
  630. watchdog('tripal_views', 'There are no foreign keys defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_WARNING);
  631. }
  632. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  633. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  634. // Join
  635. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  636. 'table' => $foreign_key_schema['table'],
  637. 'field' => $right_field,
  638. /**D6 'handler' => 'views_handler_join_chado_aggregator'*/
  639. 'handler' => 'views_handler_join'
  640. );
  641. // Relationship Handler
  642. $defn_array['fields'][$left_field]['handlers']['relationship'] = array(
  643. /**D6 'name' => 'chado_views_handler_relationship',*/
  644. 'name' => 'views_handler_relationship',
  645. 'base' => $foreign_key_schema['table'],
  646. 'base field' => $right_field,
  647. 'label' => $table_name . ' ' . $left_field . ' => ' . $foreign_key_schema['table'] . ' ' . $right_field
  648. );
  649. }
  650. }
  651. return $defn_array;
  652. }
  653. /**
  654. * Adds the joins necessary to link a chado table to it's node counterpart
  655. *
  656. * @param $defn_array
  657. * The current definition array for a given table
  658. *
  659. * @ingroup tripal_views_api
  660. */
  661. function tripal_views_add_node_relationship_to_chado_table_integration($defn_array) {
  662. $integrations[$defn_array['table']] = $defn_array;
  663. $primary_key = $defn_array['table'] . '_id';
  664. $chado_linking = 'chado_' . $defn_array['table'];
  665. if (empty($defn_array['table'])) {
  666. watchdog('tripal_views','Tried to add a node=>chado relationship for an empty table defn: %defn',
  667. array('%defn' => print_r($defn_array,TRUE)),WATCHDOG_WARNING);
  668. return FALSE;
  669. }
  670. // Add table.primary_key => chado_table.primary key join to $defn_array
  671. $integrations[$defn_array['table']]['fields'][$primary_key]['joins'][$chado_linking] = array(
  672. 'table' => $chado_linking,
  673. 'field' => $primary_key,
  674. );
  675. // Create chado_table defn_array
  676. $integrations[$chado_linking] = array(
  677. 'table' => $chado_linking,
  678. 'type' => 'drupal',
  679. 'name' => 'Chado ' . $defn_array['table'] . ' Node',
  680. 'description' => 'Links chado content to its drupal node counterpart',
  681. 'priority' => $defn_array['priority'],
  682. 'base_table' => FALSE,
  683. 'fields' => array(
  684. $primary_key => array(
  685. 'name' => $primary_key,
  686. 'title' => ucwords(str_replace('_', ' ', $primary_key)),
  687. 'type' => 'int',
  688. 'description' => 'The primary key of the chado ' . $defn_array['table'] . ' table',
  689. 'handlers' => array(),
  690. 'joins' => array(
  691. $defn_array['table'] => array(
  692. 'table' => $defn_array['table'],
  693. 'field' => $primary_key,
  694. )
  695. ),
  696. ),
  697. 'nid' => array(
  698. 'name' => 'nid',
  699. 'title' => 'Node ID',
  700. 'type' => 'int',
  701. 'description' => 'Link ' . ucfirst($defn_array['table']) . ' to it\'s node',
  702. 'handlers' => array(
  703. 'relationship' => array(
  704. 'name' => 'chado_views_handler_relationship_to_node',
  705. 'title' => ucfirst($defn_array['table']) . ' to Node',
  706. 'label' => ucfirst($defn_array['table']) . ' to Node',
  707. 'base table' => $defn_array['table'],
  708. 'base field' => $primary_key
  709. )
  710. ),
  711. 'joins' => array(
  712. 'node' => array(
  713. 'table' => 'node',
  714. 'field' => 'nid',
  715. ),
  716. ),
  717. )
  718. ),
  719. );
  720. // Create node defn_array
  721. $integrations['node'] = array(
  722. 'table' => 'node',
  723. 'name' => 'Node',
  724. 'description' => 'Primary Drupal Content',
  725. 'priority' => $defn_array['priority'],
  726. 'additional_content' => TRUE, // Allows multiple modules to add to the node setup
  727. 'fields' => array(
  728. 'nid' => array(
  729. 'name' => 'nid',
  730. 'title' => 'Node ID',
  731. 'type' => 'int',
  732. 'description' => 'the primary key of the drupal node table',
  733. 'handlers' => array(),
  734. 'joins' => array(
  735. $defn_array['table'] => array(
  736. 'table' => $defn_array['table'],
  737. 'field' => 'nid',
  738. ),
  739. $chado_linking => array(
  740. 'table' => $chado_linking,
  741. 'field' => 'nid',
  742. ),
  743. ),
  744. ),
  745. ),
  746. );
  747. return $integrations;
  748. }
  749. /**
  750. * Clone an integration
  751. *
  752. * @param $table_name
  753. * The table for which you'd like to clone an integration
  754. * @param $new_priority
  755. * The priority of the clone; this is the integration which will be created.
  756. * If no priority is supplied then one lighter then the $template_priority will be used.
  757. * @param $template_priority
  758. * The priority of the template to be used for the clone; this is an existing integration.
  759. * If no priority is supplied then the lightest priority will be used.
  760. *
  761. * @ingroup tripal_views_api
  762. */
  763. function tripal_views_clone_integration($table_name, $new_priority = NULL, $template_priority = NULL) {
  764. if (empty($template_priority)) {
  765. $template_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  766. }
  767. else {
  768. $template_setup_id = tripal_views_get_setup_id($table_name, $template_priority);
  769. }
  770. $defn_array = tripal_views_integration_export_entry($template_setup_id);
  771. if (empty($new_priority)) {
  772. $defn_array['priority'] = $new_priority;
  773. }
  774. else {
  775. $new_priority = $defn_array['priority'] - 1;
  776. $defn_array['priority'] = $defn_array['priority'] - 1;
  777. }
  778. // D7 TODO: Check DBTNG changes work
  779. tripal_views_integration_add_entry($defn_array);
  780. $setup_id = db_query(
  781. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  782. array(
  783. ':table' => $table_name,
  784. ':priority' => $new_priority
  785. )
  786. );
  787. $setup_id = $setup_id->fetchObject();
  788. if (empty($setup_id)) {
  789. watchdog('tripal_views','Unable to clone the setup for %table in order to add the following field to the integration: %field.',
  790. array('%table' => $table_name, '%field' => print_r($field_array,TRUE)),WATCHDOG_ERROR);
  791. return FALSE;
  792. }
  793. else {
  794. return $setup_id;
  795. }
  796. }
  797. /**
  798. * Adds the given field to an existing or cloned integration. In the case of a cloned
  799. * integration, the lightest integration is used as the template for the clone.
  800. *
  801. * NOTE: If that field already exists in the specified integration then it will first be
  802. * deleted and the new one added.
  803. *
  804. * @param $table_name
  805. * The name of the table the integration is for
  806. * @param $priority
  807. * The priority of the integration to use; pass NULL to use the lightest integration
  808. * @param $field
  809. * An array describing the field ot add; uses the same format as the $defn_array
  810. *
  811. * @return
  812. * TRUE if the field was added successfully; FALSE otherwise
  813. *
  814. * @ingroup tripal_views_api
  815. */
  816. function tripal_views_add_field_to_integration($table_name, $priority, $field) {
  817. $no_errors = TRUE;
  818. // If no priority is supplied then add the field to the lightest integration
  819. if (empty($priority)) {
  820. $priority = tripal_views_get_table_lightest_priority($table_name);
  821. }
  822. // First get the setup_id
  823. // D7 TODO: Check DBTNG changes work
  824. $setup_id = db_query(
  825. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  826. array(
  827. ':table' => $table_name,
  828. ':priority' => $priority
  829. )
  830. );
  831. $setup_id = $setup_id->fetchObject();
  832. // If there isn't an integration matching that table/priority combination
  833. // then clone the lightest priority integration
  834. if (empty($setup_id)) {
  835. $setup_id = tripal_views_clone_integration($table_name, $priority);
  836. }
  837. // Now delete any existing field
  838. db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column",
  839. array(':setup' => $setup_id, 'column' => $field['name'])
  840. );
  841. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column",
  842. array(':setup' => $setup_id, 'column' => $field['name'])
  843. );
  844. db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field",
  845. array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  846. );
  847. // Now we need to add/update the field
  848. $field_record = array(
  849. 'setup_id' => $setup_id,
  850. 'column_name' => $field['name'],
  851. 'name' => $field['title'],
  852. 'description' => $field['description'],
  853. 'type' => $field['type'],
  854. );
  855. if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  856. if ($defn_array['additional_content']) {
  857. // D7 TODO: Check DBTNG changes work
  858. $is = db_query(
  859. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  860. array(':column' => $field_record['column_name'], ':setup' => $field_record['setup_id'])
  861. );
  862. $is = $is->fetchObject();
  863. if (!$is->present) {
  864. $status = drupal_write_record('tripal_views_field', $field_record);
  865. }
  866. else {
  867. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  868. }
  869. }
  870. else {
  871. $status = drupal_write_record('tripal_views_field', $field_record);
  872. }
  873. }
  874. else {
  875. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  876. $status = FALSE;
  877. }
  878. if ($status) {
  879. // Insert Handler Definitions
  880. foreach ($field['handlers'] as $handler_type => $handler) {
  881. $handler_record = array(
  882. 'setup_id' => $setup_id,
  883. 'column_name' => $field['name'],
  884. 'handler_type' => $handler_type,
  885. 'handler_name' => $handler['name'],
  886. 'arguments' => serialize($handler)
  887. );
  888. if ($setup_id && $field['name'] && $handler_type && $handler['name'] && $handler) {
  889. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  890. }
  891. else {
  892. $status = FALSE;
  893. }
  894. if (!$status) {
  895. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  896. $no_errors = FALSE;
  897. }
  898. }
  899. // Insert Joins
  900. if (!is_array($field['joins'])) {
  901. $field['joins'] = array();
  902. }
  903. foreach ($field['joins'] as $join) {
  904. $join_record = array(
  905. 'setup_id' => $setup_id,
  906. 'base_table' => $defn_array['table'],
  907. 'base_field' => $field['name'],
  908. 'left_table' => $join['table'],
  909. 'left_field' => $join['field'],
  910. );
  911. if (!empty($join['handler'])) {
  912. $join_record['handler'] = $join['handler'];
  913. }
  914. else {
  915. $join_record['handler'] = 'views_join';
  916. }
  917. if ($setup_id && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  918. $status = drupal_write_record('tripal_views_join', $join_record);
  919. }
  920. else {
  921. $status = FALSE;
  922. }
  923. if (!$status) {
  924. drupal_set_message(
  925. t(
  926. 'Unable to join %left_table.%left_field with %table.%field',
  927. array(
  928. '%left_table' => $join['table'],
  929. '%left_field' => $join['field'],
  930. '%table' => $defn_array['table'],
  931. '%field' => $field['name']
  932. )
  933. ),
  934. 'error'
  935. );
  936. $no_errors = FALSE;
  937. }
  938. }
  939. }
  940. else {
  941. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  942. $no_errors = FALSE;
  943. }
  944. return $no_errors;
  945. }
  946. /**
  947. * Remove a join from an integration
  948. *
  949. * @param $setup_id
  950. * The setup_id of the integration to delete the join from
  951. * @param $base_table
  952. * The name of the base table involved the join
  953. * @param $base_field
  954. * The field from the base table involved in the join
  955. * @param $left_table
  956. * The name of the other table involved in the join
  957. * @param $left_field
  958. * The name of the field from the other table involved in the join
  959. *
  960. * @ingroup tripal_views_api
  961. */
  962. function tripal_views_remove_join_from_integration($setup_id, $base_table, $base_field, $left_table, $left_field) {
  963. db_query(
  964. "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",
  965. array(
  966. ':setup' => $setup_id,
  967. ':base-table' => $base_table,
  968. ':base-field' => $base_field,
  969. ':left-table' => $left_table,
  970. ':left-field' => $left_field
  971. )
  972. );
  973. }