tripal_stock.module 15 KB

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