tripal_contact.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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_css(drupal_get_path('theme', 'tripal') . '/css/tripal_contact.css');
  45. }
  46. /**
  47. * Implementation of hook_tripal_contact_node_info().
  48. *
  49. * This node_info, is a simple node that describes the functionallity of the module.
  50. *
  51. */
  52. function tripal_contact_node_info() {
  53. return array(
  54. 'chado_contact' => array(
  55. 'name' => t('Contact'),
  56. 'module' => 'chado_contact',
  57. 'description' => t('A contact from the Chado database'),
  58. 'title_label' => t('Article Title'),
  59. 'body_label' => t('Abstract'),
  60. 'has_title' => TRUE,
  61. 'has_body' => FALSE,
  62. ),
  63. );
  64. }
  65. /**
  66. * Tripal-contact-Menu
  67. *
  68. * Implemets hook_menu(): Adds menu items for the tripal_contact module menu. This section
  69. * gives the outline for the main menu of the Tripal-contact module
  70. *
  71. * @return
  72. * An array of menu items that is visible within the Drupal Menu, returned as soon
  73. * as the program is ran
  74. */
  75. function tripal_contact_menu() {
  76. $items = array();
  77. $items[ 'admin/tripal/tripal_contact' ]= array(
  78. 'title' => 'Contacts',
  79. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of contacts'),
  80. 'page callback' => 'theme',
  81. 'page arguments' => array('tripal_contact_admin'),
  82. 'access arguments' => array('administer tripal contacts'),
  83. 'type' => MENU_NORMAL_ITEM
  84. );
  85. $items['admin/tripal/tripal_contact/configuration'] = array(
  86. 'title' => 'Configuration',
  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_NORMAL_ITEM,
  92. );
  93. $items['admin/tripal/tripal_contact/sync'] = array(
  94. 'title' => ' Sync Contacts',
  95. 'description' => 'Sync contacts in Chado with Drupal',
  96. 'page callback' => 'drupal_get_form',
  97. 'page arguments' => array('tripal_contact_sync_form'),
  98. 'access arguments' => array('administer tripal contacts'),
  99. 'type' => MENU_NORMAL_ITEM,
  100. );
  101. // AJAX calls for adding/removing properties to a contact
  102. $items['tripal_contact/properties/add'] = array(
  103. 'page callback' => 'tripal_contact_property_add',
  104. 'access arguments' => array('edit chado_contact content'),
  105. 'type ' => MENU_CALLBACK,
  106. );
  107. $items['tripal_contact/properties/description'] = array(
  108. 'page callback' => 'tripal_contact_property_get_description',
  109. 'access arguments' => array('edit chado_contact content'),
  110. 'type ' => MENU_CALLBACK,
  111. );
  112. $items['tripal_contact/properties/minus/%/%'] = array(
  113. 'page callback' => 'tripal_contact_property_delete',
  114. 'page arguments' => array(3, 4),
  115. 'access arguments' => array('edit chado_contact content'),
  116. 'type ' => MENU_CALLBACK,
  117. );
  118. return $items;
  119. }
  120. /**
  121. * Implements hook_theme(): Register themeing functions for this module
  122. *
  123. *
  124. * @return
  125. * An array of themeing functions to register
  126. *
  127. */
  128. function tripal_contact_theme() {
  129. return array(
  130. 'tripal_contact_base' => array(
  131. 'arguments' => array('node' => NULL),
  132. ),
  133. 'tripal_contact_properties' => array(
  134. 'arguments' => array('node' => NULL)
  135. ),
  136. 'tripal_contact_relationships' => array(
  137. 'arguments' => array('node' => NULL)
  138. ),
  139. 'tripal_contact_publications' => array(
  140. 'arguments' => array('node' => NULL)
  141. ),
  142. 'tripal_contact_admin' => array(
  143. 'template' => 'tripal_contact_admin',
  144. 'arguments' => array(NULL),
  145. 'path' => drupal_get_path('module', 'tripal_contact') . '/theme'
  146. ),
  147. // Themed Forms
  148. 'chado_contact_node_form' => array(
  149. 'arguments' => array('form'),
  150. ),
  151. );
  152. }
  153. /**
  154. * Implement hook_perm().
  155. */
  156. function tripal_contact_perm() {
  157. return array(
  158. 'access chado_contact content',
  159. 'create chado_contact content',
  160. 'delete chado_contact content',
  161. 'edit chado_contact content',
  162. 'administer tripal contacts',
  163. );
  164. }
  165. /**
  166. * Implement hook_access().
  167. *
  168. * This hook allows node modules to limit access to the node types they define.
  169. *
  170. * @param $op
  171. * The operation to be performed
  172. *
  173. * @param $node
  174. * The node on which the operation is to be performed, or, if it does not yet exist, the
  175. * type of node to be created
  176. *
  177. * @param $account
  178. * A user object representing the user for whom the operation is to be performed
  179. *
  180. * @return
  181. * TRUE
  182. *
  183. */
  184. function chado_contact_access($op, $node, $account ) {
  185. if ($op == 'create') {
  186. if (!user_access('create chado_contact content', $account)) {
  187. return FALSE;
  188. }
  189. }
  190. if ($op == 'update') {
  191. if (!user_access('edit chado_contact content', $account)) {
  192. return FALSE;
  193. }
  194. }
  195. if ($op == 'delete') {
  196. if (!user_access('delete chado_contact content', $account)) {
  197. return FALSE;
  198. }
  199. }
  200. if ($op == 'view') {
  201. if (!user_access('access chado_contact content', $account)) {
  202. return FALSE;
  203. }
  204. }
  205. return NULL;
  206. }
  207. /**
  208. * Implementation of tripal_contact_insert().
  209. *
  210. * This function inserts user entered information pertaining to the contact instance into the
  211. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.
  212. *
  213. * @parm $node
  214. * Then node which contains the information stored within the node-ID
  215. *
  216. *
  217. */
  218. function chado_contact_insert($node) {
  219. // if a contact_id already exists for this node then it already exists in Chado and
  220. // we get here because we are syncing the node. Therefore, we can skip the insert
  221. if ($node->contact_id) {
  222. $contact['contact_id'] = $node->contact_id;
  223. }
  224. else {
  225. // we don't want to store the description in the description field as it may be longer than
  226. // 255 characters, so we'll use a property to store this value.
  227. $values = array(
  228. 'name' => $node->title,
  229. 'description' => '',
  230. 'type_id' => $node->type_id
  231. );
  232. $options = array('statement_name' => 'ins_contact_nadety');
  233. $contact = tripal_core_chado_insert('contact', $values, $options);
  234. if (!$contact) {
  235. drupal_set_message(t('Could not add the contact'), 'error');
  236. watchdog('tripal_contact','Could not add the contact', array(), WATCHDOG_ERROR);
  237. return FALSE;
  238. }
  239. // now add the properties
  240. $properties = array(); // stores all of the properties we need to add
  241. // get the list of properties for easy lookup (without doing lots of database queries
  242. $properties_list = array();
  243. $sql = "
  244. SELECT CVTS.cvterm_id, CVTS.name
  245. FROM {cvtermpath} CVTP
  246. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  247. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  248. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  249. WHERE
  250. CV.name = 'tripal_contact' AND
  251. NOT CVTO.name = 'Contact Type'
  252. ORDER BY CVTS.name ASC
  253. ";
  254. $prop_types = chado_query($sql);
  255. while ($prop = db_fetch_object($prop_types)) {
  256. $properties_list[$prop->cvterm_id] = $prop->name;
  257. }
  258. // get the properties that should be added. Properties are in one of two forms:
  259. // 1) prop_value-[type id]-[index]
  260. // 2) new_value-[type id]-[index]
  261. // 3) new_id, new_value
  262. foreach ($node as $name => $value) {
  263. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  264. $type_id = $matches[1];
  265. $index = $matches[2];
  266. $name = $properties_list[$type_id];
  267. $properties[$name][$index] = trim($value);
  268. }
  269. }
  270. if ($node->new_id and $node->new_value) {
  271. $type_id = $node->new_id;
  272. $index = count($properties[$name]);
  273. $name = $properties_list[$type_id];
  274. $properties[$name][$index] = trim($node->new_value);
  275. }
  276. // now add in the properties
  277. foreach ($properties as $property => $elements) {
  278. foreach ($elements as $rank => $value) {
  279. $status = tripal_contact_insert_property($contact['contact_id'], $property, $value, FALSE);
  280. if (!$status) {
  281. drupal_set_message("Error cannot add property: $property", "error");
  282. watchdog('t_contact', "Error cannot add property: %prop",
  283. array('%property' => $property), WATCHDOG_ERROR);
  284. }
  285. }
  286. }
  287. }
  288. // add the record to the chado_contact table in Drupal
  289. if ($contact) {
  290. // add the description property
  291. tripal_contact_insert_property($contact['contact_id'], 'contact_description',
  292. $node->description, TRUE);
  293. // make sure the entry for this contact doesn't already exist in the chado_contact table
  294. // if it doesn't exist then we want to add it.
  295. $contact_id = chado_get_id_for_node('contact', $node) ;
  296. if (!$contact_id) {
  297. // next add the item to the drupal table
  298. $sql = "INSERT INTO {chado_contact} (nid, vid, contact_id) ".
  299. "VALUES (%d, %d, %d)";
  300. db_query($sql, $node->nid, $node->vid, $contact['contact_id']);
  301. }
  302. }
  303. else {
  304. drupal_set_message(t('Unable to add contact.', 'warning'));
  305. watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values',
  306. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  307. }
  308. return TRUE;
  309. }
  310. /*
  311. *
  312. * Implements hook_update
  313. *
  314. * The purpose of the function is to allow the module to take action when an edited node is being
  315. * updated. It updates any name changes to the database tables that werec reated upon registering a contact.
  316. * As well, the database will be changed, so the user changed information will be saved to the database.
  317. *
  318. * @param $node
  319. * The node being updated
  320. *
  321. * @ingroup tripal_contact
  322. */
  323. function chado_contact_update($node) {
  324. if ($node->revision) {
  325. // there is no way to handle revisions in Chado but leave
  326. // this here just to make not we've addressed it.
  327. }
  328. $contact_id = chado_get_id_for_node('contact', $node) ;
  329. // check to see if this contact name doens't already exists.
  330. $sql = "SELECT contact_id FROM {contact} WHERE NOT contact_id = %d AND name = '%s'";
  331. $contact = db_fetch_object(chado_query($sql, $contact_id, $node->contact_name));
  332. if ($contact) {
  333. drupal_set_message(t('A contact with this name already exists. Cannot perform update.'), 'warning');
  334. return;
  335. }
  336. // update the contact record
  337. $match = array(
  338. 'contact_id' => $contact_id,
  339. );
  340. $values = array(
  341. 'name' => $node->title,
  342. 'description' => '',
  343. 'type_id' => $node->type_id
  344. );
  345. $status = tripal_core_chado_update('contact', $match, $values);
  346. if (!$status) {
  347. drupal_set_message("Error updating contact", "error");
  348. watchdog('t_contact', "Error updating contact", array(), WATCHDOG_ERROR);
  349. return;
  350. }
  351. // now update the properties
  352. $properties = array(); // stores all of the properties we need to add
  353. // get the list of properties for easy lookup (without doing lots of database queries
  354. $properties_list = array();
  355. $sql = "
  356. SELECT CVTS.cvterm_id, CVTS.name
  357. FROM {cvtermpath} CVTP
  358. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  359. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  360. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  361. WHERE
  362. CV.name = 'tripal_contact' AND
  363. NOT CVTO.name = 'Contact Type'
  364. ORDER BY CVTS.name ASC
  365. ";
  366. $prop_types = chado_query($sql);
  367. while ($prop = db_fetch_object($prop_types)) {
  368. $properties_list[$prop->cvterm_id] = $prop->name;
  369. }
  370. // get the properties that should be added. Properties are in one of three forms:
  371. // 1) prop_value-[type id]-[index]
  372. // 2) new_value-[type id]-[index]
  373. // 3) new_id, new_value
  374. // dpm($node);
  375. foreach ($node as $key => $value) {
  376. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  377. $type_id = $matches[1];
  378. $index = $matches[2];
  379. $name = $properties_list[$type_id];
  380. $properties[$name][$index] = trim($value);
  381. }
  382. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  383. $type_id = $matches[1];
  384. $index = $matches[2];
  385. $name = $properties_list[$type_id];
  386. $properties[$name][$index] = trim($value);
  387. }
  388. }
  389. if ($node->new_id and $node->new_value) {
  390. $type_id = $node->new_id;
  391. $name = $properties_list[$type_id];
  392. $index = count($properties[$name]);
  393. $properties[$name][$index] = trim($node->new_value);
  394. }
  395. // now add in the properties by first removing any the contact
  396. // already has and adding the ones we have
  397. tripal_core_chado_delete('contactprop', array('contact_id' => $contact_id));
  398. foreach ($properties as $property => $elements) {
  399. foreach ($elements as $rank => $value) {
  400. $status = tripal_contact_insert_property($contact_id, $property, $value, FALSE);
  401. if (!$status) {
  402. drupal_set_message("Error cannot add property: '$property'", "error");
  403. watchdog('t_contact', "Error cannot add property: '%prop'",
  404. array('%prop' => $property), WATCHDOG_ERROR);
  405. }
  406. }
  407. }
  408. tripal_contact_update_property($contact_id, 'contact_description', $node->description, 1);
  409. }
  410. /**
  411. * Implementation of tripal_contact_load().
  412. *
  413. *
  414. * @param $node
  415. * The node that is to be accessed from the database
  416. *
  417. * @return $node
  418. * The node with the information to be loaded into the database
  419. *
  420. */
  421. function chado_contact_load($node) {
  422. // get the feature details from chado
  423. $contact_id = chado_get_id_for_node('contact', $node);
  424. $values = array('contact_id' => $contact_id);
  425. $contact = tripal_core_generate_chado_var('contact', $values);
  426. // get the contact description and replace the contact.description field with this one
  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. $additions = new stdClass();
  443. $additions->contact = $contact;
  444. return $additions;
  445. }
  446. /**
  447. * Implementation of tripal_contact_delete().
  448. *
  449. * This function takes a node and if the delete button has been chosen by the user, the contact
  450. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  451. * the 'chado_contact' table.
  452. *
  453. * @parm $node
  454. * Then node which contains the information stored within the node-ID
  455. *
  456. */
  457. function chado_contact_delete(&$node) {
  458. $contact_id = chado_get_id_for_node('contact', $node);
  459. // if we don't have a contact id for this node then this isn't a node of
  460. // type chado_contact or the entry in the chado_contact table was lost.
  461. if (!$contact_id) {
  462. return;
  463. }
  464. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  465. // drupal database
  466. $sql_del = "DELETE FROM {chado_contact} ".
  467. "WHERE nid = %d ".
  468. "AND vid = %d";
  469. db_query($sql_del, $node->nid, $node->vid);
  470. $sql_del = "DELETE FROM {node_revisions} ".
  471. "WHERE nid = %d ".
  472. "AND vid = %d";
  473. db_query($sql_del, $node->nid, $node->vid);
  474. $sql_del = "DELETE FROM {node} ".
  475. "WHERE nid = %d ".
  476. "AND vid = %d";
  477. db_query($sql_del, $node->nid, $node->vid);
  478. // Remove data from contact and contactprop tables of chado database as well
  479. chado_query("DELETE FROM {contactprop} WHERE contact_id = %d", $contact_id);
  480. chado_query("DELETE FROM {contact} WHERE contact_id = %d", $contact_id);
  481. }
  482. /**
  483. *
  484. *
  485. * @ingroup tripal_contact
  486. */
  487. function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
  488. // we want to provide a new variable that contains the matched contacts.
  489. $contact = $variables['node']->contact;
  490. // normally we would use tripal_core_expand_chado_vars to expand our
  491. // organism object and add in the relationships, however whan a large
  492. // number of relationships are present this significantly slows the
  493. // query, therefore we will manually perform the query
  494. $sql = "
  495. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  496. FROM contact_relationship PR
  497. INNER JOIN {contact} C ON PR.object_id = C.contact_id
  498. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  499. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  500. WHERE PR.subject_id = %d
  501. ";
  502. $as_subject = chado_query($sql, $contact->contact_id);
  503. $sql = "
  504. SELECT C.name, C.contact_id, CP.nid, CVT.name as rel_type
  505. FROM contact_relationship PR
  506. INNER JOIN {contact} C ON PR.subject_id = C.contact_id
  507. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  508. LEFT JOIN public.chado_contact CP ON C.contact_id = CP.contact_id
  509. WHERE PR.object_id = %d
  510. ";
  511. $as_object = chado_query($sql, $contact->contact_id);
  512. // combine both object and subject relationshisp into a single array
  513. $relationships = array();
  514. $relationships['object'] = array();
  515. $relationships['subject'] = array();
  516. // iterate through the object relationships
  517. while ($relationship = db_fetch_object($as_object)) {
  518. // get the relationship and child types
  519. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  520. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  521. if (!array_key_exists($rel_type, $relationships['object'])) {
  522. $relationships['object'][$rel_type] = array();
  523. }
  524. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  525. $relationships['object'][$rel_type][$sub_type] = array();
  526. }
  527. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  528. }
  529. // now add in the subject relationships
  530. while ($relationship = db_fetch_object($as_subject)) {
  531. // get the relationship and child types
  532. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  533. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  534. if (!array_key_exists($rel_type, $relationships['subject'])) {
  535. $relationships['subject'][$rel_type] = array();
  536. }
  537. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  538. $relationships['subject'][$rel_type][$obj_type] = array();
  539. }
  540. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  541. }
  542. $contact->all_relationships = $relationships;
  543. }
  544. /*
  545. *
  546. */
  547. function tripal_contact_form_alter(&$form, &$form_state, $form_id) {
  548. if ($form_id == "chado_contact_node_form") {
  549. }
  550. }