tripal_featuremap.module 20 KB

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