tripal_views.api.inc

  1. 2.x tripal_views/api/tripal_views.api.inc
  2. 1.x tripal_views/api/tripal_views.api.inc

API functions for Tripal Views Integration

tripal_views_api Tripal Views Module API

File

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

Related topics