tripal_stock.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <?php
  2. // $Id$
  3. require("tripal_stock-administration.inc");
  4. require("tripal_stock-api_functions.inc");
  5. require("other_module_api_functions.inc");
  6. /*************************************************************************
  7. * Implementation of hook_menu()
  8. * Purpose: Add menu items for this module
  9. * Note: A Menu item for the Create chado_stock form is automatically
  10. * added to the 'Create Content' Navigation menu
  11. *
  12. * @return and array for menu descriptions
  13. */
  14. function tripal_stock_menu() {
  15. $items = array();
  16. //Administrative settings menu
  17. $items['admin/tripal/tripal_stock'] = array(
  18. 'title' => t('Stocks'),
  19. 'description' => t('Settings for Chado Stocks'),
  20. 'page callback' => 'drupal_get_form',
  21. 'page arguments' => array('tripal_stock_admin'),
  22. 'access arguments' => array('administer site configuration'),
  23. 'type' => MENU_NORMAL_ITEM
  24. );
  25. //Displaying stocks
  26. $items['stocks'] = array(
  27. 'menu_name' => ('primary-links'),
  28. 'title' => t('Stocks'),
  29. 'page callback' => 'tripal_stock_show_stocks',
  30. 'access arguments' => array('access chado_stock content'),
  31. 'type' => MENU_NORMAL_ITEM,
  32. );
  33. return $items;
  34. }
  35. /*************************************************************************
  36. * Implementation of hook_perm()
  37. * Purpose: Set the permission types that the chado stock module uses
  38. *
  39. * @return an array listing the possible permission catagories
  40. */
  41. function tripal_stock_perm() {
  42. return array(
  43. 'access chado_stock content',
  44. 'create chado_stock content',
  45. 'edit chado_stock content',
  46. 'delete chado_stock content'
  47. );
  48. }
  49. /*************************************************************************
  50. * Implements hook_access()
  51. * Purpose: Maps permission catagories to actions;
  52. * TRUE grants access; FALSE denies it
  53. */
  54. function tripal_stock_access($op, $node, $account) {
  55. if ($op == 'create') {
  56. return user_access('create chado_stock content', $account);
  57. }
  58. if ($op == 'update') {
  59. if (user_access('edit chado_stock content', $account)) {
  60. return TRUE;
  61. }
  62. }
  63. if ($op == 'delete') {
  64. if (user_access('delete chado_stock content', $account)) {
  65. return TRUE;
  66. }
  67. }
  68. if ($op == 'view') {
  69. if (user_access('access chado_stock content', $account)) {
  70. return TRUE;
  71. }
  72. }
  73. return FALSE;
  74. }
  75. /*************************************************************************
  76. * Implements hook_views_api()
  77. * Purpose: Essentially this hook tells drupal that there is views support for
  78. * for this module which then includes tripal_stock.views.inc where all the
  79. * views integration code is
  80. */
  81. function tripal_stock_views_api() {
  82. return array(
  83. 'api' => 2.0,
  84. );
  85. }
  86. /*************************************************************************
  87. * Implements hook_theme()
  88. * Purpose: Register themeing functions for this module
  89. *
  90. * @return an array of themeing functions to register
  91. */
  92. function tripal_stock_theme() {
  93. return array(
  94. 'tripal_stock_stock_table' => array (
  95. 'arguments' => array('stocks'),
  96. ),
  97. );
  98. }
  99. /*************************************************************************
  100. * Purpose: show stocks stored in drupals chado_stock table
  101. */
  102. function tripal_stock_show_stocks () {
  103. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  104. $no_stocks = db_result(db_query($sql));
  105. if($no_stocks != 0) {
  106. $stocks = get_chado_stocks();
  107. if($no_stocks != count($stocks)) {
  108. drupal_set_message("Synchronization needed.");
  109. }
  110. return theme('tripal_stock_stock_table',&$stocks);
  111. } else {
  112. return t("No Stocks exists. Please contact administrators to ".
  113. "synchronize stocks.");
  114. }
  115. }
  116. function theme_tripal_stock_stock_table (&$stocks) {
  117. // cycle through the stocks and build the stocks page
  118. $output = "<div id=\"stocks\">";
  119. $output .= '<table>';
  120. $output .= "<tr>";
  121. $output .= "<th>Name</th>";
  122. $output .= "<th>Type</th>";
  123. $output .= "<th>Organism</th>";
  124. $output .= "<th>Description</th>";
  125. $output .= "</tr>";
  126. foreach($stocks as $stock){
  127. $output .= "<tr>";
  128. $output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
  129. $output .= "<td>".$stock->stock_type."</td>";
  130. $output .= "<td nowrap>".$stock->organism->common_name."</td>";
  131. $output .= "<td>".$stock->description."</td>";
  132. $output .= "</tr>";
  133. }
  134. $output .= "</table>";
  135. $output .= "</div>";
  136. return $output;
  137. }
  138. /*************************************************************************
  139. * @section Implementation of stock Node
  140. * including node_load, form, insert, update, delete(by nid and vid)
  141. *************************************************************************/
  142. /*************************************************************************
  143. * Implements hook_node_info()
  144. */
  145. function tripal_stock_node_info() {
  146. return array(
  147. 'chado_stock' => array(
  148. 'name' => t('Stock'),
  149. 'module' => 'chado_stock',
  150. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  151. 'has_title' => TRUE,
  152. 'has_body' => FALSE,
  153. ),
  154. );
  155. }
  156. /*************************************************************************
  157. * Implements hook_load()
  158. * Everytime a chado_stock node is viewed or manipulated this hook is called first to prepare the data
  159. */
  160. function chado_stock_load($node) {
  161. // Gets the stock_id from the drupal chado_stock table---------------------------------------
  162. $map = db_fetch_object(db_query(
  163. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  164. $node->vid
  165. ));
  166. $node->stock_id = $map->stock_id;
  167. //Get the main stock information from the chado stock table-----------------------------------
  168. $previous_db = tripal_db_set_active('chado');
  169. $chado_stock = db_fetch_object(db_query(
  170. "SELECT s.name, s.uniquename, s.description, c.name as type, c.cvterm_id as type_id, s.is_obsolete, s.organism_id "
  171. ."FROM stock s, cvterm c WHERE s.type_id=c.cvterm_id AND stock_id=%d",
  172. $map->stock_id
  173. ));
  174. tripal_db_set_active($previous_db);
  175. //add to node
  176. $node->stock_name = $chado_stock->name;
  177. $node->uniquename = $chado_stock->uniquename;
  178. $node->description = $chado_stock->description;
  179. $node->stock_type = $chado_stock->type;
  180. $node->stock_type_id = $chado_stock->type_id;
  181. $node->is_obsolete = $chado_stock->is_obsolete;
  182. // Get organism details from chado & add to node-----------------------------------------------
  183. // get organism if for current stock
  184. // pull all data from chado for the organism and assign it to the current node
  185. $node->organism = get_chado_organism(NULL,$chado_stock->organism_id);
  186. // Add Synonyms for stock----------------------------------------------------------------------
  187. // These are a special case of property (stockprop table) where cvterm='synonym'
  188. $previous_db = tripal_db_set_active('chado');
  189. $results = db_query(
  190. "SELECT sp.stockprop_id, sp.value as value, sp.type_id, 'synonym' as type FROM stockprop sp, cvterm c "
  191. ."WHERE sp.stock_id=%d AND sp.type_id=c.cvterm_id AND c.name='synonym'",
  192. $map->stock_id
  193. );
  194. tripal_db_set_active($previous_db);
  195. $i=0;
  196. // Node->synonyms is an array of synonyms(strings)
  197. while ($r = db_fetch_object($results) ) {
  198. $synonyms[$i++] = $r;
  199. }
  200. $node->synonyms = $synonyms;
  201. // Add properties for stock (not including synonyms)-------------------------------------------
  202. // $node->properties is an array of objects where each object describes a single property and has a type and value
  203. $previous_db = tripal_db_set_active('chado');
  204. $results = db_query(
  205. "SELECT sp.stockprop_id, c.name as type, sp.value as value FROM stockprop sp, cvterm c "
  206. ."WHERE sp.stock_id=%d AND sp.type_id=c.cvterm_id AND c.name != 'synonym'",
  207. $map->stock_id
  208. );
  209. tripal_db_set_active($previous_db);
  210. $i=0;
  211. while ($r = db_fetch_object($results) ) {
  212. $properties[$i++] = $r;
  213. }
  214. $node->properties = $properties;
  215. // Add in main db reference-----------------------------------------------------------------------
  216. // this is the dbxref_id in the stock table
  217. //$node->main_db_reference is an object describing the dbxref and has an accession, version, description, db_name, db_description, & urlprefix
  218. $previous_db = tripal_db_set_active('chado');
  219. $db_reference = db_fetch_object(db_query(
  220. "SELECT dbx.dbxref_id, dbx.accession, dbx.version, dbx.description, db.db_id, db.name as db_name, db.description as db_description, db.urlprefix as db_urlprefix "
  221. ."FROM dbxref dbx, db db, stock s WHERE stock_id=%d AND s.dbxref_id=dbx.dbxref_id AND dbx.db_id=db.db_id",
  222. $map->stock_id
  223. ));
  224. tripal_db_set_active($previous_db);
  225. $node->main_db_reference = $db_reference;
  226. // Add in extra references to external databases---------------------------------------------------
  227. // this includes all the dbxref entries in stock_dbxref
  228. // $node->db_referencesi s an array of objects where each object describes a given dbxref and has the same fields as the main db references
  229. $previous_db = tripal_db_set_active('chado');
  230. $results = db_query(
  231. "SELECT dbx.dbxref_id, dbx.accession, dbx.version, dbx.description, db.db_id, db.name as db_name, db.description as db_description, db.urlprefix as db_urlprefix "
  232. ."FROM dbxref dbx, db db, stock_dbxref s WHERE stock_id=%d AND s.dbxref_id=dbx.dbxref_id AND dbx.db_id=db.db_id",
  233. $map->stock_id
  234. );
  235. tripal_db_set_active($previous_db);
  236. $i=0;
  237. while ($r = db_fetch_object($results) ) {
  238. $db_references[$i++] = $r;
  239. }
  240. $node->db_references = $db_references;
  241. // Add relationships for stock---------------------------------------------------------------------
  242. // Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
  243. // and those where the current stock is the object (other details stored in $node->subject_relationships)
  244. // fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
  245. // where current is subject.............................
  246. $previous_db = tripal_db_set_active('chado');
  247. $result = db_query(
  248. "SELECT sr.stock_relationship_id, c.name as relationship_type, sr.type_id as relationship_type_id, s.uniquename as object_uniquename, s.name as object_name, s.stock_id as object_id "
  249. . "FROM stock_relationship sr, stock s, cvterm c "
  250. . "WHERE sr.subject_id=%d AND sr.object_id=s.stock_id AND sr.type_id=c.cvterm_id",
  251. $map->stock_id
  252. );
  253. tripal_db_set_active($previous_db);
  254. $i=0;
  255. $relationships= array();
  256. while ($r = db_fetch_object($result) ) {
  257. //get nid
  258. $object = db_fetch_object(db_query("SELECT * FROM {node} n, {chado_stock} c WHERE c.nid=n.nid AND c.vid=n.vid AND c.stock_id=%d", $r->object_id ));
  259. $r->object_nid = $object->nid;
  260. $relationships[$i++] = $r;
  261. }
  262. $node->object_relationships = $relationships;
  263. // where current is object.............................
  264. $previous_db = tripal_db_set_active('chado');
  265. $result = db_query(
  266. "SELECT sr.stock_relationship_id, c.name as relationship_type, sr.type_id as relationship_type_id, s.uniquename as subject_uniquename, s.name as subject_name, s.stock_id as subject_id "
  267. . "FROM stock_relationship sr, stock s, cvterm c "
  268. . "WHERE sr.object_id=%d AND sr.subject_id=s.stock_id AND sr.type_id=c.cvterm_id",
  269. $map->stock_id
  270. );
  271. tripal_db_set_active($previous_db);
  272. $i=0;
  273. $relationships= array();
  274. while ($r = db_fetch_object($result) ) {
  275. //get nid
  276. $subject = db_fetch_object(db_query("SELECT * FROM {node} n, {chado_stock} c WHERE c.nid=n.nid AND c.vid=n.vid AND c.stock_id=%d", $r->subject_id ));
  277. $r->subject_nid = $subject->nid;
  278. $relationships[$i++] = $r;
  279. }
  280. $node->subject_relationships = $relationships;
  281. return $node;
  282. }
  283. /*************************************************************************
  284. * Implements hook_form()
  285. * Creates the main Add/Edit/Delete Form for chado stocks
  286. *
  287. * Parts to be added by this form
  288. * name,
  289. * uniquename,
  290. * description,
  291. * type => select from cvterm with key cvterm_id,
  292. * organism => select from available with key organism_id
  293. * main_db_reference => accession, version, description, db_name(select from dropdown)
  294. */
  295. function chado_stock_form($node, $form_state) {
  296. $type = node_get_types('type', $node);
  297. // This defines the path for the next step in a simulated multipart form
  298. // NOTE: The %node gets replaced with the nid in insert
  299. $form['next_step_path'] = array(
  300. '#type' => 'hidden',
  301. '#value' => 'node/%node/properties'
  302. );
  303. // If you don't want a multipart form set this to false
  304. // Will then do default redirect (to new node) on submit
  305. $form['simulate_multipart'] = array(
  306. '#type' => 'textfield',
  307. '#attributes'=>array('style'=>"display:none"),
  308. '#default_value' => TRUE
  309. );
  310. if (!isset($node->uniquename)) {
  311. $form['progress'] = array(
  312. '#type' => 'item',
  313. '#value' => 'blank', //implement_add_chado_properties_progress('main')
  314. );
  315. }
  316. $form['names'] = array(
  317. '#type' => 'fieldset',
  318. '#title' => t('Stock Name')
  319. );
  320. $form['names']['title'] = array(
  321. '#type' => 'textfield',
  322. '#title' => t('Name'),
  323. '#default_value' => $node->title,
  324. '#required' => TRUE
  325. );
  326. $form['names']['uniquename'] = array(
  327. '#type' => 'textfield',
  328. '#title' => t('Unique Name'),
  329. '#default_value' => $node->uniquename,
  330. '#required' => TRUE
  331. );
  332. $form['names']['stock_id'] = array(
  333. '#type' => 'hidden',
  334. '#value' => $node->stock_id
  335. );
  336. $form['details'] = array(
  337. '#type' => 'fieldset',
  338. '#title' => t('Stock Details')
  339. );
  340. $type_options = get_chado_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  341. $type_options[0] = 'Select a Type';
  342. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
  343. $form['details']['type_id'] = array(
  344. '#type' => 'select',
  345. '#title' => t('Type of Stock'),
  346. '#options' => $type_options,
  347. '#default_value' => $type_default,
  348. '#required' => TRUE,
  349. );
  350. $stock_oganism_options = get_chado_organism_options();
  351. $stock_oganism_options[0] = 'Select An Organism';
  352. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
  353. $form['details']['organism_id'] = array(
  354. '#type' => 'select',
  355. '#title' => t('Source Organism for stock'),
  356. '#default_value' => $organism_default,
  357. '#options' => $stock_oganism_options,
  358. '#required' => TRUE
  359. );
  360. $form['details']['stock_description'] = array(
  361. '#type' => 'textarea',
  362. '#title' => t('Notes'),
  363. '#default_value' => $node->description,
  364. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  365. );
  366. $form['database_reference'] = array(
  367. '#type' => 'fieldset',
  368. '#title' => t('Stock Database Reference')
  369. );
  370. $form['database_reference']['accession'] = array(
  371. '#type' => 'textfield',
  372. '#title' => t('Accession'),
  373. '#default_value' => $node->main_db_reference->accession
  374. );
  375. $form['database_reference']['db_description'] = array(
  376. '#type' => 'textarea',
  377. '#title' => t('Description of Database Reference'),
  378. '#default_value' => $node->main_db_reference->db_description,
  379. '#description' => t('Optionally enter a description about the database accession.')
  380. );
  381. $db_options = get_chado_db_options();
  382. $db_options[0] = 'Select a Database';
  383. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
  384. $form['database_reference']['database'] = array(
  385. '#type' => 'select',
  386. '#title' => t('Database'),
  387. '#options' => $db_options,
  388. '#default_value' => $db_default
  389. );
  390. return $form;
  391. }
  392. /*************************************************************************
  393. * Implements hook_validate()
  394. */
  395. function chado_stock_validate($node, &$form) {
  396. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  397. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  398. // Validate Uniquename only if add
  399. if (empty($node->stock_id)) {
  400. $previous_db = tripal_db_set_active('chado');
  401. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  402. tripal_db_set_active($previous_db);
  403. if(!empty($chado_row->stock_id)) {
  404. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  405. if (!empty($drupal_row->nid)) {
  406. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  407. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  408. } else {
  409. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  410. }
  411. }
  412. }
  413. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  414. if ( $node->type_id == 0) {
  415. form_set_error('type_id', 'Please select a type of stock.');
  416. } else {
  417. $previous_db = tripal_db_set_active('chado');
  418. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  419. tripal_db_set_active($previous_db);
  420. if ( $num_rows->count != 1) {
  421. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  422. }
  423. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  424. if ( $node->organism_id == 0) {
  425. form_set_error('organism_id', 'Please select a source organism for this stock');
  426. } else {
  427. $previous_db = tripal_db_set_active('chado');
  428. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  429. tripal_db_set_active($previous_db);
  430. if ( $num_rows->count != 1 ) {
  431. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  432. }
  433. // Check if Accession also database
  434. if ($node->accession != '') {
  435. if ($node->database == 0) {
  436. // there is an accession but no database selected
  437. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  438. }
  439. } else {
  440. if ($node->database > 0) {
  441. // there is a database selected but no accession
  442. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  443. }
  444. }
  445. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  446. if ( $node->database > 0) {
  447. $previous_db = tripal_db_set_active('chado');
  448. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  449. tripal_db_set_active($previous_db);
  450. if ($num_rows->count != 1) {
  451. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  452. }
  453. // Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
  454. if ( $node->accession != '') {
  455. $previous_db = tripal_db_set_active('chado');
  456. $num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
  457. tripal_db_set_active($previous_db);
  458. if ($num_rows->count > 0) {
  459. form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
  460. }
  461. }
  462. /*************************************************************************
  463. * Implements hook_insert()
  464. * Inserts data from chado_stock_form() into drupal and chado
  465. */
  466. function chado_stock_insert($node) {
  467. $previous_db = tripal_db_set_active('chado');
  468. // create dbxref
  469. if ( !empty($node->accession) ) {
  470. if ( !empty($node->database) ) {
  471. db_query(
  472. "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')",
  473. $node->database,
  474. $node->accession,
  475. $node->db_description
  476. );
  477. }
  478. }
  479. // create stock
  480. $dbxref_options['db_id'] = array(
  481. 'value' =>$node->database,
  482. 'type' => 'INT',
  483. );
  484. $dbxref_options['accession'] = array(
  485. 'value' =>$node->accession,
  486. 'type' => 'STRING',
  487. );
  488. $dbxref = get_chado_dbxref($dbxref_options);
  489. if($dbxref->dbxref_id != 0) {
  490. db_query(
  491. "INSERT INTO stock (dbxref_id, organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, %d, '%s', '%s', '%s', %d, 'f')",
  492. $dbxref->dbxref_id,
  493. $node->organism_id,
  494. $node->title,
  495. $node->uniquename,
  496. $node->description,
  497. $node->type_id
  498. );
  499. } else {
  500. db_query(
  501. "INSERT INTO stock (organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, '%s', '%s', '%s', %d, 'f')",
  502. $node->organism_id,
  503. $node->title,
  504. $node->uniquename,
  505. $node->description,
  506. $node->type_id
  507. );
  508. }
  509. tripal_db_set_active($previous_db);
  510. // create drupal chado_stock entry
  511. $previous_db = tripal_db_set_active('chado');
  512. $chado_stock = db_fetch_object(db_query("SELECT stock_id FROM stock WHERE uniquename='".$node->uniquename."' AND type_id=".$node->type_id.' AND organism_id='.$node->organism_id));
  513. tripal_db_set_active($previous_db);
  514. if (!empty($chado_stock->stock_id)) {
  515. db_query(
  516. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  517. ."VALUES (%d, %d, %d)",
  518. $node->nid,
  519. $node->vid,
  520. $chado_stock->stock_id
  521. );
  522. //Move on to next stage of Stock Creation based on next_stage_path field
  523. if ($node->simulate_multipart) {
  524. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  525. $_REQUEST['destination'] = $next_stage_path;
  526. }
  527. } else {
  528. drupal_set_message('Error during stock creation.', 'error');
  529. }
  530. }
  531. /*************************************************************************
  532. * Implements hook_update()
  533. * Handles Editing/Updating of main stock info (that in the chado stock table)
  534. * Currently just writes over all old data
  535. */
  536. function chado_stock_update($node) {
  537. if ($node->revision) {
  538. chado_stock_insert($node);
  539. } else {
  540. //can't change stock id which is all thats stored in drupal thus only update chado
  541. $previous_db = tripal_db_set_active('chado');
  542. db_query(
  543. "UPDATE stock SET organism_id=%d, name='%s', uniquename='%s', description='%s', type_id=%d WHERE stock_id=%d",
  544. $node->organism_id,
  545. $node->title,
  546. $node->uniquename,
  547. $node->description,
  548. $node->type_id,
  549. $node->stock_id
  550. );
  551. tripal_db_set_active($previous_db);
  552. }
  553. }
  554. /*************************************************************************
  555. * Implements hook_delete()
  556. * Handles deleting of chado_stocks
  557. */
  558. function chado_stock_delete($node) {
  559. //remove drupal node and all revisions
  560. db_query(
  561. "DELETE FROM {chado_stock} WHERE nid=%d",
  562. $node->nid
  563. );
  564. // Set stock in chado: is_obsolete = TRUE
  565. $previous_db = tripal_db_set_active('chado');
  566. db_query(
  567. "DELETE FROM stock WHERE stock_id=%d",
  568. $node->stock_id
  569. );
  570. tripal_db_set_active($previous_db);
  571. }