tripal_featuremap.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * @defgroup tripal_featuremap Feature Map Module
  4. * @ingroup tripal_modules
  5. * @{
  6. * Provides functions for managing chado maps including creating details pages for each map
  7. * @}
  8. */
  9. require('api/tripal_featuremap.api.inc');
  10. require('includes/tripal_featuremap.admin.inc');
  11. require('includes/tripal_featuremap.form.inc');
  12. /**
  13. * Display help and module information
  14. * @param path which path of the site we're displaying help
  15. * @param arg array that holds the current path as would be returned from arg()
  16. * function
  17. * @return help text for the path
  18. *
  19. * @ingroup tripal_featuremap
  20. */
  21. function tripal_featuremap_help($path, $arg) {
  22. $output = '';
  23. switch ($path) {
  24. case "admin/help#tripal_featuremap":
  25. $output = '<p>'.
  26. t("Displays links to nodes created on this date") .
  27. '</p>';
  28. break;
  29. }
  30. return $output;
  31. }
  32. /**
  33. * Provide information to drupal about the node types that we're creating
  34. * in this module
  35. *
  36. * @ingroup tripal_featuremap
  37. */
  38. function tripal_featuremap_node_info() {
  39. $nodes = array();
  40. $nodes['chado_featuremap'] = array(
  41. 'name' => t('Map'),
  42. 'module' => 'chado_featuremap',
  43. 'description' => t('A feature map from the chado database (e.g. genetic map)'),
  44. 'has_title' => FALSE,
  45. 'title_label' => t('Feature Map'),
  46. 'has_body' => FALSE,
  47. 'body_label' => t('Feature Map Description'),
  48. 'locked' => TRUE
  49. );
  50. return $nodes;
  51. }
  52. /**
  53. * Set the permission types that the chado module uses. Essentially we
  54. * want permissionis that protect creation, editing and deleting of chado
  55. * data objects
  56. *
  57. * @ingroup tripal_featuremap
  58. */
  59. function tripal_featuremap_perm() {
  60. return array(
  61. 'access chado_featuremap content',
  62. 'create chado_featuremap content',
  63. 'delete chado_featuremap content',
  64. 'edit chado_featuremap content',
  65. 'administer tripal featuremap',
  66. );
  67. }
  68. /**
  69. * Set the permission types that the module uses.
  70. *
  71. * @ingroup tripal_featuremap
  72. */
  73. function chado_featuremap_access($op, $node, $account) {
  74. if ($op == 'create') {
  75. if (!user_access('create chado_featuremap content', $account)) {
  76. return FALSE;
  77. }
  78. }
  79. if ($op == 'update') {
  80. if (!user_access('edit chado_featuremap content', $account)) {
  81. return FALSE;
  82. }
  83. }
  84. if ($op == 'delete') {
  85. if (!user_access('delete chado_featuremap content', $account)) {
  86. return FALSE;
  87. }
  88. }
  89. if ($op == 'view') {
  90. if (!user_access('access chado_featuremap content', $account)) {
  91. return FALSE;
  92. }
  93. }
  94. return NULL;
  95. }
  96. /**
  97. * Menu items are automatically added for the new node types created
  98. * by this module to the 'Create Content' Navigation menu item. This function
  99. * adds more menu items needed for this module.
  100. *
  101. * @ingroup tripal_featuremap
  102. */
  103. function tripal_featuremap_menu() {
  104. $items = array();
  105. // The administative settings menu
  106. $items['admin/tripal/tripal_featuremap'] = array(
  107. 'title' => 'Maps',
  108. 'description' => 'Basic Description of Tripal Map Module Functionality',
  109. 'page callback' => 'theme',
  110. 'page arguments' => array('tripal_featuremap_admin'),
  111. 'access arguments' => array('administer tripal featuremap'),
  112. 'type' => MENU_NORMAL_ITEM,
  113. );
  114. $items['admin/tripal/tripal_featuremap/configuration'] = array(
  115. 'title' => 'Configuration',
  116. 'description' => 'Manage integration of Chado maps including associated features.',
  117. 'page callback' => 'drupal_get_form',
  118. 'page arguments' => array('tripal_featuremap_admin'),
  119. 'access arguments' => array('administer tripal featuremap'),
  120. 'type' => MENU_NORMAL_ITEM,
  121. );
  122. // Synchronizing maps from Chado to Drupal
  123. $items['chado_sync_featuremaps'] = array(
  124. 'title' => 'Sync Data',
  125. 'page callback' => 'tripal_featuremap_sync_featuremaps',
  126. 'access arguments' => array('administer tripal featuremap'),
  127. 'type' => MENU_CALLBACK
  128. );
  129. // AJAX calls for adding/removing properties to a featuremap
  130. $items['tripal_featuremap/properties/add'] = array(
  131. 'page callback' => 'tripal_featuremap_property_add',
  132. 'access arguments' => array('edit chado_featuremap content'),
  133. 'type ' => MENU_CALLBACK,
  134. );
  135. $items['tripal_featuremap/properties/description'] = array(
  136. 'page callback' => 'tripal_featuremap_property_get_description',
  137. 'access arguments' => array('edit chado_featuremap content'),
  138. 'type ' => MENU_CALLBACK,
  139. );
  140. $items['tripal_featuremap/properties/minus/%/%'] = array(
  141. 'page callback' => 'tripal_featuremap_property_delete',
  142. 'page arguments' => array(3, 4),
  143. 'access arguments' => array('edit chado_featuremap content'),
  144. 'type ' => MENU_CALLBACK,
  145. );
  146. return $items;
  147. }
  148. /**
  149. * Implements hook_views_api()
  150. * Purpose: Essentially this hook tells drupal that there is views support for
  151. * for this module which then includes tripal_db.views.inc where all the
  152. * views integration code is
  153. *
  154. * @ingroup tripal_featuremap
  155. */
  156. function tripal_featuremap_views_api() {
  157. return array(
  158. 'api' => 2.0,
  159. );
  160. }
  161. /**
  162. * Implementation of hook_nodeapi().
  163. * Display map information for associated features or organisms
  164. * This function also provides contents for indexing
  165. *
  166. * @ingroup tripal_featuremap
  167. */
  168. function tripal_featuremap_nodeapi(&$node, $op, $teaser, $page) {
  169. switch ($op) {
  170. // Note that this function only adds map view to an organism/feature
  171. // node.
  172. case 'view':
  173. // add the map to the organism/feature search indexing
  174. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  175. $node->content['tripal_featuremap_index_version'] = array(
  176. '#value' => theme('tripal_featuremap_search_index', $node),
  177. );
  178. }
  179. elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  180. $node->content['tripal_featuremap_index_version'] = array(
  181. '#value' => theme('tripal_featuremap_search_result', $node),
  182. );
  183. }
  184. }
  185. }
  186. /**
  187. * We need to let drupal know about our theme functions and their arguments.
  188. * We create theme functions to allow users of the module to customize the
  189. * look and feel of the output generated in this module
  190. *
  191. * @ingroup tripal_featuremap
  192. */
  193. function tripal_featuremap_theme() {
  194. return array(
  195. 'tripal_featuremap_search_index' => array(
  196. 'arguments' => array('node'),
  197. ),
  198. 'tripal_featuremap_search_result' => array(
  199. 'arguments' => array('node'),
  200. ),
  201. 'tripal_featuremap_base' => array(
  202. 'arguments' => array('node' => NULL),
  203. 'template' => 'tripal_featuremap_base',
  204. ),
  205. 'tripal_featuremap_properties' => array(
  206. 'arguments' => array('node' => NULL),
  207. 'template' => 'tripal_featuremap_properties',
  208. ),
  209. 'tripal_featuremap_publication' => array(
  210. 'arguments' => array('node' => NULL),
  211. 'template' => 'tripal_featuremap_publication',
  212. ),
  213. 'tripal_featuremap_references' => array(
  214. 'arguments' => array('node' => NULL),
  215. 'template' => 'tripal_featuremap_references',
  216. ),
  217. 'tripal_featuremap_admin' => array(
  218. 'template' => 'tripal_featuremap_admin',
  219. 'arguments' => array(NULL),
  220. 'path' => drupal_get_path('module', 'tripal_featuremap') . '/theme'
  221. ),
  222. // Themed Forms
  223. 'chado_featuremap_node_form' => array(
  224. 'arguments' => array('form'),
  225. ),
  226. );
  227. }
  228. /**
  229. * This function is an extension of the chado_feature_view and
  230. * chado_organism_view by providing the markup for the map object
  231. * THAT WILL BE INDEXED.
  232. *
  233. * @ingroup tripal_featuremap
  234. */
  235. function theme_tripal_featuremap_search_index($node) {
  236. }
  237. /**
  238. *
  239. * @ingroup tripal_featuremap
  240. */
  241. function tripal_featuremap_cron() {
  242. }
  243. /**
  244. * The following function proves access control for users trying to
  245. * perform actions on data managed by this module
  246. *
  247. * @ingroup tripal_featuremap
  248. */
  249. function tripal_featuremap_map_access($op, $node, $account) {
  250. if ($op == 'create') {
  251. if (!user_access('create chado_featuremap content', $account)) {
  252. return FALSE;
  253. }
  254. }
  255. if ($op == 'update') {
  256. if (!user_access('edit any chado_featuremap content', $account) &&
  257. !user_access('edit own chado_featuremap content', $account)) {
  258. return FALSE;
  259. }
  260. if (user_access('edit own chado_featuremap content', $account) &&
  261. $account->uid != $node->uid) {
  262. return FALSE;
  263. }
  264. }
  265. if ($op == 'delete') {
  266. if (!user_access('delete any chado_featuremap content', $account) &&
  267. !user_access('delete own chado_featuremap content', $account)) {
  268. return FALSE;
  269. }
  270. if (user_access('delete own chado_featuremap content', $account) &&
  271. $account->uid != $node->uid) {
  272. return FALSE;
  273. }
  274. }
  275. return NULL;
  276. }
  277. /**
  278. * When a new chado_featuremap node is created we also need to add information
  279. * to our chado_featuremap table. This function is called on insert of a new node
  280. * of type 'chado_featuremap' and inserts the necessary information.
  281. *
  282. * @ingroup tripal_featuremap
  283. */
  284. function chado_featuremap_insert($node) {
  285. // if the featuremap_id already exists then we got to the insert via
  286. // a syncing operation. We do not need to add the feature map
  287. if ($node->featuremap_id) {
  288. $featuremap['featuremap_id'] = $node->featuremap_id;
  289. }
  290. else {
  291. $values = array(
  292. 'name' => $node->title,
  293. 'description' => $node->description,
  294. 'unittype_id' => $node->unittype_id
  295. );
  296. $featuremap = tripal_core_chado_insert('featuremap', $values);
  297. if(!$featuremap) {
  298. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  299. watchdog('tripal_featuremap', 'Unable to create feature map where values: %values',
  300. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  301. return;
  302. }
  303. // now add the properties
  304. $properties = array(); // stores all of the properties we need to add
  305. $cross_refs = array(); // stores any cross references for this featuremap
  306. // get the list of properties for easy lookup (without doing lots of database queries
  307. $properties_list = array();
  308. $sql = "
  309. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  310. FROM {cvterm} CVT
  311. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  312. WHERE
  313. CV.name = 'featuremap_property' AND
  314. NOT CVT.is_obsolete = 1
  315. ORDER BY CVT.name ASC
  316. ";
  317. $prop_types = chado_query($sql);
  318. while ($prop = db_fetch_object($prop_types)) {
  319. $properties_list[$prop->cvterm_id] = $prop->name;
  320. }
  321. // get the properties that should be added. Properties are in one of two forms:
  322. // 1) prop_value-[type id]-[index]
  323. // 2) new_value-[type id]-[index]
  324. // 3) new_id, new_value
  325. foreach ($node as $name => $value) {
  326. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  327. $type_id = $matches[1];
  328. $index = $matches[2];
  329. $name = $properties_list[$type_id];
  330. $properties[$name][$index] = trim($value);
  331. }
  332. }
  333. if ($node->new_id and $node->new_value) {
  334. $type_id = $node->new_id;
  335. $index = count($properties[$name]);
  336. $name = $properties_list[$type_id];
  337. $properties[$name][$index] = trim($node->new_value);
  338. }
  339. // iterate through all of the properties to see if the Map dbxref is set,
  340. // if so, add it to the $cross_refs array
  341. foreach ($properties as $name => $element) {
  342. foreach ($element as $index => $value) {
  343. if ($name == "Map Dbxref") {
  344. // we will add the cross-references to the featuremap_dbxref table
  345. // but we also want to keep the property in the featuremapprop table so don't unset it
  346. $cross_refs[] = $value;
  347. }
  348. }
  349. }
  350. // now add in the properties
  351. foreach ($properties as $property => $elements) {
  352. foreach ($elements as $rank => $value) {
  353. $status = tripal_featuremap_insert_property($featuremap['featuremap_id'], $property, $value, FALSE);
  354. if (!$status) {
  355. drupal_set_message("Error cannot add property: $property", "error");
  356. watchdog('t_featuremap', "Error cannot add property: %prop",
  357. array('%property' => $property), WATCHDOG_ERROR);
  358. }
  359. }
  360. }
  361. // add in any database cross-references
  362. foreach ($cross_refs as $index => $ref) {
  363. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap['featuremap_id'], trim($ref));
  364. if (!$featuremap_dbxref) {
  365. drupal_set_message("Error cannot add map cross reference: $ref", "error");
  366. watchdog('t_featuremap', "Error cannot add map cross reference: %ref",
  367. array('%ref' => $ref), WATCHDOG_ERROR);
  368. }
  369. }
  370. }
  371. // add the record to the chado_featuremap table in Drupal
  372. if ($featuremap) {
  373. // make sure the entry for this feature doesn't already exist in the chado_featuremap table
  374. // if it doesn't exist then we want to add it.
  375. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  376. if (!$featuremap_id) {
  377. // next add the item to the drupal table
  378. $sql = "INSERT INTO {chado_featuremap} (nid, vid, featuremap_id) VALUES (%d, %d, %d)";
  379. db_query($sql, $node->nid, $node->vid, $featuremap['featuremap_id']);
  380. }
  381. }
  382. }
  383. /**
  384. * Update nodes
  385. *
  386. * @ingroup tripal_featuremap
  387. */
  388. function chado_featuremap_update($node) {
  389. if ($node->revision) {
  390. // there is no way to handle revisions in Chado but leave
  391. // this here just to make not we've addressed it.
  392. }
  393. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  394. // update the map record
  395. $match = array(
  396. 'featuremap_id' => $featuremap_id,
  397. );
  398. $values = array(
  399. 'name' => $node->title,
  400. 'description' => $node->description,
  401. 'unittype_id' => $node->unittype_id
  402. );
  403. $status = tripal_core_chado_update('featuremap', $match, $values);
  404. if (!$status) {
  405. drupal_set_message("Error updating map", "error");
  406. watchdog('t_featuremap', "Error updating map", array(), WATCHDOG_ERROR);
  407. return;
  408. }
  409. // now update the properties
  410. $properties = array(); // stores all of the properties we need to add
  411. $cross_refs = array(); // stores any cross references for this map
  412. // get the list of properties for easy lookup (without doing lots of database queries
  413. $properties_list = array();
  414. $sql = "
  415. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  416. FROM {cvterm} CVT
  417. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  418. WHERE
  419. CV.name = 'featuremap_property' AND
  420. NOT CVT.is_obsolete = 1
  421. ORDER BY CVT.name ASC
  422. ";
  423. $prop_types = chado_query($sql);
  424. while ($prop = db_fetch_object($prop_types)) {
  425. $properties_list[$prop->cvterm_id] = $prop->name;
  426. }
  427. // get the properties that should be added. Properties are in one of three forms:
  428. // 1) prop_value-[type id]-[index]
  429. // 2) new_value-[type id]-[index]
  430. // 3) new_id, new_value
  431. // dpm($node);
  432. foreach ($node as $key => $value) {
  433. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  434. $type_id = $matches[1];
  435. $index = $matches[2];
  436. $name = $properties_list[$type_id];
  437. $properties[$name][$index] = trim($value);
  438. }
  439. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  440. $type_id = $matches[1];
  441. $index = $matches[2];
  442. $name = $properties_list[$type_id];
  443. $properties[$name][$index] = trim($value);
  444. }
  445. }
  446. if ($node->new_id and $node->new_value) {
  447. $type_id = $node->new_id;
  448. $name = $properties_list[$type_id];
  449. $index = count($properties[$name]);
  450. $properties[$name][$index] = trim($node->new_value);
  451. }
  452. // iterate through all of the properties to see if the Map dbxref is set,
  453. // if so, add it to the $cross_refs array
  454. foreach ($properties as $name => $element) {
  455. foreach ($element as $index => $value) {
  456. if ($name == "Map Dbxref") {
  457. // we will add the cross-references to the featuremap_dbxref table
  458. // but we also want to keep the property in the featuremapprop table so don't unset it
  459. $cross_refs[] = $value;
  460. }
  461. }
  462. }
  463. // now add in the properties by first removing any the featuremap
  464. // already has and adding the ones we have
  465. tripal_core_chado_delete('featuremapprop', array('featuremap_id' => $featuremap_id));
  466. foreach ($properties as $property => $elements) {
  467. foreach ($elements as $rank => $value) {
  468. $status = tripal_featuremap_insert_property($featuremap_id, $property, $value, FALSE);
  469. if (!$status) {
  470. drupal_set_message("Error cannot add property: '$property'", "error");
  471. watchdog('t_featuremap', "Error cannot add property: '%prop'",
  472. array('%prop' => $property), WATCHDOG_ERROR);
  473. }
  474. }
  475. }
  476. // add in any database cross-references after first removing
  477. tripal_core_chado_delete('featuremap_dbxref', array('featuremap_id' => $featuremap_id));
  478. foreach ($cross_refs as $index => $ref) {
  479. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, trim($ref));
  480. if (!$featuremap_dbxref) {
  481. drupal_set_message("Error cannot add map cross reference: $ref", "error");
  482. watchdog('t_featuremap', "Error cannot add map cross reference: %ref",
  483. array('%ref' => $ref), WATCHDOG_ERROR);
  484. }
  485. }
  486. }
  487. /**
  488. * When a node is requested by the user this function is called to allow us
  489. * to add auxiliary data to the node object.
  490. *
  491. * @ingroup tripal_featuremap
  492. */
  493. function chado_featuremap_load($node) {
  494. // get the feature details from chado
  495. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  496. $values = array('featuremap_id' => $featuremap_id);
  497. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  498. // expand the description field as it is needed by the form
  499. $featuremap = tripal_core_expand_chado_vars($featuremap, 'field', 'featuremap.description');
  500. $additions = new stdClass();
  501. $additions->featuremap = $featuremap;
  502. return $additions;
  503. }
  504. /**
  505. * This function customizes the view of the chado_featuremap node. It allows
  506. * us to generate the markup. This function is required for node [Preview]
  507. *
  508. * @ingroup tripal_featuremap
  509. */
  510. function chado_featuremap_view($node, $teaser = FALSE, $page = FALSE) {
  511. // use drupal's default node view:
  512. if (!$teaser) {
  513. $node = node_prepare($node, $teaser);
  514. }
  515. return $node;
  516. }
  517. /**
  518. * Delete data from drupal and chado databases when a node is deleted
  519. * @ingroup tripal_featuremap
  520. */
  521. function chado_featuremap_delete(&$node) {
  522. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  523. // if we don't have a map id for this node then this isn't a node of
  524. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  525. if (!$featuremap_id) {
  526. return;
  527. }
  528. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  529. // drupal database
  530. $sql_del = "DELETE FROM {chado_featuremap} ".
  531. "WHERE nid = %d ".
  532. "AND vid = %d";
  533. db_query($sql_del, $node->nid, $node->vid);
  534. $sql_del = "DELETE FROM {node} ".
  535. "WHERE nid = %d ".
  536. "AND vid = %d";
  537. db_query($sql_del, $node->nid, $node->vid);
  538. $sql_del = "DELETE FROM {node_revisions} ".
  539. "WHERE nid = %d ".
  540. "AND vid = %d";
  541. db_query($sql_del, $node->nid, $node->vid);
  542. // Remove data from map and mapprop tables of chado database as well
  543. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = %d", $featuremap_id);
  544. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = %d", $featuremap_id);
  545. }
  546. /*
  547. *
  548. */
  549. function theme_tripal_featuremap_search_result($node) {
  550. }
  551. function tripal_featuremap_form_alter(&$form, &$form_state, $form_id) {
  552. if ($form_id == "chado_featuremap_node_form") {
  553. }
  554. }