tripal_stock.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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-secondary_tables.inc");
  26. require_once("includes/tripal_stock-properties.inc");
  27. require_once("includes/tripal_stock-relationships.inc");
  28. require_once("includes/tripal_stock-db_references.inc");
  29. require_once("includes/tripal_stock.chado_node.inc");
  30. require_once("api/tripal_stock.api.inc");
  31. /**
  32. * Implements hook_menu(): Adds menu items for the tripal_stock
  33. *
  34. * @return
  35. * Menu definitions for the tripal_stock
  36. *
  37. * @ingroup tripal_stock
  38. */
  39. function tripal_stock_menu() {
  40. $items = array();
  41. //Administrative settings menu-----------------
  42. $items['admin/tripal/chado/tripal_stock'] = array(
  43. 'title' => 'Stocks',
  44. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  45. 'page callback' => 'tripal_stock_admin_stock_view',
  46. 'access arguments' => array('administer tripal stocks'),
  47. 'type' => MENU_NORMAL_ITEM
  48. );
  49. $items['admin/tripal/chado/tripal_stock/configuration'] = array(
  50. 'title' => 'Settings',
  51. 'description' => 'Settings for Chado Stocks',
  52. 'page callback' => 'drupal_get_form',
  53. 'page arguments' => array('tripal_stock_admin'),
  54. 'access arguments' => array('administer tripal stocks'),
  55. 'type' => MENU_LOCAL_TASK,
  56. 'weight' => 5
  57. );
  58. $items['admin/tripal/chado/tripal_stock/sync'] = array(
  59. 'title' => ' Sync',
  60. 'description' => 'Sync stocks from Chado with Drupal',
  61. 'page callback' => 'drupal_get_form',
  62. //'page arguments' => array('tripal_stock_sync_form'),
  63. 'page arguments' => array('tripal_core_chado_node_sync_form', 'tripal_stock', 'chado_stock'),
  64. 'access arguments' => array('administer tripal stocks'),
  65. 'type' => MENU_LOCAL_TASK,
  66. 'weight' => 0
  67. );
  68. $items['admin/tripal/chado/tripal_stock/help'] = array(
  69. 'title' => 'Help',
  70. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  71. 'page callback' => 'theme',
  72. 'page arguments' => array('tripal_stock_help'),
  73. 'access arguments' => array('administer tripal stocks'),
  74. 'type' => MENU_LOCAL_TASK,
  75. 'weight' => 10
  76. );
  77. // the menu link for addressing any stock (by name, uniquename, synonym)
  78. $items['stock/%'] = array(
  79. 'page callback' => 'tripal_stock_match_stocks_page',
  80. 'page arguments' => array(1),
  81. 'access arguments' => array('access chado_stock content'),
  82. 'type' => MENU_LOCAL_TASK,
  83. );
  84. return $items;
  85. }
  86. /**
  87. * Implements Menu wildcard_load hook
  88. *
  89. * Purpose: Allows the node ID of a chado stock to be dynamically
  90. * pulled from the path. The node is loaded from this node ID
  91. * and supplied to the page as an arguement. This is an example
  92. * of dynamic argument replacement using wildcards in the path.
  93. *
  94. * @param $nid
  95. * The node ID passed in from the path
  96. *
  97. * @return
  98. * The node object with the passed in nid
  99. *
  100. * @ingroup tripal_stock
  101. */
  102. function cs_node_load($nid) {
  103. if (is_numeric($nid)) {
  104. $node = node_load($nid);
  105. if ($node->type == 'chado_stock') {
  106. return $node;
  107. }
  108. }
  109. return FALSE;
  110. }
  111. /**
  112. * Implementation of hook_perm().
  113. *
  114. * Purpose: Set the permission types that the chado stock module uses
  115. *
  116. * @return
  117. * Listing of the possible permission catagories
  118. *
  119. * @ingroup tripal_stock
  120. */
  121. function tripal_stock_permissions() {
  122. return array(
  123. 'access chado_stock content' => array(
  124. 'title' => t('View Stocks'),
  125. 'description' => t('Allow users to view stock pages.'),
  126. ),
  127. 'create chado_stock content' => array(
  128. 'title' => t('Create Stocks'),
  129. 'description' => t('Allow users to create new stock pages.'),
  130. ),
  131. 'delete chado_stock content' => array(
  132. 'title' => t('Delete Stocks'),
  133. 'description' => t('Allow users to delete stock pages.'),
  134. ),
  135. 'edit chado_stock content' => array(
  136. 'title' => t('Edit Stocks'),
  137. 'description' => t('Allow users to edit stock pages.'),
  138. ),
  139. 'adminster tripal stock' => array(
  140. 'title' => t('Administer Stocks'),
  141. 'description' => t('Allow users to administer all stocks.'),
  142. ),
  143. );
  144. }
  145. /**
  146. * Implement hook_access().
  147. *
  148. * This hook allows node modules to limit access to the node types they define.
  149. *
  150. * @param $node
  151. * The node on which the operation is to be performed, or, if it does not yet exist, the
  152. * type of node to be created
  153. *
  154. * @param $op
  155. * The operation to be performed
  156. *
  157. * @param $account
  158. * A user object representing the user for whom the operation is to be performed
  159. *
  160. * @return
  161. * If the permission for the specified operation is not set then return FALSE. If the
  162. * permission is set then return NULL as this allows other modules to disable
  163. * access. The only exception is when the $op == 'create'. We will always
  164. * return TRUE if the permission is set.
  165. *
  166. * @ingroup tripal_stock
  167. */
  168. function chado_stock_node_access($node, $op, $account) {
  169. if ($op == 'create') {
  170. if (!user_access('create chado_stock content', $account)) {
  171. return FALSE;
  172. }
  173. return TRUE;
  174. }
  175. if ($op == 'update') {
  176. if (!user_access('edit chado_stock content', $account)) {
  177. return FALSE;
  178. }
  179. }
  180. if ($op == 'delete') {
  181. if (!user_access('delete chado_stock content', $account)) {
  182. return FALSE;
  183. }
  184. }
  185. if ($op == 'view') {
  186. if (!user_access('access chado_stock content', $account)) {
  187. return FALSE;
  188. }
  189. }
  190. return NULL;
  191. }
  192. /**
  193. * Implements hook_views_api()
  194. *
  195. * Purpose: Essentially this hook tells drupal that there is views support for
  196. * for this module which then includes tripal_stock.views.inc where all the
  197. * views integration code is
  198. *
  199. * @return
  200. * An array with fields important for views integration
  201. *
  202. * @ingroup tripal_stock
  203. */
  204. function tripal_stock_views_api() {
  205. return array(
  206. 'api' => 2.0,
  207. );
  208. }
  209. /**
  210. * Implements hook_theme(): Register themeing functions for this module
  211. *
  212. * @return
  213. * An array of themeing functions to register
  214. *
  215. * @ingroup tripal_stock
  216. */
  217. function tripal_stock_theme() {
  218. $theme_path = drupal_get_path('module', 'tripal_stock') . '/theme';
  219. $items = array(
  220. // property edit forms function templates
  221. 'tripal_stock_edit_ALL_properties_form' => array(
  222. 'arguments' => array('form'),
  223. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  224. ),
  225. 'tripal_stock_edit_ALL_db_references_form' => array(
  226. 'arguments' => array('form'),
  227. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  228. ),
  229. 'tripal_stock_edit_ALL_relationships_form' => array(
  230. 'arguments' => array('form'),
  231. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  232. ),
  233. // tripal_stock templates
  234. 'node__chado_stock' => array(
  235. 'template' => 'node--chado-generic',
  236. 'path' => drupal_get_path('module', 'tripal_core') . '/theme',
  237. 'render element' => 'node',
  238. 'base hook' => 'node',
  239. ),
  240. 'tripal_stock_base' => array(
  241. 'arguments' => array('node' => NULL),
  242. 'template' => 'tripal_stock_base',
  243. 'path' => "$theme_path/tripal_stock",
  244. ),
  245. 'tripal_stock_properties' => array(
  246. 'arguments' => array('node' => NULL),
  247. 'template' => 'tripal_stock_properties',
  248. 'path' => "$theme_path/tripal_stock",
  249. ),
  250. 'tripal_stock_publications' => array(
  251. 'arguments' => array('node' => NULL),
  252. 'template' => 'tripal_stock.publications',
  253. 'path' => "$theme_path/tripal_stock",
  254. ),
  255. 'tripal_stock_references' => array(
  256. 'arguments' => array('node' => NULL),
  257. 'template' => 'tripal_stock_references',
  258. 'path' => "$theme_path/tripal_stock",
  259. ),
  260. 'tripal_stock_relationships' => array(
  261. 'arguments' => array('node' => NULL),
  262. 'template' => 'tripal_stock_relationships',
  263. 'path' => "$theme_path/tripal_stock",
  264. ),
  265. 'tripal_stock_synonyms' => array(
  266. 'arguments' => array('node' => NULL),
  267. 'template' => 'tripal_stock_synonyms',
  268. 'path' => "$theme_path/tripal_stock",
  269. ),
  270. 'tripal_stock_collections' => array(
  271. 'arguments' => array('node' => NULL),
  272. 'template' => 'tripal_stock_collections',
  273. 'path' => "$theme_path/tripal_stock",
  274. ),
  275. 'tripal_stock_collections' => array(
  276. 'arguments' => array('node' => NULL),
  277. 'template' => 'tripal_stock_collections',
  278. 'path' => "$theme_path/tripal_stock",
  279. ),
  280. 'tripal_stock_phenotypes' => array(
  281. 'arguments' => array('node' => NULL),
  282. 'template' => 'tripal_stock_phenotypes',
  283. 'path' => "$theme_path/tripal_stock",
  284. ),
  285. 'tripal_stock_locations' => array(
  286. 'arguments' => array('node' => NULL),
  287. 'template' => 'tripal_stock_locations',
  288. 'path' => "$theme_path/tripal_stock",
  289. ),
  290. // tripal_organism templates
  291. 'tripal_organism_stocks' => array(
  292. 'arguments' => array('node' => NULL),
  293. 'template' => 'tripal_organism_stocks',
  294. 'path' => "$theme_path/tripal_organism",
  295. ),
  296. // help template
  297. 'tripal_stock_help' => array(
  298. 'template' => 'tripal_stock_help',
  299. 'arguments' => array(NULL),
  300. 'path' => $theme_path,
  301. ),
  302. );
  303. return $items;
  304. }
  305. /**
  306. * Implements hook_help()
  307. * Purpose: Adds a help page to the module list
  308. */
  309. function tripal_stock_help ($path, $arg) {
  310. if ($path == 'admin/help#tripal_stock') {
  311. return theme('tripal_stock_help', array());
  312. }
  313. }
  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. *
  342. * @ingroup tripal_stock
  343. */
  344. function tripal_stock_block_view($delta = '') {
  345. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  346. $nid = arg(1);
  347. $node = node_load($nid);
  348. $block = array();
  349. switch ($delta) {
  350. case 'base':
  351. $block['subject'] = t('Stock Details');
  352. $block['content'] = theme('tripal_stock_base', $node);
  353. break;
  354. case 'properties':
  355. $block['subject'] = t('Properties');
  356. $block['content'] = theme('tripal_stock_properties', $node);
  357. break;
  358. case 'references':
  359. $block['subject'] = t('References');
  360. $block['content'] = theme('tripal_stock_references', $node);
  361. break;
  362. case 'relationships':
  363. $block['subject'] = t('Relationships');
  364. $block['content'] = theme('tripal_stock_relationships', $node);
  365. break;
  366. case 'synonyms':
  367. $block['subject'] = t('Synonyms');
  368. $block['content'] = theme('tripal_stock_synonyms', $node);
  369. break;
  370. case 'collections':
  371. $block['subject'] = t('Stock Collections');
  372. $block['content'] = theme('tripal_stock_collections', $node);
  373. break;
  374. case 'phenotypes':
  375. $block['subject'] = t('Stock Phenotypes');
  376. $block['content'] = theme('tripal_stock_phenotypes', $node);
  377. break;
  378. case 'genotypes':
  379. $block['subject'] = t('Stock Genotypes');
  380. $block['content'] = theme('tripal_stock_genotypes', $node);
  381. break;
  382. case 'locations':
  383. $block['subject'] = t('Stock Locations');
  384. $block['content'] = theme('tripal_stock_locations', $node);
  385. break;
  386. case 'orgstocks':
  387. $block['subject'] = t('Organism Stocks');
  388. $block['content'] = theme('tripal_organism_stocks', $node);
  389. break;
  390. }
  391. return $block;
  392. }
  393. }
  394. /**
  395. *
  396. *
  397. * @ingroup tripal_stock
  398. */
  399. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  400. // we want to provide a new variable that contains the matched stocks.
  401. $stock = $variables['node']->stock;
  402. // normally we would use tripal_core_expand_chado_vars to expand our
  403. // organism object and add in the relationships, however whan a large
  404. // number of relationships are present this significantly slows the
  405. // query, therefore we will manually perform the query
  406. $sql = "
  407. SELECT
  408. S.name, S.uniquename, S.stock_id, CS.nid,
  409. CVT.name as rel_type, CVTs.name as obj_type,
  410. SR.value
  411. FROM {stock_relationship} SR
  412. INNER JOIN {stock} S on SR.object_id = S.stock_id
  413. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  414. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  415. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  416. WHERE SR.subject_id = :stock_id
  417. ";
  418. $as_subject = chado_query($sql, array(':stock_id' => $stock->stock_id));
  419. $sql = "
  420. SELECT
  421. S.name, S.uniquename, S.stock_id, CS.nid,
  422. CVT.name as rel_type, CVTs.name as sub_type,
  423. SR.value
  424. FROM {stock_relationship} SR
  425. INNER JOIN {stock} S on SR.subject_id = S.stock_id
  426. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  427. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  428. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  429. WHERE SR.object_id = :stock_id
  430. ";
  431. $as_object = chado_query($sql, array(':stock_id' => $stock->stock_id));
  432. // combine both object and subject relationshisp into a single array
  433. $relationships = array();
  434. $relationships['object'] = array();
  435. $relationships['subject'] = array();
  436. // iterate through the object relationships
  437. while ($relationship = $as_object->fetchObject()) {
  438. // get the relationship and child types
  439. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  440. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  441. if (!array_key_exists($rel_type, $relationships['object'])) {
  442. $relationships['object'][$rel_type] = array();
  443. }
  444. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  445. $relationships['object'][$rel_type][$sub_type] = array();
  446. }
  447. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  448. }
  449. // now add in the subject relationships
  450. while ($relationship = $as_subject->fetchObject()) {
  451. // get the relationship and child types
  452. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  453. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  454. if (!array_key_exists($rel_type, $relationships['subject'])) {
  455. $relationships['subject'][$rel_type] = array();
  456. }
  457. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  458. $relationships['subject'][$rel_type][$obj_type] = array();
  459. }
  460. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  461. }
  462. $stock->all_relationships = $relationships;
  463. }
  464. /*
  465. * Uses the value provided in the $id argument to find all stocks that match
  466. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  467. * stock it will redirect to that stock page, otherwise, a list of matching
  468. * stocks is shown.
  469. */
  470. function tripal_stock_match_stocks_page($id) {
  471. // if the URL alias configuration is set such that the URL
  472. // always begins with 'stock' then we want to use the ID as it is and
  473. // forward it on. Otherwise, try to find the matching stock.
  474. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  475. if (!$url_alias) {
  476. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  477. }
  478. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  479. if (preg_match('/^stock\//', $url_alias)) {
  480. drupal_goto($id);
  481. }
  482. $sql = "
  483. SELECT
  484. S.name, S.uniquename, S.stock_id,
  485. O.genus, O.species, O.organism_id,
  486. CVT.cvterm_id, CVT.name as type_name,
  487. CS.nid
  488. FROM {stock} S
  489. INNER JOIN {organism} O on S.organism_id = O.organism_id
  490. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  491. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  492. WHERE
  493. S.uniquename = :uname or S.name = :name
  494. ";
  495. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  496. $num_matches = 0;
  497. // iterate through the matches and build the table for showing matches
  498. $header = array('Uniquename', 'Name', 'Type', 'Species');
  499. $rows = array();
  500. $curr_match;
  501. while ($match = $results->fetchObject()) {
  502. $curr_match = $match;
  503. $rows[] = array(
  504. $match->uniquename,
  505. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  506. $match->type_name,
  507. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  508. );
  509. $num_matches++;
  510. }
  511. // if we have more than one match then generate the table, otherwise, redirect
  512. // to the matched stock
  513. if ($num_matches == 1) {
  514. drupal_goto("node/" . $curr_match->nid);
  515. }
  516. if ($num_matches == 0) {
  517. return "<p>No stocks matched the given name '$id'</p>";
  518. }
  519. $table_attrs = array(
  520. 'class' => 'tripal-table tripal-table-horz'
  521. );
  522. $output = "<p>The following stocks match the name '$id'.</p>";
  523. $output .= theme_table($header, $rows, $table_attrs, $caption);
  524. return $output;
  525. }
  526. /**
  527. * Implementation of hook_form_alter()
  528. *
  529. * @param $form
  530. * @param $form_state
  531. * @param $form_id
  532. */
  533. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  534. // turn of preview button for insert/updates
  535. if ($form_id == "chado_stock_node_form") {
  536. $form['actions']['preview']['#access'] = FALSE;
  537. }
  538. }