tripal_views.api.inc 34 KB

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