tripal_stock.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. // 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. * 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($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. * 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. // expand the stock object to include the stock relationships.
  399. $options = array(
  400. 'return_array' => 1,
  401. 'order_by' => array('rank' => 'ASC'),
  402. // we don't want to fully recurse we only need information about the
  403. // relationship type and the object and subject stocks (including stock type
  404. // and organism)
  405. 'include_fk' => array(
  406. 'type_id' => 1,
  407. 'object_id' => array(
  408. 'type_id' => 1,
  409. 'organism_id' => 1
  410. ),
  411. 'subject_id' => array(
  412. 'type_id' => 1,
  413. 'organism_id' => 1
  414. ),
  415. ),
  416. );
  417. $stock = tripal_core_expand_chado_vars($stock, 'table', 'stock_relationship', $options);
  418. // get the subject relationships
  419. $srelationships = $stock->stock_relationship->subject_id;
  420. $orelationships = $stock->stock_relationship->object_id;
  421. // combine both object and subject relationshisp into a single array
  422. $relationships = array();
  423. $relationships['object'] = array();
  424. $relationships['subject'] = array();
  425. // iterate through the object relationships
  426. if ($orelationships) {
  427. foreach ($orelationships as $relationship) {
  428. $rel = new stdClass();
  429. $rel->record = $relationship;
  430. // get the relationship and child types
  431. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  432. $child_type = $relationship->subject_id->type_id->name;
  433. // get the node id of the subject
  434. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id = :stock_id";
  435. $n = db_query($sql, array(':stock_id' => $relationship->subject_id->stock_id))->fetchObject();
  436. if ($n) {
  437. $rel->record->nid = $n->nid;
  438. }
  439. if (!array_key_exists($rel_type, $relationships['object'])) {
  440. $relationships['object'][$rel_type] = array();
  441. }
  442. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  443. $relationships['object'][$rel_type][$child_type] = array();
  444. }
  445. $relationships['object'][$rel_type][$child_type][] = $rel;
  446. }
  447. }
  448. // now add in the subject relationships
  449. if ($srelationships) {
  450. foreach ($srelationships as $relationship) {
  451. $rel = new stdClass();
  452. $rel->record = $relationship;
  453. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  454. $parent_type = $relationship->object_id->type_id->name;
  455. // get the node id of the subject
  456. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id = :stock_id";
  457. $n = db_query($sql, array(':stock_id' => $relationship->object_id->stock_id))->fetchObject();
  458. if ($n) {
  459. $rel->record->nid = $n->nid;
  460. }
  461. if (!array_key_exists($rel_type, $relationships['subject'])) {
  462. $relationships['subject'][$rel_type] = array();
  463. }
  464. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  465. $relationships['subject'][$rel_type][$parent_type] = array();
  466. }
  467. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  468. }
  469. }
  470. $stock->all_relationships = $relationships;
  471. }
  472. /*
  473. * Uses the value provided in the $id argument to find all stocks that match
  474. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  475. * stock it will redirect to that stock page, otherwise, a list of matching
  476. * stocks is shown.
  477. */
  478. function tripal_stock_match_stocks_page($id) {
  479. // if the URL alias configuration is set such that the URL
  480. // always begins with 'stock' then we want to use the ID as it is and
  481. // forward it on. Otherwise, try to find the matching stock.
  482. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  483. if (!$url_alias) {
  484. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  485. }
  486. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  487. if (preg_match('/^stock\//', $url_alias)) {
  488. drupal_goto($id);
  489. }
  490. $sql = "
  491. SELECT
  492. S.name, S.uniquename, S.stock_id,
  493. O.genus, O.species, O.organism_id,
  494. CVT.cvterm_id, CVT.name as type_name,
  495. CS.nid
  496. FROM {stock} S
  497. INNER JOIN {organism} O on S.organism_id = O.organism_id
  498. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  499. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  500. WHERE
  501. S.uniquename = :uname or S.name = :name
  502. ";
  503. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  504. $num_matches = 0;
  505. // iterate through the matches and build the table for showing matches
  506. $header = array('Uniquename', 'Name', 'Type', 'Species');
  507. $rows = array();
  508. $curr_match;
  509. while ($match = $results->fetchObject()) {
  510. $curr_match = $match;
  511. $rows[] = array(
  512. $match->uniquename,
  513. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  514. $match->type_name,
  515. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  516. );
  517. $num_matches++;
  518. }
  519. // if we have more than one match then generate the table, otherwise, redirect
  520. // to the matched stock
  521. if ($num_matches == 1) {
  522. drupal_goto("node/" . $curr_match->nid);
  523. }
  524. if ($num_matches == 0) {
  525. return "<p>No stocks matched the given name '$id'</p>";
  526. }
  527. $table_attrs = array(
  528. 'class' => 'tripal-table tripal-table-horz'
  529. );
  530. $output = "<p>The following stocks match the name '$id'.</p>";
  531. $output .= theme_table($header, $rows, $table_attrs, $caption);
  532. return $output;
  533. }
  534. /**
  535. * Implementation of hook_form_alter()
  536. *
  537. * @param $form
  538. * @param $form_state
  539. * @param $form_id
  540. */
  541. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  542. // turn of preview button for insert/updates
  543. if ($form_id == "chado_stock_node_form") {
  544. $form['actions']['preview']['#access'] = FALSE;
  545. }
  546. }