tripal_featuremap.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /**
  3. * @defgroup tripal_featuremap Map
  4. * @{
  5. * Provides functions for managing chado maps including creating details pages for each map
  6. * @}
  7. * @ingroup tripal_modules
  8. */
  9. require('api/tripal_featuremap.api.inc');
  10. require('includes/tripal_featuremap.admin.inc');
  11. /**
  12. * Display help and module information
  13. * @param path which path of the site we're displaying help
  14. * @param arg array that holds the current path as would be returned from arg()
  15. * function
  16. * @return help text for the path
  17. *
  18. * @ingroup tripal_featuremap
  19. */
  20. function tripal_featuremap_help($path, $arg) {
  21. $output = '';
  22. switch ($path) {
  23. case "admin/help#tripal_featuremap":
  24. $output = '<p>'.
  25. t("Displays links to nodes created on this date") .
  26. '</p>';
  27. break;
  28. }
  29. return $output;
  30. }
  31. /**
  32. * Provide information to drupal about the node types that we're creating
  33. * in this module
  34. *
  35. * @ingroup tripal_featuremap
  36. */
  37. function tripal_featuremap_node_info() {
  38. $nodes = array();
  39. $nodes['chado_featuremap'] = array(
  40. 'name' => t('Map'),
  41. 'module' => 'chado_featuremap',
  42. 'description' => t('A feature map from the chado database (e.g. genetic map)'),
  43. 'has_title' => FALSE,
  44. 'title_label' => t('Feature Map'),
  45. 'has_body' => FALSE,
  46. 'body_label' => t('Feature Map Description'),
  47. 'locked' => TRUE
  48. );
  49. return $nodes;
  50. }
  51. /**
  52. * Set the permission types that the chado module uses. Essentially we
  53. * want permissionis that protect creation, editing and deleting of chado
  54. * data objects
  55. *
  56. * @ingroup tripal_featuremap
  57. */
  58. function tripal_featuremap_perm() {
  59. return array(
  60. 'access chado_featuremap content',
  61. 'create chado_featuremap content',
  62. 'delete chado_featuremap content',
  63. 'edit chado_featuremap content',
  64. 'administer tripal featuremap',
  65. );
  66. }
  67. /**
  68. * Set the permission types that the module uses.
  69. *
  70. * @ingroup tripal_featuremap
  71. */
  72. function chado_featuremap_access($op, $node, $account) {
  73. if ($op == 'create') {
  74. if (!user_access('create chado_featuremap content', $account)) {
  75. return FALSE;
  76. }
  77. }
  78. if ($op == 'update') {
  79. if (!user_access('edit chado_featuremap content', $account)) {
  80. return FALSE;
  81. }
  82. }
  83. if ($op == 'delete') {
  84. if (!user_access('delete chado_featuremap content', $account)) {
  85. return FALSE;
  86. }
  87. }
  88. if ($op == 'view') {
  89. if (!user_access('access chado_featuremap content', $account)) {
  90. return FALSE;
  91. }
  92. }
  93. return NULL;
  94. }
  95. /**
  96. * Menu items are automatically added for the new node types created
  97. * by this module to the 'Create Content' Navigation menu item. This function
  98. * adds more menu items needed for this module.
  99. *
  100. * @ingroup tripal_featuremap
  101. */
  102. function tripal_featuremap_menu() {
  103. $items = array();
  104. // The administative settings menu
  105. $items['admin/tripal/tripal_featuremap'] = array(
  106. 'title' => 'Maps',
  107. 'description' => 'Basic Description of Tripal Map Module Functionality',
  108. 'page callback' => 'theme',
  109. 'page arguments' => array('tripal_featuremap_admin'),
  110. 'access arguments' => array('administer tripal featuremap'),
  111. 'type' => MENU_NORMAL_ITEM,
  112. );
  113. $items['admin/tripal/tripal_featuremap/configuration'] = array(
  114. 'title' => 'Configuration',
  115. 'description' => 'Manage integration of Chado maps including associated features.',
  116. 'page callback' => 'drupal_get_form',
  117. 'page arguments' => array('tripal_featuremap_admin'),
  118. 'access arguments' => array('administer tripal featuremap'),
  119. 'type' => MENU_NORMAL_ITEM,
  120. );
  121. // Synchronizing maps from Chado to Drupal
  122. $items['chado_sync_featuremaps'] = array(
  123. 'title' => 'Sync Data',
  124. 'page callback' => 'tripal_featuremap_sync_featuremaps',
  125. 'access arguments' => array('administer tripal featuremap'),
  126. 'type' => MENU_CALLBACK
  127. );
  128. return $items;
  129. }
  130. /**
  131. * Implements hook_views_api()
  132. * Purpose: Essentially this hook tells drupal that there is views support for
  133. * for this module which then includes tripal_db.views.inc where all the
  134. * views integration code is
  135. *
  136. * @ingroup tripal_featuremap
  137. */
  138. function tripal_featuremap_views_api() {
  139. return array(
  140. 'api' => 2.0,
  141. );
  142. }
  143. /**
  144. * Implementation of hook_nodeapi().
  145. * Display map information for associated features or organisms
  146. * This function also provides contents for indexing
  147. *
  148. * @ingroup tripal_featuremap
  149. */
  150. function tripal_featuremap_nodeapi(&$node, $op, $teaser, $page) {
  151. switch ($op) {
  152. // Note that this function only adds map view to an organism/feature
  153. // node.
  154. case 'view':
  155. // add the map to the organism/feature search indexing
  156. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  157. $node->content['tripal_featuremap_index_version'] = array(
  158. '#value' => theme('tripal_featuremap_search_index', $node),
  159. );
  160. }
  161. elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  162. $node->content['tripal_featuremap_index_version'] = array(
  163. '#value' => theme('tripal_featuremap_search_result', $node),
  164. );
  165. }
  166. }
  167. }
  168. /**
  169. * We need to let drupal know about our theme functions and their arguments.
  170. * We create theme functions to allow users of the module to customize the
  171. * look and feel of the output generated in this module
  172. *
  173. * @ingroup tripal_featuremap
  174. */
  175. function tripal_featuremap_theme() {
  176. return array(
  177. 'tripal_featuremap_search_index' => array(
  178. 'arguments' => array('node'),
  179. ),
  180. 'tripal_featuremap_search_result' => array(
  181. 'arguments' => array('node'),
  182. ),
  183. 'tripal_featuremap_base' => array(
  184. 'arguments' => array('node' => NULL),
  185. 'template' => 'tripal_featuremap_base',
  186. ),
  187. 'tripal_featuremap_properties' => array(
  188. 'arguments' => array('node' => NULL),
  189. 'template' => 'tripal_featuremap_properties',
  190. ),
  191. 'tripal_featuremap_admin' => array(
  192. 'template' => 'tripal_featuremap_admin',
  193. 'arguments' => array(NULL),
  194. 'path' => drupal_get_path('module', 'tripal_featuremap') . '/theme'
  195. ),
  196. );
  197. }
  198. /**
  199. * This function is an extension of the chado_feature_view and
  200. * chado_organism_view by providing the markup for the map object
  201. * THAT WILL BE INDEXED.
  202. *
  203. * @ingroup tripal_featuremap
  204. */
  205. function theme_tripal_featuremap_search_index($node) {
  206. }
  207. /**
  208. *
  209. * @ingroup tripal_featuremap
  210. */
  211. function tripal_featuremap_cron() {
  212. }
  213. /**
  214. * The following function proves access control for users trying to
  215. * perform actions on data managed by this module
  216. *
  217. * @ingroup tripal_featuremap
  218. */
  219. function tripal_featuremap_map_access($op, $node, $account) {
  220. if ($op == 'create') {
  221. if (!user_access('create chado_featuremap content', $account)) {
  222. return FALSE;
  223. }
  224. }
  225. if ($op == 'update') {
  226. if (!user_access('edit any chado_featuremap content', $account) &&
  227. !user_access('edit own chado_featuremap content', $account)) {
  228. return FALSE;
  229. }
  230. if (user_access('edit own chado_featuremap content', $account) &&
  231. $account->uid != $node->uid) {
  232. return FALSE;
  233. }
  234. }
  235. if ($op == 'delete') {
  236. if (!user_access('delete any chado_featuremap content', $account) &&
  237. !user_access('delete own chado_featuremap content', $account)) {
  238. return FALSE;
  239. }
  240. if (user_access('delete own chado_featuremap content', $account) &&
  241. $account->uid != $node->uid) {
  242. return FALSE;
  243. }
  244. }
  245. return NULL;
  246. }
  247. /**
  248. * When editing or creating a new node of type 'chado_featuremap' we need
  249. * a form. This function creates the form that will be used for this.
  250. *
  251. * @ingroup tripal_featuremap
  252. */
  253. function chado_featuremap_form($node) {
  254. $type = node_get_types('type', $node);
  255. $form = array();
  256. $featuremap = $node->featuremap;
  257. // keep track of the map id if we have. If we do have one then
  258. // this is an update as opposed to an insert.
  259. $form['featuremap_id'] = array(
  260. '#type' => 'value',
  261. '#value' => $featuremap->featuremap_id,
  262. );
  263. $form['title']= array(
  264. '#type' => 'textfield',
  265. '#title' => t('Map Name'),
  266. '#description' => t('Please enter a name for this map'),
  267. '#required' => TRUE,
  268. '#default_value' => $featuremap->name,
  269. );
  270. $form['description']= array(
  271. '#type' => 'textarea',
  272. '#title' => t('Map Description'),
  273. '#description' => t('A description of the map.'),
  274. '#required' => TRUE,
  275. '#default_value' => $featuremap->description,
  276. );
  277. // get the list of unit types
  278. $values = array(
  279. 'cv_id' => array(
  280. 'name' => 'tripal_featuremap',
  281. )
  282. );
  283. $columns = array('cvterm_id','name');
  284. $options = array('order_by' => array('name' => 'ASC'));
  285. $featuremap_units = tripal_core_chado_select('cvterm', $columns, $values, $options);
  286. $units = array();
  287. $units[''] = '';
  288. foreach($featuremap_units as $unit) {
  289. $units[$unit->cvterm_id] = $unit->name;
  290. }
  291. $form['unittype_id'] = array(
  292. '#title' => t('Map Units'),
  293. '#type' => t('select'),
  294. '#description' => t("Chose the units for this map"),
  295. '#required' => TRUE,
  296. '#default_value' => $featuremap->unittype_id->cvterm_id,
  297. '#options' => $units,
  298. );
  299. return $form;
  300. }
  301. /**
  302. * validates submission of form when adding or updating a map node
  303. *
  304. * @ingroup tripal_featuremap
  305. */
  306. function chado_featuremap_validate($node) {
  307. $map = 0;
  308. // check to make sure the unique name on the map is unique
  309. // before we try to insert into chado. If this is an update then we will
  310. // have a featuremap_id, therefore we want to look for another map with this
  311. // name but with a different featuremap_id. If this is an insert, just look
  312. // for a case where the name already exists.
  313. if ($node->featuremap_id) {
  314. $sql = "SELECT * FROM ".
  315. "{featuremap} WHERE ".
  316. "name = '%s' ".
  317. "AND NOT featuremap_id = %d";
  318. $map = db_fetch_object(chado_query($sql, $node->title, $node->featuremap_id));
  319. }
  320. else {
  321. $sql = "SELECT * FROM ".
  322. "{featuremap} ".
  323. "WHERE name = '%s'";
  324. $map = db_fetch_object(chado_query($sql, $node->title));
  325. }
  326. if ($map) {
  327. form_set_error('name', t('The unique map name already exists. Please choose another'));
  328. }
  329. }
  330. /**
  331. * When a new chado_featuremap node is created we also need to add information
  332. * to our chado_featuremap table. This function is called on insert of a new node
  333. * of type 'chado_featuremap' and inserts the necessary information.
  334. *
  335. * @ingroup tripal_featuremap
  336. */
  337. function chado_featuremap_insert($node) {
  338. if ($node->featuremap_id) {
  339. $featuremap['featuremap_id'] = $node->featuremap_id;
  340. }
  341. else {
  342. $values = array(
  343. 'name' => $node->title,
  344. 'description' => $node->description,
  345. 'unittype_id' => $node->unittype_id
  346. );
  347. $featuremap = tripal_core_chado_insert('featuremap', $values);
  348. }
  349. if ($featuremap) {
  350. // make sure the entry for this feature doesn't already exist in the chado_featuremap table
  351. // if it doesn't exist then we want to add it.
  352. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  353. if (!$featuremap_id) {
  354. // next add the item to the drupal table
  355. $sql = "INSERT INTO {chado_featuremap} (nid, vid, featuremap_id) ".
  356. "VALUES (%d, %d, %d)";
  357. db_query($sql, $node->nid, $node->vid, $featuremap['featuremap_id']);
  358. }
  359. }
  360. else {
  361. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  362. watchdog('tripal_featuremap',
  363. 'Insert feature: Unable to create featuremap where values: %values',
  364. array('%values' => print_r($values, TRUE)),
  365. WATCHDOG_WARNING
  366. );
  367. }
  368. }
  369. /**
  370. * Update nodes
  371. *
  372. * @ingroup tripal_featuremap
  373. */
  374. function chado_featuremap_update($node) {
  375. if ($node->revision) {
  376. // there is no way to handle revisions in Chado but leave
  377. // this here just to make not we've addressed it.
  378. }
  379. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  380. // update the map record
  381. $match = array(
  382. 'featuremap_id' => $featuremap_id,
  383. );
  384. $values = array(
  385. 'name' => $node->title,
  386. 'unittype_id' => $node->unittype_id
  387. );
  388. $status = tripal_core_chado_update('featuremap', $match, $values);
  389. }
  390. /**
  391. * When a node is requested by the user this function is called to allow us
  392. * to add auxiliary data to the node object.
  393. *
  394. * @ingroup tripal_featuremap
  395. */
  396. function chado_featuremap_load($node) {
  397. // get the feature details from chado
  398. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  399. $values = array('featuremap_id' => $featuremap_id);
  400. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  401. $additions = new stdClass();
  402. $additions->featuremap = $featuremap;
  403. return $additions;
  404. }
  405. /**
  406. * This function customizes the view of the chado_featuremap node. It allows
  407. * us to generate the markup. This function is required for node [Preview]
  408. *
  409. * @ingroup tripal_featuremap
  410. */
  411. function chado_featuremap_view($node, $teaser = FALSE, $page = FALSE) {
  412. // use drupal's default node view:
  413. if (!$teaser) {
  414. $node = node_prepare($node, $teaser);
  415. }
  416. return $node;
  417. }
  418. /**
  419. * Delete data from drupal and chado databases when a node is deleted
  420. * @ingroup tripal_featuremap
  421. */
  422. function chado_featuremap_delete(&$node) {
  423. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  424. // if we don't have a map id for this node then this isn't a node of
  425. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  426. if (!$featuremap_id) {
  427. return;
  428. }
  429. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  430. // drupal database
  431. $sql_del = "DELETE FROM {chado_featuremap} ".
  432. "WHERE nid = %d ".
  433. "AND vid = %d";
  434. db_query($sql_del, $node->nid, $node->vid);
  435. $sql_del = "DELETE FROM {node} ".
  436. "WHERE nid = %d ".
  437. "AND vid = %d";
  438. db_query($sql_del, $node->nid, $node->vid);
  439. $sql_del = "DELETE FROM {node_revisions} ".
  440. "WHERE nid = %d ".
  441. "AND vid = %d";
  442. db_query($sql_del, $node->nid, $node->vid);
  443. // Remove data from map and mapprop tables of chado database as well
  444. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = %d", $featuremap_id);
  445. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = %d", $featuremap_id);
  446. }
  447. /*
  448. *
  449. */
  450. function theme_tripal_featuremap_search_result($node) {
  451. }