tripal_stock.module 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Implements Tripal Stock Module hooks
  6. */
  7. /**
  8. * @defgroup tripal_stock Stock Module
  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("tripal_stock-administration.inc");
  23. require("tripal_stock.api.inc");
  24. require("other_module_api_functions.inc");
  25. require("tripal_stock-secondary_tables.inc");
  26. require("tripal_stock-properties.inc");
  27. require("tripal_stock-relationships.inc");
  28. require("tripal_stock-db_references.inc");
  29. /**
  30. * Implements hook_menu(): Adds menu items for the tripal_stock
  31. *
  32. * @return
  33. * Menu definitions for the tripal_stock
  34. *
  35. * @ingroup tripal_stock
  36. */
  37. function tripal_stock_menu() {
  38. $items = array();
  39. //Administrative settings menu-----------------
  40. $items['admin/tripal/tripal_stock'] = array(
  41. 'title' => t('Stocks'),
  42. 'description' => t('Basic Description of Tripal Stock Module Functionality'),
  43. 'page callback' => 'tripal_stock_module_description_page',
  44. 'access arguments' => array('administer site configuration'),
  45. 'type' => MENU_NORMAL_ITEM
  46. );
  47. $items['admin/tripal/tripal_stock/configuration'] = array(
  48. 'title' => t('Configuration'),
  49. 'description' => t('Settings for Chado Stocks'),
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('tripal_stock_admin'),
  52. 'access arguments' => array('administer site configuration'),
  53. 'type' => MENU_NORMAL_ITEM
  54. );
  55. //Displaying stocks----------------------------
  56. $items['stocks'] = array(
  57. 'menu_name' => ('primary-links'),
  58. 'title' => t('Stocks'),
  59. 'page callback' => 'tripal_stock_show_stocks',
  60. 'access arguments' => array('access chado_stock content'),
  61. 'type' => MENU_NORMAL_ITEM,
  62. );
  63. // Adding Secondary Properties-----------------
  64. $items['node/%cs_node/properties'] = array(
  65. 'title' => t('Add Properties & Synonyms'),
  66. 'description' => t('Settings for Chado Stocks'),
  67. 'page callback' => 'tripal_stock_add_ALL_property_page',
  68. 'page arguments' => array(1),
  69. 'access arguments' => array('create chado_stock content'),
  70. 'type' => MENU_CALLBACK
  71. );
  72. $items['node/%cs_node/db_references'] = array(
  73. 'title' => t('Add Database References'),
  74. 'description' => t('Settings for Chado Stocks'),
  75. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  76. 'page arguments' => array(1),
  77. 'access arguments' => array('create chado_stock content'),
  78. 'type' => MENU_CALLBACK
  79. );
  80. $items['node/%cs_node/relationships'] = array(
  81. 'title' => t('Add Relationships'),
  82. 'description' => t('Settings for Chado Stocks'),
  83. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  84. 'page arguments' => array(1),
  85. 'access arguments' => array('create chado_stock content'),
  86. 'type' => MENU_CALLBACK
  87. );
  88. //Edit/Deleting Secondary Properties-------------
  89. $items['node/%cs_node/edit_properties'] = array(
  90. 'title' => t('Edit Properties'),
  91. 'description' => t('Settings for Chado Stocks'),
  92. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  93. 'page arguments' => array(1),
  94. 'access arguments' => array('edit chado_stock content'),
  95. 'type' => MENU_LOCAL_TASK,
  96. 'weight' => 8,
  97. );
  98. $items['node/%cs_node/edit_relationships'] = array(
  99. 'title' => t('Edit Relationships'),
  100. 'description' => t('Settings for Chado Stocks'),
  101. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  102. 'page arguments' => array(1),
  103. 'access arguments' => array('edit chado_stock content'),
  104. 'type' => MENU_LOCAL_TASK,
  105. 'weight' => 9,
  106. );
  107. $items['node/%cs_node/edit_db_references'] = array(
  108. 'title' => t('Edit DB References'),
  109. 'description' => t('Settings for Chado Stocks'),
  110. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  111. 'page arguments' => array(1),
  112. 'access arguments' => array('edit chado_stock content'),
  113. 'type' => MENU_LOCAL_TASK,
  114. 'weight' => 10,
  115. );
  116. return $items;
  117. }
  118. /**
  119. * Implements Menu wildcard_load hook
  120. *
  121. * Purpose: Allows the node ID of a chado stock to be dynamically
  122. * pulled from the path. The node is loaded from this node ID
  123. * and supplied to the page as an arguement. This is an example
  124. * of dynamic argument replacement using wildcards in the path.
  125. *
  126. * @param $nid
  127. * The node ID passed in from the path
  128. *
  129. * @return
  130. * The node object with the passed in nid
  131. *
  132. * @ingroup tripal_stock
  133. */
  134. function cs_node_load($nid) {
  135. if (is_numeric($nid)) {
  136. $node = node_load($nid);
  137. if ($node->type == 'chado_stock') {
  138. return $node;
  139. }
  140. }
  141. return FALSE;
  142. }
  143. /**
  144. * Implementation of hook_perm()
  145. *
  146. * Purpose: Set the permission types that the chado stock module uses
  147. *
  148. * @return
  149. * Listing of the possible permission catagories
  150. *
  151. * @ingroup tripal_stock
  152. */
  153. function tripal_stock_perm() {
  154. return array(
  155. 'access chado_stock content',
  156. 'create chado_stock content',
  157. 'edit chado_stock content',
  158. 'delete chado_stock content'
  159. );
  160. }
  161. /**
  162. * Implements hook_access(): Maps permission catagories to actions
  163. *
  164. * @param $op
  165. * The operation current operation: one of create, update, delete
  166. * @param $node
  167. * The node object the current operation is being performed on
  168. * @param $account
  169. * A user object representing the user for whom the operation is to be performed
  170. *
  171. * @return
  172. * TRUE grants access; FALSE denies it
  173. *
  174. * @ingroup tripal_stock
  175. */
  176. function chado_stock_access($op, $node, $account) {
  177. if ($op == 'create') {
  178. return user_access('create chado_stock content', $account);
  179. }
  180. if ($op == 'update') {
  181. if (user_access('edit chado_stock content', $account)) {
  182. return TRUE;
  183. }
  184. }
  185. if ($op == 'delete') {
  186. if (user_access('delete chado_stock content', $account)) {
  187. return TRUE;
  188. }
  189. }
  190. if ($op == 'view') {
  191. if (user_access('access chado_stock content', $account)) {
  192. return TRUE;
  193. }
  194. }
  195. return FALSE;
  196. }
  197. /**
  198. * Implements hook_views_api()
  199. *
  200. * Purpose: Essentially this hook tells drupal that there is views support for
  201. * for this module which then includes tripal_stock.views.inc where all the
  202. * views integration code is
  203. *
  204. * @return
  205. * An array with fields important for views integration
  206. *
  207. * @ingroup tripal_stock
  208. */
  209. function tripal_stock_views_api() {
  210. return array(
  211. 'api' => 2.0,
  212. );
  213. }
  214. /**
  215. * Implements hook_theme(): Register themeing functions for this module
  216. *
  217. * @return
  218. * An array of themeing functions to register
  219. *
  220. * @ingroup tripal_stock
  221. */
  222. function tripal_stock_theme() {
  223. return array(
  224. 'tripal_stock_stock_table' => array (
  225. 'arguments' => array('stocks'),
  226. ),
  227. // Property edit forms--------------------------
  228. 'tripal_stock_edit_ALL_properties_form' => array(
  229. 'arguments' => array('form'),
  230. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  231. ),
  232. 'tripal_stock_edit_ALL_db_references_form' => array(
  233. 'arguments' => array('form'),
  234. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  235. ),
  236. 'tripal_stock_edit_ALL_relationships_form' => array(
  237. 'arguments' => array('form'),
  238. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  239. ),
  240. // Block Templates------------------------------
  241. 'tripal_stock_base' => array (
  242. 'arguments' => array('node'=> null),
  243. 'template' => 'tripal_stock_base',
  244. ),
  245. 'tripal_stock_properties' => array (
  246. 'arguments' => array('node'=> null),
  247. 'template' => 'tripal_stock_properties',
  248. ),
  249. 'tripal_stock_references' => array (
  250. 'arguments' => array('node'=> null),
  251. 'template' => 'tripal_stock_references',
  252. ),
  253. 'tripal_stock_relationships_as_object' => array (
  254. 'arguments' => array('node'=> null),
  255. 'template' => 'tripal_stock_relationships_as_object',
  256. ),
  257. 'tripal_stock_relationships_as_subject' => array (
  258. 'arguments' => array('node'=> null),
  259. 'template' => 'tripal_stock_relationships_as_subject',
  260. ),
  261. 'tripal_stock_synonyms' => array (
  262. 'arguments' => array('node'=> null),
  263. 'template' => 'tripal_stock_synonyms',
  264. ),
  265. );
  266. }
  267. /**
  268. * Purpose: show stocks stored in drupals chado_stock table
  269. *
  270. * This function provides the default html representation of the stock table. This
  271. * representation can be overridden using Drupal Views2 to create more flexible tables
  272. * listing stocks.
  273. *
  274. * @return
  275. * HTML representation of a table of stocks
  276. *
  277. * @ingroup tripal_stock
  278. */
  279. function tripal_stock_show_stocks () {
  280. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  281. $no_stocks = db_result(db_query($sql));
  282. if($no_stocks != 0) {
  283. $stocks = tripal_stock_get_all_stocks();
  284. if($no_stocks != count($stocks)) {
  285. drupal_set_message("Synchronization needed.");
  286. }
  287. return theme('tripal_stock_stock_table',&$stocks);
  288. } else {
  289. return t("No Stocks exists. Please contact administrators to ".
  290. "synchronize stocks.");
  291. }
  292. }
  293. /**
  294. * A themeing funtion for the default tripal stock table
  295. *
  296. * @param $stocks
  297. * An array of all stock nodes
  298. *
  299. * @return
  300. * HTML representation of a table of stocks
  301. *
  302. * @ingroup tripal_stock
  303. */
  304. function theme_tripal_stock_stock_table (&$stocks) {
  305. // cycle through the stocks and build the stocks page
  306. $output = "<div id=\"stocks\">";
  307. $output .= '<table>';
  308. $output .= "<tr>";
  309. $output .= "<th>Name</th>";
  310. $output .= "<th>Type</th>";
  311. $output .= "<th>Organism</th>";
  312. $output .= "<th>Description</th>";
  313. $output .= "</tr>";
  314. foreach($stocks as $stock){
  315. $output .= "<tr>";
  316. $output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
  317. $output .= "<td>".$stock->stock_type."</td>";
  318. $output .= "<td nowrap>".$stock->organism->common_name."</td>";
  319. $output .= "<td>".$stock->description."</td>";
  320. $output .= "</tr>";
  321. }
  322. $output .= "</table>";
  323. $output .= "</div>";
  324. return $output;
  325. }
  326. /**
  327. * Implements hook_node_info(): registers a stock node type
  328. *
  329. * @return
  330. * An array describing various details of the node
  331. *
  332. * @ingroup tripal_stock
  333. */
  334. function tripal_stock_node_info() {
  335. return array(
  336. 'chado_stock' => array(
  337. 'name' => t('Stock'),
  338. 'module' => 'chado_stock',
  339. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  340. 'has_title' => TRUE,
  341. 'has_body' => FALSE,
  342. ),
  343. );
  344. }
  345. /**
  346. * Implements hook_load(): Prepares the chado_stock node
  347. *
  348. * @param $node
  349. * The basic node containing all variables common to all nodes
  350. *
  351. * @return
  352. * A stock node containing all the variables from the basic node and all stock specific variables
  353. *
  354. * @ingroup tripal_stock
  355. */
  356. function chado_stock_load($node) {
  357. // Get stock_id from chado_stock linking table
  358. $map = db_fetch_object(db_query(
  359. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  360. $node->vid
  361. ));
  362. // Get stock content and add to node
  363. $stock = tripal_core_generate_chado_var('stock', array('stock_id'=>$map->stock_id));
  364. //
  365. // move expandable fields downwards
  366. $node->expandable_fields = $stock->expandable_fields;
  367. unset($stock->expandable_fields);
  368. $node->expandable_tables = $stock->expandable_tables;
  369. unset($stock->expandable_tables);
  370. $node->expandable_nodes = $stock->expandable_nodes;
  371. unset($stock->expandable_nodes);
  372. $node->stock = $stock;
  373. return $node;
  374. }
  375. /**
  376. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  377. *
  378. * Parts to be added by this form
  379. * name,
  380. * uniquename,
  381. * description,
  382. * type => select from cvterm with key cvterm_id,
  383. * organism => select from available with key organism_id
  384. * main_db_reference => accession, version, description, db_name(select from dropdown)
  385. *
  386. * @param $node
  387. * An empty node object on insert OR the current stock node object on update
  388. * @param $form_state
  389. * The current state of the form
  390. *
  391. * @return
  392. * A description of the form to be rendered by drupal_get_form()
  393. *
  394. * @ingroup tripal_stock
  395. */
  396. function chado_stock_form($node, $form_state) {
  397. $type = node_get_types('type', $node);
  398. // Expand all fields needed
  399. $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
  400. foreach ($fields_needed as $field_name) {
  401. // Check to see if it's excluded and expand it if so
  402. if (in_array($field_name, $node->expandable_fields)) {
  403. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  404. }
  405. }
  406. // This defines the path for the next step in a simulated multipart form
  407. // NOTE: The %node gets replaced with the nid in insert
  408. $form['next_step_path'] = array(
  409. '#type' => 'hidden',
  410. '#value' => 'node/%node/properties'
  411. );
  412. // If you don't want a multipart form set this to false
  413. // Will then do default redirect (to new node) on submit
  414. $form['simulate_multipart'] = array(
  415. '#type' => 'textfield',
  416. '#attributes'=>array('style'=>"display:none"),
  417. '#default_value' => TRUE
  418. );
  419. if (!isset($node->stock->uniquename)) {
  420. $form['progress'] = array(
  421. '#type' => 'item',
  422. '#value' => tripal_stock_add_chado_properties_progress('main')
  423. );
  424. }
  425. $form['names'] = array(
  426. '#type' => 'fieldset',
  427. '#title' => t('Stock Name')
  428. );
  429. $form['names']['title'] = array(
  430. '#type' => 'textfield',
  431. '#title' => t('Name'),
  432. '#default_value' => $node->stock->name,
  433. '#required' => TRUE
  434. );
  435. $form['names']['uniquename'] = array(
  436. '#type' => 'textfield',
  437. '#title' => t('Unique Name'),
  438. '#default_value' => $node->stock->uniquename,
  439. '#required' => TRUE
  440. );
  441. $form['names']['stock_id'] = array(
  442. '#type' => 'hidden',
  443. '#value' => $node->stock->stock_id
  444. );
  445. $form['details'] = array(
  446. '#type' => 'fieldset',
  447. '#title' => t('Stock Details')
  448. );
  449. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  450. $type_options[0] = 'Select a Type';
  451. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock->type_id->cvterm_id; }
  452. $form['details']['type_id'] = array(
  453. '#type' => 'select',
  454. '#title' => t('Type of Stock'),
  455. '#options' => $type_options,
  456. '#default_value' => $type_default,
  457. '#required' => TRUE,
  458. );
  459. $stock_oganism_options = tripal_organism_get_organism_options();
  460. $stock_oganism_options[0] = 'Select An Organism';
  461. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->stock->organism_id->organism_id; }
  462. $form['details']['organism_id'] = array(
  463. '#type' => 'select',
  464. '#title' => t('Source Organism for stock'),
  465. '#default_value' => $organism_default,
  466. '#options' => $stock_oganism_options,
  467. '#required' => TRUE
  468. );
  469. $form['details']['stock_description'] = array(
  470. '#type' => 'textarea',
  471. '#title' => t('Notes'),
  472. '#default_value' => $node->stock->description,
  473. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  474. );
  475. $form['database_reference'] = array(
  476. '#type' => 'fieldset',
  477. '#title' => t('Stock Database Reference')
  478. );
  479. $form['database_reference']['accession'] = array(
  480. '#type' => 'textfield',
  481. '#title' => t('Accession'),
  482. '#default_value' => $node->stock->dbxref_id->accession
  483. );
  484. $form['database_reference']['db_description'] = array(
  485. '#type' => 'textarea',
  486. '#title' => t('Description of Database Reference'),
  487. '#default_value' => $node->stock->dbxref_id->description,
  488. '#description' => t('Optionally enter a description about the database accession.')
  489. );
  490. $db_options = tripal_db_get_db_options();
  491. $db_options[0] = 'Select a Database';
  492. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  493. $form['database_reference']['database'] = array(
  494. '#type' => 'select',
  495. '#title' => t('Database'),
  496. '#options' => $db_options,
  497. '#default_value' => $db_default
  498. );
  499. return $form;
  500. }
  501. /**
  502. * Implements hook_validate(): Validate the input from the chado_stock node form
  503. *
  504. * @param $node
  505. * The current node including fields with the form element names and submitted values
  506. * @param $form
  507. * A description of the form to be rendered by drupal_get_form()
  508. *
  509. * @ingroup tripal_stock
  510. */
  511. function chado_stock_validate($node, &$form) {
  512. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  513. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  514. // Validate Uniquename only if add
  515. if (empty($node->stock_id)) {
  516. $previous_db = tripal_db_set_active('chado');
  517. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  518. tripal_db_set_active($previous_db);
  519. if(!empty($chado_row->stock_id)) {
  520. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  521. if (!empty($drupal_row->nid)) {
  522. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  523. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  524. } else {
  525. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  526. }
  527. }
  528. }
  529. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  530. if ( $node->type_id == 0) {
  531. form_set_error('type_id', 'Please select a type of stock.');
  532. } else {
  533. $previous_db = tripal_db_set_active('chado');
  534. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  535. tripal_db_set_active($previous_db);
  536. if ( $num_rows->count != 1) {
  537. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  538. }
  539. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  540. if ( $node->organism_id == 0) {
  541. form_set_error('organism_id', 'Please select a source organism for this stock');
  542. } else {
  543. $previous_db = tripal_db_set_active('chado');
  544. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  545. tripal_db_set_active($previous_db);
  546. if ( $num_rows->count != 1 ) {
  547. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  548. }
  549. // Check if Accession also database
  550. if ($node->accession != '') {
  551. if ($node->database == 0) {
  552. // there is an accession but no database selected
  553. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  554. }
  555. } else {
  556. if ($node->database > 0) {
  557. // there is a database selected but no accession
  558. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  559. }
  560. }
  561. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  562. if ( $node->database > 0) {
  563. $previous_db = tripal_db_set_active('chado');
  564. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  565. tripal_db_set_active($previous_db);
  566. if ($num_rows->count != 1) {
  567. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  568. }
  569. }
  570. /**
  571. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  572. *
  573. * @param $node
  574. * The current node including fields with the form element names and submitted values
  575. *
  576. * @return
  577. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  578. *
  579. * @ingroup tripal_stock
  580. */
  581. function chado_stock_insert($node) {
  582. // create dbxref
  583. if ( !empty($node->accession) ) {
  584. if ( !empty($node->database) ) {
  585. $values = array(
  586. 'db_id' => $node->database,
  587. 'accession' => $node->accession,
  588. );
  589. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  590. $values['description'] = $node->db_description;
  591. $values['version'] = '1';
  592. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  593. if (!$dbxref_status) {
  594. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  595. watchdog('tripal_stock',
  596. 'Insert Stock: Unable to create dbxref where values:%values',
  597. array('%values' => print_r($values, TRUE)),
  598. WATCHDOG_WARNING
  599. );
  600. }
  601. } else { $dbxref_status = 1; }
  602. }
  603. }
  604. // create stock
  605. if($dbxref_status) {
  606. $values = array(
  607. 'dbxref_id' => array(
  608. 'db_id' => $node->database,
  609. 'accession' => $node->accession
  610. ),
  611. 'organism_id' => $node->organism_id,
  612. 'name' => $node->title,
  613. 'uniquename' => $node->uniquename,
  614. 'description' => $node->stock_description,
  615. 'type_id' => $node->type_id
  616. );
  617. $stock_status = tripal_core_chado_insert('stock', $values);
  618. } else {
  619. $values = array(
  620. 'organism_id' => $node->organism_id,
  621. 'name' => $node->title,
  622. 'uniquename' => $node->uniquename,
  623. 'description' => $node->stock_description,
  624. 'type_id' => $node->type_id
  625. );
  626. $stock_status = tripal_core_chado_insert('stock', $values);
  627. }
  628. // create drupal chado_stock entry
  629. if ($stock_status) {
  630. $values = array(
  631. 'organism_id' => $node->organism_id,
  632. 'uniquename' => $node->uniquename,
  633. 'type_id' => $node->type_id
  634. );
  635. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  636. if (!empty($chado_stock[0]->stock_id)) {
  637. db_query(
  638. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  639. ."VALUES (%d, %d, %d)",
  640. $node->nid,
  641. $node->vid,
  642. $chado_stock[0]->stock_id
  643. );
  644. //Move on to next stage of Stock Creation based on next_stage_path field
  645. if ($node->simulate_multipart) {
  646. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  647. $_REQUEST['destination'] = $next_stage_path;
  648. }
  649. } else {
  650. drupal_set_message('Error during stock creation.', 'error');
  651. watchdog('tripal_stock',
  652. 'Insert Stock: Unable to find newly created stock where values:%values',
  653. array('%values' => print_r($values, TRUE)),
  654. WATCHDOG_ERROR
  655. );
  656. return FALSE;
  657. }
  658. } else {
  659. drupal_set_message('Error during stock creation.', 'error');
  660. watchdog('tripal_stock',
  661. 'Insert Stock: Unable to create stock where values:%values',
  662. array('%values' => print_r($values, TRUE)),
  663. WATCHDOG_WARNING
  664. );
  665. return FALSE;
  666. }
  667. }
  668. /**
  669. * Implements hook_update(): Handles Editing/Updating of main stock info
  670. *
  671. * NOTE: Currently just writes over all old data
  672. *
  673. * @param $node
  674. * The current node including fields with the form element names and submitted values
  675. *
  676. * @return
  677. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  678. *
  679. * @ingroup tripal_stock
  680. */
  681. function chado_stock_update($node) {
  682. if ($node->revision) {
  683. chado_stock_insert($node);
  684. } else {
  685. //update dbxref
  686. if ($node->database) {
  687. if($node->accession) {
  688. $dbxref_mode = '';
  689. $stock = tripal_core_chado_select(
  690. 'stock',
  691. array('dbxref_id', 'type_id'),
  692. array('stock_id' => $node->stock_id)
  693. );
  694. if ($stock[0]->dbxref_id) {
  695. $values = array(
  696. 'db_id' => $node->database,
  697. 'accession' => $node->accession,
  698. 'description' => $node->db_description
  699. );
  700. $dbxref_status = tripal_core_chado_update(
  701. 'dbxref',
  702. array('dbxref_id' => $stock[0]->dbxref_id),
  703. $values
  704. );
  705. $dbxref_mode = 'Update';
  706. } else {
  707. if ($stock[0]->type_id) {
  708. //create the dbxref
  709. //used the type_id as a control to check we have a stock but not a dbxref
  710. $values = array(
  711. 'db_id' => $node->database,
  712. 'accession' => $node->accession,
  713. 'description' => $node->db_description,
  714. 'version' => '1',
  715. );
  716. $dbxref_status = tripal_core_chado_insert(
  717. 'dbxref',
  718. $values
  719. );
  720. $dbxref_mode = 'Create';
  721. } else {
  722. drupal_set_message('Unable to find stock to Update', 'error');
  723. watchdog(
  724. 'tripal_stock',
  725. 'Stock Update: Unable to find stock to update using values: %values',
  726. array('%values', print_r($values, TRUE)),
  727. WATCHDOG_ERROR
  728. );
  729. return FALSE;
  730. }
  731. }
  732. }
  733. }
  734. if (!$dbxref_status) {
  735. watchdog(
  736. 'tripal_stock',
  737. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  738. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  739. WATCHDOG_WARNING
  740. );
  741. }
  742. //can't change stock id which is all thats stored in drupal thus only update chado
  743. $update_values = array(
  744. 'organism_id' => $node->organism_id,
  745. 'name' => $node->title,
  746. 'uniquename' => $node->uniquename,
  747. 'description' => $node->stock_description,
  748. 'type_id' => $node->type_id,
  749. );
  750. if ($dbxref_status) {
  751. $update_values['dbxref_id'] = array(
  752. 'db_id' => $node->database,
  753. 'accession' => $node->accession
  754. );
  755. }
  756. $status = tripal_core_chado_update(
  757. 'stock',
  758. array('stock_id' => $node->stock_id),
  759. $update_values
  760. );
  761. if (!$status) {
  762. drupal_set_message('Unable to update stock', 'error');
  763. watchdog(
  764. 'tripal_stock',
  765. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  766. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  767. WATCHDOG_ERROR
  768. );
  769. }
  770. }
  771. }
  772. /**
  773. * Implements hook_delete(): Handles deleting of chado_stocks
  774. *
  775. * NOTE: Currently deletes data -no undo or record-keeping functionality
  776. *
  777. * @param $node
  778. * The current node including fields with the form element names and submitted values
  779. *
  780. * @return
  781. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  782. *
  783. * @ingroup tripal_stock
  784. */
  785. function chado_stock_delete($node) {
  786. //remove drupal node and all revisions
  787. db_query(
  788. "DELETE FROM {chado_stock} WHERE nid=%d",
  789. $node->nid
  790. );
  791. // Set stock in chado: is_obsolete = TRUE
  792. $previous_db = tripal_db_set_active('chado');
  793. db_query(
  794. "DELETE FROM stock WHERE stock_id=%d",
  795. $node->stock_id
  796. );
  797. tripal_db_set_active($previous_db);
  798. }
  799. /**
  800. * Purpose: Implement Blocks relating to stock content
  801. *
  802. * @param $op
  803. * What kind of information to retrieve about the block or blocks.
  804. * Possible values include list, configure, save, view.
  805. * @param $delta
  806. * Which block to return (not applicable if $op is 'list').
  807. * @param $edit
  808. * If $op is 'save', the submitted form data from the configuration form.
  809. *
  810. * @return
  811. * One of the following depending on $op: An array of block descriptions (list), the configuration
  812. * form (configure), nothing (save), an array defining subject and content for the block indexed
  813. * by $delta (view)
  814. *
  815. * @ingroup tripal_stock
  816. */
  817. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  818. switch($op) {
  819. case 'list':
  820. $blocks['base']['info'] = t('Tripal Stock Details');
  821. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  822. $blocks['properties']['info'] = t('Tripal Stock Properties');
  823. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  824. $blocks['references']['info'] = t('Tripal Stock References');
  825. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  826. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
  827. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  828. $blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
  829. $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
  830. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  831. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  832. return $blocks;
  833. case 'view':
  834. if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  835. $nid = arg(1);
  836. $node = node_load($nid);
  837. $block = array();
  838. switch($delta){
  839. case 'base':
  840. $block['subject'] = t('Stock Details');
  841. $block['content'] = theme('tripal_stock_base',$node);
  842. break;
  843. case 'properties':
  844. $block['subject'] = t('Properties');
  845. $block['content'] = theme('tripal_stock_properties',$node);
  846. break;
  847. case 'references':
  848. $block['subject'] = t('References');
  849. $block['content'] = theme('tripal_stock_references',$node);
  850. break;
  851. case 'relationships_as_object':
  852. $block['subject'] = t('Object Relationships');
  853. $block['content'] = theme('tripal_stock_relationships_as_object',$node);
  854. break;
  855. case 'relationships_as_subject':
  856. $block['subject'] = t('Subject Relationships');
  857. $block['content'] = theme('tripal_stock_relationships_as_subject',$node);
  858. break;
  859. case 'synonyms':
  860. $block['subject'] = t('Synonyms');
  861. $block['content'] = theme('tripal_stock_synonyms',$node);
  862. break;
  863. }
  864. return $block;
  865. }
  866. }
  867. }
  868. /**
  869. *
  870. *
  871. * @ingroup tripal_stock
  872. */
  873. function tripal_stock_preprocess(&$variables){
  874. // if the template file is the default node template file then we want
  875. // to add all of our variables.
  876. if($variables['template_files'][0] == 'node-chado_stock'){
  877. // stock properties
  878. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stockprop');
  879. // stock synonyms
  880. $variables['node']->stock->stock_synonyms = tripal_core_generate_chado_var(
  881. 'stockprop',
  882. array(
  883. 'stock_id'=>$variables['node']->stock->stock_id,
  884. 'type_id' => array(
  885. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  886. 'name' => 'synonym'
  887. ),
  888. )
  889. );
  890. // Stock database references
  891. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stock_dbxref');
  892. // Stock Object Relationships
  893. $variables['node']->stock->stock_object_relationships = tripal_core_generate_chado_var(
  894. 'stock_relationship',
  895. array('subject_id'=>$variables['node']->stock->stock_id)
  896. );
  897. // Stock Subject Relationships
  898. $variables['node']->stock->stock_subject_relationships = tripal_core_generate_chado_var(
  899. 'stock_relationship',
  900. array('object_id'=>$variables['node']->stock->stock_id)
  901. );
  902. }
  903. }
  904. /**
  905. *
  906. *
  907. * @ingroup tripal_stock
  908. */
  909. function tripal_stock_preprocess_tripal_stock_properties(&$variables){
  910. // stock properties
  911. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stockprop');
  912. }
  913. /**
  914. *
  915. *
  916. * @ingroup tripal_stock
  917. */
  918. function tripal_stock_preprocess_tripal_stock_synonyms(&$variables){
  919. // stock synonyms
  920. $variables['node']->stock->stock_synonyms = tripal_core_generate_chado_var(
  921. 'stockprop',
  922. array(
  923. 'stock_id'=>$variables['node']->stock->stock_id,
  924. 'type_id' => array(
  925. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  926. 'name' => 'synonym'
  927. ),
  928. )
  929. );
  930. }
  931. /**
  932. *
  933. *
  934. * @ingroup tripal_stock
  935. */
  936. function tripal_stock_preprocess_tripal_stock_references(&$variables){
  937. // Stock database references
  938. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stock_dbxref');
  939. }
  940. /**
  941. *
  942. *
  943. * @ingroup tripal_stock
  944. */
  945. function tripal_stock_preprocess_tripal_stock_relationships_as_object(&$variables){
  946. // Stock Object Relationships
  947. $variables['node']->stock->stock_object_relationships = tripal_core_generate_chado_var(
  948. 'stock_relationship',
  949. array('subject_id'=>$variables['node']->stock->stock_id)
  950. );
  951. }
  952. /**
  953. *
  954. *
  955. * @ingroup tripal_stock
  956. */
  957. function tripal_stock_preprocess_tripal_stock_relationships_as_subject(&$variables){
  958. // Stock Subject Relationships
  959. $variables['node']->stock->stock_subject_relationships = tripal_core_generate_chado_var(
  960. 'stock_relationship',
  961. array('object_id'=>$variables['node']->stock->stock_id)
  962. );
  963. }