tripal_contact.module 19 KB

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