tripal_views.api.inc 35 KB

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