tripal_stock.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. /**
  3. * @file
  4. * Implements Tripal Stock Module hooks
  5. */
  6. /**
  7. * @defgroup tripal_stock Stock Module
  8. * @ingroup tripal_modules
  9. * @{
  10. * Provides functions for managing chado stocks including creating details pages for each stock
  11. *
  12. * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado
  13. * stocks. The stock module was designed to store information about stock collections in a
  14. * laboratory. What is called a stock could also be called a strain or an accession. There is a lot
  15. * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all
  16. * come from some taxon, have genotypes, physical locations in the lab, some conceivable
  17. * relationship with a publication, some conceivable relationship with a sequence feature (such as a
  18. * transgene), and could be described by some ontology term. For more information about the chado
  19. * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
  20. * @}
  21. */
  22. require_once("includes/tripal_stock.admin.inc");
  23. require_once("includes/tripal_stock.sync_stocks.inc");
  24. require_once("includes/other_module_api_functions.inc");
  25. require_once("includes/tripal_stock.chado_node.inc");
  26. require_once("api/tripal_stock.api.inc");
  27. /**
  28. * Implements hook_menu(): 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. //Administrative settings menu-----------------
  38. $items['admin/tripal/chado/tripal_stock'] = array(
  39. 'title' => 'Stocks',
  40. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  41. 'page callback' => 'tripal_stock_admin_stock_view',
  42. 'access arguments' => array('administer tripal stocks'),
  43. 'type' => MENU_NORMAL_ITEM
  44. );
  45. $items['admin/tripal/chado/tripal_stock/configuration'] = array(
  46. 'title' => 'Settings',
  47. 'description' => 'Settings for Chado Stocks',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('tripal_stock_admin'),
  50. 'access arguments' => array('administer tripal stocks'),
  51. 'type' => MENU_LOCAL_TASK,
  52. 'weight' => 5
  53. );
  54. $items['admin/tripal/chado/tripal_stock/sync'] = array(
  55. 'title' => ' Sync',
  56. 'description' => 'Sync stocks from Chado with Drupal',
  57. 'page callback' => 'drupal_get_form',
  58. //'page arguments' => array('tripal_stock_sync_form'),
  59. 'page arguments' => array('tripal_core_chado_node_sync_form', 'tripal_stock', 'chado_stock'),
  60. 'access arguments' => array('administer tripal stocks'),
  61. 'type' => MENU_LOCAL_TASK,
  62. 'weight' => 0
  63. );
  64. $items['admin/tripal/chado/tripal_stock/help'] = array(
  65. 'title' => 'Help',
  66. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  67. 'page callback' => 'theme',
  68. 'page arguments' => array('tripal_stock_help'),
  69. 'access arguments' => array('administer tripal stocks'),
  70. 'type' => MENU_LOCAL_TASK,
  71. 'weight' => 10
  72. );
  73. // the menu link for addressing any stock (by name, uniquename, synonym)
  74. $items['stock/%'] = array(
  75. 'page callback' => 'tripal_stock_match_stocks_page',
  76. 'page arguments' => array(1),
  77. 'access arguments' => array('access chado_stock content'),
  78. 'type' => MENU_LOCAL_TASK,
  79. );
  80. return $items;
  81. }
  82. /**
  83. * Implements Menu wildcard_load hook
  84. *
  85. * Purpose: 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. * Implementation of hook_perm().
  109. *
  110. * Purpose: 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_permissions() {
  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_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' => 2.0,
  203. );
  204. }
  205. /**
  206. * Implements hook_theme(): 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() {
  214. $theme_path = drupal_get_path('module', 'tripal_stock') . '/theme';
  215. $items = array(
  216. // property edit forms function templates
  217. 'tripal_stock_edit_ALL_properties_form' => array(
  218. 'arguments' => array('form'),
  219. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  220. ),
  221. 'tripal_stock_edit_ALL_db_references_form' => array(
  222. 'arguments' => array('form'),
  223. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  224. ),
  225. 'tripal_stock_edit_ALL_relationships_form' => array(
  226. 'arguments' => 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. 'path' => drupal_get_path('module', 'tripal_core') . '/theme',
  233. 'render element' => 'node',
  234. 'base hook' => 'node',
  235. ),
  236. 'tripal_stock_base' => array(
  237. 'arguments' => array('node' => NULL),
  238. 'template' => 'tripal_stock_base',
  239. 'path' => "$theme_path/tripal_stock",
  240. ),
  241. 'tripal_stock_properties' => array(
  242. 'arguments' => array('node' => NULL),
  243. 'template' => 'tripal_stock_properties',
  244. 'path' => "$theme_path/tripal_stock",
  245. ),
  246. 'tripal_stock_publications' => array(
  247. 'arguments' => array('node' => NULL),
  248. 'template' => 'tripal_stock.publications',
  249. 'path' => "$theme_path/tripal_stock",
  250. ),
  251. 'tripal_stock_references' => array(
  252. 'arguments' => array('node' => NULL),
  253. 'template' => 'tripal_stock_references',
  254. 'path' => "$theme_path/tripal_stock",
  255. ),
  256. 'tripal_stock_relationships' => array(
  257. 'arguments' => array('node' => NULL),
  258. 'template' => 'tripal_stock_relationships',
  259. 'path' => "$theme_path/tripal_stock",
  260. ),
  261. 'tripal_stock_synonyms' => array(
  262. 'arguments' => array('node' => NULL),
  263. 'template' => 'tripal_stock_synonyms',
  264. 'path' => "$theme_path/tripal_stock",
  265. ),
  266. 'tripal_stock_collections' => array(
  267. 'arguments' => array('node' => NULL),
  268. 'template' => 'tripal_stock_collections',
  269. 'path' => "$theme_path/tripal_stock",
  270. ),
  271. 'tripal_stock_collections' => array(
  272. 'arguments' => array('node' => NULL),
  273. 'template' => 'tripal_stock_collections',
  274. 'path' => "$theme_path/tripal_stock",
  275. ),
  276. 'tripal_stock_phenotypes' => array(
  277. 'arguments' => array('node' => NULL),
  278. 'template' => 'tripal_stock_phenotypes',
  279. 'path' => "$theme_path/tripal_stock",
  280. ),
  281. 'tripal_stock_locations' => array(
  282. 'arguments' => array('node' => NULL),
  283. 'template' => 'tripal_stock_locations',
  284. 'path' => "$theme_path/tripal_stock",
  285. ),
  286. // tripal_organism templates
  287. 'tripal_organism_stocks' => array(
  288. 'arguments' => array('node' => NULL),
  289. 'template' => 'tripal_organism_stocks',
  290. 'path' => "$theme_path/tripal_organism",
  291. ),
  292. // help template
  293. 'tripal_stock_help' => array(
  294. 'template' => 'tripal_stock_help',
  295. 'arguments' => array(NULL),
  296. 'path' => $theme_path,
  297. ),
  298. );
  299. return $items;
  300. }
  301. /**
  302. * Implements hook_help()
  303. * Purpose: Adds a help page to the module list
  304. */
  305. function tripal_stock_help ($path, $arg) {
  306. if ($path == 'admin/help#tripal_stock') {
  307. return theme('tripal_stock_help', array());
  308. }
  309. }
  310. /**
  311. * @ingroup tripal_stock
  312. */
  313. function tripal_stock_block_info() {
  314. $blocks['base']['info'] = t('Tripal Stock Details');
  315. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  316. $blocks['properties']['info'] = t('Tripal Stock Properties');
  317. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  318. $blocks['references']['info'] = t('Tripal Stock References');
  319. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  320. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  321. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  322. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  323. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  324. $blocks['collections']['info'] = t('Tripal Stock Collections');
  325. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  326. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  327. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  328. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  329. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  330. $blocks['locations']['info'] = t('Tripal Stock Locations');
  331. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  332. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  333. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  334. return $blocks;
  335. }
  336. /**
  337. *
  338. * @ingroup tripal_stock
  339. */
  340. function tripal_stock_block_view($delta = '') {
  341. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  342. $nid = arg(1);
  343. $node = node_load($nid);
  344. $block = array();
  345. switch ($delta) {
  346. case 'base':
  347. $block['subject'] = t('Stock Details');
  348. $block['content'] = theme('tripal_stock_base', $node);
  349. break;
  350. case 'properties':
  351. $block['subject'] = t('Properties');
  352. $block['content'] = theme('tripal_stock_properties', $node);
  353. break;
  354. case 'references':
  355. $block['subject'] = t('References');
  356. $block['content'] = theme('tripal_stock_references', $node);
  357. break;
  358. case 'relationships':
  359. $block['subject'] = t('Relationships');
  360. $block['content'] = theme('tripal_stock_relationships', $node);
  361. break;
  362. case 'synonyms':
  363. $block['subject'] = t('Synonyms');
  364. $block['content'] = theme('tripal_stock_synonyms', $node);
  365. break;
  366. case 'collections':
  367. $block['subject'] = t('Stock Collections');
  368. $block['content'] = theme('tripal_stock_collections', $node);
  369. break;
  370. case 'phenotypes':
  371. $block['subject'] = t('Stock Phenotypes');
  372. $block['content'] = theme('tripal_stock_phenotypes', $node);
  373. break;
  374. case 'genotypes':
  375. $block['subject'] = t('Stock Genotypes');
  376. $block['content'] = theme('tripal_stock_genotypes', $node);
  377. break;
  378. case 'locations':
  379. $block['subject'] = t('Stock Locations');
  380. $block['content'] = theme('tripal_stock_locations', $node);
  381. break;
  382. case 'orgstocks':
  383. $block['subject'] = t('Organism Stocks');
  384. $block['content'] = theme('tripal_organism_stocks', $node);
  385. break;
  386. }
  387. return $block;
  388. }
  389. }
  390. /**
  391. *
  392. *
  393. * @ingroup tripal_stock
  394. */
  395. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  396. // we want to provide a new variable that contains the matched stocks.
  397. $stock = $variables['node']->stock;
  398. // normally we would use tripal_core_expand_chado_vars to expand our
  399. // organism object and add in the relationships, however whan a large
  400. // number of relationships are present this significantly slows the
  401. // query, therefore we will manually perform the query
  402. $sql = "
  403. SELECT
  404. S.name, S.uniquename, S.stock_id, CS.nid,
  405. CVT.name as rel_type, CVTs.name as obj_type,
  406. SR.value
  407. FROM {stock_relationship} SR
  408. INNER JOIN {stock} S on SR.object_id = S.stock_id
  409. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  410. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  411. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  412. WHERE SR.subject_id = :stock_id
  413. ";
  414. $as_subject = chado_query($sql, array(':stock_id' => $stock->stock_id));
  415. $sql = "
  416. SELECT
  417. S.name, S.uniquename, S.stock_id, CS.nid,
  418. CVT.name as rel_type, CVTs.name as sub_type,
  419. SR.value
  420. FROM {stock_relationship} SR
  421. INNER JOIN {stock} S on SR.subject_id = S.stock_id
  422. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  423. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  424. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  425. WHERE SR.object_id = :stock_id
  426. ";
  427. $as_object = chado_query($sql, array(':stock_id' => $stock->stock_id));
  428. // combine both object and subject relationshisp into a single array
  429. $relationships = array();
  430. $relationships['object'] = array();
  431. $relationships['subject'] = array();
  432. // iterate through the object relationships
  433. while ($relationship = $as_object->fetchObject()) {
  434. // get the relationship and child types
  435. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  436. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  437. if (!array_key_exists($rel_type, $relationships['object'])) {
  438. $relationships['object'][$rel_type] = array();
  439. }
  440. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  441. $relationships['object'][$rel_type][$sub_type] = array();
  442. }
  443. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  444. }
  445. // now add in the subject relationships
  446. while ($relationship = $as_subject->fetchObject()) {
  447. // get the relationship and child types
  448. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  449. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  450. if (!array_key_exists($rel_type, $relationships['subject'])) {
  451. $relationships['subject'][$rel_type] = array();
  452. }
  453. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  454. $relationships['subject'][$rel_type][$obj_type] = array();
  455. }
  456. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  457. }
  458. $stock->all_relationships = $relationships;
  459. }
  460. /*
  461. * Uses the value provided in the $id argument to find all stocks that match
  462. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  463. * stock it will redirect to that stock page, otherwise, a list of matching
  464. * stocks is shown.
  465. */
  466. function tripal_stock_match_stocks_page($id) {
  467. // if the URL alias configuration is set such that the URL
  468. // always begins with 'stock' then we want to use the ID as it is and
  469. // forward it on. Otherwise, try to find the matching stock.
  470. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  471. if (!$url_alias) {
  472. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  473. }
  474. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  475. if (preg_match('/^stock\//', $url_alias)) {
  476. drupal_goto($id);
  477. }
  478. $sql = "
  479. SELECT
  480. S.name, S.uniquename, S.stock_id,
  481. O.genus, O.species, O.organism_id,
  482. CVT.cvterm_id, CVT.name as type_name,
  483. CS.nid
  484. FROM {stock} S
  485. INNER JOIN {organism} O on S.organism_id = O.organism_id
  486. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  487. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  488. WHERE
  489. S.uniquename = :uname or S.name = :name
  490. ";
  491. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  492. $num_matches = 0;
  493. // iterate through the matches and build the table for showing matches
  494. $header = array('Uniquename', 'Name', 'Type', 'Species');
  495. $rows = array();
  496. $curr_match;
  497. while ($match = $results->fetchObject()) {
  498. $curr_match = $match;
  499. $rows[] = array(
  500. $match->uniquename,
  501. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  502. $match->type_name,
  503. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  504. );
  505. $num_matches++;
  506. }
  507. // if we have more than one match then generate the table, otherwise, redirect
  508. // to the matched stock
  509. if ($num_matches == 1) {
  510. drupal_goto("node/" . $curr_match->nid);
  511. }
  512. if ($num_matches == 0) {
  513. return "<p>No stocks matched the given name '$id'</p>";
  514. }
  515. $table_attrs = array(
  516. 'class' => 'tripal-table tripal-table-horz'
  517. );
  518. $output = "<p>The following stocks match the name '$id'.</p>";
  519. $output .= theme_table($header, $rows, $table_attrs, $caption);
  520. return $output;
  521. }
  522. /**
  523. * Implementation of hook_form_alter()
  524. *
  525. * @param $form
  526. * @param $form_state
  527. * @param $form_id
  528. */
  529. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  530. // turn of preview button for insert/updates
  531. if ($form_id == "chado_stock_node_form") {
  532. $form['actions']['preview']['#access'] = FALSE;
  533. }
  534. }