tripal_contact.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.chado_node.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. 'tripal_contact_teaser' => array(
  174. 'variables' => array('node' => NULL),
  175. 'template' => 'tripal_contact_teaser',
  176. 'path' => "$path/theme/tripal_contact",
  177. ),
  178. );
  179. return $items;
  180. }
  181. /**
  182. * @ingroup tripal_contact
  183. */
  184. function tripal_contact_block_info() {
  185. $blocks['contbase']['info'] = t('Tripal Contact Details');
  186. $blocks['contbase']['cache'] = 'DRUPAL_NO_CACHE';
  187. $blocks['contprops']['info'] = t('Tripal Contact Properties');
  188. $blocks['contprops']['cache'] = 'DRUPAL_NO_CACHE';
  189. $blocks['contrels']['info'] = t('Tripal Contact Relationships');
  190. $blocks['contrels']['cache'] = 'DRUPAL_NO_CACHE';
  191. $blocks['contpubs']['info'] = t('Tripal Contact Publications');
  192. $blocks['contpubs']['cache'] = 'DRUPAL_NO_CACHE';
  193. return $blocks;
  194. }
  195. /**
  196. * @ingroup tripal_contact
  197. */
  198. function tripal_contact_block_view($delta = '') {
  199. if (user_access('access chado_contact content') and arg(0) == 'node' and is_numeric(arg(1))) {
  200. $nid = arg(1);
  201. $node = node_load($nid);
  202. $block = array();
  203. switch ($delta) {
  204. case 'contbase':
  205. $block['subject'] = t('Details');
  206. $block['content'] = array(
  207. '#theme' => 'tripal_contact_base',
  208. '#nodes' => $node,
  209. '#title' => '',
  210. );
  211. break;
  212. case 'contprops':
  213. $block['subject'] = t('Properties');
  214. $block['content'] = array(
  215. '#theme' => 'tripal_contact_properties',
  216. '#nodes' => $node,
  217. '#title' => '',
  218. );
  219. break;
  220. case 'contrels':
  221. $block['subject'] = t('Relationships');
  222. $block['content'] = array(
  223. '#theme' => 'tripal_contact_relationships',
  224. '#nodes' => $node,
  225. '#title' => '',
  226. );
  227. break;
  228. case 'contpubs':
  229. $block['subject'] = t('Publications');
  230. $block['content'] = array(
  231. '#theme' => 'tripal_contact_publications',
  232. '#nodes' => $node,
  233. '#title' => '',
  234. );
  235. break;
  236. default :
  237. }
  238. return $block;
  239. }
  240. }
  241. /**
  242. * Implement hook_perm().
  243. */
  244. function tripal_contact_permissions() {
  245. return array(
  246. 'access chado_contact content' => array(
  247. 'title' => t('View Contacts'),
  248. 'description' => t('Allow users to view contact pages.'),
  249. ),
  250. 'create chado_contact content' => array(
  251. 'title' => t('Create Contacts'),
  252. 'description' => t('Allow users to create new contact pages.'),
  253. ),
  254. 'delete chado_contact content' => array(
  255. 'title' => t('Delete Contacts'),
  256. 'description' => t('Allow users to delete contact pages.'),
  257. ),
  258. 'edit chado_contact content' => array(
  259. 'title' => t('Edit Contacts'),
  260. 'description' => t('Allow users to edit contact pages.'),
  261. ),
  262. 'adminster tripal contact' => array(
  263. 'title' => t('Administer Contacts'),
  264. 'description' => t('Allow users to administer all contacts.'),
  265. ),
  266. );
  267. }
  268. /**
  269. *
  270. *
  271. * @ingroup tripal_contact
  272. */
  273. function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
  274. // we want to provide a new variable that contains the matched contacts.
  275. $contact = $variables['node']->contact;
  276. // normally we would use tripal_core_expand_chado_vars to expand our
  277. // organism object and add in the relationships, however whan a large
  278. // number of relationships are present this significantly slows the
  279. // query, therefore we will manually perform the query
  280. $sql = "
  281. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  282. FROM {contact_relationship} PR
  283. INNER JOIN {contact} C ON PR.object_id = C.contact_id
  284. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  285. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  286. WHERE PR.subject_id = :contact_id
  287. ";
  288. $as_subject = chado_query($sql, array(':contact_id' => $contact->contact_id));
  289. $sql = "
  290. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  291. FROM {contact_relationship} PR
  292. INNER JOIN {contact} C ON PR.subject_id = C.contact_id
  293. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  294. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  295. WHERE PR.object_id = :contact_id
  296. ";
  297. $as_object = chado_query($sql, array(':contact_id' => $contact->contact_id));
  298. // combine both object and subject relationshisp into a single array
  299. $relationships = array();
  300. $relationships['object'] = array();
  301. $relationships['subject'] = array();
  302. // iterate through the object relationships
  303. while ($relationship = $as_object->fetchObject()) {
  304. // get the relationship and child types
  305. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  306. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  307. if (!array_key_exists($rel_type, $relationships['object'])) {
  308. $relationships['object'][$rel_type] = array();
  309. }
  310. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  311. $relationships['object'][$rel_type][$sub_type] = array();
  312. }
  313. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  314. }
  315. // now add in the subject relationships
  316. while ($relationship = $as_subject->fetchObject()) {
  317. // get the relationship and child types
  318. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  319. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  320. if (!array_key_exists($rel_type, $relationships['subject'])) {
  321. $relationships['subject'][$rel_type] = array();
  322. }
  323. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  324. $relationships['subject'][$rel_type][$obj_type] = array();
  325. }
  326. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  327. }
  328. $contact->all_relationships = $relationships;
  329. }
  330. /**
  331. * Implementation of hook_form_alter()
  332. *
  333. * @param $form
  334. * @param $form_state
  335. * @param $form_id
  336. */
  337. function tripal_contact_form_alter(&$form, &$form_state, $form_id) {
  338. // turn of preview button for insert/updates
  339. if ($form_id == "chado_contact_node_form") {
  340. $form['actions']['preview']['#access'] = FALSE;
  341. }
  342. }
  343. /**
  344. *
  345. * @ingroup tripal_feature
  346. */
  347. function tripal_contact_node_view($node, $view_mode, $langcode) {
  348. switch ($node->type) {
  349. case 'chado_contact':
  350. // Show feature browser and counts
  351. if ($view_mode == 'full') {
  352. $node->content['tripal_contact_base'] = array(
  353. '#value' => theme('tripal_contact_base', array('node' => $node)),
  354. );
  355. $node->content['tripal_contact_properties'] = array(
  356. '#value' => theme('tripal_contact_properties', array('node' => $node)),
  357. );
  358. $node->content['tripal_contact_publications'] = array(
  359. '#value' => theme('tripal_contact_publications', array('node' => $node)),
  360. );
  361. $node->content['tripal_contact_relationships'] = array(
  362. '#value' => theme('tripal_contact_relationships', array('node' => $node)),
  363. );
  364. }
  365. if ($view_mode == 'teaser') {
  366. $node->content['tripal_contact_teaser'] = array(
  367. '#value' => theme('tripal_contact_teaser', array('node' => $node)),
  368. );
  369. }
  370. break;
  371. }
  372. }