tripal_views.api.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. $join_record['handler'] = (!empty($join['handler'])) ? $join['handler'] : 'views_join';
  321. $join_record['relationship_handler'] = (!empty($join['relationship_handler'])) ? $join['relationship_handler'] : 'views_handler_relationship';
  322. $join_record['relationship_only'] = (!empty($join['relationship_only'])) ? $join['relationship_only'] : 0;
  323. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  324. $status = drupal_write_record('tripal_views_join', $join_record);
  325. }
  326. else {
  327. $status = FALSE;
  328. }
  329. if (!$status) {
  330. drupal_set_message(
  331. t(
  332. 'Unable to join %left_table.%left_field with %table.%field',
  333. array(
  334. '%left_table' => $join['table'],
  335. '%left_field' => $join['field'],
  336. '%table' => $defn_array['table'],
  337. '%field' => $field['name']
  338. )
  339. ),
  340. 'error'
  341. );
  342. $no_errors = FALSE;
  343. }
  344. }
  345. }
  346. else {
  347. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  348. $no_errors = FALSE;
  349. }
  350. }
  351. }
  352. else {
  353. drupal_set_message(t('Unable to set default tripal views integration'), 'error');
  354. $no_errors = FALSE;
  355. }
  356. return $no_errors;
  357. }
  358. /**
  359. * Export Views integration records
  360. *
  361. * @param $setup_id
  362. * The unique setup id of the tripal views integration
  363. *
  364. * @return
  365. * A views integration definition array as used by tripal_views_integration_add_entry()
  366. *
  367. * @ingroup tripal_views_api
  368. */
  369. function tripal_views_integration_export_entry($setup_id) {
  370. // Main setup details
  371. // D7 TODO: Check DBTNG changes work
  372. $r = db_query("SELECT * FROM {tripal_views} WHERE setup_id=:setup", array(':setup' => $setup_id));
  373. $r = $r->fetchObject();
  374. $defn_array = array(
  375. 'table' => $r->table_name,
  376. 'name' => $r->name,
  377. 'type' => ($r->mview_id) ? 'mview' : 'chado',
  378. 'description' => $r->comment,
  379. 'priority' => $r->priority,
  380. 'base_table' => $r->base_table,
  381. 'fields' => array(),
  382. );
  383. // Add fields
  384. // D7 TODO: Check DBTNG changes work
  385. $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=:setup", array(':setup' => $setup_id));
  386. foreach ($resource as $r) {
  387. $defn_array['fields'][ $r->column_name ] = array(
  388. 'name' => $r->column_name,
  389. 'title' => $r->name,
  390. 'description' => $r->description,
  391. 'type' => $r->type,
  392. 'handlers' => array(),
  393. 'joins' => array()
  394. );
  395. }
  396. // Add handlers
  397. // D7 TODO: Check DBTNG changes work
  398. $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=:setup", array(':setup' => $setup_id));
  399. foreach ($resource as $r) {
  400. $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
  401. 'name' => $r->handler_name
  402. );
  403. }
  404. // Add joins
  405. // D7 TODO: Check DBTNG changes work
  406. $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=:setup", array(':setup' => $setup_id));
  407. foreach ($resource as $r) {
  408. $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ] = array(
  409. 'table' => $r->left_table,
  410. 'field' => $r->left_field,
  411. 'handler' => $r->handler,
  412. );
  413. }
  414. return $defn_array;
  415. }
  416. /**
  417. * Removes a View Integration Entry
  418. *
  419. * @param $table_name
  420. * The name of the table to remove a views integration entry for
  421. * @param $priority
  422. * The priority of the of views integration entry
  423. *
  424. * @return
  425. * TRUE on Success; FALSE otherwise
  426. *
  427. * @ingroup tripal_views_api
  428. */
  429. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  430. // D7 TODO: Check DBTNG changes work
  431. $views = db_query(
  432. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  433. array(
  434. ':table' => $table_name,
  435. ':priority' => $priority
  436. )
  437. );
  438. $views = $views->fetchObject();
  439. if ($views->setup_id) {
  440. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  441. return TRUE;
  442. }
  443. else {
  444. return FALSE;
  445. }
  446. }
  447. /**
  448. * Removes a View Integration Entry
  449. *
  450. * @param $setup_id
  451. * The setup ID of the views integration entry to remove
  452. *
  453. * @ingroup tripal_views_api
  454. */
  455. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  456. // D7 TODO: Check DBTNG changes work
  457. db_query('DELETE FROM {tripal_views} WHERE setup_id=:setup', array(':setup' => $setup_id));
  458. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=:setup', array(':setup' => $setup_id));
  459. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup', array(':setup' => $setup_id));
  460. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=:setup', array(':setup' => $setup_id));
  461. }
  462. /**
  463. * Integrate all chado tables in the schema api. This integration only occurs
  464. * once and sets all Chado tables to a priority of 10
  465. *
  466. * @ingroup tripal_views_api
  467. */
  468. function tripal_views_integrate_all_chado_tables() {
  469. $tables = tripal_core_get_chado_tables(TRUE);
  470. $base_tables = array(
  471. 'acquisition', 'analysis', 'assay', 'biomaterial', 'contact', 'cv', 'cvterm',
  472. 'db', 'dbxref', 'environment', 'expression', 'feature', 'featuremap', 'genotype',
  473. 'library', 'nd_experiment', 'nd_geolocation', 'nd_protocol', 'nd_reagent',
  474. 'organism', 'phendesc', 'phenotype', 'phenstatement', 'phylonode', 'phylotree',
  475. 'project', 'protocol', 'pub', 'stock', 'study', 'synonym'
  476. );
  477. foreach ($tables as $tablename) {
  478. $priority = 10;
  479. if (!tripal_views_is_integrated($tablename, $priority)) {
  480. if (in_array($tablename, $base_tables)) {
  481. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
  482. }
  483. else {
  484. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
  485. }
  486. if ($table_integration_array) {
  487. tripal_views_integration_add_entry($table_integration_array);
  488. }
  489. }
  490. }
  491. }
  492. /**
  493. * Returns the array needed to integrate a given chado table with views
  494. *
  495. * @param $tablename
  496. * The table to generate the tripal views integration array for
  497. * @return
  498. * The tripal views integration array which is the parameter for
  499. * tripal_views_integration_add_entry($defn_array)
  500. *
  501. * @ingroup tripal_views_api
  502. */
  503. function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE, $priority = 9) {
  504. // Get the schema for this table (via the chado schema api)
  505. $schema = tripal_core_get_chado_table_schema($table_name);
  506. // Base definition array
  507. $defn_array = array(
  508. 'table' => $table_name,
  509. 'type' => 'chado',
  510. 'name' => 'Chado ' . ucwords(str_replace('_', ' ', $table_name)),
  511. 'description' => (!empty($schema['description'])) ? $schema['description'] : ' ',
  512. 'priority' => $priority,
  513. 'base_table' => $base_table,
  514. 'fields' => array(),
  515. );
  516. // Add fields
  517. if (!isset($schema['fields'])) {
  518. watchdog('tripal_views', 'There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_NOTICE);
  519. return FALSE;
  520. }
  521. foreach ($schema['fields'] as $field_name => $field_schema) {
  522. // Base field definition
  523. if (!empty($field_name) && !empty($field_schema['type'])) {
  524. $defn_array['fields'][$field_name] = array(
  525. 'name' => $field_name,
  526. 'title' => ucwords(str_replace('_', ' ', $field_name)),
  527. 'type' => $field_schema['type'],
  528. 'description' => ($field_schema['description']) ? $field_schema['description'] : ucwords(str_replace('_', ' ', $field_name)),
  529. 'handlers' => array(),
  530. 'joins' => array()
  531. );
  532. // Add handlers based on type
  533. if (preg_match('/^int/', $field_schema['type'])) {
  534. $defn_array['fields'][$field_name]['handlers'] = array(
  535. /** D6
  536. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  537. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  538. 'sort' => array('name' => 'chado_views_handler_sort'),
  539. */
  540. 'field' => array('name' => 'views_handler_field_numeric'),
  541. 'filter' => array('name' => 'views_handler_filter_numeric'),
  542. 'sort' => array('name' => 'views_handler_sort'),
  543. );
  544. }
  545. elseif (preg_match('/^serial/', $field_schema['type'])) {
  546. $defn_array['fields'][$field_name]['handlers'] = array(
  547. /** D6
  548. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  549. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  550. 'sort' => array('name' => 'chado_views_handler_sort'),
  551. */
  552. 'field' => array('name' => 'views_handler_field_numeric'),
  553. 'filter' => array('name' => 'views_handler_filter_numeric'),
  554. 'sort' => array('name' => 'views_handler_sort'),
  555. );
  556. $defn_array['fields'][$field_name]['type'] = 'int';
  557. }
  558. elseif (preg_match('/^varchar/', $field_schema['type'])) {
  559. $defn_array['fields'][$field_name]['handlers'] = array(
  560. /** D6
  561. 'field' => array('name' => 'chado_views_handler_field'),
  562. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  563. 'sort' => array('name' => 'chado_views_handler_sort'),
  564. */
  565. 'field' => array('name' => 'views_handler_field'),
  566. 'filter' => array('name' => 'views_handler_filter_string'),
  567. 'sort' => array('name' => 'views_handler_sort'),
  568. );
  569. }
  570. elseif (preg_match('/^text/', $field_schema['type'])) {
  571. $defn_array['fields'][$field_name]['handlers'] = array(
  572. /** D6
  573. 'field' => array('name' => 'chado_views_handler_field'),
  574. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  575. 'sort' => array('name' => 'chado_views_handler_sort'),
  576. */
  577. 'field' => array('name' => 'views_handler_field'),
  578. 'filter' => array('name' => 'views_handler_filter_string'),
  579. 'sort' => array('name' => 'views_handler_sort'),
  580. );
  581. }
  582. elseif (preg_match('/^boolean/', $field_schema['type'])) {
  583. $defn_array['fields'][$field_name]['handlers'] = array(
  584. /**
  585. 'field' => array('name' => 'chado_views_handler_field_boolean'),
  586. 'filter' => array('name' => 'chado_views_handler_filter_boolean_operator'),
  587. 'sort' => array('name' => 'chado_views_handler_sort'),
  588. */
  589. 'field' => array('name' => 'views_handler_field_boolean'),
  590. 'filter' => array('name' => 'views_handler_filter_boolean_operator'),
  591. 'sort' => array('name' => 'views_handler_sort'),
  592. );
  593. }
  594. elseif (preg_match('/^datetime/', $field_schema['type'])) {
  595. $defn_array['fields'][$field_name]['handlers'] = array(
  596. /** D6
  597. 'field' => array('name' => 'chado_views_handler_field_date'),
  598. 'filter' => array('name' => 'chado_views_handler_filter_date'),
  599. 'sort' => array('name' => 'views_handler_sort_date'),
  600. */
  601. 'field' => array('name' => 'views_handler_field_date'),
  602. 'filter' => array('name' => 'views_handler_filter_date'),
  603. 'sort' => array('name' => 'views_handler_sort_date'),
  604. );
  605. }
  606. else {
  607. $defn_array['fields'][$field_name]['handlers'] = array(
  608. /** D6
  609. 'field' => array('name' => 'chado_views_handler_field'),
  610. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  611. 'sort' => array('name' => 'chado_views_handler_sort'),
  612. */
  613. 'field' => array('name' => 'views_handler_field'),
  614. 'filter' => array('name' => 'views_handler_filter_string'),
  615. 'sort' => array('name' => 'views_handler_sort'),
  616. );
  617. }
  618. // Specify specialty handlers
  619. if ($field_name == 'type_id' OR $field_name == 'cvterm_id') {
  620. $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_cvterm';
  621. }
  622. }
  623. }
  624. // Add Joins & Relationships for foreign keys to fields
  625. if (!isset($schema['foreign keys'])) {
  626. $schema['foreign keys'] = array();
  627. watchdog('tripal_views', 'There are no foreign keys defined for %table in the Chado Schema API.', array('%table' => $table_name), WATCHDOG_WARNING);
  628. }
  629. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  630. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  631. // Join
  632. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  633. 'table' => $foreign_key_schema['table'],
  634. 'field' => $right_field,
  635. /**D6 'handler' => 'views_handler_join_chado_aggregator'*/
  636. 'handler' => 'views_handler_join',
  637. 'relationship_handler' => 'views_handler_relationship',
  638. );
  639. }
  640. }
  641. // Add in reverse relationships
  642. if (isset($schema['referring_tables'])) {
  643. foreach ($schema['referring_tables'] as $referring_table) {
  644. // D7 @todo: fix referring_tables in schema to list the keys like foreign keys does
  645. $referring_schema = tripal_core_get_chado_table_schema($referring_table);
  646. $referring_schema_fk_columns = $referring_schema['foreign keys'][$table_name]['columns'];
  647. foreach ($referring_schema_fk_columns as $left_field => $right_field) {
  648. $defn_array['fields'][$right_field]['joins'][ $referring_table ] = array(
  649. 'table' => $referring_table,
  650. 'field' => $left_field,
  651. 'relationship_handler' => 'views_handler_relationship',
  652. 'relationship_only' => 1
  653. );
  654. }
  655. }
  656. }
  657. return $defn_array;
  658. }
  659. /**
  660. * Clone an integration
  661. *
  662. * @param $table_name
  663. * The table for which you'd like to clone an integration
  664. * @param $new_priority
  665. * The priority of the clone; this is the integration which will be created.
  666. * If no priority is supplied then one lighter then the $template_priority will be used.
  667. * @param $template_priority
  668. * The priority of the template to be used for the clone; this is an existing integration.
  669. * If no priority is supplied then the lightest priority will be used.
  670. *
  671. * @ingroup tripal_views_api
  672. */
  673. function tripal_views_clone_integration($table_name, $new_priority = NULL, $template_priority = NULL) {
  674. if (empty($template_priority)) {
  675. $template_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  676. }
  677. else {
  678. $template_setup_id = tripal_views_get_setup_id($table_name, $template_priority);
  679. }
  680. $defn_array = tripal_views_integration_export_entry($template_setup_id);
  681. if (empty($new_priority)) {
  682. $defn_array['priority'] = $new_priority;
  683. }
  684. else {
  685. $new_priority = $defn_array['priority'] - 1;
  686. $defn_array['priority'] = $defn_array['priority'] - 1;
  687. }
  688. // D7 TODO: Check DBTNG changes work
  689. tripal_views_integration_add_entry($defn_array);
  690. $setup_id = db_query(
  691. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  692. array(
  693. ':table' => $table_name,
  694. ':priority' => $new_priority
  695. )
  696. );
  697. $setup_id = $setup_id->fetchObject();
  698. if (empty($setup_id)) {
  699. watchdog('tripal_views','Unable to clone the setup for %table in order to add the following field to the integration: %field.',
  700. array('%table' => $table_name, '%field' => print_r($field_array,TRUE)),WATCHDOG_ERROR);
  701. return FALSE;
  702. }
  703. else {
  704. return $setup_id;
  705. }
  706. }
  707. /**
  708. * Adds the given field to an existing or cloned integration. In the case of a cloned
  709. * integration, the lightest integration is used as the template for the clone.
  710. *
  711. * NOTE: If that field already exists in the specified integration then it will first be
  712. * deleted and the new one added.
  713. *
  714. * @param $table_name
  715. * The name of the table the integration is for
  716. * @param $priority
  717. * The priority of the integration to use; pass NULL to use the lightest integration
  718. * @param $field
  719. * An array describing the field ot add; uses the same format as the $defn_array
  720. *
  721. * @return
  722. * TRUE if the field was added successfully; FALSE otherwise
  723. *
  724. * @ingroup tripal_views_api
  725. */
  726. function tripal_views_add_field_to_integration($table_name, $priority, $field) {
  727. $no_errors = TRUE;
  728. // If no priority is supplied then add the field to the lightest integration
  729. if (empty($priority)) {
  730. $priority = tripal_views_get_table_lightest_priority($table_name);
  731. }
  732. // First get the setup_id
  733. // D7 TODO: Check DBTNG changes work
  734. $setup_id = db_query(
  735. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  736. array(
  737. ':table' => $table_name,
  738. ':priority' => $priority
  739. )
  740. );
  741. $setup_id = $setup_id->fetchObject();
  742. // If there isn't an integration matching that table/priority combination
  743. // then clone the lightest priority integration
  744. if (empty($setup_id)) {
  745. $setup_id = tripal_views_clone_integration($table_name, $priority);
  746. }
  747. // Now delete any existing field
  748. db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column",
  749. array(':setup' => $setup_id, 'column' => $field['name'])
  750. );
  751. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column",
  752. array(':setup' => $setup_id, 'column' => $field['name'])
  753. );
  754. db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field",
  755. array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  756. );
  757. // Now we need to add/update the field
  758. $field_record = array(
  759. 'setup_id' => $setup_id,
  760. 'column_name' => $field['name'],
  761. 'name' => $field['title'],
  762. 'description' => $field['description'],
  763. 'type' => $field['type'],
  764. );
  765. if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  766. if ($defn_array['additional_content']) {
  767. // D7 TODO: Check DBTNG changes work
  768. $is = db_query(
  769. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  770. array(':column' => $field_record['column_name'], ':setup' => $field_record['setup_id'])
  771. );
  772. $is = $is->fetchObject();
  773. if (!$is->present) {
  774. $status = drupal_write_record('tripal_views_field', $field_record);
  775. }
  776. else {
  777. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  778. }
  779. }
  780. else {
  781. $status = drupal_write_record('tripal_views_field', $field_record);
  782. }
  783. }
  784. else {
  785. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  786. $status = FALSE;
  787. }
  788. if ($status) {
  789. // Insert Handler Definitions
  790. foreach ($field['handlers'] as $handler_type => $handler) {
  791. $handler_record = array(
  792. 'setup_id' => $setup_id,
  793. 'column_name' => $field['name'],
  794. 'handler_type' => $handler_type,
  795. 'handler_name' => $handler['name'],
  796. 'arguments' => serialize($handler)
  797. );
  798. if ($setup_id && $field['name'] && $handler_type && $handler['name'] && $handler) {
  799. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  800. }
  801. else {
  802. $status = FALSE;
  803. }
  804. if (!$status) {
  805. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  806. $no_errors = FALSE;
  807. }
  808. }
  809. // Insert Joins
  810. if (!is_array($field['joins'])) {
  811. $field['joins'] = array();
  812. }
  813. foreach ($field['joins'] as $join) {
  814. $join_record = array(
  815. 'setup_id' => $setup_id,
  816. 'base_table' => $defn_array['table'],
  817. 'base_field' => $field['name'],
  818. 'left_table' => $join['table'],
  819. 'left_field' => $join['field'],
  820. );
  821. if (!empty($join['handler'])) {
  822. $join_record['handler'] = $join['handler'];
  823. }
  824. else {
  825. $join_record['handler'] = 'views_join';
  826. }
  827. if ($setup_id && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  828. $status = drupal_write_record('tripal_views_join', $join_record);
  829. }
  830. else {
  831. $status = FALSE;
  832. }
  833. if (!$status) {
  834. drupal_set_message(
  835. t(
  836. 'Unable to join %left_table.%left_field with %table.%field',
  837. array(
  838. '%left_table' => $join['table'],
  839. '%left_field' => $join['field'],
  840. '%table' => $defn_array['table'],
  841. '%field' => $field['name']
  842. )
  843. ),
  844. 'error'
  845. );
  846. $no_errors = FALSE;
  847. }
  848. }
  849. }
  850. else {
  851. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  852. $no_errors = FALSE;
  853. }
  854. return $no_errors;
  855. }
  856. /**
  857. * Remove a join from an integration
  858. *
  859. * @param $setup_id
  860. * The setup_id of the integration to delete the join from
  861. * @param $base_table
  862. * The name of the base table involved the join
  863. * @param $base_field
  864. * The field from the base table involved in the join
  865. * @param $left_table
  866. * The name of the other table involved in the join
  867. * @param $left_field
  868. * The name of the field from the other table involved in the join
  869. *
  870. * @ingroup tripal_views_api
  871. */
  872. function tripal_views_remove_join_from_integration($setup_id, $base_table, $base_field, $left_table, $left_field) {
  873. db_query(
  874. "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",
  875. array(
  876. ':setup' => $setup_id,
  877. ':base-table' => $base_table,
  878. ':base-field' => $base_field,
  879. ':left-table' => $left_table,
  880. ':left-field' => $left_field
  881. )
  882. );
  883. }