tripal_stock.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if ($op == 'create') {
  166. if (!user_access('create chado_stock content', $account)) {
  167. return FALSE;
  168. }
  169. return TRUE;
  170. }
  171. if ($op == 'update') {
  172. if (!user_access('edit chado_stock content', $account)) {
  173. return FALSE;
  174. }
  175. }
  176. if ($op == 'delete') {
  177. if (!user_access('delete chado_stock content', $account)) {
  178. return FALSE;
  179. }
  180. }
  181. if ($op == 'view') {
  182. if (!user_access('access chado_stock content', $account)) {
  183. return FALSE;
  184. }
  185. }
  186. return NULL;
  187. }
  188. /**
  189. * Implements hook_views_api().
  190. *
  191. * Purpose: Essentially this hook tells drupal that there is views support for
  192. * for this module which then includes tripal_stock.views.inc where all the
  193. * views integration code is
  194. *
  195. * @return
  196. * An array with fields important for views integration
  197. *
  198. * @ingroup tripal_stock
  199. */
  200. function tripal_stock_views_api() {
  201. return array(
  202. 'api' => 3.0,
  203. );
  204. }
  205. /**
  206. * Implements hook_theme().
  207. * Register themeing functions for this module
  208. *
  209. * @return
  210. * An array of themeing functions to register
  211. *
  212. * @ingroup tripal_stock
  213. */
  214. function tripal_stock_theme($existing, $type, $theme, $path) {
  215. $core_path = drupal_get_path('module', 'tripal_core');
  216. $items = array(
  217. // property edit forms function templates
  218. 'tripal_stock_edit_ALL_properties_form' => array(
  219. 'variables' => array('form'),
  220. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  221. ),
  222. 'tripal_stock_edit_ALL_db_references_form' => array(
  223. 'variables' => array('form'),
  224. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  225. ),
  226. 'tripal_stock_edit_ALL_relationships_form' => array(
  227. 'variables' => array('form'),
  228. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  229. ),
  230. // tripal_stock templates
  231. 'node__chado_stock' => array(
  232. 'template' => 'node--chado-generic',
  233. 'render element' => 'node',
  234. 'base hook' => 'node',
  235. 'path' => "$core_path/theme",
  236. ),
  237. 'tripal_stock_base' => array(
  238. 'variables' => array('node' => NULL),
  239. 'template' => 'tripal_stock_base',
  240. 'path' => "$path/theme/tripal_stock",
  241. ),
  242. 'tripal_stock_properties' => array(
  243. 'variables' => array('node' => NULL),
  244. 'template' => 'tripal_stock_properties',
  245. 'path' => "$path/theme/tripal_stock",
  246. ),
  247. 'tripal_stock_publications' => array(
  248. 'variables' => array('node' => NULL),
  249. 'template' => 'tripal_stock_publications',
  250. 'path' => "$path/theme/tripal_stock",
  251. ),
  252. 'tripal_stock_references' => array(
  253. 'variables' => array('node' => NULL),
  254. 'template' => 'tripal_stock_references',
  255. 'path' => "$path/theme/tripal_stock",
  256. ),
  257. 'tripal_stock_relationships' => array(
  258. 'variables' => array('node' => NULL),
  259. 'template' => 'tripal_stock_relationships',
  260. 'path' => "$path/theme/tripal_stock",
  261. ),
  262. 'tripal_stock_synonyms' => array(
  263. 'arguments' => array('node' => NULL),
  264. 'template' => 'tripal_stock_synonyms',
  265. 'path' => "$path/theme/tripal_stock",
  266. ),
  267. 'tripal_stock_collections' => array(
  268. 'variables' => array('node' => NULL),
  269. 'template' => 'tripal_stock_collections',
  270. 'path' => "$path/theme/tripal_stock",
  271. ),
  272. 'tripal_stock_collections' => array(
  273. 'variables' => array('node' => NULL),
  274. 'template' => 'tripal_stock_collections',
  275. 'path' => "$path/theme/tripal_stock",
  276. ),
  277. 'tripal_stock_phenotypes' => array(
  278. 'variables' => array('node' => NULL),
  279. 'template' => 'tripal_stock_phenotypes',
  280. 'path' => "$path/theme/tripal_stock",
  281. ),
  282. 'tripal_stock_locations' => array(
  283. 'variables' => array('node' => NULL),
  284. 'template' => 'tripal_stock_locations',
  285. 'path' => "$path/theme/tripal_stock",
  286. ),
  287. // tripal_organism templates
  288. 'tripal_organism_stocks' => array(
  289. 'variables' => array('node' => NULL),
  290. 'template' => 'tripal_organism_stocks',
  291. 'path' => "$path/theme/tripal_organism",
  292. ),
  293. // help template
  294. 'tripal_stock_help' => array(
  295. 'template' => 'tripal_stock_help',
  296. 'variables' => array(NULL),
  297. 'path' => "$path/theme/",
  298. ),
  299. );
  300. return $items;
  301. }
  302. /**
  303. * Implements hook_help().
  304. * Adds a help page to the module list
  305. *
  306. * @ingroup tripal_stock
  307. */
  308. function tripal_stock_help ($path, $arg) {
  309. if ($path == 'admin/help#tripal_stock') {
  310. return theme('tripal_stock_help', array());
  311. }
  312. }
  313. /**
  314. * Implements hook_block_info().
  315. *
  316. * @ingroup tripal_stock
  317. */
  318. function tripal_stock_block_info() {
  319. $blocks['base']['info'] = t('Tripal Stock Details');
  320. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  321. $blocks['properties']['info'] = t('Tripal Stock Properties');
  322. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  323. $blocks['references']['info'] = t('Tripal Stock References');
  324. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  325. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  326. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  327. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  328. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  329. $blocks['collections']['info'] = t('Tripal Stock Collections');
  330. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  331. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  332. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  333. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  334. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  335. $blocks['locations']['info'] = t('Tripal Stock Locations');
  336. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  337. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  338. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  339. return $blocks;
  340. }
  341. /**
  342. * Implements hook_block_view().
  343. *
  344. * @ingroup tripal_stock
  345. */
  346. function tripal_stock_block_view($delta = '') {
  347. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  348. $nid = arg(1);
  349. $node = node_load($nid);
  350. $block = array();
  351. switch ($delta) {
  352. case 'base':
  353. $block['subject'] = t('Stock Details');
  354. $block['content'] = theme('tripal_stock_base', $node);
  355. break;
  356. case 'properties':
  357. $block['subject'] = t('Properties');
  358. $block['content'] = theme('tripal_stock_properties', $node);
  359. break;
  360. case 'references':
  361. $block['subject'] = t('References');
  362. $block['content'] = theme('tripal_stock_references', $node);
  363. break;
  364. case 'relationships':
  365. $block['subject'] = t('Relationships');
  366. $block['content'] = theme('tripal_stock_relationships', $node);
  367. break;
  368. case 'synonyms':
  369. $block['subject'] = t('Synonyms');
  370. $block['content'] = theme('tripal_stock_synonyms', $node);
  371. break;
  372. case 'collections':
  373. $block['subject'] = t('Stock Collections');
  374. $block['content'] = theme('tripal_stock_collections', $node);
  375. break;
  376. case 'phenotypes':
  377. $block['subject'] = t('Stock Phenotypes');
  378. $block['content'] = theme('tripal_stock_phenotypes', $node);
  379. break;
  380. case 'genotypes':
  381. $block['subject'] = t('Stock Genotypes');
  382. $block['content'] = theme('tripal_stock_genotypes', $node);
  383. break;
  384. case 'locations':
  385. $block['subject'] = t('Stock Locations');
  386. $block['content'] = theme('tripal_stock_locations', $node);
  387. break;
  388. case 'orgstocks':
  389. $block['subject'] = t('Organism Stocks');
  390. $block['content'] = theme('tripal_organism_stocks', $node);
  391. break;
  392. }
  393. return $block;
  394. }
  395. }
  396. /*
  397. * Uses the value provided in the $id argument to find all stocks that match
  398. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  399. * stock it will redirect to that stock page, otherwise, a list of matching
  400. * stocks is shown.
  401. *
  402. * @ingroup tripal_stock
  403. */
  404. function tripal_stock_match_stocks_page($id) {
  405. // if the URL alias configuration is set such that the URL
  406. // always begins with 'stock' then we want to use the ID as it is and
  407. // forward it on. Otherwise, try to find the matching stock.
  408. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  409. if (!$url_alias) {
  410. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  411. }
  412. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  413. if (preg_match('/^stock\//', $url_alias)) {
  414. drupal_goto($id);
  415. }
  416. $sql = "
  417. SELECT
  418. S.name, S.uniquename, S.stock_id,
  419. O.genus, O.species, O.organism_id,
  420. CVT.cvterm_id, CVT.name as type_name,
  421. CS.nid
  422. FROM {stock} S
  423. INNER JOIN {organism} O on S.organism_id = O.organism_id
  424. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  425. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  426. WHERE
  427. S.uniquename = :uname or S.name = :name
  428. ";
  429. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  430. $num_matches = 0;
  431. // iterate through the matches and build the table for showing matches
  432. $header = array('Uniquename', 'Name', 'Type', 'Species');
  433. $rows = array();
  434. $curr_match;
  435. while ($match = $results->fetchObject()) {
  436. $curr_match = $match;
  437. $rows[] = array(
  438. $match->uniquename,
  439. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  440. $match->type_name,
  441. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  442. );
  443. $num_matches++;
  444. }
  445. // if we have more than one match then generate the table, otherwise, redirect
  446. // to the matched stock
  447. if ($num_matches == 1) {
  448. drupal_goto("node/" . $curr_match->nid);
  449. }
  450. if ($num_matches == 0) {
  451. return "<p>No stocks matched the given name '$id'</p>";
  452. }
  453. $table_attrs = array(
  454. 'class' => 'tripal-table tripal-table-horz'
  455. );
  456. $output = "<p>The following stocks match the name '$id'.</p>";
  457. $output .= theme_table($header, $rows, $table_attrs, $caption);
  458. return $output;
  459. }
  460. /**
  461. * Implementation of hook_form_alter().
  462. *
  463. * @param $form
  464. * @param $form_state
  465. * @param $form_id
  466. *
  467. * @ingroup tripal_stock
  468. */
  469. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  470. // turn of preview button for insert/updates
  471. if ($form_id == "chado_stock_node_form") {
  472. $form['actions']['preview']['#access'] = FALSE;
  473. }
  474. }