tripal_stock.module 16 KB

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