tripal_stock.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * @file
  4. * Basic functionality for stocks
  5. */
  6. require_once 'api/tripal_stock.DEPRECATED.inc';
  7. require_once 'theme/tripal_stock.theme.inc';
  8. require_once 'includes/tripal_stock.admin.inc';
  9. require_once 'includes/tripal_stock.chado_node.inc';
  10. /**
  11. * @defgroup tripal_stock Stock Module
  12. * @ingroup tripal_modules
  13. * @{
  14. * Integrates the Chado Stock module with Drupal Nodes & Views
  15. *
  16. * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado
  17. * stocks. The stock module was designed to store information about stock collections in a
  18. * laboratory. What is called a stock could also be called a strain or an accession. There is a lot
  19. * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all
  20. * come from some taxon, have genotypes, physical locations in the lab, some conceivable
  21. * relationship with a publication, some conceivable relationship with a sequence feature (such as a
  22. * transgene), and could be described by some ontology term. For more information about the chado
  23. * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
  24. * @}
  25. */
  26. /**
  27. * Implements hook_menu().
  28. * Adds menu items for the tripal_stock
  29. *
  30. * @return
  31. * Menu definitions for the tripal_stock
  32. *
  33. * @ingroup tripal_stock
  34. */
  35. function tripal_stock_menu() {
  36. $items = array();
  37. // the menu link for addressing any stock (by name, uniquename, synonym)
  38. $items['stock/%'] = array(
  39. 'page callback' => 'tripal_stock_match_stocks_page',
  40. 'page arguments' => array(1),
  41. 'access arguments' => array('access chado_stock content'),
  42. 'type' => MENU_LOCAL_TASK,
  43. );
  44. //Administrative settings menu-----------------
  45. /*
  46. $items['admin/tripal/chado/tripal_stock'] = array(
  47. 'title' => 'Stocks',
  48. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  49. 'page callback' => 'tripal_stock_admin_stock_view',
  50. 'access arguments' => array('administer tripal stock'),
  51. 'type' => MENU_NORMAL_ITEM
  52. );
  53. $items['admin/tripal/chado/tripal_stock/configuration'] = array(
  54. 'title' => 'Settings',
  55. 'description' => 'Settings for Chado Stocks',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('tripal_stock_admin'),
  58. 'access arguments' => array('administer tripal stock'),
  59. 'type' => MENU_LOCAL_TASK,
  60. 'weight' => 5
  61. );
  62. $items['admin/tripal/chado/tripal_stock/sync'] = array(
  63. 'title' => ' Sync',
  64. 'description' => 'Sync stocks from Chado with Drupal',
  65. 'page callback' => 'drupal_get_form',
  66. //'page arguments' => array('tripal_stock_sync_form'),
  67. 'page arguments' => array('chado_node_sync_form', 'tripal_stock', 'chado_stock'),
  68. 'access arguments' => array('administer tripal stock'),
  69. 'type' => MENU_LOCAL_TASK,
  70. 'weight' => 0
  71. );
  72. $items['admin/tripal/chado/tripal_stock/chado_stock_toc'] = array(
  73. 'title' => ' TOC',
  74. 'description' => 'Manage the table of contents for stock nodes.',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('tripal_core_content_type_toc_form', 'chado_stock'),
  77. 'access arguments' => array('administer tripal stock'),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'file' => 'includes/tripal_core.toc.inc',
  80. 'file path' => drupal_get_path('module', 'tripal_core'),
  81. 'weight' => 3
  82. );
  83. $items['admin/tripal/chado/tripal_stock/help'] = array(
  84. 'title' => 'Help',
  85. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  86. 'page callback' => 'theme',
  87. 'page arguments' => array('tripal_stock_help'),
  88. 'access arguments' => array('administer tripal stock'),
  89. 'type' => MENU_LOCAL_TASK,
  90. 'weight' => 10
  91. );
  92. */
  93. return $items;
  94. }
  95. /**
  96. * Implements hook_search_biological_data_views().
  97. *
  98. * Adds the described views to the "Search Data" Page created by Tripal Views
  99. */
  100. function tripal_stock_search_biological_data_views() {
  101. return array(
  102. 'tripal_stock_user_stocks' => array(
  103. 'machine_name' => 'tripal_stock_user_stocks',
  104. 'human_name' => 'Stocks',
  105. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  106. 'link' => 'chado/stock'
  107. ),
  108. );
  109. }
  110. /**
  111. * Implements Menu wildcard_load hook
  112. *
  113. * Allows the node ID of a chado stock to be dynamically
  114. * pulled from the path. The node is loaded from this node ID
  115. * and supplied to the page as an arguement. This is an example
  116. * of dynamic argument replacement using wildcards in the path.
  117. *
  118. * @param $nid
  119. * The node ID passed in from the path
  120. *
  121. * @return
  122. * The node object with the passed in nid
  123. *
  124. * @ingroup tripal_stock
  125. */
  126. function cs_node_load($nid) {
  127. if (is_numeric($nid)) {
  128. $node = node_load($nid);
  129. if ($node->type == 'chado_stock') {
  130. return $node;
  131. }
  132. }
  133. return FALSE;
  134. }
  135. /**
  136. * Implements hook_permission().
  137. * Set the permission types that the chado stock module uses
  138. *
  139. * @return
  140. * Listing of the possible permission catagories
  141. *
  142. * @ingroup tripal_stock
  143. */
  144. function tripal_stock_permission() {
  145. return array(
  146. /*
  147. 'access chado_stock content' => array(
  148. 'title' => t('View Stocks'),
  149. 'description' => t('Allow users to view stock pages.'),
  150. ),
  151. 'create chado_stock content' => array(
  152. 'title' => t('Create Stocks'),
  153. 'description' => t('Allow users to create new stock pages.'),
  154. ),
  155. 'delete chado_stock content' => array(
  156. 'title' => t('Delete Stocks'),
  157. 'description' => t('Allow users to delete stock pages.'),
  158. ),
  159. 'edit chado_stock content' => array(
  160. 'title' => t('Edit Stocks'),
  161. 'description' => t('Allow users to edit stock pages.'),
  162. ),
  163. 'administer tripal stock' => array(
  164. 'title' => t('Administer Stocks'),
  165. 'description' => t('Allow users to administer all stocks.'),
  166. ),
  167. */
  168. );
  169. }
  170. /**
  171. * Implement hook_node_access().
  172. *
  173. * This hook allows node modules to limit access to the node types they define.
  174. *
  175. * @param $node
  176. * The node on which the operation is to be performed, or, if it does not yet exist, the
  177. * type of node to be created
  178. *
  179. * @param $op
  180. * The operation to be performed
  181. *
  182. * @param $account
  183. * A user object representing the user for whom the operation is to be performed
  184. *
  185. * @return
  186. * If the permission for the specified operation is not set then return FALSE. If the
  187. * permission is set then return NULL as this allows other modules to disable
  188. * access. The only exception is when the $op == 'create'. We will always
  189. * return TRUE if the permission is set.
  190. *
  191. * @ingroup tripal_stock
  192. */
  193. function tripal_stock_node_access($node, $op, $account) {
  194. $node_type = $node;
  195. if (is_object($node)) {
  196. $node_type = $node->type;
  197. }
  198. if($node_type == 'chado_stock') {
  199. if ($op == 'create') {
  200. if (!user_access('create chado_stock content', $account)) {
  201. return NODE_ACCESS_DENY;
  202. }
  203. return NODE_ACCESS_ALLOW;
  204. }
  205. if ($op == 'update') {
  206. if (!user_access('edit chado_stock content', $account)) {
  207. return NODE_ACCESS_DENY;
  208. }
  209. }
  210. if ($op == 'delete') {
  211. if (!user_access('delete chado_stock content', $account)) {
  212. return NODE_ACCESS_DENY;
  213. }
  214. }
  215. if ($op == 'view') {
  216. if (!user_access('access chado_stock content', $account)) {
  217. return NODE_ACCESS_DENY;
  218. }
  219. }
  220. return NODE_ACCESS_IGNORE;
  221. }
  222. }
  223. /**
  224. * Implements hook_views_api().
  225. *
  226. * Purpose: Essentially this hook tells drupal that there is views support for
  227. * for this module which then includes tripal_stock.views.inc where all the
  228. * views integration code is
  229. *
  230. * @return
  231. * An array with fields important for views integration
  232. *
  233. * @ingroup tripal_stock
  234. */
  235. function tripal_stock_views_api() {
  236. return array(
  237. 'api' => 3.0,
  238. );
  239. }
  240. /**
  241. * Implements hook_theme().
  242. * Register themeing functions for this module
  243. *
  244. * @return
  245. * An array of themeing functions to register
  246. *
  247. * @ingroup tripal_stock
  248. */
  249. function tripal_stock_theme($existing, $type, $theme, $path) {
  250. $core_path = drupal_get_path('module', 'tripal_core');
  251. $items = array(
  252. // tripal_stock templates
  253. 'node__chado_stock' => array(
  254. 'template' => 'node--chado-generic',
  255. 'render element' => 'node',
  256. 'base hook' => 'node',
  257. 'path' => "$core_path/theme/templates",
  258. ),
  259. 'tripal_stock_base' => array(
  260. 'variables' => array('node' => NULL),
  261. 'template' => 'tripal_stock_base',
  262. 'path' => "$path/theme/templates",
  263. ),
  264. 'tripal_stock_properties' => array(
  265. 'variables' => array('node' => NULL),
  266. 'template' => 'tripal_stock_properties',
  267. 'path' => "$path/theme/templates",
  268. ),
  269. 'tripal_stock_publications' => array(
  270. 'variables' => array('node' => NULL),
  271. 'template' => 'tripal_stock_publications',
  272. 'path' => "$path/theme/templates",
  273. ),
  274. 'tripal_stock_references' => array(
  275. 'variables' => array('node' => NULL),
  276. 'template' => 'tripal_stock_references',
  277. 'path' => "$path/theme/templates",
  278. ),
  279. 'tripal_stock_relationships' => array(
  280. 'variables' => array('node' => NULL),
  281. 'template' => 'tripal_stock_relationships',
  282. 'path' => "$path/theme/templates",
  283. ),
  284. 'tripal_stock_synonyms' => array(
  285. 'variables' => array('node' => NULL),
  286. 'template' => 'tripal_stock_synonyms',
  287. 'path' => "$path/theme/templates",
  288. ),
  289. 'tripal_stock_collections' => array(
  290. 'variables' => array('node' => NULL),
  291. 'template' => 'tripal_stock_collections',
  292. 'path' => "$path/theme/templates",
  293. ),
  294. 'tripal_stock_collections' => array(
  295. 'variables' => array('node' => NULL),
  296. 'template' => 'tripal_stock_collections',
  297. 'path' => "$path/theme/templates",
  298. ),
  299. 'tripal_stock_phenotypes' => array(
  300. 'variables' => array('node' => NULL),
  301. 'template' => 'tripal_stock_phenotypes',
  302. 'path' => "$path/theme/templates",
  303. ),
  304. 'tripal_stock_locations' => array(
  305. 'variables' => array('node' => NULL),
  306. 'template' => 'tripal_stock_locations',
  307. 'path' => "$path/theme/templates",
  308. ),
  309. // tripal_organism templates
  310. 'tripal_organism_stocks' => array(
  311. 'variables' => array('node' => NULL),
  312. 'template' => 'tripal_organism_stocks',
  313. 'path' => "$path/theme/templates",
  314. ),
  315. // help template
  316. 'tripal_stock_help' => array(
  317. 'template' => 'tripal_stock_help',
  318. 'variables' => array(NULL),
  319. 'path' => "$path/theme/templates",
  320. ),
  321. // themed teaser
  322. 'tripal_stock_teaser' => array(
  323. 'variables' => array('node' => NULL),
  324. 'template' => 'tripal_stock_teaser',
  325. 'path' => "$path/theme/templates",
  326. ),
  327. );
  328. return $items;
  329. }
  330. /**
  331. * Implements hook_help().
  332. * Adds a help page to the module list
  333. *
  334. * @ingroup tripal_stock
  335. */
  336. function tripal_stock_help ($path, $arg) {
  337. if ($path == 'admin/help#tripal_stock') {
  338. return theme('tripal_stock_help', array());
  339. }
  340. }
  341. /*
  342. * Uses the value provided in the $id argument to find all stocks that match
  343. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  344. * stock it will redirect to that stock page, otherwise, a list of matching
  345. * stocks is shown.
  346. *
  347. * @ingroup tripal_stock
  348. */
  349. function tripal_stock_match_stocks_page($id) {
  350. // if the URL alias configuration is set such that the URL
  351. // always begins with 'stock' then we want to use the ID as it is and
  352. // forward it on. Otherwise, try to find the matching stock.
  353. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  354. if (!$url_alias) {
  355. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  356. }
  357. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  358. if (preg_match('/^stock\//', $url_alias)) {
  359. drupal_goto($id);
  360. }
  361. $sql = "
  362. SELECT
  363. S.name, S.uniquename, S.stock_id,
  364. O.genus, O.species, O.organism_id,
  365. CVT.cvterm_id, CVT.name as type_name,
  366. CS.nid
  367. FROM {stock} S
  368. INNER JOIN {organism} O on S.organism_id = O.organism_id
  369. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  370. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  371. WHERE
  372. S.uniquename = :uname or S.name = :name
  373. ";
  374. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  375. $num_matches = 0;
  376. // iterate through the matches and build the table for showing matches
  377. $header = array('Uniquename', 'Name', 'Type', 'Species');
  378. $rows = array();
  379. $curr_match;
  380. while ($match = $results->fetchObject()) {
  381. $curr_match = $match;
  382. $rows[] = array(
  383. $match->uniquename,
  384. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  385. $match->type_name,
  386. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  387. );
  388. $num_matches++;
  389. }
  390. // if we have more than one match then generate the table, otherwise, redirect
  391. // to the matched stock
  392. if ($num_matches == 1) {
  393. drupal_goto("node/" . $curr_match->nid);
  394. }
  395. if ($num_matches == 0) {
  396. return "<p>No stocks matched the given name '$id'</p>";
  397. }
  398. $table_attrs = array(
  399. 'class' => 'tripal-table tripal-table-horz'
  400. );
  401. $output = "<p>The following stocks match the name '$id'.</p>";
  402. $output .= theme_table($header, $rows, $table_attrs, $caption);
  403. return $output;
  404. }
  405. /**
  406. * Implementation of hook_form_alter().
  407. *
  408. * @param $form
  409. * @param $form_state
  410. * @param $form_id
  411. *
  412. * @ingroup tripal_stock
  413. */
  414. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  415. // turn of preview button for insert/updates
  416. if ($form_id == "chado_stock_node_form") {
  417. $form['actions']['preview']['#access'] = FALSE;
  418. //remove the body field
  419. unset($form['body']);
  420. }
  421. }
  422. /**
  423. * Load the arguments for the organism stock counts browser
  424. *
  425. * @param $organism
  426. * The organism of interest
  427. *
  428. * @ingroup tripal_stock
  429. */
  430. function tripal_stock_load_organism_stock_counts($organism) {
  431. $args = array();
  432. $order = array();
  433. $names = array();
  434. // build the where clause for the SQL statement if we have a custom term list
  435. // we'll also keep track of the names the admin provided (if any) and the
  436. // order that the terms should appear.
  437. $is_custom = 0;
  438. $temp = rtrim(variable_get('tripal_stock_summary_report_mapping', ''));
  439. $where = '';
  440. if ($temp) {
  441. $is_custom = 1;
  442. $temp = explode("\n", $temp);
  443. $i = 0;
  444. foreach ($temp as $value) {
  445. // separate the key value pairs
  446. $temp2 = explode("=", $value);
  447. $stock_type = rtrim($temp2[0]);
  448. $order[] = $stock_type; // save the order of the these terms
  449. $where .= " OFC.stock_type = :name$i OR ";
  450. $args[":name$i"] = rtrim($temp2[0]);
  451. // if the admin specified a new name then store that otherwise use the
  452. // the default sequence ontology term name
  453. if(count($temp2) == 2) {
  454. $names[] = rtrim($temp2[1]);
  455. }
  456. else {
  457. $names[] = $stock_type;
  458. }
  459. $i++;
  460. }
  461. if ($where) {
  462. $where = drupal_substr($where, 0, -4); # remove OR from the end
  463. $where = "($where) AND";
  464. }
  465. }
  466. // get the stock counts. This is dependent on a materialized view
  467. // installed with the organism module
  468. $sql = "
  469. SELECT OFC.num_stocks,OFC.stock_type,CVT.definition
  470. FROM {organism_stock_count} OFC
  471. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  472. WHERE $where organism_id = :organism_id
  473. ORDER BY num_stocks desc
  474. ";
  475. $args[':organism_id'] = $organism->organism_id;
  476. $org_stocks = chado_query($sql, $args);
  477. // iterate through the types
  478. $types = array();
  479. while ($type = $org_stocks->fetchObject()) {
  480. $types[$type->stock_type] = $type;
  481. // if we don't have an order this means we didn't go through the loop
  482. // above to set the names, so do that now
  483. if (!$is_custom) {
  484. $names[] = $type->stock_type;
  485. $order[] = $type->stock_type;
  486. }
  487. }
  488. // now reorder the types
  489. $ordered_types = array();
  490. foreach ($order as $type) {
  491. $ordered_types[] = $types[$type];
  492. }
  493. return array(
  494. 'types' => $ordered_types,
  495. 'names' => $names
  496. );
  497. }