tripal_contact.module 16 KB

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