tripal_contact.module

  1. 2.x tripal_contact/tripal_contact.module
  2. 3.x legacy/tripal_contact/tripal_contact.module
  3. 1.x tripal_contact/tripal_contact.module

This file contains the basic functions needed for this drupal module. The drupal tripal_contact module maps directly to the chado X module.

For documentation regarding the Chado X module:

See also

http://gmod.org/wiki/Chado_General_Module

File

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