tripal_contact.module 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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($existing, $type, $theme, $path) {
  140. $core_path = drupal_get_path('module', 'tripal_core');
  141. $items = array(
  142. 'node__chado_contact' => array(
  143. 'template' => 'node--chado-generic',
  144. 'render element' => 'node',
  145. 'base hook' => 'node',
  146. 'path' => "$core_path/theme",
  147. ),
  148. 'tripal_contact_base' => array(
  149. 'variables' => array('node' => NULL),
  150. 'template' => 'tripal_contact_base',
  151. 'path' => "$path/theme/tripal_contact",
  152. ),
  153. 'tripal_contact_properties' => array(
  154. 'variables' => array('node' => NULL),
  155. 'template' => 'tripal_contact_properties',
  156. 'path' => "$path/theme/tripal_contact",
  157. ),
  158. 'tripal_contact_relationships' => array(
  159. 'variables' => array('node' => NULL),
  160. 'template' => 'tripal_contact_relationships',
  161. 'path' => "$path/theme/tripal_contact",
  162. ),
  163. 'tripal_contact_publications' => array(
  164. 'variables' => array('node' => NULL),
  165. 'template' => 'tripal_contact_publications',
  166. 'path' => "$path/theme/tripal_contact",
  167. ),
  168. 'tripal_contact_help' => array(
  169. 'template' => 'tripal_contact_help',
  170. 'variables' => array(NULL),
  171. 'path' => "$path/theme",
  172. ),
  173. );
  174. return $items;
  175. }
  176. /**
  177. * @ingroup tripal_contact
  178. */
  179. function tripal_contact_block_info() {
  180. $blocks['contbase']['info'] = t('Tripal Contact Details');
  181. $blocks['contbase']['cache'] = 'BLOCK_NO_CACHE';
  182. $blocks['contprops']['info'] = t('Tripal Contact Properties');
  183. $blocks['contprops']['cache'] = 'BLOCK_NO_CACHE';
  184. $blocks['contrels']['info'] = t('Tripal Contact Relationships');
  185. $blocks['contrels']['cache'] = 'BLOCK_NO_CACHE';
  186. $blocks['contpubs']['info'] = t('Tripal Cotact Publications');
  187. $blocks['contpubs']['cache'] = 'BLOCK_NO_CACHE';
  188. return $blocks;
  189. }
  190. /**
  191. * @ingroup tripal_contact
  192. */
  193. function tripal_contact_block_view($delta = '') {
  194. if (user_access('access chado_contact content') and arg(0) == 'node' and is_numeric(arg(1))) {
  195. $nid = arg(1);
  196. $node = node_load($nid);
  197. $block = array();
  198. switch ($delta) {
  199. case 'contbase':
  200. $block['subject'] = t('Details');
  201. $block['content'] = theme('tripal_contact_base', $node);
  202. break;
  203. case 'contprops':
  204. $block['subject'] = t('Properties');
  205. $block['content'] = theme('tripal_contact_properties', $node);
  206. break;
  207. case 'contrels':
  208. $block['subject'] = t('Relationships');
  209. $block['content'] = theme('tripal_contact_relationships', $node);
  210. break;
  211. case 'contpubs':
  212. $block['subject'] = t('Publications');
  213. $block['content'] = theme('tripal_contact_publications', $node);
  214. break;
  215. default :
  216. }
  217. return $block;
  218. }
  219. }
  220. /**
  221. * Implement hook_perm().
  222. */
  223. function tripal_contact_permissions() {
  224. return array(
  225. 'access chado_contact content' => array(
  226. 'title' => t('View Contacts'),
  227. 'description' => t('Allow users to view contact pages.'),
  228. ),
  229. 'create chado_contact content' => array(
  230. 'title' => t('Create Contacts'),
  231. 'description' => t('Allow users to create new contact pages.'),
  232. ),
  233. 'delete chado_contact content' => array(
  234. 'title' => t('Delete Contacts'),
  235. 'description' => t('Allow users to delete contact pages.'),
  236. ),
  237. 'edit chado_contact content' => array(
  238. 'title' => t('Edit Contacts'),
  239. 'description' => t('Allow users to edit contact pages.'),
  240. ),
  241. 'adminster tripal contact' => array(
  242. 'title' => t('Administer Contacts'),
  243. 'description' => t('Allow users to administer all contacts.'),
  244. ),
  245. );
  246. }
  247. /**
  248. * Implement hook_access().
  249. *
  250. * This hook allows node modules to limit access to the node types they define.
  251. *
  252. * @param $node
  253. * The node on which the operation is to be performed, or, if it does not yet exist, the
  254. * type of node to be created
  255. *
  256. * @param $op
  257. * The operation to be performed
  258. *
  259. * @param $account
  260. * A user object representing the user for whom the operation is to be performed
  261. *
  262. * @return
  263. * If the permission for the specified operation is not set then return FALSE. If the
  264. * permission is set then return NULL as this allows other modules to disable
  265. * access. The only exception is when the $op == 'create'. We will always
  266. * return TRUE if the permission is set.
  267. *
  268. */
  269. function chado_contact_node_access($node, $op, $account ) {
  270. if ($op == 'create') {
  271. if (!user_access('create chado_contact content', $account)) {
  272. return FALSE;
  273. }
  274. return TRUE;
  275. }
  276. if ($op == 'update') {
  277. if (!user_access('edit chado_contact content', $account)) {
  278. return FALSE;
  279. }
  280. }
  281. if ($op == 'delete') {
  282. if (!user_access('delete chado_contact content', $account)) {
  283. return FALSE;
  284. }
  285. }
  286. if ($op == 'view') {
  287. if (!user_access('access chado_contact content', $account)) {
  288. return FALSE;
  289. }
  290. }
  291. return NULL;
  292. }
  293. /**
  294. * Implementation of tripal_contact_insert().
  295. *
  296. * This function inserts user entered information pertaining to the contact instance into the
  297. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.
  298. *
  299. * @parm $node
  300. * Then node which contains the information stored within the node-ID
  301. *
  302. *
  303. */
  304. function chado_contact_insert($node) {
  305. // remove surrounding white-space on submitted values
  306. $node->title = trim($node->title);
  307. $node->description = trim($node->description);
  308. // if there is a contact_id in the $node object then this must be a sync so
  309. // we can skip adding the contact as it is already there, although
  310. // we do need to proceed with the rest of the insert
  311. if (!property_exists($node, 'contact_id')) {
  312. // insert and then get the newly inserted contact record
  313. $values = array(
  314. 'name' => $node->title,
  315. 'description' => '',
  316. 'type_id' => $node->type_id,
  317. );
  318. $contact = tripal_core_chado_insert('contact', $values);
  319. if (!$contact) {
  320. drupal_set_message(t('Unable to add contact.', 'warning'));
  321. watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values',
  322. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  323. return;
  324. }
  325. $contact_id = $contact['contact_id'];
  326. // now add in the properties
  327. $properties = tripal_core_properties_form_retreive($node, 'tripal_contact');
  328. foreach ($properties as $property => $elements) {
  329. foreach ($elements as $rank => $value) {
  330. $status = tripal_contact_insert_property($contact_id, $property, $value, FALSE);
  331. if (!$status) {
  332. drupal_set_message("Error cannot add property: $property", "error");
  333. watchdog('t_contact', "Error cannot add property: %prop",
  334. array('%property' => $property), WATCHDOG_ERROR);
  335. }
  336. }
  337. }
  338. // add in the description as a separate property
  339. tripal_contact_insert_property($contact_id, 'contact_description', $node->description, FALSE);
  340. }
  341. else {
  342. $contact_id = $node->contact_id;
  343. }
  344. // Make sure the entry for this contact doesn't already exist in the
  345. // chado_contact table if it doesn't exist then we want to add it.
  346. $check_org_id = chado_get_id_for_node('contact', $node->nid);
  347. if (!$check_org_id) {
  348. $record = new stdClass();
  349. $record->nid = $node->nid;
  350. $record->vid = $node->vid;
  351. $record->contact_id = $contact_id;
  352. drupal_write_record('chado_contact', $record);
  353. }
  354. return TRUE;
  355. }
  356. /*
  357. *
  358. * Implements hook_update
  359. *
  360. * The purpose of the function is to allow the module to take action when an edited node is being
  361. * updated. It updates any name changes to the database tables that werec reated upon registering a contact.
  362. * As well, the database will be changed, so the user changed information will be saved to the database.
  363. *
  364. * @param $node
  365. * The node being updated
  366. *
  367. * @ingroup tripal_contact
  368. */
  369. function chado_contact_update($node) {
  370. // remove surrounding white-space on submitted values
  371. $node->title = trim($node->title);
  372. $node->description = trim($node->description);
  373. $contact_id = chado_get_id_for_node('contact', $node->nid) ;
  374. // update the contact record
  375. $match = array(
  376. 'contact_id' => $contact_id,
  377. );
  378. $values = array(
  379. 'name' => $node->title,
  380. 'description' => '',
  381. 'type_id' => $node->type_id
  382. );
  383. $status = tripal_core_chado_update('contact', $match, $values);
  384. if (!$status) {
  385. drupal_set_message("Error updating contact", "error");
  386. watchdog('t_contact', "Error updating contact", array(), WATCHDOG_ERROR);
  387. return;
  388. }
  389. // now add in the properties by first removing any the contact
  390. // already has and adding the ones we have
  391. tripal_core_chado_delete('contactprop', array('contact_id' => $contact_id));
  392. $properties = tripal_core_properties_form_retreive($node, 'tripal_contact');
  393. foreach ($properties as $property => $elements) {
  394. foreach ($elements as $rank => $value) {
  395. $status = tripal_contact_insert_property($contact_id, $property, $value, FALSE);
  396. if (!$status) {
  397. drupal_set_message("Error cannot add property: '$property'", "error");
  398. watchdog('t_contact', "Error cannot add property: '%prop'",
  399. array('%prop' => $property), WATCHDOG_ERROR);
  400. }
  401. }
  402. }
  403. // add in the description as a separate property
  404. tripal_contact_update_property($contact_id, 'contact_description', $node->description, 1);
  405. }
  406. /**
  407. * Implementation of tripal_contact_load().
  408. *
  409. *
  410. * @param $node
  411. * The node that is to be accessed from the database
  412. *
  413. * @return $node
  414. * The node with the information to be loaded into the database
  415. *
  416. */
  417. function chado_contact_load($nodes) {
  418. foreach ($nodes as $nid => $node) {
  419. // find the contact and add in the details
  420. $contact_id = chado_get_id_for_node('contact', $nid);
  421. // get the contact
  422. $values = array('contact_id' => $contact_id);
  423. $contact = tripal_core_generate_chado_var('contact', $values);
  424. // get the contact description from the contactprop table and replace
  425. // the contact.description field with this one (we don't use the contact.description
  426. // field because it is only 255 characters (too small)).
  427. $values = array(
  428. 'contact_id' => $contact->contact_id,
  429. 'type_id' => array(
  430. 'name' => 'contact_description',
  431. ),
  432. );
  433. $options = array(
  434. 'return_array' => 1,
  435. 'include_fk' => array('type_id' => 1),
  436. );
  437. $description = tripal_core_generate_chado_var('contactprop', $values, $options);
  438. if (count($description) == 1) {
  439. $description = tripal_core_expand_chado_vars($description, 'field', 'contactprop.value');
  440. $contact->description = $description[0]->value;
  441. }
  442. $nodes[$nid]->contact = $contact;
  443. }
  444. }
  445. /**
  446. * Implementation of tripal_contact_delete().
  447. *
  448. * This function takes a node and if the delete button has been chosen by the user, the contact
  449. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  450. * the 'chado_contact' table.
  451. *
  452. * @parm $node
  453. * Then node which contains the information stored within the node-ID
  454. *
  455. */
  456. function chado_contact_delete(&$node) {
  457. $contact_id = chado_get_id_for_node('contact', $node->nid);
  458. // if we don't have a contact id for this node then this isn't a node of
  459. // type chado_contact or the entry in the chado_contact table was lost.
  460. if (!$contact_id) {
  461. return;
  462. }
  463. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  464. // drupal database
  465. $sql_del = "DELETE FROM {chado_contact} WHERE nid = :nid AND vid = :vid";
  466. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  467. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  468. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  469. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  470. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  471. // Remove data from contact and contactprop tables of chado database as well
  472. chado_query("DELETE FROM {contactprop} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  473. chado_query("DELETE FROM {contact} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  474. }
  475. /**
  476. *
  477. *
  478. * @ingroup tripal_contact
  479. */
  480. function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
  481. // we want to provide a new variable that contains the matched contacts.
  482. $contact = $variables['node']->contact;
  483. // normally we would use tripal_core_expand_chado_vars to expand our
  484. // organism object and add in the relationships, however whan a large
  485. // number of relationships are present this significantly slows the
  486. // query, therefore we will manually perform the query
  487. $sql = "
  488. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  489. FROM {contact_relationship} PR
  490. INNER JOIN {contact} C ON PR.object_id = C.contact_id
  491. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  492. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  493. WHERE PR.subject_id = :contact_id
  494. ";
  495. $as_subject = chado_query($sql, array(':contact_id' => $contact->contact_id));
  496. $sql = "
  497. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  498. FROM {contact_relationship} PR
  499. INNER JOIN {contact} C ON PR.subject_id = C.contact_id
  500. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  501. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  502. WHERE PR.object_id = :contact_id
  503. ";
  504. $as_object = chado_query($sql, array(':contact_id' => $contact->contact_id));
  505. // combine both object and subject relationshisp into a single array
  506. $relationships = array();
  507. $relationships['object'] = array();
  508. $relationships['subject'] = array();
  509. // iterate through the object relationships
  510. while ($relationship = $as_object->fetchObject()) {
  511. // get the relationship and child types
  512. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  513. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  514. if (!array_key_exists($rel_type, $relationships['object'])) {
  515. $relationships['object'][$rel_type] = array();
  516. }
  517. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  518. $relationships['object'][$rel_type][$sub_type] = array();
  519. }
  520. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  521. }
  522. // now add in the subject relationships
  523. while ($relationship = $as_subject->fetchObject()) {
  524. // get the relationship and child types
  525. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  526. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  527. if (!array_key_exists($rel_type, $relationships['subject'])) {
  528. $relationships['subject'][$rel_type] = array();
  529. }
  530. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  531. $relationships['subject'][$rel_type][$obj_type] = array();
  532. }
  533. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  534. }
  535. $contact->all_relationships = $relationships;
  536. }
  537. /**
  538. * Implementation of hook_form_alter()
  539. *
  540. * @param $form
  541. * @param $form_state
  542. * @param $form_id
  543. */
  544. function tripal_contact_form_alter(&$form, &$form_state, $form_id) {
  545. // turn of preview button for insert/updates
  546. if ($form_id == "chado_contact_node_form") {
  547. $form['actions']['preview']['#access'] = FALSE;
  548. }
  549. }
  550. /**
  551. *
  552. * @ingroup tripal_feature
  553. */
  554. function tripal_contact_node_view($node, $view_mode, $langcode) {
  555. switch ($node->type) {
  556. case 'chado_contact':
  557. // Show feature browser and counts
  558. if ($view_mode == 'full') {
  559. $node->content['tripal_contact_base'] = array(
  560. '#value' => theme('tripal_contact_base', array('node' => $node)),
  561. );
  562. $node->content['tripal_contact_properties'] = array(
  563. '#value' => theme('tripal_contact_properties', array('node' => $node)),
  564. );
  565. $node->content['tripal_contact_publications'] = array(
  566. '#value' => theme('tripal_contact_publications', array('node' => $node)),
  567. );
  568. $node->content['tripal_contact_relationships'] = array(
  569. '#value' => theme('tripal_contact_relationships', array('node' => $node)),
  570. );
  571. }
  572. if ($view_mode == 'teaser') {
  573. $node->content['tripal_contact_teaser'] = array(
  574. '#value' => theme('tripal_contact_teaser', array('node' => $node)),
  575. );
  576. }
  577. break;
  578. }
  579. }