tripal_views.api.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * @file
  4. * API functions for Tripal Views Integration
  5. */
  6. /**
  7. * Retrieve the views integration setup with the lightest priority for a given table
  8. *
  9. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  10. * and -10 is of highest priority.
  11. *
  12. * @param $table_name
  13. * The name of the table to retrieve the setup ID for. This can be either a materialized
  14. * view or a chado table
  15. *
  16. * @return
  17. * On success, the setup_id to use for integration of this table; otherwise FALSE
  18. */
  19. function tripal_views_get_lightest_priority_setup($table_name) {
  20. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
  21. $setup = db_fetch_object(db_query($sql, $table_name));
  22. if ($setup) {
  23. return $setup->setup_id;
  24. }
  25. else {
  26. return FALSE;
  27. }
  28. }
  29. /**
  30. * Check to see if this table already has an integration record with the given priority
  31. *
  32. * @param $table_name
  33. * The name of the table to check for integration
  34. * @param $priority (optional)
  35. * The priority of record to check for
  36. *
  37. * @return
  38. * If the table is already integrated, the setup_id of the existing integration
  39. * record is returned (If priority is not specified this will be the lightest record);
  40. * Otherwise the table is not already integrated and FALSE is returned.
  41. */
  42. function tripal_views_is_integrated($table_name, $priority = NULL) {
  43. if ($priority) {
  44. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' AND priority=%d";
  45. $setup = db_fetch_object(db_query($sql, $table_name, $priority));
  46. }
  47. else {
  48. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
  49. $setup = db_fetch_object(db_query($sql, $table_name));
  50. }
  51. if ($setup) {
  52. return $setup->setup_id;
  53. }
  54. else {
  55. return FALSE;
  56. }
  57. }
  58. /**
  59. * Checks if you are dealing with the lightest priority setup for a given table
  60. *
  61. * @param $setup_id
  62. * The ID of the setup to check (is this setup the lightest one?)
  63. * @param $table_name
  64. * The name of the table associated with this setup
  65. *
  66. * @return TRUE is this is the lightest priority; FALSE otherwise
  67. */
  68. function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
  69. $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  70. if ($lightest_priority_setup_id == $setup_id) {
  71. return TRUE;
  72. }
  73. else {
  74. return FALSE;
  75. }
  76. }
  77. /**
  78. * Add views integration records into the tripal_views* tables
  79. *
  80. * @param $defn_array
  81. * An array describing the structure and fields of the table
  82. *
  83. * @return
  84. * True/False if completed successfully/not
  85. *
  86. * Example usage (in hook_install()):
  87. * @code
  88. $defn_array = array(
  89. 'table' => 'feature', //tablename or materialized view name
  90. 'name' => 'Sequence Features', // Human readable name
  91. 'type' => 'chado', //either chado or mview depending on tablename
  92. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  93. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  94. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  95. 'fields' => array(
  96. 'feature_id' => array(
  97. 'name' => 'feature_id', //field name in database
  98. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  99. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  100. 'type' => 'int', // the type of field
  101. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  102. 'field' => array(
  103. 'name' => 'chado_views_handler_numeric' //name of handler
  104. ),
  105. 'filter' => array( ... ),
  106. ...
  107. ),
  108. 'join' => array( //describe a table that joins to this one via this field
  109. 'table' => 'featureprop', //table to join to
  110. 'field' => 'feature_id', //field in above table (featureprop)
  111. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  112. ),
  113. )
  114. ),
  115. );
  116. tripal_views_integration_add_entry($defn_array);
  117. * @endcode
  118. */
  119. function tripal_views_integration_add_entry($defn_array) {
  120. $no_errors = TRUE;
  121. // First insert into tripal_views
  122. $view_record = array(
  123. 'table_name' => $defn_array['table'],
  124. 'name' => $defn_array['name'],
  125. 'comment' => $defn_array['description'],
  126. 'priority' => $defn_array['priority'],
  127. 'base_table' => $defn_array['base_table'],
  128. );
  129. if ($defn_array['type'] == 'mview') {
  130. $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
  131. $view_record['mview_id'] = $mview->mview_id;
  132. if (!$mview->mview_id) {
  133. return FALSE;
  134. }
  135. }
  136. if ($view_record['name'] && $view_record['comment']) {
  137. $status = drupal_write_record('tripal_views', $view_record);
  138. }
  139. else {
  140. $status = FALSE;
  141. drupal_set_message(t('Unable to integrate %table table due to a missing name or comment field.', array('%table' => $defn_array['table'])), 'error');
  142. }
  143. if ($status) {
  144. // Need to update the tripal_views record so base_table can be false
  145. // this is a fix because drupal_write_record() puts in defaults if !isset()
  146. // and a variable is considered not set if its null!
  147. db_query(
  148. "UPDATE {tripal_views} SET base_table=%d WHERE table_name='%s' AND priority=%d",
  149. $defn_array['base_table'],
  150. $defn_array['table'],
  151. $defn_array['priority']
  152. );
  153. // Insert Field Definitions
  154. foreach ($defn_array['fields'] as $field) {
  155. $field_record = array(
  156. 'setup_id' => $view_record['setup_id'],
  157. 'column_name' => $field['name'],
  158. 'name' => $field['title'],
  159. 'description' => $field['description'],
  160. 'type' => $field['type'],
  161. );
  162. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  163. $status = drupal_write_record('tripal_views_field', $field_record);
  164. }
  165. else {
  166. drupal_set_message(t('Unable to integrate %name field due to a missing required fields.', array('%name' => $field['name'])), 'error');
  167. $status = FALSE;
  168. }
  169. if ($status) {
  170. // Insert Handler Definitions
  171. foreach ($field['handlers'] as $handler_type => $handler) {
  172. $handler_record = array(
  173. 'setup_id' => $view_record['setup_id'],
  174. 'column_name' => $field['name'],
  175. 'handler_type' => $handler_type,
  176. 'handler_name' => $handler['name'],
  177. 'arguments' => serialize($handler)
  178. );
  179. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  180. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  181. }
  182. else {
  183. $status = FALSE;
  184. }
  185. if (!$status) {
  186. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  187. $no_errors = FALSE;
  188. }
  189. }
  190. // Insert Joins
  191. if (!is_array($field['joins'])) {
  192. $field['joins'] = array();
  193. }
  194. foreach ($field['joins'] as $join) {
  195. $join_record = array(
  196. 'setup_id' => $view_record['setup_id'],
  197. 'base_table' => $defn_array['table'],
  198. 'base_field' => $field['name'],
  199. 'left_table' => $join['table'],
  200. 'left_field' => $join['field'],
  201. );
  202. if (!empty($join['handler'])) {
  203. $join_record['handler'] = $join['handler'];
  204. }
  205. else {
  206. $join_record['handler'] = 'views_join';
  207. }
  208. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  209. $status = drupal_write_record('tripal_views_join', $join_record);
  210. }
  211. else {
  212. $status = FALSE;
  213. }
  214. if (!$status) {
  215. drupal_set_message(
  216. t(
  217. 'Unable to join %left_table.%left_field with %table.%field',
  218. array(
  219. '%left_table' => $join['table'],
  220. '%left_field' => $join['field'],
  221. '%table' => $defn_array['table'],
  222. '%field' => $field['name']
  223. )
  224. ),
  225. 'error'
  226. );
  227. $no_errors = FALSE;
  228. }
  229. }
  230. }
  231. else {
  232. drupal_set_message(t('Unable to integrate field: %field_name', array('%field_name' => $field['name'])), 'error');
  233. $no_errors = FALSE;
  234. }
  235. }
  236. }
  237. else {
  238. drupal_set_message(t('Unable to set default views integration'), 'error');
  239. $no_errors = FALSE;
  240. }
  241. return $no_errors;
  242. }
  243. /**
  244. * Export Views integration records
  245. *
  246. * @param $setup_id
  247. * The unique setup id of the tripal views integration
  248. *
  249. * @return
  250. * A views integration definition array as used by tripal_views_integration_add_entry()
  251. */
  252. function tripal_views_integration_export_entry($setup_id) {
  253. // Main setup details
  254. $r = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE setup_id=%d", $setup_id));
  255. $defn_array = array(
  256. 'table' => $r->table_name,
  257. 'name' => $r->name,
  258. 'type' => ($r->mview_id) ? 'mview' : 'chado',
  259. 'description' => $r->comment,
  260. 'priority' => $r->priority,
  261. 'base_table' => $r->base_table,
  262. 'fields' => array(),
  263. );
  264. // Add fields
  265. $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=%d", $setup_id);
  266. while ($r = db_fetch_object($resource)) {
  267. $defn_array['fields'][ $r->column_name ] = array(
  268. 'name' => $r->column_name,
  269. 'title' => $r->name,
  270. 'description' => $r->description,
  271. 'type' => $r->type,
  272. 'handlers' => array(),
  273. 'joins' => array()
  274. );
  275. }
  276. // Add handlers
  277. $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d", $setup_id);
  278. while ($r = db_fetch_object($resource)) {
  279. $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
  280. 'name' => $r->handler_name
  281. );
  282. }
  283. // Add joins
  284. $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=%d", $setup_id);
  285. while ($r = db_fetch_object($resource)) {
  286. $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ] = array(
  287. 'table' => $r->left_table,
  288. 'field' => $r->left_field,
  289. 'handler' => $r->handler,
  290. );
  291. }
  292. return $defn_array;
  293. }
  294. /**
  295. * Removes a View Integration Entry
  296. *
  297. * @param $table_name
  298. * The name of the table to remove a views integration entry for
  299. * @param $priority
  300. * The priority of the of views integration entry
  301. *
  302. * @return
  303. * TRUE on Success; FALSE otherwise
  304. */
  305. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  306. $views = db_fetch_object(db_query(
  307. "SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",
  308. $table_name,
  309. $priority
  310. ));
  311. if ($views->setup_id) {
  312. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  313. return TRUE;
  314. }
  315. else {
  316. return FALSE;
  317. }
  318. }
  319. /**
  320. * Removes a View Integration Entry
  321. *
  322. * @param $setup_id
  323. * The setup ID of the views integration entry to remove
  324. */
  325. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  326. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d', $setup_id);
  327. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d', $setup_id);
  328. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d', $setup_id);
  329. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d', $setup_id);
  330. }
  331. /**
  332. * Returns the array needed to integrate a given chado table with views
  333. *
  334. * @param $tablename
  335. * The table to generate the tripal views integration array for
  336. * @return
  337. * The tripal views integration array which is the parameter for
  338. * tripal_views_integration_add_entry($defn_array)
  339. */
  340. function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE) {
  341. // Get the schema for this table (via the chado schema api)
  342. $schema = module_invoke_all('chado_' . $table_name . '_schema');
  343. // Base definition array
  344. $defn_array = array(
  345. 'table' => $table_name,
  346. 'type' => 'chado',
  347. 'name' => 'Chado ' . ucwords(str_replace('_', ' ', $table_name)),
  348. 'description' => (!empty($schema['description'])) ? $schema['description'] : ' ',
  349. 'priority' => 10,
  350. 'base_table' => $base_table,
  351. 'fields' => array(),
  352. );
  353. // Add fields
  354. if (!isset($schema['fields'])) {
  355. $schema['fields'] = array();
  356. drupal_set_message(t('There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name)), 'warning');
  357. }
  358. foreach ($schema['fields'] as $field_name => $field_schema) {
  359. // Base field definition
  360. if (!empty($field_name) && !empty($field_schema['type'])) {
  361. $defn_array['fields'][$field_name] = array(
  362. 'name' => $field_name,
  363. 'title' => ucwords(str_replace('_', ' ', $field_name)),
  364. 'type' => $field_schema['type'],
  365. 'description' => ($field_schema['description']) ? $field_schema['description'] : ucwords(str_replace('_', ' ', $field_name)),
  366. 'handlers' => array(),
  367. 'joins' => array()
  368. );
  369. // Add handlers based on type
  370. if (preg_match('/^int/', $field_schema['type'])) {
  371. $defn_array['fields'][$field_name]['handlers'] = array(
  372. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  373. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  374. 'sort' => array('name' => 'chado_views_handler_sort'),
  375. );
  376. }
  377. elseif (preg_match('/^serial/', $field_schema['type'])) {
  378. $defn_array['fields'][$field_name]['handlers'] = array(
  379. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  380. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  381. 'sort' => array('name' => 'chado_views_handler_sort'),
  382. );
  383. $defn_array['fields'][$field_name]['type'] = 'int';
  384. }
  385. elseif (preg_match('/^varchar/', $field_schema['type'])) {
  386. $defn_array['fields'][$field_name]['handlers'] = array(
  387. 'field' => array('name' => 'chado_views_handler_field'),
  388. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  389. 'sort' => array('name' => 'chado_views_handler_sort'),
  390. );
  391. }
  392. elseif (preg_match('/^text/', $field_schema['type'])) {
  393. $defn_array['fields'][$field_name]['handlers'] = array(
  394. 'field' => array('name' => 'chado_views_handler_field'),
  395. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  396. 'sort' => array('name' => 'chado_views_handler_sort'),
  397. );
  398. }
  399. else {
  400. $defn_array['fields'][$field_name]['handlers'] = array(
  401. 'field' => array('name' => 'chado_views_handler_field'),
  402. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  403. 'sort' => array('name' => 'chado_views_handler_sort'),
  404. );
  405. }
  406. // Specify specialty handlers
  407. if ($field_name == 'type_id') {
  408. $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'views_handler_filter_chado_select_cvterm_name';
  409. }
  410. }
  411. }
  412. // Add Joins to fields
  413. if (!isset($schema['foreign keys'])) {
  414. $schema['foreign keys'] = array();
  415. drupal_set_message(t('There are no foreign keys defined for %table in the Chado Schema API.', array('%table' => $table_name)), 'warning');
  416. }
  417. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  418. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  419. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  420. 'table' => $foreign_key_schema['table'],
  421. 'field' => $right_field,
  422. 'handler' => 'views_handler_join_chado_aggregtor'
  423. );
  424. }
  425. }
  426. return $defn_array;
  427. }