tripal_stock.module 16 KB

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