tripal_stock.chado_node.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. <?php
  2. /**
  3. * @file Stock Node Functionality
  4. */
  5. /**
  6. * Implements hook_node_info(): registers a stock node type
  7. *
  8. * @return
  9. * An array describing various details of the node
  10. *
  11. * @ingroup tripal_stock
  12. */
  13. function tripal_stock_node_info() {
  14. return array(
  15. 'chado_stock' => array(
  16. 'name' => t('Stock'),
  17. 'base' => 'chado_stock',
  18. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  19. 'has_title' => TRUE,
  20. 'has_body' => FALSE,
  21. 'chado_node_api' => array(
  22. 'base_table' => 'stock',
  23. 'hook_prefix' => 'chado_stock',
  24. 'title' => array(
  25. 'singular' => t('Stock'),
  26. 'plural' => t('Stocks')
  27. ),
  28. 'select_by' => array(
  29. 'type_id' => TRUE,
  30. 'organism_id' => TRUE
  31. ),
  32. )
  33. ),
  34. );
  35. }
  36. /**
  37. * Implements hook_load(): Prepares the chado_stock node
  38. *
  39. * @param $node
  40. * The basic node containing all variables common to all nodes
  41. *
  42. * @return
  43. * A stock node containing all the variables from the basic node and all stock specific variables
  44. *
  45. * D7 @todo: Make optimizations to take advantage of $nodes
  46. *
  47. * @ingroup tripal_stock
  48. */
  49. function chado_stock_load($nodes) {
  50. $new_nodes = array();
  51. foreach ($nodes as $nid => $node) {
  52. // get the stock details from chado
  53. $stock_id = chado_get_id_for_node('stock', $node->nid);
  54. if (empty($stock_id)) {
  55. tripal_core_report_error(
  56. 'tripal_stock',
  57. TRIPAL_ERROR,
  58. 'Unable to retrieve stock_id for %title (NID = %nid).',
  59. array('%title' => $node->title, '%nid' => $node->nid)
  60. );
  61. }
  62. else {
  63. // build the variable with all the stock details
  64. $values = array('stock_id' => $stock_id);
  65. $stock = tripal_core_generate_chado_var('stock', $values);
  66. // by default, the titles are saved using the unique constraint. We will
  67. // keep it the same, but remove the duplicate name if the unique name and name
  68. // are identical
  69. $title_type = variable_get('chado_stock_title', 'unique_constraint');
  70. if($title_type == 'unique_constraint') {
  71. if (strcmp($stock->name, $stock->uniquename)==0) {
  72. $node->title = $stock->name . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  73. }
  74. // in previous version of Tripal, the stock title was simply the unique name.
  75. // so, we recreate the title just to be sure all of our stock pages are consistent
  76. else {
  77. $node->title = $stock->name . ", " . $stock->uniquename . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  78. }
  79. }
  80. // set the title to be the stock name or uniquename as configured
  81. if($title_type == 'stock_name') {
  82. $node->title = $stock->name;
  83. }
  84. if($title_type == 'stock_unique_name') {
  85. $node->title = $stock->uniquename;
  86. }
  87. // add this to the node
  88. $node->stock = $stock;
  89. }
  90. $new_nodes[$nid] = $node;
  91. }
  92. return $new_nodes;
  93. }
  94. /**
  95. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  96. *
  97. * Parts to be added by this form
  98. * name,
  99. * uniquename,
  100. * description,
  101. * type => select from cvterm with key cvterm_id,
  102. * organism => select from available with key organism_id
  103. * main_db_reference => accession, version, description, db_name(select from dropdown)
  104. *
  105. * @param $node
  106. * An empty node object on insert OR the current stock node object on update
  107. * @param $form_state
  108. * The current state of the form
  109. *
  110. * @return
  111. * A description of the form to be rendered by drupal_get_form()
  112. *
  113. * @ingroup tripal_stock
  114. */
  115. function chado_stock_form($node, $form_state) {
  116. // If existing stock then expand all fields needed using the chado API
  117. if (isset($node->nid)) {
  118. $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');
  119. foreach ($fields_needed as $field_name) {
  120. // Check to see if it's excluded and expand it if so
  121. if (isset($node->expandable_fields)) {
  122. if (in_array($field_name, $node->expandable_fields)) {
  123. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  124. }
  125. }
  126. }
  127. }
  128. // Default values can come in the following ways:
  129. //
  130. // 1) as elements of the $node object. This occurs when editing an existing stock
  131. // 2) in the $form_state['values'] array which occurs on a failed validation or
  132. // ajax callbacks from non submit form elements
  133. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  134. // form elements and the form is being rebuilt
  135. //
  136. // set form field defaults
  137. $sname = '';
  138. $uniquename = '';
  139. $stock_id = 0;
  140. $type_id = 0;
  141. $organism_id = 0;
  142. $sdescription = '';
  143. $dbxref_accession = '';
  144. $dbxref_description = '';
  145. $dbxref_database = 0;
  146. // 1) if we are editing an existing node then the stock is already part of the node
  147. if (property_exists($node, 'stock')) {
  148. $sname = $node->stock->name;
  149. $uniquename = $node->stock->uniquename;
  150. $stock_id = $node->stock->stock_id;
  151. $type_id = $node->stock->type_id->cvterm_id;
  152. $organism_id = $node->stock->organism_id->organism_id;
  153. $sdescription = $node->stock->description;
  154. $dbxref_accession = $node->stock->dbxref_id->accession;
  155. $dbxref_description = $node->stock->dbxref_id->description;
  156. $dbxref_database = $node->stock->dbxref_id->db_id->db_id;
  157. }
  158. // 2) if we are re constructing the form from a failed validation or ajax callback
  159. // then use the $form_state['values'] values
  160. if (array_key_exists('values', $form_state)) {
  161. $sname = $form_state['values']['sname'];
  162. $uniquename = $form_state['values']['uniquename'];
  163. $stock_id = $form_state['values']['stock_id'];
  164. $type_id = $form_state['values']['type_id'];
  165. $organism_id = $form_state['values']['organism_id'];
  166. $sdescription = $form_state['values']['description'];
  167. $dbxref_accession = $form_state['values']['accession'];
  168. $dbxref_description = $form_state['values']['db_description'];
  169. $dbxref_database = $form_state['values']['database'];
  170. }
  171. // 3) if we are re building the form from after submission (from ajax call) then
  172. // the values are in the $form_state['input'] array
  173. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  174. $sname = $form_state['input']['sname'];
  175. $uniquename = $form_state['input']['uniquename'];
  176. $stock_id = $form_state['input']['stock_id'];
  177. $type_id = $form_state['input']['type_id'];
  178. $organism_id = $form_state['input']['organism_id'];
  179. $sdescription = $form_state['input']['description'];
  180. $dbxref_accession = $form_state['input']['accession'];
  181. $dbxref_description = $form_state['input']['db_description'];
  182. $dbxref_database = $form_state['input']['database'];
  183. }
  184. $form['names'] = array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Stock Name')
  187. );
  188. $form['names']['sname'] = array(
  189. '#type' => 'textfield',
  190. '#title' => t('Name'),
  191. '#default_value' => $sname,
  192. '#required' => TRUE
  193. );
  194. $form['names']['uniquename'] = array(
  195. '#type' => 'textfield',
  196. '#title' => t('Unique Name'),
  197. '#default_value' => $uniquename,
  198. '#required' => TRUE
  199. );
  200. $form['names']['stock_id'] = array(
  201. '#type' => 'hidden',
  202. '#value' => $stock_id,
  203. );
  204. $form['details'] = array(
  205. '#type' => 'fieldset',
  206. '#title' => t('Stock Details')
  207. );
  208. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  209. $type_options[0] = 'Select a Type';
  210. $form['details']['type_id'] = array(
  211. '#type' => 'select',
  212. '#title' => t('Type of Stock'),
  213. '#options' => $type_options,
  214. '#default_value' => $type_id,
  215. '#required' => TRUE,
  216. );
  217. // get the list of organisms
  218. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  219. $org_rset = chado_query($sql);
  220. $organisms = array();
  221. $organisms[''] = '';
  222. while ($organism = $org_rset->fetchObject()) {
  223. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  224. }
  225. $form['details']['organism_id'] = array(
  226. '#type' => 'select',
  227. '#title' => t('Source Organism for stock'),
  228. '#default_value' => $organism_id,
  229. '#options' => $organisms,
  230. '#required' => TRUE
  231. );
  232. $form['details']['stock_description'] = array(
  233. '#type' => 'textarea',
  234. '#title' => t('Notes'),
  235. '#default_value' => $sdescription,
  236. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  237. );
  238. $form['database_reference'] = array(
  239. '#type' => 'fieldset',
  240. '#title' => t('Stock Database Reference')
  241. );
  242. $form['database_reference']['accession'] = array(
  243. '#type' => 'textfield',
  244. '#title' => t('Accession'),
  245. '#default_value' => $dbxref_accession,
  246. );
  247. $form['database_reference']['db_description'] = array(
  248. '#type' => 'textarea',
  249. '#title' => t('Description of Database Reference'),
  250. '#default_value' => $dbxref_description,
  251. '#description' => t('Optionally enter a description about the database accession.')
  252. );
  253. $db_options = tripal_db_get_db_options();
  254. $db_options[0] = 'Select a Database';
  255. $form['database_reference']['database'] = array(
  256. '#type' => 'select',
  257. '#title' => t('Database'),
  258. '#options' => $db_options,
  259. '#default_value' => $dbxref_database
  260. );
  261. return $form;
  262. }
  263. /**
  264. * Implements hook_validate(): Validate the input from the chado_stock node form
  265. *
  266. * @param $node
  267. * The current node including fields with the form element names and submitted values
  268. * @param $form
  269. * A description of the form to be rendered by drupal_get_form()
  270. *
  271. * @ingroup tripal_stock
  272. */
  273. function chado_stock_validate($node, $form, &$form_state) {
  274. // remove surrounding whitespace
  275. $node->uniquename = trim($node->uniquename);
  276. $node->sname = trim($node->sname);
  277. $node->stock_description = trim($node->stock_description);
  278. $node->accession = trim($node->accession);
  279. $node->db_description = trim($node->db_description);
  280. // if this is a delete then don't validate
  281. if($node->op == 'Delete') {
  282. return;
  283. }
  284. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  285. // need to validate during syncing so just skip it.
  286. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  287. return;
  288. }
  289. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  290. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  291. // if this is an update, we want to make sure that a different stock for
  292. // the organism doesn't already have this uniquename. We don't want to give
  293. // two sequences the same uniquename
  294. if (property_exists($node, 'nid')) {
  295. $sql = "
  296. SELECT *
  297. FROM {stock} S
  298. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  299. WHERE
  300. uniquename = :uname AND organism_id = :organism_id AND
  301. CVT.name = :cvtname AND NOT stock_id = :stock_id
  302. ";
  303. $result = chado_query($sql, array(':uname' => $node->uniquename,
  304. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  305. ':stock_id' => $node->stock_id))->fetchObject();
  306. if ($result) {
  307. form_set_error('uniquename', t("Stock update cannot proceed. The stock name '$node->uniquename' is not unique for this organism. Please provide a unique name for this stock."));
  308. }
  309. }
  310. // if this is an insert then we just need to make sure this name doesn't
  311. // already exist for this organism if it does then we need to throw an error
  312. else {
  313. $sql = "
  314. SELECT *
  315. FROM {Stock} S
  316. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  317. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  318. $result = chado_query($sql, array(':uname' => $node->uniquename,
  319. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  320. if ($result) {
  321. form_set_error('uniquename', t("Stock insert cannot proceed. The stock name '$node->uniquename' already exists for this organism. Please provide a unique name for this stock."));
  322. }
  323. }
  324. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  325. if ($node->type_id == 0) {
  326. form_set_error('type_id', 'Please select a type of stock.');
  327. }
  328. else {
  329. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  330. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  331. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  332. if ( $num_rows->count != 1) {
  333. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  334. }
  335. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  336. if ( $node->organism_id == 0) {
  337. form_set_error('organism_id', 'Please select a source organism for this stock');
  338. }
  339. else {
  340. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  341. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  342. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  343. if ( $num_rows->count != 1 ) {
  344. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  345. }
  346. // Check if Accession also database
  347. if ($node->accession != '') {
  348. if ($node->database == 0) {
  349. // there is an accession but no database selected
  350. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  351. }
  352. }
  353. else {
  354. if ($node->database > 0) {
  355. // there is a database selected but no accession
  356. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  357. }
  358. }
  359. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  360. if ( $node->database > 0) {
  361. $replace = array(':table' => 'db', ':column' => 'db_id');
  362. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  363. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  364. if ($num_rows->count != 1) {
  365. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  366. }
  367. }
  368. /**
  369. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  370. *
  371. * @param $node
  372. * The current node including fields with the form element names and submitted values
  373. *
  374. * @return
  375. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  376. *
  377. * @ingroup tripal_stock
  378. */
  379. function chado_stock_insert($node) {
  380. // If the chado stock exists (e.g. this is only a syncing operation)
  381. // then don't create but simply link to node
  382. if (isset($node->chado_stock_exists)) {
  383. if ($node->chado_stock_exists) {
  384. if (!empty($node->stock_id)) {
  385. db_insert('chado_stock')->fields(array(
  386. 'nid' => $node->nid,
  387. 'vid' => $node->vid,
  388. 'stock_id' => $node->stock_id
  389. ));
  390. }
  391. return $node;
  392. }
  393. }
  394. // if there is an stock_id in the $node object then this must be a sync so
  395. // we can skip adding the stock to chado as it is already there, although
  396. // we do need to proceed with the rest of the insert
  397. if (!property_exists($node, 'stock_id')) {
  398. // before we can add the stock, we must add the dbxref if one has been
  399. // provided by the user.
  400. $dbxref_status = 0;
  401. if (!empty($node->accession) ) {
  402. if (!empty($node->database) ) {
  403. $values = array(
  404. 'db_id' => $node->database,
  405. 'accession' => $node->accession,
  406. );
  407. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  408. $values['description'] = $node->db_description;
  409. $values['version'] = '1';
  410. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  411. if (!$dbxref_status) {
  412. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  413. watchdog('tripal_stock',
  414. 'Insert Stock: Unable to create dbxref where values:%values',
  415. array('%values' => print_r($values, TRUE)),
  416. WATCHDOG_WARNING
  417. );
  418. }
  419. }
  420. else {
  421. $dbxref_status = 1;
  422. }
  423. }
  424. }
  425. // create stock including the dbxref
  426. $stock = '';
  427. if ($dbxref_status) {
  428. $values = array(
  429. 'dbxref_id' => array(
  430. 'db_id' => $node->database,
  431. 'accession' => $node->accession
  432. ),
  433. 'organism_id' => $node->organism_id,
  434. 'name' => $node->sname,
  435. 'uniquename' => $node->uniquename,
  436. 'description' => $node->stock_description,
  437. 'type_id' => $node->type_id
  438. );
  439. $stock = tripal_core_chado_insert('stock', $values);
  440. }
  441. // create a stock without a dbxref
  442. else {
  443. $values = array(
  444. 'organism_id' => $node->organism_id,
  445. 'name' => $node->sname,
  446. 'uniquename' => $node->uniquename,
  447. 'description' => $node->stock_description,
  448. 'type_id' => $node->type_id
  449. );
  450. $stock = tripal_core_chado_insert('stock', $values);
  451. }
  452. if (is_array($stock)) {
  453. $stock_added = TRUE;
  454. }
  455. else {
  456. $stock_added = FALSE;
  457. }
  458. } //end of adding stock to chado
  459. else {
  460. // stock already exists since this is a sync
  461. $stock_added = TRUE;
  462. $stock['stock_id'] = $node->stock_id;
  463. }
  464. // if the stock creation was succesful then add the URL and the entry in the
  465. // chado_stock table
  466. if ($stock_added) {
  467. // add the entry to the chado_stock table
  468. db_insert('chado_stock')->fields(array(
  469. 'nid' => (int) $node->nid,
  470. 'vid' => (int) $node->vid,
  471. 'stock_id' => (int) $stock['stock_id']
  472. ))->execute();
  473. }
  474. else {
  475. drupal_set_message(t('Error during stock creation.'), 'error');
  476. watchdog('tripal_stock',
  477. 'Insert Stock: Unable to create stock where values:%values',
  478. array('%values' => print_r($values, TRUE)),
  479. WATCHDOG_WARNING
  480. );
  481. return FALSE;
  482. }
  483. }
  484. /**
  485. * Implements hook_update(): Handles Editing/Updating of main stock info
  486. *
  487. * NOTE: Currently just writes over all old data
  488. *
  489. * @param $node
  490. * The current node including fields with the form element names and submitted values
  491. *
  492. * @return
  493. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  494. *
  495. * @ingroup tripal_stock
  496. */
  497. function chado_stock_update($node) {
  498. if ($node->revision) {
  499. // there is no way to handle revisions in Chado but leave
  500. // this here just to make not we've addressed it.
  501. }
  502. //update dbxref
  503. if ($node->database) {
  504. if ($node->accession) {
  505. $dbxref_mode = '';
  506. $stock = tripal_core_chado_select(
  507. 'stock',
  508. array('dbxref_id', 'type_id'),
  509. array('stock_id' => $node->stock_id)
  510. );
  511. if ($stock[0]->dbxref_id) {
  512. $values = array(
  513. 'db_id' => $node->database,
  514. 'accession' => $node->accession,
  515. 'description' => $node->db_description
  516. );
  517. $dbxref_status = tripal_core_chado_update(
  518. 'dbxref',
  519. array('dbxref_id' => $stock[0]->dbxref_id),
  520. $values
  521. );
  522. $dbxref_mode = 'Update';
  523. }
  524. else {
  525. if ($stock[0]->type_id) {
  526. //create the dbxref
  527. //used the type_id as a control to check we have a stock but not a dbxref
  528. $values = array(
  529. 'db_id' => $node->database,
  530. 'accession' => $node->accession,
  531. 'description' => $node->db_description,
  532. 'version' => '1',
  533. );
  534. $dbxref_status = tripal_core_chado_insert(
  535. 'dbxref',
  536. $values
  537. );
  538. $dbxref_mode = 'Create';
  539. }
  540. else {
  541. drupal_set_message(t('Unable to find stock to Update'), 'error');
  542. watchdog(
  543. 'tripal_stock',
  544. 'Stock Update: Unable to find stock to update using values: %values',
  545. array('%values', print_r($values, TRUE)),
  546. WATCHDOG_ERROR
  547. );
  548. return FALSE;
  549. }
  550. }
  551. }
  552. }
  553. if (!$dbxref_status) {
  554. watchdog(
  555. 'tripal_stock',
  556. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  557. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  558. WATCHDOG_WARNING
  559. );
  560. }
  561. //can't change stock id which is all thats stored in drupal thus only update chado
  562. $update_values = array(
  563. 'organism_id' => $node->organism_id,
  564. 'name' => $node->sname,
  565. 'uniquename' => $node->uniquename,
  566. 'description' => $node->stock_description,
  567. 'type_id' => $node->type_id,
  568. );
  569. if ($dbxref_status) {
  570. $update_values['dbxref_id'] = array(
  571. 'db_id' => $node->database,
  572. 'accession' => $node->accession
  573. );
  574. }
  575. $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
  576. if (!$status) {
  577. drupal_set_message(t('Unable to update stock'), 'error');
  578. watchdog(
  579. 'tripal_stock',
  580. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  581. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  582. WATCHDOG_ERROR
  583. );
  584. }
  585. else {
  586. // set the URL for this stock page
  587. $values = array('stock_id' => $node->stock_id);
  588. $stock = tripal_core_chado_select('stock', array('*'), $values);
  589. }
  590. }
  591. /**
  592. * Implements hook_delete(): Handles deleting of chado_stocks
  593. *
  594. * NOTE: Currently deletes data -no undo or record-keeping functionality
  595. *
  596. * @param $node
  597. * The current node including fields with the form element names and submitted values
  598. *
  599. * @return
  600. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  601. *
  602. * @ingroup tripal_stock
  603. */
  604. function chado_stock_delete($node) {
  605. // Set stock in chado: is_obsolete = TRUE
  606. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  607. //remove drupal node and all revisions
  608. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  609. }
  610. /**
  611. * Used by Tripal Chado Node API during sync'ing of nodes
  612. */
  613. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  614. $new_node->organism_id = $record->organism_id;
  615. $new_node->sname = $record->name;
  616. $new_node->uniquename = $record->uniquename;
  617. $new_node->type_id = $record->type_id;
  618. return $new_node;
  619. }
  620. /**
  621. * @ingroup tripal_stock
  622. */
  623. function tripal_stock_node_presave($node) {
  624. switch ($node->type) {
  625. case 'chado_stock':
  626. $values = array('organism_id' => $node->organism_id);
  627. $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
  628. $values = array('cvterm_id' => $node->type_id);
  629. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  630. $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  631. break;
  632. }
  633. }
  634. /**
  635. * @ingroup tripal_stock
  636. */
  637. function tripal_stock_node_insert($node) {
  638. switch ($node->type) {
  639. case 'chado_stock':
  640. if (!$node->stock_id) {
  641. $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
  642. $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  643. $node->stock_id = $chado_stock->stock_id;
  644. }
  645. // remove any previous alias
  646. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  647. // set the URL for this stock page
  648. $url_alias = tripal_stock_get_stock_url($node);
  649. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  650. path_save($path_alias);
  651. break;
  652. }
  653. }
  654. /**
  655. * @ingroup tripal_stock
  656. */
  657. function tripal_stock_node_update($node) {
  658. switch ($node->type) {
  659. case 'chado_stock':
  660. // remove any previous alias
  661. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  662. // set the URL for this stock page
  663. $url_alias = tripal_stock_get_stock_url($node);
  664. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  665. path_save($path_alias);
  666. break;
  667. }
  668. }
  669. /**
  670. * @ingroup tripal_stock
  671. */
  672. function tripal_stock_node_view($node, $view_mode, $langcode) {
  673. switch ($node->type) {
  674. case 'chado_organism':
  675. if ($view_mode == 'full') {
  676. // Show stock if the organism/feature is not at teaser view
  677. $node->content['tripal_organism_stocks'] = array(
  678. '#value' => theme('tripal_organism_stocks', $node),
  679. );
  680. }
  681. break;
  682. case 'chado_stock':
  683. if ($view_mode == 'full') {
  684. $node->content['tripal_stock_base'] = array(
  685. '#value' => theme('tripal_stock_base', array('node' => $node)),
  686. );
  687. // Cross References
  688. $node->content['tripal_stock_references'] = array(
  689. '#value' => theme('tripal_stock_references', array('node' => $node)),
  690. );
  691. // Properties
  692. $node->content['tripal_stock_properties'] = array(
  693. '#value' => theme('tripal_stock_properties', array('node' => $node)),
  694. );
  695. // Synonyms
  696. $node->content['tripal_stock_synonyms'] = array(
  697. '#value' => theme('tripal_stock_synonyms', array('node' => $node)),
  698. );
  699. // Relationships
  700. $node->content['tripal_stock_relationships'] = array(
  701. '#value' => theme('tripal_stock_relationships', array('node' => $node)),
  702. );
  703. // Stock Collections
  704. $node->content['tripal_stock_collections'] = array(
  705. '#value' => theme('tripal_stock_collections', array('node' => $node)),
  706. );
  707. // Stock Genotypes
  708. $node->content['tripal_stock_genotypes'] = array(
  709. '#value' => theme('tripal_stock_genotypes', array('node' => $node)),
  710. );
  711. }
  712. if ($view_mode == 'teaser') {
  713. $node->content['tripal_stock_teaser'] = array(
  714. '#value' => theme('tripal_stock_teaser', array('node' => $node)),
  715. );
  716. }
  717. break;
  718. }
  719. }