tripal_featuremap.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. /**
  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_publication' => array(
  192. 'arguments' => array('node' => NULL),
  193. 'template' => 'tripal_featuremap_publication',
  194. ),
  195. 'tripal_featuremap_admin' => array(
  196. 'template' => 'tripal_featuremap_admin',
  197. 'arguments' => array(NULL),
  198. 'path' => drupal_get_path('module', 'tripal_featuremap') . '/theme'
  199. ),
  200. );
  201. }
  202. /**
  203. * This function is an extension of the chado_feature_view and
  204. * chado_organism_view by providing the markup for the map object
  205. * THAT WILL BE INDEXED.
  206. *
  207. * @ingroup tripal_featuremap
  208. */
  209. function theme_tripal_featuremap_search_index($node) {
  210. }
  211. /**
  212. *
  213. * @ingroup tripal_featuremap
  214. */
  215. function tripal_featuremap_cron() {
  216. }
  217. /**
  218. * The following function proves access control for users trying to
  219. * perform actions on data managed by this module
  220. *
  221. * @ingroup tripal_featuremap
  222. */
  223. function tripal_featuremap_map_access($op, $node, $account) {
  224. if ($op == 'create') {
  225. if (!user_access('create chado_featuremap content', $account)) {
  226. return FALSE;
  227. }
  228. }
  229. if ($op == 'update') {
  230. if (!user_access('edit any chado_featuremap content', $account) &&
  231. !user_access('edit own chado_featuremap content', $account)) {
  232. return FALSE;
  233. }
  234. if (user_access('edit own chado_featuremap content', $account) &&
  235. $account->uid != $node->uid) {
  236. return FALSE;
  237. }
  238. }
  239. if ($op == 'delete') {
  240. if (!user_access('delete any chado_featuremap content', $account) &&
  241. !user_access('delete own chado_featuremap content', $account)) {
  242. return FALSE;
  243. }
  244. if (user_access('delete own chado_featuremap content', $account) &&
  245. $account->uid != $node->uid) {
  246. return FALSE;
  247. }
  248. }
  249. return NULL;
  250. }
  251. /**
  252. * When editing or creating a new node of type 'chado_featuremap' we need
  253. * a form. This function creates the form that will be used for this.
  254. *
  255. * @ingroup tripal_featuremap
  256. */
  257. function chado_featuremap_form($node) {
  258. $type = node_get_types('type', $node);
  259. $form = array();
  260. $featuremap = $node->featuremap;
  261. // keep track of the map id if we have. If we do have one then
  262. // this is an update as opposed to an insert.
  263. $form['featuremap_id'] = array(
  264. '#type' => 'value',
  265. '#value' => $featuremap->featuremap_id,
  266. );
  267. $form['title']= array(
  268. '#type' => 'textfield',
  269. '#title' => t('Map Name'),
  270. '#description' => t('Please enter a name for this map'),
  271. '#required' => TRUE,
  272. '#default_value' => $featuremap->name,
  273. );
  274. $form['description']= array(
  275. '#type' => 'textarea',
  276. '#title' => t('Map Description'),
  277. '#description' => t('A description of the map.'),
  278. '#required' => TRUE,
  279. '#default_value' => $featuremap->description,
  280. );
  281. // get the list of unit types
  282. $values = array(
  283. 'cv_id' => array(
  284. 'name' => 'tripal_featuremap',
  285. )
  286. );
  287. $columns = array('cvterm_id','name');
  288. $options = array('order_by' => array('name' => 'ASC'));
  289. $featuremap_units = tripal_core_chado_select('cvterm', $columns, $values, $options);
  290. $units = array();
  291. $units[''] = '';
  292. foreach($featuremap_units as $unit) {
  293. $units[$unit->cvterm_id] = $unit->name;
  294. }
  295. $form['unittype_id'] = array(
  296. '#title' => t('Map Units'),
  297. '#type' => t('select'),
  298. '#description' => t("Chose the units for this map"),
  299. '#required' => TRUE,
  300. '#default_value' => $featuremap->unittype_id->cvterm_id,
  301. '#options' => $units,
  302. );
  303. return $form;
  304. }
  305. /**
  306. * validates submission of form when adding or updating a map node
  307. *
  308. * @ingroup tripal_featuremap
  309. */
  310. function chado_featuremap_validate($node) {
  311. $map = 0;
  312. // check to make sure the unique name on the map is unique
  313. // before we try to insert into chado. If this is an update then we will
  314. // have a featuremap_id, therefore we want to look for another map with this
  315. // name but with a different featuremap_id. If this is an insert, just look
  316. // for a case where the name already exists.
  317. if ($node->featuremap_id) {
  318. $sql = "SELECT * FROM ".
  319. "{featuremap} WHERE ".
  320. "name = '%s' ".
  321. "AND NOT featuremap_id = %d";
  322. $map = db_fetch_object(chado_query($sql, $node->title, $node->featuremap_id));
  323. }
  324. else {
  325. $sql = "SELECT * FROM ".
  326. "{featuremap} ".
  327. "WHERE name = '%s'";
  328. $map = db_fetch_object(chado_query($sql, $node->title));
  329. }
  330. if ($map) {
  331. form_set_error('name', t('The unique map name already exists. Please choose another'));
  332. }
  333. }
  334. /**
  335. * When a new chado_featuremap node is created we also need to add information
  336. * to our chado_featuremap table. This function is called on insert of a new node
  337. * of type 'chado_featuremap' and inserts the necessary information.
  338. *
  339. * @ingroup tripal_featuremap
  340. */
  341. function chado_featuremap_insert($node) {
  342. if ($node->featuremap_id) {
  343. $featuremap['featuremap_id'] = $node->featuremap_id;
  344. }
  345. else {
  346. $values = array(
  347. 'name' => $node->title,
  348. 'description' => $node->description,
  349. 'unittype_id' => $node->unittype_id
  350. );
  351. $featuremap = tripal_core_chado_insert('featuremap', $values);
  352. }
  353. if ($featuremap) {
  354. // make sure the entry for this feature doesn't already exist in the chado_featuremap table
  355. // if it doesn't exist then we want to add it.
  356. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  357. if (!$featuremap_id) {
  358. // next add the item to the drupal table
  359. $sql = "INSERT INTO {chado_featuremap} (nid, vid, featuremap_id) ".
  360. "VALUES (%d, %d, %d)";
  361. db_query($sql, $node->nid, $node->vid, $featuremap['featuremap_id']);
  362. }
  363. }
  364. else {
  365. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  366. watchdog('tripal_featuremap',
  367. 'Insert feature: Unable to create featuremap where values: %values',
  368. array('%values' => print_r($values, TRUE)),
  369. WATCHDOG_WARNING
  370. );
  371. }
  372. }
  373. /**
  374. * Update nodes
  375. *
  376. * @ingroup tripal_featuremap
  377. */
  378. function chado_featuremap_update($node) {
  379. if ($node->revision) {
  380. // there is no way to handle revisions in Chado but leave
  381. // this here just to make not we've addressed it.
  382. }
  383. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  384. // update the map record
  385. $match = array(
  386. 'featuremap_id' => $featuremap_id,
  387. );
  388. $values = array(
  389. 'name' => $node->title,
  390. 'unittype_id' => $node->unittype_id
  391. );
  392. $status = tripal_core_chado_update('featuremap', $match, $values);
  393. }
  394. /**
  395. * When a node is requested by the user this function is called to allow us
  396. * to add auxiliary data to the node object.
  397. *
  398. * @ingroup tripal_featuremap
  399. */
  400. function chado_featuremap_load($node) {
  401. // get the feature details from chado
  402. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  403. $values = array('featuremap_id' => $featuremap_id);
  404. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  405. $additions = new stdClass();
  406. $additions->featuremap = $featuremap;
  407. return $additions;
  408. }
  409. /**
  410. * This function customizes the view of the chado_featuremap node. It allows
  411. * us to generate the markup. This function is required for node [Preview]
  412. *
  413. * @ingroup tripal_featuremap
  414. */
  415. function chado_featuremap_view($node, $teaser = FALSE, $page = FALSE) {
  416. // use drupal's default node view:
  417. if (!$teaser) {
  418. $node = node_prepare($node, $teaser);
  419. }
  420. return $node;
  421. }
  422. /**
  423. * Delete data from drupal and chado databases when a node is deleted
  424. * @ingroup tripal_featuremap
  425. */
  426. function chado_featuremap_delete(&$node) {
  427. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  428. // if we don't have a map id for this node then this isn't a node of
  429. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  430. if (!$featuremap_id) {
  431. return;
  432. }
  433. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  434. // drupal database
  435. $sql_del = "DELETE FROM {chado_featuremap} ".
  436. "WHERE nid = %d ".
  437. "AND vid = %d";
  438. db_query($sql_del, $node->nid, $node->vid);
  439. $sql_del = "DELETE FROM {node} ".
  440. "WHERE nid = %d ".
  441. "AND vid = %d";
  442. db_query($sql_del, $node->nid, $node->vid);
  443. $sql_del = "DELETE FROM {node_revisions} ".
  444. "WHERE nid = %d ".
  445. "AND vid = %d";
  446. db_query($sql_del, $node->nid, $node->vid);
  447. // Remove data from map and mapprop tables of chado database as well
  448. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = %d", $featuremap_id);
  449. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = %d", $featuremap_id);
  450. }
  451. /*
  452. *
  453. */
  454. function theme_tripal_featuremap_search_result($node) {
  455. }