tripal_views.api.inc 31 KB

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