tripal_stock.chado_node.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. 'record_type_title' => array(
  25. 'singular' => t('Stock'),
  26. 'plural' => t('Stocks')
  27. ),
  28. 'sync_filters' => 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. // PROPERTIES FORM
  262. //---------------------------------------------
  263. $details = array(
  264. 'property_table' => 'stockprop',
  265. 'base_foreign_key' => 'stock_id',
  266. 'base_key_value' => $stock_id,
  267. 'cv_id' => variable_get('chado_stock_prop_types_cv', FALSE)
  268. );
  269. chado_node_properties_form($form, $form_state, $details);
  270. // ADDITIONAL DBXREFS FORM
  271. //---------------------------------------------
  272. $details = array(
  273. 'linking_table' => 'stock_dbxref',
  274. 'base_foreign_key' => 'stock_id',
  275. 'base_key_value' => $stock_id
  276. );
  277. tripal_core_additional_dbxrefs_form($form, $form_state, $details);
  278. // RELATIONSHIPS FORM
  279. //---------------------------------------------
  280. $details = array(
  281. 'relationship_table' => 'stock_relationship',
  282. 'base_table' => 'stock',
  283. 'base_foreign_key' => 'stock_id',
  284. 'base_key_value' => $stock_id,
  285. 'nodetype' => 'stock',
  286. 'cv_id' => variable_get('chado_stock_relationship_cv', 0)
  287. );
  288. tripal_core_relationships_form($form, $form_state, $details);
  289. return $form;
  290. }
  291. /**
  292. * Implements hook_validate(): Validate the input from the chado_stock node form
  293. *
  294. * @param $node
  295. * The current node including fields with the form element names and submitted values
  296. * @param $form
  297. * A description of the form to be rendered by drupal_get_form()
  298. *
  299. * @ingroup tripal_stock
  300. */
  301. function chado_stock_validate(&$node, $form, &$form_state) {
  302. // remove surrounding whitespace
  303. $node->uniquename = trim($node->uniquename);
  304. $node->sname = trim($node->sname);
  305. $node->stock_description = trim($node->stock_description);
  306. $node->accession = trim($node->accession);
  307. $node->db_description = trim($node->db_description);
  308. // if this is a delete then don't validate
  309. if($node->op == 'Delete') {
  310. return;
  311. }
  312. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  313. // need to validate during syncing so just skip it.
  314. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  315. return;
  316. }
  317. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  318. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  319. // if this is an update, we want to make sure that a different stock for
  320. // the organism doesn't already have this uniquename. We don't want to give
  321. // two sequences the same uniquename
  322. if (property_exists($node, 'nid')) {
  323. $sql = "
  324. SELECT *
  325. FROM {stock} S
  326. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  327. WHERE
  328. uniquename = :uname AND organism_id = :organism_id AND
  329. CVT.name = :cvtname AND NOT stock_id = :stock_id
  330. ";
  331. $result = chado_query($sql, array(':uname' => $node->uniquename,
  332. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  333. ':stock_id' => $node->stock_id))->fetchObject();
  334. if ($result) {
  335. 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."));
  336. }
  337. }
  338. // if this is an insert then we just need to make sure this name doesn't
  339. // already exist for this organism if it does then we need to throw an error
  340. else {
  341. $sql = "
  342. SELECT *
  343. FROM {Stock} S
  344. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  345. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  346. $result = chado_query($sql, array(':uname' => $node->uniquename,
  347. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  348. if ($result) {
  349. 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."));
  350. }
  351. }
  352. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  353. if ($node->type_id == 0) {
  354. form_set_error('type_id', 'Please select a type of stock.');
  355. }
  356. else {
  357. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  358. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  359. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  360. if ( $num_rows->count != 1) {
  361. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  362. }
  363. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  364. if ( $node->organism_id == 0) {
  365. form_set_error('organism_id', 'Please select a source organism for this stock');
  366. }
  367. else {
  368. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  369. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  370. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  371. if ( $num_rows->count != 1 ) {
  372. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  373. }
  374. // Check if Accession also database
  375. if ($node->accession != '') {
  376. if ($node->database == 0) {
  377. // there is an accession but no database selected
  378. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  379. }
  380. }
  381. else {
  382. if ($node->database > 0) {
  383. // there is a database selected but no accession
  384. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  385. }
  386. }
  387. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  388. if ( $node->database > 0) {
  389. $replace = array(':table' => 'db', ':column' => 'db_id');
  390. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  391. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  392. if ($num_rows->count != 1) {
  393. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  394. }
  395. }
  396. /**
  397. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  398. *
  399. * @param $node
  400. * The current node including fields with the form element names and submitted values
  401. *
  402. * @return
  403. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  404. *
  405. * @ingroup tripal_stock
  406. */
  407. function chado_stock_insert($node) {
  408. // If the chado stock exists (e.g. this is only a syncing operation)
  409. // then don't create but simply link to node
  410. if (isset($node->chado_stock_exists)) {
  411. if ($node->chado_stock_exists) {
  412. if (!empty($node->stock_id)) {
  413. db_insert('chado_stock')->fields(array(
  414. 'nid' => $node->nid,
  415. 'vid' => $node->vid,
  416. 'stock_id' => $node->stock_id
  417. ));
  418. }
  419. return $node;
  420. }
  421. }
  422. // if there is an stock_id in the $node object then this must be a sync so
  423. // we can skip adding the stock to chado as it is already there, although
  424. // we do need to proceed with the rest of the insert
  425. if (!property_exists($node, 'stock_id')) {
  426. // before we can add the stock, we must add the dbxref if one has been
  427. // provided by the user.
  428. $dbxref_status = 0;
  429. if (!empty($node->accession) ) {
  430. if (!empty($node->database) ) {
  431. $values = array(
  432. 'db_id' => $node->database,
  433. 'accession' => $node->accession,
  434. );
  435. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  436. $values['description'] = $node->db_description;
  437. $values['version'] = '1';
  438. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  439. if (!$dbxref_status) {
  440. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  441. watchdog('tripal_stock',
  442. 'Insert Stock: Unable to create dbxref where values:%values',
  443. array('%values' => print_r($values, TRUE)),
  444. WATCHDOG_WARNING
  445. );
  446. }
  447. }
  448. else {
  449. $dbxref_status = 1;
  450. }
  451. }
  452. }
  453. // create stock including the dbxref
  454. $stock = '';
  455. if ($dbxref_status) {
  456. $values = array(
  457. 'dbxref_id' => array(
  458. 'db_id' => $node->database,
  459. 'accession' => $node->accession
  460. ),
  461. 'organism_id' => $node->organism_id,
  462. 'name' => $node->sname,
  463. 'uniquename' => $node->uniquename,
  464. 'description' => $node->stock_description,
  465. 'type_id' => $node->type_id
  466. );
  467. $stock = tripal_core_chado_insert('stock', $values);
  468. }
  469. // create a stock without a dbxref
  470. else {
  471. $values = array(
  472. 'organism_id' => $node->organism_id,
  473. 'name' => $node->sname,
  474. 'uniquename' => $node->uniquename,
  475. 'description' => $node->stock_description,
  476. 'type_id' => $node->type_id
  477. );
  478. $stock = tripal_core_chado_insert('stock', $values);
  479. }
  480. if (is_array($stock)) {
  481. $stock_added = TRUE;
  482. }
  483. else {
  484. $stock_added = FALSE;
  485. }
  486. // Now add properties
  487. if ($stock_added) {
  488. $details = array(
  489. 'property_table' => 'stockprop',
  490. 'base_table' => 'stock',
  491. 'foreignkey_name' => 'stock_id',
  492. 'foreignkey_value' => $stock_id
  493. );
  494. chado_node_properties_form_update_properties($node, $details);
  495. }
  496. // Now add the additional references
  497. if ($stock_added) {
  498. tripal_core_relationships_form_update_relationships(
  499. $node,
  500. 'stock_dbxref',
  501. 'stock_id',
  502. $stock_id
  503. );
  504. }
  505. // Now add in relationships
  506. if ($stock_added) {
  507. tripal_core_relationships_form_update_dbxrefs(
  508. $node,
  509. 'stock_relationship',
  510. $stock_id
  511. );
  512. }
  513. } //end of adding stock to chado
  514. else {
  515. // stock already exists since this is a sync
  516. $stock_added = TRUE;
  517. $stock['stock_id'] = $node->stock_id;
  518. }
  519. // if the stock creation was succesful then add the URL and the entry in the
  520. // chado_stock table
  521. if ($stock_added) {
  522. // add the entry to the chado_stock table
  523. db_insert('chado_stock')->fields(array(
  524. 'nid' => (int) $node->nid,
  525. 'vid' => (int) $node->vid,
  526. 'stock_id' => (int) $stock['stock_id']
  527. ))->execute();
  528. }
  529. else {
  530. drupal_set_message(t('Error during stock creation.'), 'error');
  531. watchdog('tripal_stock',
  532. 'Insert Stock: Unable to create stock where values:%values',
  533. array('%values' => print_r($values, TRUE)),
  534. WATCHDOG_WARNING
  535. );
  536. return FALSE;
  537. }
  538. }
  539. /**
  540. * Implements hook_update(): Handles Editing/Updating of main stock info
  541. *
  542. * NOTE: Currently just writes over all old data
  543. *
  544. * @param $node
  545. * The current node including fields with the form element names and submitted values
  546. *
  547. * @return
  548. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  549. *
  550. * @ingroup tripal_stock
  551. */
  552. function chado_stock_update($node) {
  553. if ($node->revision) {
  554. // there is no way to handle revisions in Chado but leave
  555. // this here just to make not we've addressed it.
  556. }
  557. //update dbxref
  558. if ($node->database) {
  559. if ($node->accession) {
  560. $dbxref_mode = '';
  561. $stock = tripal_core_chado_select(
  562. 'stock',
  563. array('dbxref_id', 'type_id'),
  564. array('stock_id' => $node->stock_id)
  565. );
  566. if ($stock[0]->dbxref_id) {
  567. $values = array(
  568. 'db_id' => $node->database,
  569. 'accession' => $node->accession,
  570. 'description' => $node->db_description
  571. );
  572. $dbxref_status = tripal_core_chado_update(
  573. 'dbxref',
  574. array('dbxref_id' => $stock[0]->dbxref_id),
  575. $values
  576. );
  577. $dbxref_mode = 'Update';
  578. }
  579. else {
  580. if ($stock[0]->type_id) {
  581. //create the dbxref
  582. //used the type_id as a control to check we have a stock but not a dbxref
  583. $values = array(
  584. 'db_id' => $node->database,
  585. 'accession' => $node->accession,
  586. 'description' => $node->db_description,
  587. 'version' => '1',
  588. );
  589. $dbxref_status = tripal_core_chado_insert(
  590. 'dbxref',
  591. $values
  592. );
  593. $dbxref_mode = 'Create';
  594. }
  595. else {
  596. drupal_set_message(t('Unable to find stock to Update'), 'error');
  597. watchdog(
  598. 'tripal_stock',
  599. 'Stock Update: Unable to find stock to update using values: %values',
  600. array('%values', print_r($values, TRUE)),
  601. WATCHDOG_ERROR
  602. );
  603. return FALSE;
  604. }
  605. }
  606. }
  607. }
  608. if (!$dbxref_status) {
  609. watchdog(
  610. 'tripal_stock',
  611. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  612. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  613. WATCHDOG_WARNING
  614. );
  615. }
  616. //can't change stock id which is all thats stored in drupal thus only update chado
  617. $update_values = array(
  618. 'organism_id' => $node->organism_id,
  619. 'name' => $node->sname,
  620. 'uniquename' => $node->uniquename,
  621. 'description' => $node->stock_description,
  622. 'type_id' => $node->type_id,
  623. );
  624. if ($dbxref_status) {
  625. $update_values['dbxref_id'] = array(
  626. 'db_id' => $node->database,
  627. 'accession' => $node->accession
  628. );
  629. }
  630. $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
  631. if (!$status) {
  632. drupal_set_message(t('Unable to update stock'), 'error');
  633. watchdog(
  634. 'tripal_stock',
  635. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  636. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  637. WATCHDOG_ERROR
  638. );
  639. }
  640. else {
  641. // set the URL for this stock page
  642. $values = array('stock_id' => $node->stock_id);
  643. $stock = tripal_core_chado_select('stock', array('*'), $values);
  644. }
  645. // now update the properties
  646. if ($node->stock_id > 0) {
  647. $details = array(
  648. 'property_table' => 'stockprop',
  649. 'base_table' => 'stock',
  650. 'foreignkey_name' => 'stock_id',
  651. 'foreignkey_value' => $node->stock_id
  652. );
  653. chado_node_properties_form_update_properties($node, $details);
  654. }
  655. // now update the additional dbxrefs
  656. if ($node->stock_id > 0) {
  657. tripal_core_additional_dbxrefs_form_update_dbxrefs(
  658. $node,
  659. 'stock_dbxref',
  660. 'stock_id',
  661. $node->stock_id
  662. );
  663. }
  664. // now update relationships
  665. if ($node->stock_id > 0) {
  666. tripal_core_relationships_form_update_relationships(
  667. $node,
  668. 'stock_relationship',
  669. $node->stock_id
  670. );
  671. }
  672. }
  673. /**
  674. * Implements hook_delete(): Handles deleting of chado_stocks
  675. *
  676. * NOTE: Currently deletes data -no undo or record-keeping functionality
  677. *
  678. * @param $node
  679. * The current node including fields with the form element names and submitted values
  680. *
  681. * @return
  682. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  683. *
  684. * @ingroup tripal_stock
  685. */
  686. function chado_stock_delete($node) {
  687. // Set stock in chado: is_obsolete = TRUE
  688. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  689. //remove drupal node and all revisions
  690. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  691. }
  692. /**
  693. * Used by Tripal Chado Node API during sync'ing of nodes
  694. */
  695. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  696. $new_node->organism_id = $record->organism_id;
  697. $new_node->sname = $record->name;
  698. $new_node->uniquename = $record->uniquename;
  699. $new_node->type_id = $record->type_id;
  700. return $new_node;
  701. }
  702. /**
  703. * @ingroup tripal_stock
  704. */
  705. function tripal_stock_node_presave($node) {
  706. switch ($node->type) {
  707. case 'chado_stock':
  708. $values = array('organism_id' => $node->organism_id);
  709. $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
  710. $values = array('cvterm_id' => $node->type_id);
  711. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  712. $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  713. break;
  714. }
  715. }
  716. /**
  717. * @ingroup tripal_stock
  718. */
  719. function tripal_stock_node_insert($node) {
  720. switch ($node->type) {
  721. case 'chado_stock':
  722. if (!$node->stock_id) {
  723. $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
  724. $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  725. $node->stock_id = $chado_stock->stock_id;
  726. }
  727. // remove any previous alias
  728. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  729. // set the URL for this stock page
  730. $url_alias = tripal_stock_get_stock_url($node);
  731. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  732. path_save($path_alias);
  733. break;
  734. }
  735. }
  736. /**
  737. * @ingroup tripal_stock
  738. */
  739. function tripal_stock_node_update($node) {
  740. switch ($node->type) {
  741. case 'chado_stock':
  742. // remove any previous alias
  743. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  744. // set the URL for this stock page
  745. $url_alias = tripal_stock_get_stock_url($node);
  746. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  747. path_save($path_alias);
  748. break;
  749. }
  750. }
  751. /**
  752. * @ingroup tripal_stock
  753. */
  754. function tripal_stock_node_view($node, $view_mode, $langcode) {
  755. switch ($node->type) {
  756. case 'chado_stock':
  757. if ($view_mode == 'full') {
  758. $node->content['tripal_stock_base'] = array(
  759. '#value' => theme('tripal_stock_base', array('node' => $node)),
  760. );
  761. $node->content['tripal_stock_collections'] = array(
  762. '#value' => theme('tripal_stock_collections', array('node' => $node)),
  763. );
  764. $node->content['tripal_stock_properties'] = array(
  765. '#value' => theme('tripal_stock_properties', array('node' => $node)),
  766. );
  767. $node->content['tripal_stock_references'] = array(
  768. '#value' => theme('tripal_stock_references', array('node' => $node)),
  769. );
  770. $node->content['tripal_stock_relationships'] = array(
  771. '#value' => theme('tripal_stock_relationships', array('node' => $node)),
  772. );
  773. $node->content['tripal_stock_synonyms'] = array(
  774. '#value' => theme('tripal_stock_synonyms', array('node' => $node)),
  775. );
  776. $node->content['tripal_stock_publications'] = array(
  777. '#value' => theme('tripal_stock_publications', array('node' => $node)),
  778. );
  779. }
  780. if ($view_mode == 'teaser') {
  781. $node->content['tripal_stock_teaser'] = array(
  782. '#value' => theme('tripal_stock_teaser', array('node' => $node)),
  783. );
  784. }
  785. break;
  786. case 'chado_organism':
  787. if ($view_mode == 'full') {
  788. $node->content['tripal_organism_stocks'] = array(
  789. '#value' => theme('tripal_organism_stocks', $node),
  790. );
  791. }
  792. break;
  793. }
  794. }