tripal_contact.module 23 KB

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