tripal_featuremap.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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_featurepos' => array(
  218. 'arguments' => array('node' => NULL),
  219. 'template' => 'tripal_featuremap_featurepos',
  220. ),
  221. 'tripal_featuremap_publication' => array(
  222. 'arguments' => array('node' => NULL),
  223. 'template' => 'tripal_featuremap_publication',
  224. ),
  225. 'tripal_featuremap_references' => array(
  226. 'arguments' => array('node' => NULL),
  227. 'template' => 'tripal_featuremap_references',
  228. ),
  229. 'tripal_featuremap_admin' => array(
  230. 'template' => 'tripal_featuremap_admin',
  231. 'arguments' => array(NULL),
  232. 'path' => drupal_get_path('module', 'tripal_featuremap') . '/theme'
  233. ),
  234. // Themed Forms
  235. 'chado_featuremap_node_form' => array(
  236. 'arguments' => array('form'),
  237. ),
  238. );
  239. }
  240. /**
  241. * This function is an extension of the chado_feature_view and
  242. * chado_organism_view by providing the markup for the map object
  243. * THAT WILL BE INDEXED.
  244. *
  245. * @ingroup tripal_featuremap
  246. */
  247. function theme_tripal_featuremap_search_index($node) {
  248. }
  249. /**
  250. *
  251. * @ingroup tripal_featuremap
  252. */
  253. function tripal_featuremap_cron() {
  254. }
  255. /**
  256. * The following function proves access control for users trying to
  257. * perform actions on data managed by this module
  258. *
  259. * @ingroup tripal_featuremap
  260. */
  261. function tripal_featuremap_map_access($op, $node, $account) {
  262. if ($op == 'create') {
  263. if (!user_access('create chado_featuremap content', $account)) {
  264. return FALSE;
  265. }
  266. }
  267. if ($op == 'update') {
  268. if (!user_access('edit any chado_featuremap content', $account) &&
  269. !user_access('edit own chado_featuremap content', $account)) {
  270. return FALSE;
  271. }
  272. if (user_access('edit own chado_featuremap content', $account) &&
  273. $account->uid != $node->uid) {
  274. return FALSE;
  275. }
  276. }
  277. if ($op == 'delete') {
  278. if (!user_access('delete any chado_featuremap content', $account) &&
  279. !user_access('delete own chado_featuremap content', $account)) {
  280. return FALSE;
  281. }
  282. if (user_access('delete own chado_featuremap content', $account) &&
  283. $account->uid != $node->uid) {
  284. return FALSE;
  285. }
  286. }
  287. return NULL;
  288. }
  289. /**
  290. * When a new chado_featuremap node is created we also need to add information
  291. * to our chado_featuremap table. This function is called on insert of a new node
  292. * of type 'chado_featuremap' and inserts the necessary information.
  293. *
  294. * @ingroup tripal_featuremap
  295. */
  296. function chado_featuremap_insert($node) {
  297. // if the featuremap_id already exists then we got to the insert via
  298. // a syncing operation. We do not need to add the feature map
  299. if ($node->featuremap_id) {
  300. $featuremap['featuremap_id'] = $node->featuremap_id;
  301. }
  302. else {
  303. $values = array(
  304. 'name' => $node->title,
  305. 'description' => $node->description,
  306. 'unittype_id' => $node->unittype_id
  307. );
  308. $featuremap = tripal_core_chado_insert('featuremap', $values);
  309. if(!$featuremap) {
  310. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  311. watchdog('tripal_featuremap', 'Unable to create feature map where values: %values',
  312. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  313. return;
  314. }
  315. // now add the properties
  316. $properties = array(); // stores all of the properties we need to add
  317. $cross_refs = array(); // stores any cross references for this featuremap
  318. // get the list of properties for easy lookup (without doing lots of database queries
  319. $properties_list = array();
  320. $sql = "
  321. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  322. FROM {cvterm} CVT
  323. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  324. WHERE
  325. CV.name = 'featuremap_property' AND
  326. NOT CVT.is_obsolete = 1
  327. ORDER BY CVT.name ASC
  328. ";
  329. $prop_types = chado_query($sql);
  330. while ($prop = db_fetch_object($prop_types)) {
  331. $properties_list[$prop->cvterm_id] = $prop->name;
  332. }
  333. // get the properties that should be added. Properties are in one of two forms:
  334. // 1) prop_value-[type id]-[index]
  335. // 2) new_value-[type id]-[index]
  336. // 3) new_id, new_value
  337. foreach ($node as $name => $value) {
  338. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  339. $type_id = $matches[1];
  340. $index = $matches[2];
  341. $name = $properties_list[$type_id];
  342. $properties[$name][$index] = trim($value);
  343. }
  344. }
  345. if ($node->new_id and $node->new_value) {
  346. $type_id = $node->new_id;
  347. $index = count($properties[$name]);
  348. $name = $properties_list[$type_id];
  349. $properties[$name][$index] = trim($node->new_value);
  350. }
  351. // iterate through all of the properties to see if the Map dbxref is set,
  352. // if so, add it to the $cross_refs array
  353. foreach ($properties as $name => $element) {
  354. foreach ($element as $index => $value) {
  355. if ($name == "Map Dbxref") {
  356. // we will add the cross-references to the featuremap_dbxref table
  357. // but we also want to keep the property in the featuremapprop table so don't unset it
  358. $cross_refs[] = $value;
  359. }
  360. }
  361. }
  362. // now add in the properties
  363. foreach ($properties as $property => $elements) {
  364. foreach ($elements as $rank => $value) {
  365. $status = tripal_featuremap_insert_property($featuremap['featuremap_id'], $property, $value, FALSE);
  366. if (!$status) {
  367. drupal_set_message("Error cannot add property: $property", "error");
  368. watchdog('t_featuremap', "Error cannot add property: %prop",
  369. array('%property' => $property), WATCHDOG_ERROR);
  370. }
  371. }
  372. }
  373. // add in any database cross-references
  374. foreach ($cross_refs as $index => $ref) {
  375. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap['featuremap_id'], trim($ref));
  376. if (!$featuremap_dbxref) {
  377. drupal_set_message("Error cannot add map cross reference: $ref", "error");
  378. watchdog('t_featuremap', "Error cannot add map cross reference: %ref",
  379. array('%ref' => $ref), WATCHDOG_ERROR);
  380. }
  381. }
  382. }
  383. // add the record to the chado_featuremap table in Drupal
  384. if ($featuremap) {
  385. // make sure the entry for this feature doesn't already exist in the chado_featuremap table
  386. // if it doesn't exist then we want to add it.
  387. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  388. if (!$featuremap_id) {
  389. // next add the item to the drupal table
  390. $sql = "INSERT INTO {chado_featuremap} (nid, vid, featuremap_id) VALUES (%d, %d, %d)";
  391. db_query($sql, $node->nid, $node->vid, $featuremap['featuremap_id']);
  392. }
  393. }
  394. }
  395. /**
  396. * Update nodes
  397. *
  398. * @ingroup tripal_featuremap
  399. */
  400. function chado_featuremap_update($node) {
  401. if ($node->revision) {
  402. // there is no way to handle revisions in Chado but leave
  403. // this here just to make not we've addressed it.
  404. }
  405. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  406. // update the map record
  407. $match = array(
  408. 'featuremap_id' => $featuremap_id,
  409. );
  410. $values = array(
  411. 'name' => $node->title,
  412. 'description' => $node->description,
  413. 'unittype_id' => $node->unittype_id
  414. );
  415. $status = tripal_core_chado_update('featuremap', $match, $values);
  416. if (!$status) {
  417. drupal_set_message("Error updating map", "error");
  418. watchdog('t_featuremap', "Error updating map", array(), WATCHDOG_ERROR);
  419. return;
  420. }
  421. // now update the properties
  422. $properties = array(); // stores all of the properties we need to add
  423. $cross_refs = array(); // stores any cross references for this map
  424. // get the list of properties for easy lookup (without doing lots of database queries
  425. $properties_list = array();
  426. $sql = "
  427. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  428. FROM {cvterm} CVT
  429. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  430. WHERE
  431. CV.name = 'featuremap_property' AND
  432. NOT CVT.is_obsolete = 1
  433. ORDER BY CVT.name ASC
  434. ";
  435. $prop_types = chado_query($sql);
  436. while ($prop = db_fetch_object($prop_types)) {
  437. $properties_list[$prop->cvterm_id] = $prop->name;
  438. }
  439. // get the properties that should be added. Properties are in one of three forms:
  440. // 1) prop_value-[type id]-[index]
  441. // 2) new_value-[type id]-[index]
  442. // 3) new_id, new_value
  443. // dpm($node);
  444. foreach ($node as $key => $value) {
  445. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  446. $type_id = $matches[1];
  447. $index = $matches[2];
  448. $name = $properties_list[$type_id];
  449. $properties[$name][$index] = trim($value);
  450. }
  451. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  452. $type_id = $matches[1];
  453. $index = $matches[2];
  454. $name = $properties_list[$type_id];
  455. $properties[$name][$index] = trim($value);
  456. }
  457. }
  458. if ($node->new_id and $node->new_value) {
  459. $type_id = $node->new_id;
  460. $name = $properties_list[$type_id];
  461. $index = count($properties[$name]);
  462. $properties[$name][$index] = trim($node->new_value);
  463. }
  464. // iterate through all of the properties to see if the Map dbxref is set,
  465. // if so, add it to the $cross_refs array
  466. foreach ($properties as $name => $element) {
  467. foreach ($element as $index => $value) {
  468. if ($name == "Map Dbxref") {
  469. // we will add the cross-references to the featuremap_dbxref table
  470. // but we also want to keep the property in the featuremapprop table so don't unset it
  471. $cross_refs[] = $value;
  472. }
  473. }
  474. }
  475. // now add in the properties by first removing any the featuremap
  476. // already has and adding the ones we have
  477. tripal_core_chado_delete('featuremapprop', array('featuremap_id' => $featuremap_id));
  478. foreach ($properties as $property => $elements) {
  479. foreach ($elements as $rank => $value) {
  480. $status = tripal_featuremap_insert_property($featuremap_id, $property, $value, FALSE);
  481. if (!$status) {
  482. drupal_set_message("Error cannot add property: '$property'", "error");
  483. watchdog('t_featuremap', "Error cannot add property: '%prop'",
  484. array('%prop' => $property), WATCHDOG_ERROR);
  485. }
  486. }
  487. }
  488. // add in any database cross-references after first removing
  489. tripal_core_chado_delete('featuremap_dbxref', array('featuremap_id' => $featuremap_id));
  490. foreach ($cross_refs as $index => $ref) {
  491. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, trim($ref));
  492. if (!$featuremap_dbxref) {
  493. drupal_set_message("Error cannot add map cross reference: $ref", "error");
  494. watchdog('t_featuremap', "Error cannot add map cross reference: %ref",
  495. array('%ref' => $ref), WATCHDOG_ERROR);
  496. }
  497. }
  498. }
  499. /**
  500. * When a node is requested by the user this function is called to allow us
  501. * to add auxiliary data to the node object.
  502. *
  503. * @ingroup tripal_featuremap
  504. */
  505. function chado_featuremap_load($node) {
  506. // get the feature details from chado
  507. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  508. $values = array('featuremap_id' => $featuremap_id);
  509. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  510. // expand the description field as it is needed by the form
  511. $featuremap = tripal_core_expand_chado_vars($featuremap, 'field', 'featuremap.description');
  512. $additions = new stdClass();
  513. $additions->featuremap = $featuremap;
  514. return $additions;
  515. }
  516. /**
  517. * This function customizes the view of the chado_featuremap node. It allows
  518. * us to generate the markup. This function is required for node [Preview]
  519. *
  520. * @ingroup tripal_featuremap
  521. */
  522. function chado_featuremap_view($node, $teaser = FALSE, $page = FALSE) {
  523. // use drupal's default node view:
  524. if (!$teaser) {
  525. $node = node_prepare($node, $teaser);
  526. }
  527. return $node;
  528. }
  529. /**
  530. * Delete data from drupal and chado databases when a node is deleted
  531. * @ingroup tripal_featuremap
  532. */
  533. function chado_featuremap_delete(&$node) {
  534. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  535. // if we don't have a map id for this node then this isn't a node of
  536. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  537. if (!$featuremap_id) {
  538. return;
  539. }
  540. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  541. // drupal database
  542. $sql_del = "DELETE FROM {chado_featuremap} ".
  543. "WHERE nid = %d ".
  544. "AND vid = %d";
  545. db_query($sql_del, $node->nid, $node->vid);
  546. $sql_del = "DELETE FROM {node} ".
  547. "WHERE nid = %d ".
  548. "AND vid = %d";
  549. db_query($sql_del, $node->nid, $node->vid);
  550. $sql_del = "DELETE FROM {node_revisions} ".
  551. "WHERE nid = %d ".
  552. "AND vid = %d";
  553. db_query($sql_del, $node->nid, $node->vid);
  554. // Remove data from map and mapprop tables of chado database as well
  555. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = %d", $featuremap_id);
  556. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = %d", $featuremap_id);
  557. }
  558. /*
  559. *
  560. */
  561. function theme_tripal_featuremap_search_result($node) {
  562. }
  563. function tripal_featuremap_form_alter(&$form, &$form_state, $form_id) {
  564. if ($form_id == "chado_featuremap_node_form") {
  565. }
  566. }