tripal_stock.chado_node.inc 30 KB

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