tripal_contact.module 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the basic functions needed for this drupal module.
  5. * The drupal tripal_contact module maps directly to the chado X module.
  6. *
  7. * For documentation regarding the Chado X module:
  8. * @see http://gmod.org/wiki/Chado_General_Module
  9. */
  10. require('api/tripal_contact.api.inc');
  11. require('includes/contact_sync.inc');
  12. require('includes/tripal_contact.admin.inc');
  13. require('includes/tripal_contact.form.inc');
  14. /**
  15. * @defgroup tripal_contact Contact Module
  16. * @ingroup tripal_modules
  17. * @{
  18. * Currently this module only provides support for integration with Drupal
  19. * views and some support for querying using the Tripal Core API.
  20. *
  21. * This module needs further development to support full management of
  22. * contact information within Chado, and full definitions for foreign
  23. * key relationships in Chado.
  24. * @}
  25. */
  26. /*************************************************************************
  27. * Implements hook_views_api()
  28. * Purpose: Essentially this hook tells drupal that there is views support for
  29. * for this module which then includes tripal_contact.views.inc where all the
  30. * views integration code is
  31. *
  32. * @ingroup tripal_contact
  33. */
  34. function tripal_contact_views_api() {
  35. return array(
  36. 'api' => 2.0,
  37. );
  38. }
  39. /**
  40. *
  41. * @ingroup tripal_contact
  42. */
  43. function tripal_contact_init() {
  44. drupal_add_js(drupal_get_path('module', 'tripal_contact') . '/theme/js/tripal_contact.js');
  45. drupal_add_css(drupal_get_path('module', 'tripal_contact') . '/theme/css/tripal_contact.css');
  46. }
  47. /**
  48. * Implementation of hook_tripal_contact_node_info().
  49. *
  50. * This node_info, is a simple node that describes the functionallity of the module.
  51. *
  52. */
  53. function tripal_contact_node_info() {
  54. return array(
  55. 'chado_contact' => array(
  56. 'name' => t('Contact'),
  57. 'base' => 'chado_contact',
  58. 'description' => t('A contact from the Chado database'),
  59. 'title_label' => t('Article Title'),
  60. 'body_label' => t('Abstract'),
  61. 'has_title' => TRUE,
  62. 'has_body' => FALSE,
  63. ),
  64. );
  65. }
  66. /**
  67. * Tripal-contact-Menu
  68. *
  69. * Implemets hook_menu(): Adds menu items for the tripal_contact module menu. This section
  70. * gives the outline for the main menu of the Tripal-contact module
  71. *
  72. * @return
  73. * An array of menu items that is visible within the Drupal Menu, returned as soon
  74. * as the program is ran
  75. */
  76. function tripal_contact_menu() {
  77. $items = array();
  78. $items[ 'admin/tripal/tripal_contact' ]= array(
  79. 'title' => 'Contacts',
  80. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of contacts'),
  81. 'page callback' => 'theme',
  82. 'page arguments' => array('tripal_contact_admin'),
  83. 'access arguments' => array('administer tripal contacts'),
  84. 'type' => MENU_NORMAL_ITEM
  85. );
  86. $items['admin/tripal/tripal_contact/configuration'] = array(
  87. 'title' => 'Configuration',
  88. 'description' => 'Integration of Chado contacts.',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('tripal_contact_admin'),
  91. 'access arguments' => array('administer tripal contact'),
  92. 'type' => MENU_NORMAL_ITEM,
  93. );
  94. $items['admin/tripal/tripal_contact/sync'] = array(
  95. 'title' => ' Sync Contacts',
  96. 'description' => 'Sync contacts in Chado with Drupal',
  97. 'page callback' => 'drupal_get_form',
  98. 'page arguments' => array('tripal_contact_sync_form'),
  99. 'access arguments' => array('administer tripal contacts'),
  100. 'type' => MENU_NORMAL_ITEM,
  101. );
  102. // AJAX calls for adding/removing properties to a contact
  103. $items['tripal_contact/properties/add'] = array(
  104. 'page callback' => 'tripal_contact_property_add',
  105. 'access arguments' => array('edit chado_contact content'),
  106. 'type ' => MENU_CALLBACK,
  107. );
  108. $items['tripal_contact/properties/description'] = array(
  109. 'page callback' => 'tripal_contact_property_get_description',
  110. 'access arguments' => array('edit chado_contact content'),
  111. 'type ' => MENU_CALLBACK,
  112. );
  113. $items['tripal_contact/properties/minus/%/%'] = array(
  114. 'page callback' => 'tripal_contact_property_delete',
  115. 'page arguments' => array(3, 4),
  116. 'access arguments' => array('edit chado_contact content'),
  117. 'type ' => MENU_CALLBACK,
  118. );
  119. return $items;
  120. }
  121. /**
  122. * Implements hook_theme(): Register themeing functions for this module
  123. *
  124. *
  125. * @return
  126. * An array of themeing functions to register
  127. *
  128. */
  129. function tripal_contact_theme() {
  130. return array(
  131. 'tripal_contact_base' => array(
  132. 'arguments' => array('node' => NULL),
  133. 'template' => 'tripal_contact_base',
  134. ),
  135. 'tripal_contact_properties' => array(
  136. 'arguments' => array('node' => NULL),
  137. 'template' => 'tripal_contact_properties',
  138. ),
  139. 'tripal_contact_relationships' => array(
  140. 'arguments' => array('node' => NULL),
  141. 'template' => 'tripal_contact_relationships',
  142. ),
  143. 'tripal_contact_publications' => array(
  144. 'arguments' => array('node' => NULL),
  145. 'template' => 'tripal_contact_publications',
  146. ),
  147. 'tripal_contact_admin' => array(
  148. 'template' => 'tripal_contact_admin',
  149. 'arguments' => array(NULL),
  150. 'path' => drupal_get_path('module', 'tripal_contact') . '/theme'
  151. ),
  152. // Themed Forms
  153. 'chado_contact_node_form' => array(
  154. 'arguments' => array('form'),
  155. ),
  156. );
  157. }
  158. /**
  159. * @ingroup tripal_library
  160. */
  161. function tripal_contact_block_info() {
  162. $blocks['contbase']['info'] = t('Tripal Contact Details');
  163. $blocks['contbase']['cache'] = BLOCK_NO_CACHE;
  164. $blocks['contprops']['info'] = t('Tripal Contact Properties');
  165. $blocks['contprops']['cache'] = BLOCK_NO_CACHE;
  166. $blocks['contrels']['info'] = t('Tripal Contact Relationships');
  167. $blocks['contrels']['cache'] = BLOCK_NO_CACHE;
  168. $blocks['contpubs']['info'] = t('Tripal Cotact Publications');
  169. $blocks['contpubs']['cache'] = BLOCK_NO_CACHE;
  170. return $blocks;
  171. }
  172. /**
  173. * @ingroup tripal_library
  174. */
  175. function tripal_contact_block_view($delta = '') {
  176. if (user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
  177. $nid = arg(1);
  178. $node = node_load($nid);
  179. $block = array();
  180. switch ($delta) {
  181. case 'contbase':
  182. $block['subject'] = t('Details');
  183. $block['content'] = theme('tripal_contact_base', $node);
  184. break;
  185. case 'contprops':
  186. $block['subject'] = t('Properties');
  187. $block['content'] = theme('tripal_contact_properties', $node);
  188. break;
  189. case 'contrels':
  190. $block['subject'] = t('Relationships');
  191. $block['content'] = theme('tripal_contact_relationships', $node);
  192. break;
  193. case 'contpubs':
  194. $block['subject'] = t('Publications');
  195. $block['content'] = theme('tripal_contact_publications', $node);
  196. break;
  197. default :
  198. }
  199. return $block;
  200. }
  201. }
  202. /**
  203. * Implement hook_perm().
  204. */
  205. function tripal_contact_permissions() {
  206. return array(
  207. 'access chado_contact content' => array(
  208. 'title' => t('View Contacts'),
  209. 'description' => t('Allow users to view contact pages.'),
  210. ),
  211. 'create chado_contact content' => array(
  212. 'title' => t('Create Contacts'),
  213. 'description' => t('Allow users to create new contact pages.'),
  214. ),
  215. 'delete chado_contact content' => array(
  216. 'title' => t('Delete Contacts'),
  217. 'description' => t('Allow users to delete contact pages.'),
  218. ),
  219. 'edit chado_contact content' => array(
  220. 'title' => t('Edit Contacts'),
  221. 'description' => t('Allow users to edit contact pages.'),
  222. ),
  223. 'adminster tripal contact' => array(
  224. 'title' => t('Administer Contacts'),
  225. 'description' => t('Allow users to administer all contacts.'),
  226. ),
  227. );
  228. }
  229. /**
  230. * Implement hook_access().
  231. *
  232. * This hook allows node modules to limit access to the node types they define.
  233. *
  234. * @param $node
  235. * The node on which the operation is to be performed, or, if it does not yet exist, the
  236. * type of node to be created
  237. *
  238. * @param $op
  239. * The operation to be performed
  240. *
  241. * @param $account
  242. * A user object representing the user for whom the operation is to be performed
  243. *
  244. * @return
  245. * If the permission for the specified operation is not set then return FALSE. If the
  246. * permission is set then return NULL as this allows other modules to disable
  247. * access. The only exception is when the $op == 'create'. We will always
  248. * return TRUE if the permission is set.
  249. *
  250. */
  251. function chado_contact_node_access($node, $op, $account ) {
  252. if ($op == 'create') {
  253. if (!user_access('create chado_contact content', $account)) {
  254. return FALSE;
  255. }
  256. return TRUE;
  257. }
  258. if ($op == 'update') {
  259. if (!user_access('edit chado_contact content', $account)) {
  260. return FALSE;
  261. }
  262. }
  263. if ($op == 'delete') {
  264. if (!user_access('delete chado_contact content', $account)) {
  265. return FALSE;
  266. }
  267. }
  268. if ($op == 'view') {
  269. if (!user_access('access chado_contact content', $account)) {
  270. return FALSE;
  271. }
  272. }
  273. return NULL;
  274. }
  275. /**
  276. * Implementation of tripal_contact_insert().
  277. *
  278. * This function inserts user entered information pertaining to the contact instance into the
  279. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.
  280. *
  281. * @parm $node
  282. * Then node which contains the information stored within the node-ID
  283. *
  284. *
  285. */
  286. function chado_contact_insert($node) {
  287. // if a contact_id already exists for this node then it already exists in Chado and
  288. // we get here because we are syncing the node. Therefore, we can skip the insert
  289. if ($node->contact_id) {
  290. $contact['contact_id'] = $node->contact_id;
  291. }
  292. else {
  293. // we don't want to store the description in the description field as it may be longer than
  294. // 255 characters, so we'll use a property to store this value.
  295. $values = array(
  296. 'name' => $node->title,
  297. 'description' => '',
  298. 'type_id' => $node->type_id
  299. );
  300. $options = array('statement_name' => 'ins_contact_nadety');
  301. $contact = tripal_core_chado_insert('contact', $values, $options);
  302. if (!$contact) {
  303. drupal_set_message(t('Could not add the contact'), 'error');
  304. watchdog('tripal_contact','Could not add the contact', array(), WATCHDOG_ERROR);
  305. return FALSE;
  306. }
  307. // now add the properties
  308. $properties = array(); // stores all of the properties we need to add
  309. // get the list of properties for easy lookup (without doing lots of database queries
  310. $properties_list = array();
  311. $sql = "
  312. SELECT CVTS.cvterm_id, CVTS.name
  313. FROM {cvtermpath} CVTP
  314. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  315. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  316. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  317. WHERE
  318. CV.name = 'tripal_contact' AND
  319. NOT CVTO.name = 'Contact Type'
  320. ORDER BY CVTS.name ASC
  321. ";
  322. $prop_types = chado_query($sql);
  323. while ($prop = $prop_types->fetchObject()) {
  324. $properties_list[$prop->cvterm_id] = $prop->name;
  325. }
  326. // get the properties that should be added. Properties are in one of two forms:
  327. // 1) prop_value-[type id]-[index]
  328. // 2) new_value-[type id]-[index]
  329. // 3) new_id, new_value
  330. foreach ($node as $name => $value) {
  331. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  332. $type_id = $matches[1];
  333. $index = $matches[2];
  334. $name = $properties_list[$type_id];
  335. $properties[$name][$index] = trim($value);
  336. }
  337. }
  338. if ($node->new_id and $node->new_value) {
  339. $type_id = $node->new_id;
  340. $index = count($properties[$name]);
  341. $name = $properties_list[$type_id];
  342. $properties[$name][$index] = trim($node->new_value);
  343. }
  344. // now add in the properties
  345. foreach ($properties as $property => $elements) {
  346. foreach ($elements as $rank => $value) {
  347. $status = tripal_contact_insert_property($contact['contact_id'], $property, $value, FALSE);
  348. if (!$status) {
  349. drupal_set_message("Error cannot add property: $property", "error");
  350. watchdog('t_contact', "Error cannot add property: %prop",
  351. array('%property' => $property), WATCHDOG_ERROR);
  352. }
  353. }
  354. }
  355. }
  356. // add the record to the chado_contact table in Drupal
  357. if ($contact) {
  358. // add the description property
  359. tripal_contact_insert_property($contact['contact_id'], 'contact_description',
  360. $node->description, TRUE);
  361. // make sure the entry for this contact doesn't already exist in the chado_contact table
  362. // if it doesn't exist then we want to add it.
  363. $contact_id = chado_get_id_for_node('contact', $node->nid) ;
  364. if (!$contact_id) {
  365. // next add the item to the drupal table
  366. $sql = "INSERT INTO {chado_contact} (nid, vid, contact_id) ".
  367. "VALUES (:nid, :vid, :contact_id)";
  368. db_query($sql, array(':nid'=> $node->nid, ':vid' => $node->vid, ':contact_id' => $contact['contact_id']));
  369. }
  370. }
  371. else {
  372. drupal_set_message(t('Unable to add contact.', 'warning'));
  373. watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values',
  374. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  375. }
  376. return TRUE;
  377. }
  378. /*
  379. *
  380. * Implements hook_update
  381. *
  382. * The purpose of the function is to allow the module to take action when an edited node is being
  383. * updated. It updates any name changes to the database tables that werec reated upon registering a contact.
  384. * As well, the database will be changed, so the user changed information will be saved to the database.
  385. *
  386. * @param $node
  387. * The node being updated
  388. *
  389. * @ingroup tripal_contact
  390. */
  391. function chado_contact_update($node) {
  392. if ($node->revision) {
  393. // there is no way to handle revisions in Chado but leave
  394. // this here just to make not we've addressed it.
  395. }
  396. $contact_id = chado_get_id_for_node('contact', $node->nid) ;
  397. // check to see if this contact name doens't already exists.
  398. $sql = "SELECT contact_id FROM {contact} WHERE NOT contact_id = :contact_id AND name = :name";
  399. $contact = chado_query($sql, array(':contact_id' => $contact_id, ':name' => $node->contact_name))->fetchObject();
  400. if ($contact) {
  401. drupal_set_message(t('A contact with this name already exists. Cannot perform update.'), 'warning');
  402. return;
  403. }
  404. // update the contact record
  405. $match = array(
  406. 'contact_id' => $contact_id,
  407. );
  408. $values = array(
  409. 'name' => $node->title,
  410. 'description' => '',
  411. 'type_id' => $node->type_id
  412. );
  413. $status = tripal_core_chado_update('contact', $match, $values);
  414. if (!$status) {
  415. drupal_set_message("Error updating contact", "error");
  416. watchdog('t_contact', "Error updating contact", array(), WATCHDOG_ERROR);
  417. return;
  418. }
  419. // now update the properties
  420. $properties = array(); // stores all of the properties we need to add
  421. // get the list of properties for easy lookup (without doing lots of database queries
  422. $properties_list = array();
  423. $sql = "
  424. SELECT CVTS.cvterm_id, CVTS.name
  425. FROM {cvtermpath} CVTP
  426. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  427. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  428. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  429. WHERE
  430. CV.name = 'tripal_contact' AND
  431. NOT CVTO.name = 'Contact Type'
  432. ORDER BY CVTS.name ASC
  433. ";
  434. $prop_types = chado_query($sql);
  435. while ($prop = $prop_types->fetchObject()) {
  436. $properties_list[$prop->cvterm_id] = $prop->name;
  437. }
  438. // get the properties that should be added. Properties are in one of three forms:
  439. // 1) prop_value-[type id]-[index]
  440. // 2) new_value-[type id]-[index]
  441. // 3) new_id, new_value
  442. // dpm($node);
  443. foreach ($node as $key => $value) {
  444. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  445. $type_id = $matches[1];
  446. $index = $matches[2];
  447. $name = $properties_list[$type_id];
  448. $properties[$name][$index] = trim($value);
  449. }
  450. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  451. $type_id = $matches[1];
  452. $index = $matches[2];
  453. $name = $properties_list[$type_id];
  454. $properties[$name][$index] = trim($value);
  455. }
  456. }
  457. if ($node->new_id and $node->new_value) {
  458. $type_id = $node->new_id;
  459. $name = $properties_list[$type_id];
  460. $index = count($properties[$name]);
  461. $properties[$name][$index] = trim($node->new_value);
  462. }
  463. // now add in the properties by first removing any the contact
  464. // already has and adding the ones we have
  465. tripal_core_chado_delete('contactprop', array('contact_id' => $contact_id));
  466. foreach ($properties as $property => $elements) {
  467. foreach ($elements as $rank => $value) {
  468. $status = tripal_contact_insert_property($contact_id, $property, $value, FALSE);
  469. if (!$status) {
  470. drupal_set_message("Error cannot add property: '$property'", "error");
  471. watchdog('t_contact', "Error cannot add property: '%prop'",
  472. array('%prop' => $property), WATCHDOG_ERROR);
  473. }
  474. }
  475. }
  476. tripal_contact_update_property($contact_id, 'contact_description', $node->description, 1);
  477. }
  478. /**
  479. * Implementation of tripal_contact_load().
  480. *
  481. *
  482. * @param $node
  483. * The node that is to be accessed from the database
  484. *
  485. * @return $node
  486. * The node with the information to be loaded into the database
  487. *
  488. */
  489. function chado_contact_load($node) {
  490. // get the feature details from chado
  491. $contact_id = chado_get_id_for_node('contact', $node->nid);
  492. $values = array('contact_id' => $contact_id);
  493. $contact = tripal_core_generate_chado_var('contact', $values);
  494. // get the contact description and replace the contact.description field with this one
  495. $values = array(
  496. 'contact_id' => $contact->contact_id,
  497. 'type_id' => array(
  498. 'name' => 'contact_description',
  499. ),
  500. );
  501. $options = array(
  502. 'return_array' => 1,
  503. 'include_fk' => array('type_id' => 1),
  504. );
  505. $description = tripal_core_generate_chado_var('contactprop', $values, $options);
  506. if (count($description) == 1) {
  507. $description = tripal_core_expand_chado_vars($description, 'field', 'contactprop.value');
  508. $contact->description = $description[0]->value;
  509. }
  510. $additions = new stdClass();
  511. $additions->contact = $contact;
  512. return $additions;
  513. }
  514. /**
  515. * Implementation of tripal_contact_delete().
  516. *
  517. * This function takes a node and if the delete button has been chosen by the user, the contact
  518. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  519. * the 'chado_contact' table.
  520. *
  521. * @parm $node
  522. * Then node which contains the information stored within the node-ID
  523. *
  524. */
  525. function chado_contact_delete(&$node) {
  526. $contact_id = chado_get_id_for_node('contact', $node->nid);
  527. // if we don't have a contact id for this node then this isn't a node of
  528. // type chado_contact or the entry in the chado_contact table was lost.
  529. if (!$contact_id) {
  530. return;
  531. }
  532. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  533. // drupal database
  534. $sql_del = "DELETE FROM {chado_contact} WHERE nid = :nid AND vid = :vid";
  535. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  536. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  537. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  538. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  539. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  540. // Remove data from contact and contactprop tables of chado database as well
  541. chado_query("DELETE FROM {contactprop} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  542. chado_query("DELETE FROM {contact} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  543. }
  544. /**
  545. *
  546. *
  547. * @ingroup tripal_contact
  548. */
  549. function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
  550. // we want to provide a new variable that contains the matched contacts.
  551. $contact = $variables['node']->contact;
  552. // normally we would use tripal_core_expand_chado_vars to expand our
  553. // organism object and add in the relationships, however whan a large
  554. // number of relationships are present this significantly slows the
  555. // query, therefore we will manually perform the query
  556. $sql = "
  557. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  558. FROM contact_relationship PR
  559. INNER JOIN {contact} C ON PR.object_id = C.contact_id
  560. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  561. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  562. WHERE PR.subject_id = :contact_id
  563. ";
  564. $as_subject = chado_query($sql, array(':contact_id' => $contact->contact_id));
  565. $sql = "
  566. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  567. FROM contact_relationship PR
  568. INNER JOIN {contact} C ON PR.subject_id = C.contact_id
  569. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  570. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  571. WHERE PR.object_id = :contact_id
  572. ";
  573. $as_object = chado_query($sql, array(':contact_id' => $contact->contact_id));
  574. // combine both object and subject relationshisp into a single array
  575. $relationships = array();
  576. $relationships['object'] = array();
  577. $relationships['subject'] = array();
  578. // iterate through the object relationships
  579. while ($relationship = $as_object->fetchObject()) {
  580. // get the relationship and child types
  581. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  582. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  583. if (!array_key_exists($rel_type, $relationships['object'])) {
  584. $relationships['object'][$rel_type] = array();
  585. }
  586. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  587. $relationships['object'][$rel_type][$sub_type] = array();
  588. }
  589. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  590. }
  591. // now add in the subject relationships
  592. while ($relationship = $as_subject->fetchObject()) {
  593. // get the relationship and child types
  594. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  595. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  596. if (!array_key_exists($rel_type, $relationships['subject'])) {
  597. $relationships['subject'][$rel_type] = array();
  598. }
  599. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  600. $relationships['subject'][$rel_type][$obj_type] = array();
  601. }
  602. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  603. }
  604. $contact->all_relationships = $relationships;
  605. }
  606. /*
  607. *
  608. */
  609. function tripal_contact_form_alter(&$form, &$form_state, $form_id) {
  610. if ($form_id == "chado_contact_node_form") {
  611. }
  612. }