tripal_views.api.inc 35 KB

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