tripal_stock.module 16 KB

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