tripal_stock.module 29 KB

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