tripal_contact.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. /**
  14. * @defgroup tripal_contact Contact Module
  15. * @ingroup tripal_modules
  16. * @{
  17. * Currently this module only provides support for integration with Drupal
  18. * views and some support for querying using the Tripal Core API.
  19. *
  20. * This module needs further development to support full management of
  21. * contact information within Chado, and full definitions for foreign
  22. * key relationships in Chado.
  23. * @}
  24. */
  25. /*************************************************************************
  26. * Implements hook_views_api()
  27. * Purpose: Essentially this hook tells drupal that there is views support for
  28. * for this module which then includes tripal_contact.views.inc where all the
  29. * views integration code is
  30. *
  31. * @ingroup tripal_contact
  32. */
  33. function tripal_contact_views_api() {
  34. return array(
  35. 'api' => 2.0,
  36. );
  37. }
  38. /**
  39. *
  40. * @ingroup tripal_contact
  41. */
  42. function tripal_contact_init() {
  43. }
  44. /**
  45. * Implementation of hook_tripal_contact_node_info().
  46. *
  47. * This node_info, is a simple node that describes the functionallity of the module.
  48. *
  49. */
  50. function tripal_contact_node_info() {
  51. return array(
  52. 'chado_contact' => array(
  53. 'name' => t('Contact'),
  54. 'module' => 'chado_contact',
  55. 'description' => t('A contact from the Chado database'),
  56. 'title_label' => t('Article Title'),
  57. 'body_label' => t('Abstract'),
  58. 'has_title' => TRUE,
  59. 'has_body' => FALSE,
  60. ),
  61. );
  62. }
  63. /**
  64. * Tripal-contact-Menu
  65. *
  66. * Implemets hook_menu(): Adds menu items for the tripal_contact module menu. This section
  67. * gives the outline for the main menu of the Tripal-contact module
  68. *
  69. * @return
  70. * An array of menu items that is visible within the Drupal Menu, returned as soon
  71. * as the program is ran
  72. */
  73. function tripal_contact_menu() {
  74. $items = array();
  75. $items[ 'admin/tripal/tripal_contact' ]= array(
  76. 'title' => 'Contacts',
  77. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of contacts'),
  78. 'page callback' => 'theme',
  79. 'page arguments' => array('tripal_contact_admin'),
  80. 'access arguments' => array('administer tripal contacts'),
  81. 'type' => MENU_NORMAL_ITEM
  82. );
  83. $items['admin/tripal/tripal_contact/sync'] = array(
  84. 'title' => ' Sync Contacts',
  85. 'description' => 'Sync contacts in Chado with Drupal',
  86. 'page callback' => 'drupal_get_form',
  87. 'page arguments' => array('tripal_contact_sync_form'),
  88. 'access arguments' => array('administer tripal contacts'),
  89. 'type' => MENU_NORMAL_ITEM,
  90. );
  91. return $items;
  92. }
  93. /**
  94. * Implements hook_theme(): Register themeing functions for this module
  95. *
  96. *
  97. * @return
  98. * An array of themeing functions to register
  99. *
  100. */
  101. function tripal_contact_theme() {
  102. return array(
  103. 'tripal_contact_base' => array(
  104. 'arguments' => array('node' => NULL),
  105. ),
  106. 'tripal_contact_properties' => array(
  107. 'arguments' => array('node' => NULL)
  108. ),
  109. 'tripal_contact_relationships' => array(
  110. 'arguments' => array('node' => NULL)
  111. ),
  112. 'tripal_contact_admin' => array(
  113. 'template' => 'tripal_contact_admin',
  114. 'arguments' => array(NULL),
  115. 'path' => drupal_get_path('module', 'tripal_contact') . '/theme'
  116. ),
  117. );
  118. }
  119. /**
  120. * Implement hook_perm().
  121. */
  122. function tripal_contact_perm() {
  123. return array(
  124. 'access chado_contact content',
  125. 'create chado_contact content',
  126. 'delete chado_contact content',
  127. 'edit chado_contact content',
  128. 'administer tripal contacts',
  129. );
  130. }
  131. /**
  132. * Implement hook_access().
  133. *
  134. * This hook allows node modules to limit access to the node types they define.
  135. *
  136. * @param $op
  137. * The operation to be performed
  138. *
  139. * @param $node
  140. * The node on which the operation is to be performed, or, if it does not yet exist, the
  141. * type of node to be created
  142. *
  143. * @param $account
  144. * A user object representing the user for whom the operation is to be performed
  145. *
  146. * @return
  147. * TRUE
  148. *
  149. */
  150. function chado_contact_access($op, $node, $account ) {
  151. if ($op == 'create') {
  152. if (!user_access('create chado_contact content', $account)) {
  153. return FALSE;
  154. }
  155. }
  156. if ($op == 'update') {
  157. if (!user_access('edit chado_contact content', $account)) {
  158. return FALSE;
  159. }
  160. }
  161. if ($op == 'delete') {
  162. if (!user_access('delete chado_contact content', $account)) {
  163. return FALSE;
  164. }
  165. }
  166. if ($op == 'view') {
  167. if (!user_access('access chado_contact content', $account)) {
  168. return FALSE;
  169. }
  170. }
  171. return NULL;
  172. }
  173. /**
  174. * Implementation of tripal_contact_form().
  175. *
  176. *
  177. *
  178. * @parm &$node
  179. * The node that is created when the database is initialized
  180. *
  181. * @parm $form_state
  182. * The state of the form, that has the user entered information that is neccessary for, setting
  183. * up the database tables for the contact
  184. *
  185. * @return $form
  186. * The information that was enterd allong with
  187. *
  188. */
  189. function chado_contact_form(&$node, $form_state) {
  190. $form = array();
  191. $contact = $node->contact;
  192. // get the contact default values. When this module was first created
  193. // the contact description was incorrectly stored in the $node->body field.
  194. // It is better to store it in the Chado tables. However, the 'description'
  195. // field of the contact table is only 255 characters. So, we are going
  196. // to follow the same as the contact module and store the description in
  197. // the contactprop table and leave the contact.description field blank.
  198. // however, for backwards compatibitily, we check to see if the description
  199. // is in the $node->body field. If it is we'll use that. When the node is
  200. // edited the text will be moved out of the body and into the contactprop
  201. // table where it should belong.
  202. if ($node->body) {
  203. $contact_description = $node->body;
  204. }
  205. else {
  206. $contact_description = $node->contact_description;
  207. }
  208. if (!$contact_description) {
  209. $contactprop = tripal_contact_get_property($contact->contact_id, 'contact_description');
  210. $contact_description = $contactprop->value;
  211. }
  212. $form['contact_id'] = array(
  213. '#type' => 'hidden',
  214. '#value' => (isset($node->contact_id)) ? $node->contact_id->contact_id : NULL ,
  215. );
  216. $sql = "
  217. SELECT CVTS.cvterm_id, CVTS.name
  218. FROM {cvtermpath} CVTP
  219. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  220. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  221. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  222. WHERE
  223. CV.name = 'tripal_contact' AND
  224. CVTO.name = 'Contact Type' AND
  225. CVTP.pathdistance = 1
  226. ORDER BY CVTS.name ASC
  227. ";
  228. $results = chado_query($sql);
  229. $contact_types = array();
  230. $default_type = '';
  231. while ($contact_type = db_fetch_object($results)) {
  232. $contact_types[$contact_type->cvterm_id] = $contact_type->name;
  233. if (strcmp($contact_type->name,"Person") == 0) {
  234. $default_type = $contact_type->cvterm_id;
  235. }
  236. }
  237. $form['type_id'] = array(
  238. '#type' => 'select',
  239. '#title' => t('Contact Type'),
  240. '#options' => $contact_types,
  241. '#required' => TRUE,
  242. '#default_value' => isset($contact->contact_id->type_id) ? $contact->contact_id->type_id : $default_type,
  243. );
  244. $form['title']= array(
  245. '#type' => 'textfield',
  246. '#title' => t('Contact Name'),
  247. '#description' => t('Enter the name of this contact'),
  248. '#required' => TRUE,
  249. '#default_value' => $contact->name,
  250. '#weight' => 1
  251. );
  252. $form['contact_description']= array(
  253. '#type' => 'textarea',
  254. '#title' => t('Contact Description'),
  255. '#description' => t('A brief description of the contact'),
  256. '#required' => TRUE,
  257. '#default_value' => $contact_description,
  258. '#weight' => 5
  259. );
  260. return $form;
  261. }
  262. /**
  263. * Implementation of tripal_contact_insert().
  264. *
  265. * This function inserts user entered information pertaining to the contact instance into the
  266. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.
  267. *
  268. * @parm $node
  269. * Then node which contains the information stored within the node-ID
  270. *
  271. *
  272. */
  273. function chado_contact_insert($node) {
  274. // if a contact_id already exists for this node then it already exists in Chado and
  275. // we get here because we are syncing the node. Therefore, we can skip the insert
  276. if ($node->contact_id) {
  277. $contact['contact_id'] = $node->contact_id;
  278. }
  279. else {
  280. // check to see if this contact name already exists.
  281. $values = array('name' => $node->contact_name);
  282. $options = array('statement_name' => 'sel_contact_na');
  283. $contact = tripal_core_chado_select('contact', array('contact_id'), $values, $options);
  284. if (count($contact) > 0) {
  285. drupal_set_message(t('A contact with this name already exists.', 'error'));
  286. watchdog('tripal_contact','Insert Contact: A contact with this name already exists. %name',
  287. array('%name' => $node->contact_name), WATCHDOG_ERROR);
  288. return FALSE;
  289. }
  290. else {
  291. $values = array(
  292. 'name' => $node->title,
  293. 'description' => '',
  294. 'type_id' => $node->type_id
  295. );
  296. $options = array('statement_name' => 'ins_contact_nadety');
  297. $contact = tripal_core_chado_insert('contact', $values, $options);
  298. if (!$contact) {
  299. drupal_set_message(t('Could not add the contact'), 'error');
  300. watchdog('tripal_contact','Could not add the contact',
  301. array(), WATCHDOG_ERROR);
  302. return FALSE;
  303. }
  304. }
  305. }
  306. if ($contact) {
  307. // add the description property
  308. tripal_contact_insert_property($contact['contact_id'], 'contact_description',
  309. $node->contact_description, TRUE);
  310. // make sure the entry for this contact doesn't already exist in the chado_contact table
  311. // if it doesn't exist then we want to add it.
  312. $contact_id = chado_get_id_for_node('contact', $node) ;
  313. if (!$contact_id) {
  314. // next add the item to the drupal table
  315. $sql = "INSERT INTO {chado_contact} (nid, vid, contact_id) ".
  316. "VALUES (%d, %d, %d)";
  317. db_query($sql, $node->nid, $node->vid, $contact['contact_id']);
  318. }
  319. }
  320. else {
  321. drupal_set_message(t('Unable to add contact.', 'warning'));
  322. watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values',
  323. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  324. }
  325. return TRUE;
  326. }
  327. /*
  328. *
  329. * Implements hook_update
  330. *
  331. * The purpose of the function is to allow the module to take action when an edited node is being
  332. * updated. It updates any name changes to the database tables that werec reated upon registering a contact.
  333. * As well, the database will be changed, so the user changed information will be saved to the database.
  334. *
  335. * @param $node
  336. * The node being updated
  337. *
  338. * @ingroup tripal_contact
  339. */
  340. function chado_contact_update($node) {
  341. if ($node->revision) {
  342. // there is no way to handle revisions in Chado but leave
  343. // this here just to make not we've addressed it.
  344. }
  345. $contact_id = chado_get_id_for_node('contact', $node) ;
  346. // check to see if this contact name doens't already exists.
  347. $sql = "SELECT contact_id FROM {contact} WHERE NOT contact_id = %d AND name = '%s'";
  348. $contact = db_fetch_object(chado_query($sql, $contact_id, $node->contact_name));
  349. if ($contact) {
  350. drupal_set_message(t('A contact with this name already exists. Cannot perform update.'), 'warning');
  351. return;
  352. }
  353. // update the contact record
  354. $match = array(
  355. 'contact_id' => $contact_id,
  356. );
  357. $values = array(
  358. 'name' => $node->title,
  359. 'description' => '',
  360. 'type_id' => $node->type_id
  361. );
  362. $status = tripal_core_chado_update('contact', $match, $values);
  363. tripal_contact_update_property($contact_id, 'contact_description', $node->contact_description, 1);
  364. }
  365. /**
  366. * Implementation of tripal_contact_load().
  367. *
  368. *
  369. * @param $node
  370. * The node that is to be accessed from the database
  371. *
  372. * @return $node
  373. * The node with the information to be loaded into the database
  374. *
  375. */
  376. function chado_contact_load($node) {
  377. // get the feature details from chado
  378. $contact_id = chado_get_id_for_node('contact', $node);
  379. $values = array('contact_id' => $contact_id);
  380. $contact = tripal_core_generate_chado_var('contact', $values);
  381. $additions = new stdClass();
  382. $additions->contact = $contact;
  383. return $additions;
  384. }
  385. /**
  386. * Implementation of tripal_contact_delete().
  387. *
  388. * This function takes a node and if the delete button has been chosen by the user, the contact
  389. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  390. * the 'chado_contact' table.
  391. *
  392. * @parm $node
  393. * Then node which contains the information stored within the node-ID
  394. *
  395. */
  396. function chado_contact_delete(&$node) {
  397. $contact_id = chado_get_id_for_node('contact', $node);
  398. // if we don't have a contact id for this node then this isn't a node of
  399. // type chado_contact or the entry in the chado_contact table was lost.
  400. if (!$contact_id) {
  401. return;
  402. }
  403. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  404. // drupal database
  405. $sql_del = "DELETE FROM {chado_contact} ".
  406. "WHERE nid = %d ".
  407. "AND vid = %d";
  408. db_query($sql_del, $node->nid, $node->vid);
  409. $sql_del = "DELETE FROM {node_revisions} ".
  410. "WHERE nid = %d ".
  411. "AND vid = %d";
  412. db_query($sql_del, $node->nid, $node->vid);
  413. $sql_del = "DELETE FROM {node} ".
  414. "WHERE nid = %d ".
  415. "AND vid = %d";
  416. db_query($sql_del, $node->nid, $node->vid);
  417. // Remove data from contact and contactprop tables of chado database as well
  418. chado_query("DELETE FROM {contactprop} WHERE contact_id = %d", $contact_id);
  419. chado_query("DELETE FROM {contact} WHERE contact_id = %d", $contact_id);
  420. }
  421. /**
  422. *
  423. *
  424. * @ingroup tripal_contact
  425. */
  426. function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
  427. // we want to provide a new variable that contains the matched contacts.
  428. $contact = $variables['node']->contact;
  429. // normally we would use tripal_core_expand_chado_vars to expand our
  430. // organism object and add in the relationships, however whan a large
  431. // number of relationships are present this significantly slows the
  432. // query, therefore we will manually perform the query
  433. $sql = "
  434. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  435. FROM contact_relationship PR
  436. INNER JOIN {contact} C ON PR.object_id = C.contact_id
  437. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  438. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  439. WHERE PR.subject_id = %d
  440. ";
  441. $as_subject = chado_query($sql, $contact->contact_id);
  442. $sql = "
  443. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  444. FROM contact_relationship PR
  445. INNER JOIN {contact} C ON PR.subject_id = C.contact_id
  446. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  447. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  448. WHERE PR.object_id = %d
  449. ";
  450. $as_object = chado_query($sql, $contact->contact_id);
  451. // combine both object and subject relationshisp into a single array
  452. $relationships = array();
  453. $relationships['object'] = array();
  454. $relationships['subject'] = array();
  455. // iterate through the object relationships
  456. while ($relationship = db_fetch_object($as_object)) {
  457. // get the relationship and child types
  458. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  459. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  460. if (!array_key_exists($rel_type, $relationships['object'])) {
  461. $relationships['object'][$rel_type] = array();
  462. }
  463. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  464. $relationships['object'][$rel_type][$sub_type] = array();
  465. }
  466. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  467. }
  468. // now add in the subject relationships
  469. while ($relationship = db_fetch_object($as_subject)) {
  470. // get the relationship and child types
  471. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  472. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  473. if (!array_key_exists($rel_type, $relationships['subject'])) {
  474. $relationships['subject'][$rel_type] = array();
  475. }
  476. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  477. $relationships['subject'][$rel_type][$obj_type] = array();
  478. }
  479. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  480. }
  481. $contact->all_relationships = $relationships;
  482. }