tripal_stock.module

  1. 2.x tripal_stock/tripal_stock.module
  2. 3.x legacy/tripal_stock/tripal_stock.module
  3. 1.x tripal_stock/tripal_stock.module

Basic functionality for stocks

File

tripal_stock/tripal_stock.module
View source
  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 tripal_stock_node_access($node, $op, $account) {
  191. $node_type = $node;
  192. if (is_object($node)) {
  193. $node_type = $node->type;
  194. }
  195. if($node_type == 'chado_stock') {
  196. if ($op == 'create') {
  197. if (!user_access('create chado_stock content', $account)) {
  198. return NODE_ACCESS_DENY;
  199. }
  200. return NODE_ACCESS_ALLOW;
  201. }
  202. if ($op == 'update') {
  203. if (!user_access('edit chado_stock content', $account)) {
  204. return NODE_ACCESS_DENY;
  205. }
  206. }
  207. if ($op == 'delete') {
  208. if (!user_access('delete chado_stock content', $account)) {
  209. return NODE_ACCESS_DENY;
  210. }
  211. }
  212. if ($op == 'view') {
  213. if (!user_access('access chado_stock content', $account)) {
  214. return NODE_ACCESS_DENY;
  215. }
  216. }
  217. return NODE_ACCESS_IGNORE;
  218. }
  219. }
  220. /**
  221. * Implements hook_views_api().
  222. *
  223. * Purpose: Essentially this hook tells drupal that there is views support for
  224. * for this module which then includes tripal_stock.views.inc where all the
  225. * views integration code is
  226. *
  227. * @return
  228. * An array with fields important for views integration
  229. *
  230. * @ingroup tripal_stock
  231. */
  232. function tripal_stock_views_api() {
  233. return array(
  234. 'api' => 3.0,
  235. );
  236. }
  237. /**
  238. * Implements hook_theme().
  239. * Register themeing functions for this module
  240. *
  241. * @return
  242. * An array of themeing functions to register
  243. *
  244. * @ingroup tripal_stock
  245. */
  246. function tripal_stock_theme($existing, $type, $theme, $path) {
  247. $core_path = drupal_get_path('module', 'tripal_core');
  248. $items = array(
  249. // tripal_stock templates
  250. 'node__chado_stock' => array(
  251. 'template' => 'node--chado-generic',
  252. 'render element' => 'node',
  253. 'base hook' => 'node',
  254. 'path' => "$core_path/theme/templates",
  255. ),
  256. 'tripal_stock_base' => array(
  257. 'variables' => array('node' => NULL),
  258. 'template' => 'tripal_stock_base',
  259. 'path' => "$path/theme/templates",
  260. ),
  261. 'tripal_stock_properties' => array(
  262. 'variables' => array('node' => NULL),
  263. 'template' => 'tripal_stock_properties',
  264. 'path' => "$path/theme/templates",
  265. ),
  266. 'tripal_stock_publications' => array(
  267. 'variables' => array('node' => NULL),
  268. 'template' => 'tripal_stock_publications',
  269. 'path' => "$path/theme/templates",
  270. ),
  271. 'tripal_stock_references' => array(
  272. 'variables' => array('node' => NULL),
  273. 'template' => 'tripal_stock_references',
  274. 'path' => "$path/theme/templates",
  275. ),
  276. 'tripal_stock_relationships' => array(
  277. 'variables' => array('node' => NULL),
  278. 'template' => 'tripal_stock_relationships',
  279. 'path' => "$path/theme/templates",
  280. ),
  281. 'tripal_stock_synonyms' => array(
  282. 'variables' => array('node' => NULL),
  283. 'template' => 'tripal_stock_synonyms',
  284. 'path' => "$path/theme/templates",
  285. ),
  286. 'tripal_stock_collections' => array(
  287. 'variables' => array('node' => NULL),
  288. 'template' => 'tripal_stock_collections',
  289. 'path' => "$path/theme/templates",
  290. ),
  291. 'tripal_stock_collections' => array(
  292. 'variables' => array('node' => NULL),
  293. 'template' => 'tripal_stock_collections',
  294. 'path' => "$path/theme/templates",
  295. ),
  296. 'tripal_stock_phenotypes' => array(
  297. 'variables' => array('node' => NULL),
  298. 'template' => 'tripal_stock_phenotypes',
  299. 'path' => "$path/theme/templates",
  300. ),
  301. 'tripal_stock_locations' => array(
  302. 'variables' => array('node' => NULL),
  303. 'template' => 'tripal_stock_locations',
  304. 'path' => "$path/theme/templates",
  305. ),
  306. // tripal_organism templates
  307. 'tripal_organism_stocks' => array(
  308. 'variables' => array('node' => NULL),
  309. 'template' => 'tripal_organism_stocks',
  310. 'path' => "$path/theme/templates",
  311. ),
  312. // help template
  313. 'tripal_stock_help' => array(
  314. 'template' => 'tripal_stock_help',
  315. 'variables' => array(NULL),
  316. 'path' => "$path/theme/templates",
  317. ),
  318. // themed teaser
  319. 'tripal_stock_teaser' => array(
  320. 'variables' => array('node' => NULL),
  321. 'template' => 'tripal_stock_teaser',
  322. 'path' => "$path/theme/templates",
  323. ),
  324. );
  325. return $items;
  326. }
  327. /**
  328. * Implements hook_help().
  329. * Adds a help page to the module list
  330. *
  331. * @ingroup tripal_stock
  332. */
  333. function tripal_stock_help ($path, $arg) {
  334. if ($path == 'admin/help#tripal_stock') {
  335. return theme('tripal_stock_help', array());
  336. }
  337. }
  338. /*
  339. * Uses the value provided in the $id argument to find all stocks that match
  340. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  341. * stock it will redirect to that stock page, otherwise, a list of matching
  342. * stocks is shown.
  343. *
  344. * @ingroup tripal_stock
  345. */
  346. function tripal_stock_match_stocks_page($id) {
  347. // if the URL alias configuration is set such that the URL
  348. // always begins with 'stock' then we want to use the ID as it is and
  349. // forward it on. Otherwise, try to find the matching stock.
  350. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  351. if (!$url_alias) {
  352. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  353. }
  354. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  355. if (preg_match('/^stock\//', $url_alias)) {
  356. drupal_goto($id);
  357. }
  358. // @todo: re-write to support external chado databases.
  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. }