tripal_stock.module 16 KB

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