tripal_featuremap.module 19 KB

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