tripal_views.api.inc 35 KB

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