tripal_stock.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. /*
  194. function chado_stock_node_access($node, $op, $account) {
  195. if ($op == 'create') {
  196. if (!user_access('create chado_stock content', $account)) {
  197. return FALSE;
  198. }
  199. return TRUE;
  200. }
  201. if ($op == 'update') {
  202. if (!user_access('edit chado_stock content', $account)) {
  203. return FALSE;
  204. }
  205. }
  206. if ($op == 'delete') {
  207. if (!user_access('delete chado_stock content', $account)) {
  208. return FALSE;
  209. }
  210. }
  211. if ($op == 'view') {
  212. if (!user_access('access chado_stock content', $account)) {
  213. return FALSE;
  214. }
  215. }
  216. return NULL;
  217. }
  218. */
  219. /**
  220. * Implements hook_views_api().
  221. *
  222. * Purpose: Essentially this hook tells drupal that there is views support for
  223. * for this module which then includes tripal_stock.views.inc where all the
  224. * views integration code is
  225. *
  226. * @return
  227. * An array with fields important for views integration
  228. *
  229. * @ingroup tripal_stock
  230. */
  231. function tripal_stock_views_api() {
  232. return array(
  233. 'api' => 3.0,
  234. );
  235. }
  236. /**
  237. * Implements hook_theme().
  238. * Register themeing functions for this module
  239. *
  240. * @return
  241. * An array of themeing functions to register
  242. *
  243. * @ingroup tripal_stock
  244. */
  245. function tripal_stock_theme($existing, $type, $theme, $path) {
  246. $core_path = drupal_get_path('module', 'tripal_core');
  247. $items = array(
  248. // tripal_stock templates
  249. 'node__chado_stock' => array(
  250. 'template' => 'node--chado-generic',
  251. 'render element' => 'node',
  252. 'base hook' => 'node',
  253. 'path' => "$core_path/theme/templates",
  254. ),
  255. 'tripal_stock_base' => array(
  256. 'variables' => array('node' => NULL),
  257. 'template' => 'tripal_stock_base',
  258. 'path' => "$path/theme/templates",
  259. ),
  260. 'tripal_stock_properties' => array(
  261. 'variables' => array('node' => NULL),
  262. 'template' => 'tripal_stock_properties',
  263. 'path' => "$path/theme/templates",
  264. ),
  265. 'tripal_stock_publications' => array(
  266. 'variables' => array('node' => NULL),
  267. 'template' => 'tripal_stock_publications',
  268. 'path' => "$path/theme/templates",
  269. ),
  270. 'tripal_stock_references' => array(
  271. 'variables' => array('node' => NULL),
  272. 'template' => 'tripal_stock_references',
  273. 'path' => "$path/theme/templates",
  274. ),
  275. 'tripal_stock_relationships' => array(
  276. 'variables' => array('node' => NULL),
  277. 'template' => 'tripal_stock_relationships',
  278. 'path' => "$path/theme/templates",
  279. ),
  280. 'tripal_stock_synonyms' => array(
  281. 'variables' => array('node' => NULL),
  282. 'template' => 'tripal_stock_synonyms',
  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_collections' => array(
  291. 'variables' => array('node' => NULL),
  292. 'template' => 'tripal_stock_collections',
  293. 'path' => "$path/theme/templates",
  294. ),
  295. 'tripal_stock_phenotypes' => array(
  296. 'variables' => array('node' => NULL),
  297. 'template' => 'tripal_stock_phenotypes',
  298. 'path' => "$path/theme/templates",
  299. ),
  300. 'tripal_stock_locations' => array(
  301. 'variables' => array('node' => NULL),
  302. 'template' => 'tripal_stock_locations',
  303. 'path' => "$path/theme/templates",
  304. ),
  305. // tripal_organism templates
  306. 'tripal_organism_stocks' => array(
  307. 'variables' => array('node' => NULL),
  308. 'template' => 'tripal_organism_stocks',
  309. 'path' => "$path/theme/templates",
  310. ),
  311. // help template
  312. 'tripal_stock_help' => array(
  313. 'template' => 'tripal_stock_help',
  314. 'variables' => array(NULL),
  315. 'path' => "$path/theme/templates",
  316. ),
  317. // themed teaser
  318. 'tripal_stock_teaser' => array(
  319. 'variables' => array('node' => NULL),
  320. 'template' => 'tripal_stock_teaser',
  321. 'path' => "$path/theme/templates",
  322. ),
  323. );
  324. return $items;
  325. }
  326. /**
  327. * Implements hook_help().
  328. * Adds a help page to the module list
  329. *
  330. * @ingroup tripal_stock
  331. */
  332. function tripal_stock_help ($path, $arg) {
  333. if ($path == 'admin/help#tripal_stock') {
  334. return theme('tripal_stock_help', array());
  335. }
  336. }
  337. /*
  338. * Uses the value provided in the $id argument to find all stocks that match
  339. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  340. * stock it will redirect to that stock page, otherwise, a list of matching
  341. * stocks is shown.
  342. *
  343. * @ingroup tripal_stock
  344. */
  345. function tripal_stock_match_stocks_page($id) {
  346. // if the URL alias configuration is set such that the URL
  347. // always begins with 'stock' then we want to use the ID as it is and
  348. // forward it on. Otherwise, try to find the matching stock.
  349. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  350. if (!$url_alias) {
  351. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  352. }
  353. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  354. if (preg_match('/^stock\//', $url_alias)) {
  355. drupal_goto($id);
  356. }
  357. $sql = "
  358. SELECT
  359. S.name, S.uniquename, S.stock_id,
  360. O.genus, O.species, O.organism_id,
  361. CVT.cvterm_id, CVT.name as type_name,
  362. CS.nid
  363. FROM {stock} S
  364. INNER JOIN {organism} O on S.organism_id = O.organism_id
  365. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  366. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  367. WHERE
  368. S.uniquename = :uname or S.name = :name
  369. ";
  370. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  371. $num_matches = 0;
  372. // iterate through the matches and build the table for showing matches
  373. $header = array('Uniquename', 'Name', 'Type', 'Species');
  374. $rows = array();
  375. $curr_match;
  376. while ($match = $results->fetchObject()) {
  377. $curr_match = $match;
  378. $rows[] = array(
  379. $match->uniquename,
  380. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  381. $match->type_name,
  382. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  383. );
  384. $num_matches++;
  385. }
  386. // if we have more than one match then generate the table, otherwise, redirect
  387. // to the matched stock
  388. if ($num_matches == 1) {
  389. drupal_goto("node/" . $curr_match->nid);
  390. }
  391. if ($num_matches == 0) {
  392. return "<p>No stocks matched the given name '$id'</p>";
  393. }
  394. $table_attrs = array(
  395. 'class' => 'tripal-table tripal-table-horz'
  396. );
  397. $output = "<p>The following stocks match the name '$id'.</p>";
  398. $output .= theme_table($header, $rows, $table_attrs, $caption);
  399. return $output;
  400. }
  401. /**
  402. * Implementation of hook_form_alter().
  403. *
  404. * @param $form
  405. * @param $form_state
  406. * @param $form_id
  407. *
  408. * @ingroup tripal_stock
  409. */
  410. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  411. // turn of preview button for insert/updates
  412. if ($form_id == "chado_stock_node_form") {
  413. $form['actions']['preview']['#access'] = FALSE;
  414. //remove the body field
  415. unset($form['body']);
  416. }
  417. }
  418. /**
  419. * Load the arguments for the organism stock counts browser
  420. *
  421. * @param $organism
  422. * The organism of interest
  423. *
  424. * @ingroup tripal_stock
  425. */
  426. function tripal_stock_load_organism_stock_counts($organism) {
  427. $args = array();
  428. $order = array();
  429. $names = array();
  430. // build the where clause for the SQL statement if we have a custom term list
  431. // we'll also keep track of the names the admin provided (if any) and the
  432. // order that the terms should appear.
  433. $is_custom = 0;
  434. $temp = rtrim(variable_get('tripal_stock_summary_report_mapping', ''));
  435. $where = '';
  436. if ($temp) {
  437. $is_custom = 1;
  438. $temp = explode("\n", $temp);
  439. $i = 0;
  440. foreach ($temp as $value) {
  441. // separate the key value pairs
  442. $temp2 = explode("=", $value);
  443. $stock_type = rtrim($temp2[0]);
  444. $order[] = $stock_type; // save the order of the these terms
  445. $where .= " OFC.stock_type = :name$i OR ";
  446. $args[":name$i"] = rtrim($temp2[0]);
  447. // if the admin specified a new name then store that otherwise use the
  448. // the default sequence ontology term name
  449. if(count($temp2) == 2) {
  450. $names[] = rtrim($temp2[1]);
  451. }
  452. else {
  453. $names[] = $stock_type;
  454. }
  455. $i++;
  456. }
  457. if ($where) {
  458. $where = drupal_substr($where, 0, -4); # remove OR from the end
  459. $where = "($where) AND";
  460. }
  461. }
  462. // get the stock counts. This is dependent on a materialized view
  463. // installed with the organism module
  464. $sql = "
  465. SELECT OFC.num_stocks,OFC.stock_type,CVT.definition
  466. FROM {organism_stock_count} OFC
  467. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  468. WHERE $where organism_id = :organism_id
  469. ORDER BY num_stocks desc
  470. ";
  471. $args[':organism_id'] = $organism->organism_id;
  472. $org_stocks = chado_query($sql, $args);
  473. // iterate through the types
  474. $types = array();
  475. while ($type = $org_stocks->fetchObject()) {
  476. $types[$type->stock_type] = $type;
  477. // if we don't have an order this means we didn't go through the loop
  478. // above to set the names, so do that now
  479. if (!$is_custom) {
  480. $names[] = $type->stock_type;
  481. $order[] = $type->stock_type;
  482. }
  483. }
  484. // now reorder the types
  485. $ordered_types = array();
  486. foreach ($order as $type) {
  487. $ordered_types[] = $types[$type];
  488. }
  489. return array(
  490. 'types' => $ordered_types,
  491. 'names' => $names
  492. );
  493. }