tripal_stock.chado_node.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. if (isset($node->stock->dbxref_id->db_id)) {
  155. $dbxref_accession = $node->stock->dbxref_id->accession;
  156. $dbxref_description = $node->stock->dbxref_id->description;
  157. $dbxref_database = $node->stock->dbxref_id->db_id->db_id;
  158. }
  159. }
  160. // 2) if we are re constructing the form from a failed validation or ajax callback
  161. // then use the $form_state['values'] values
  162. if (array_key_exists('values', $form_state)) {
  163. $sname = $form_state['values']['sname'];
  164. $uniquename = $form_state['values']['uniquename'];
  165. $stock_id = $form_state['values']['stock_id'];
  166. $type_id = $form_state['values']['type_id'];
  167. $organism_id = $form_state['values']['organism_id'];
  168. $sdescription = $form_state['values']['description'];
  169. $dbxref_accession = $form_state['values']['accession'];
  170. $dbxref_description = $form_state['values']['db_description'];
  171. $dbxref_database = $form_state['values']['database'];
  172. }
  173. // 3) if we are re building the form from after submission (from ajax call) then
  174. // the values are in the $form_state['input'] array
  175. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  176. $sname = $form_state['input']['sname'];
  177. $uniquename = $form_state['input']['uniquename'];
  178. $stock_id = $form_state['input']['stock_id'];
  179. $type_id = $form_state['input']['type_id'];
  180. $organism_id = $form_state['input']['organism_id'];
  181. $sdescription = $form_state['input']['description'];
  182. $dbxref_accession = $form_state['input']['accession'];
  183. $dbxref_description = $form_state['input']['db_description'];
  184. $dbxref_database = $form_state['input']['database'];
  185. }
  186. $form['manually_edit_chado'] = array(
  187. '#type' => 'hidden',
  188. '#value' => TRUE,
  189. );
  190. $form['names'] = array(
  191. '#type' => 'fieldset',
  192. '#title' => t('Stock Name')
  193. );
  194. $form['names']['sname'] = array(
  195. '#type' => 'textfield',
  196. '#title' => t('Name'),
  197. '#default_value' => $sname,
  198. '#required' => TRUE
  199. );
  200. $form['names']['uniquename'] = array(
  201. '#type' => 'textfield',
  202. '#title' => t('Unique Name'),
  203. '#default_value' => $uniquename,
  204. '#required' => TRUE
  205. );
  206. $form['names']['stock_id'] = array(
  207. '#type' => 'hidden',
  208. '#value' => $stock_id,
  209. );
  210. $form['details'] = array(
  211. '#type' => 'fieldset',
  212. '#title' => t('Stock Details')
  213. );
  214. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  215. $type_options[0] = 'Select a Type';
  216. $form['details']['type_id'] = array(
  217. '#type' => 'select',
  218. '#title' => t('Type of Stock'),
  219. '#options' => $type_options,
  220. '#default_value' => $type_id,
  221. '#required' => TRUE,
  222. );
  223. // get the list of organisms
  224. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  225. $org_rset = chado_query($sql);
  226. $organisms = array();
  227. $organisms[''] = '';
  228. while ($organism = $org_rset->fetchObject()) {
  229. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  230. }
  231. $form['details']['organism_id'] = array(
  232. '#type' => 'select',
  233. '#title' => t('Source Organism for stock'),
  234. '#default_value' => $organism_id,
  235. '#options' => $organisms,
  236. '#required' => TRUE
  237. );
  238. $form['details']['stock_description'] = array(
  239. '#type' => 'textarea',
  240. '#title' => t('Notes'),
  241. '#default_value' => $sdescription,
  242. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  243. );
  244. $form['database_reference'] = array(
  245. '#type' => 'fieldset',
  246. '#title' => t('Stock Database Reference')
  247. );
  248. $form['database_reference']['accession'] = array(
  249. '#type' => 'textfield',
  250. '#title' => t('Accession'),
  251. '#default_value' => $dbxref_accession,
  252. );
  253. $form['database_reference']['db_description'] = array(
  254. '#type' => 'textarea',
  255. '#title' => t('Description of Database Reference'),
  256. '#default_value' => $dbxref_description,
  257. '#description' => t('Optionally enter a description about the database accession.')
  258. );
  259. $db_options = tripal_db_get_db_options();
  260. $db_options[0] = 'Select a Database';
  261. $form['database_reference']['database'] = array(
  262. '#type' => 'select',
  263. '#title' => t('Database'),
  264. '#options' => $db_options,
  265. '#default_value' => $dbxref_database
  266. );
  267. // PROPERTIES FORM
  268. //---------------------------------------------
  269. $details = array(
  270. 'property_table' => 'stockprop',
  271. 'base_foreign_key' => 'stock_id',
  272. 'base_key_value' => $stock_id,
  273. 'cv_id' => variable_get('chado_stock_prop_types_cv', FALSE)
  274. );
  275. chado_node_properties_form($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_node_additional_dbxrefs_form($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_node_relationships_form($form, $form_state, $details);
  295. return $form;
  296. }
  297. /**
  298. * Implements hook_validate(): Validate the input from the chado_stock node form
  299. *
  300. * @param $node
  301. * The current node including fields with the form element names and submitted values
  302. * @param $form
  303. * A description of the form to be rendered by drupal_get_form()
  304. *
  305. * @ingroup tripal_stock
  306. */
  307. function chado_stock_validate(&$node, $form, &$form_state) {
  308. // remove surrounding whitespace
  309. $node->uniquename = trim($node->uniquename);
  310. $node->sname = trim($node->sname);
  311. $node->stock_description = trim($node->stock_description);
  312. $node->accession = trim($node->accession);
  313. $node->db_description = trim($node->db_description);
  314. // if this is a delete then don't validate
  315. if($node->op == 'Delete') {
  316. return;
  317. }
  318. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  319. // need to validate during syncing so just skip it.
  320. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  321. return;
  322. }
  323. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  324. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  325. // if this is an update, we want to make sure that a different stock for
  326. // the organism doesn't already have this uniquename. We don't want to give
  327. // two sequences the same uniquename
  328. if (property_exists($node, 'nid')) {
  329. $sql = "
  330. SELECT *
  331. FROM {stock} S
  332. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  333. WHERE
  334. uniquename = :uname AND organism_id = :organism_id AND
  335. CVT.name = :cvtname AND NOT stock_id = :stock_id
  336. ";
  337. $result = chado_query($sql, array(':uname' => $node->uniquename,
  338. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  339. ':stock_id' => $node->stock_id))->fetchObject();
  340. if ($result) {
  341. 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."));
  342. }
  343. }
  344. // if this is an insert then we just need to make sure this name doesn't
  345. // already exist for this organism if it does then we need to throw an error
  346. else {
  347. $sql = "
  348. SELECT *
  349. FROM {Stock} S
  350. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  351. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  352. $result = chado_query($sql, array(':uname' => $node->uniquename,
  353. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  354. if ($result) {
  355. 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."));
  356. }
  357. }
  358. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  359. if ($node->type_id == 0) {
  360. form_set_error('type_id', 'Please select a type of stock.');
  361. }
  362. else {
  363. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  364. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  365. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  366. if ( $num_rows->count != 1) {
  367. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  368. }
  369. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  370. if ( $node->organism_id == 0) {
  371. form_set_error('organism_id', 'Please select a source organism for this stock');
  372. }
  373. else {
  374. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  375. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  376. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  377. if ( $num_rows->count != 1 ) {
  378. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  379. }
  380. // Check if Accession also database
  381. if ($node->accession != '') {
  382. if ($node->database == 0) {
  383. // there is an accession but no database selected
  384. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  385. }
  386. }
  387. else {
  388. if ($node->database > 0) {
  389. // there is a database selected but no accession
  390. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  391. }
  392. }
  393. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  394. if ( $node->database > 0) {
  395. $replace = array(':table' => 'db', ':column' => 'db_id');
  396. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  397. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  398. if ($num_rows->count != 1) {
  399. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  400. }
  401. }
  402. /**
  403. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  404. *
  405. * @param $node
  406. * The current node including fields with the form element names and submitted values
  407. *
  408. * @return
  409. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  410. *
  411. * @ingroup tripal_stock
  412. */
  413. function chado_stock_insert($node) {
  414. // if there is an stock_id in the $node object then this must be a sync so
  415. // we can skip adding the stock to chado as it is already there, although
  416. // we do need to proceed with the rest of the insert
  417. if (property_exists($node, 'manually_edit_chado')) {
  418. // before we can add the stock, we must add the dbxref if one has been
  419. // provided by the user.
  420. $dbxref_status = 0;
  421. if (!empty($node->accession) ) {
  422. if (!empty($node->database) ) {
  423. $values = array(
  424. 'db_id' => $node->database,
  425. 'accession' => $node->accession,
  426. );
  427. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  428. $values['description'] = $node->db_description;
  429. $values['version'] = '1';
  430. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  431. if (!$dbxref_status) {
  432. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  433. watchdog('tripal_stock',
  434. 'Insert Stock: Unable to create dbxref where values:%values',
  435. array('%values' => print_r($values, TRUE)),
  436. WATCHDOG_WARNING
  437. );
  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 = tripal_core_chado_insert('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 = tripal_core_chado_insert('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_node_properties_form_update_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_node_additional_dbxrefs_form_update_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_node_relationships_form_update_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. watchdog('tripal_stock',
  521. 'Insert Stock: Unable to create stock where values:%values',
  522. array('%values' => print_r($values, TRUE)),
  523. WATCHDOG_WARNING
  524. );
  525. return FALSE;
  526. }
  527. }
  528. /**
  529. * Implements hook_update(): Handles Editing/Updating of main stock info
  530. *
  531. * NOTE: Currently just writes over all old data
  532. *
  533. * @param $node
  534. * The current node including fields with the form element names and submitted values
  535. *
  536. * @return
  537. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  538. *
  539. * @ingroup tripal_stock
  540. */
  541. function chado_stock_update($node) {
  542. if ($node->revision) {
  543. // there is no way to handle revisions in Chado but leave
  544. // this here just to make not we've addressed it.
  545. }
  546. //update dbxref
  547. $dbxref_status = NULL;
  548. $dbxref_present = FALSE;
  549. if ($node->database) {
  550. $dbxref_present = TRUE;
  551. if ($node->accession) {
  552. $dbxref_mode = '';
  553. $stock = tripal_core_chado_select(
  554. 'stock',
  555. array('dbxref_id', 'type_id'),
  556. array('stock_id' => $node->stock_id)
  557. );
  558. if ($stock[0]->dbxref_id) {
  559. $values = array(
  560. 'db_id' => $node->database,
  561. 'accession' => $node->accession,
  562. 'description' => $node->db_description
  563. );
  564. $dbxref_status = tripal_core_chado_update(
  565. 'dbxref',
  566. array('dbxref_id' => $stock[0]->dbxref_id),
  567. $values
  568. );
  569. $dbxref_mode = 'Update';
  570. }
  571. else {
  572. if ($stock[0]->type_id) {
  573. //create the dbxref
  574. //used the type_id as a control to check we have a stock but not a dbxref
  575. $values = array(
  576. 'db_id' => $node->database,
  577. 'accession' => $node->accession,
  578. 'description' => $node->db_description,
  579. 'version' => '1',
  580. );
  581. $dbxref_status = tripal_core_chado_insert(
  582. 'dbxref',
  583. $values
  584. );
  585. $dbxref_mode = 'Create';
  586. }
  587. else {
  588. drupal_set_message(t('Unable to find stock to Update'), 'error');
  589. watchdog(
  590. 'tripal_stock',
  591. 'Stock Update: Unable to find stock to update using values: %values',
  592. array('%values', print_r($values, TRUE)),
  593. WATCHDOG_ERROR
  594. );
  595. return FALSE;
  596. }
  597. }
  598. }
  599. if (!$dbxref_status) {
  600. watchdog(
  601. 'tripal_stock',
  602. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  603. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  604. WATCHDOG_WARNING
  605. );
  606. }
  607. }
  608. //can't change stock id which is all thats stored in drupal thus only update chado
  609. $update_values = array(
  610. 'organism_id' => $node->organism_id,
  611. 'name' => $node->sname,
  612. 'uniquename' => $node->uniquename,
  613. 'description' => $node->stock_description,
  614. 'type_id' => $node->type_id,
  615. );
  616. if ($dbxref_present) {
  617. if ($dbxref_status) {
  618. $update_values['dbxref_id'] = array(
  619. 'db_id' => $node->database,
  620. 'accession' => $node->accession
  621. );
  622. }
  623. }
  624. $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
  625. if (!$status) {
  626. drupal_set_message(t('Unable to update stock'), 'error');
  627. watchdog(
  628. 'tripal_stock',
  629. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  630. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  631. WATCHDOG_ERROR
  632. );
  633. }
  634. else {
  635. // set the URL for this stock page
  636. $values = array('stock_id' => $node->stock_id);
  637. $stock = tripal_core_chado_select('stock', array('*'), $values);
  638. }
  639. // now update the properties
  640. if ($node->stock_id > 0) {
  641. $details = array(
  642. 'property_table' => 'stockprop',
  643. 'base_table' => 'stock',
  644. 'foreignkey_name' => 'stock_id',
  645. 'foreignkey_value' => $node->stock_id
  646. );
  647. chado_node_properties_form_update_properties($node, $details);
  648. }
  649. // now update the additional dbxrefs
  650. if ($node->stock_id > 0) {
  651. $details = array(
  652. 'linking_table' => 'stock_dbxref',
  653. 'foreignkey_name' => 'stock_id',
  654. 'foreignkey_value' => $node->stock_id
  655. );
  656. chado_node_additional_dbxrefs_form_update_dbxrefs($node, $details);
  657. }
  658. // now update relationships
  659. if ($node->stock_id > 0) {
  660. $details = array(
  661. 'relationship_table' => 'stock_relationship',
  662. 'foreignkey_value' => $node->stock_id
  663. );
  664. chado_node_relationships_form_update_relationships($node, $details);
  665. }
  666. }
  667. /**
  668. * Implements hook_delete(): Handles deleting of chado_stocks
  669. *
  670. * NOTE: Currently deletes data -no undo or record-keeping functionality
  671. *
  672. * @param $node
  673. * The current node including fields with the form element names and submitted values
  674. *
  675. * @return
  676. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  677. *
  678. * @ingroup tripal_stock
  679. */
  680. function chado_stock_delete($node) {
  681. // Set stock in chado: is_obsolete = TRUE
  682. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  683. //remove drupal node and all revisions
  684. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  685. }
  686. /**
  687. * Used by Tripal Chado Node API during sync'ing of nodes
  688. */
  689. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  690. $new_node->organism_id = $record->organism_id;
  691. $new_node->sname = $record->name;
  692. $new_node->uniquename = $record->uniquename;
  693. $new_node->type_id = $record->type_id;
  694. return $new_node;
  695. }
  696. /**
  697. * @ingroup tripal_stock
  698. */
  699. function tripal_stock_node_presave($node) {
  700. switch ($node->type) {
  701. case 'chado_stock':
  702. // for a form submission the fields part of the node object
  703. // but for a sync the feilds are in an object of the node
  704. $organism_id = null;
  705. $sname = '';
  706. $uniquename = '';
  707. $type = '';
  708. if(property_exists($node, 'organism_id')) {
  709. $organism_id = $node->organism_id;
  710. $sname = $node->sname;
  711. $uniquename = $node->uniquename;
  712. $type_id = $node->type_id;
  713. $values = array('cvterm_id' => $node->type_id);
  714. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  715. $type = $cvterm[0]->name;
  716. }
  717. else {
  718. $organism_id = $node->stock->organism_id;
  719. $sname = $node->stock->name;
  720. $uniquename = $node->stock->uniquename;
  721. $type = $node->stock->type_id->name;
  722. }
  723. $values = array('organism_id' => $organism_id);
  724. $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
  725. $node->title = "$sname, $uniquename ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  726. if ($name == $uname) {
  727. $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  728. }
  729. break;
  730. }
  731. }
  732. /**
  733. * @ingroup tripal_stock
  734. */
  735. function tripal_stock_node_insert($node) {
  736. switch ($node->type) {
  737. case 'chado_stock':
  738. if (!$node->stock_id) {
  739. $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
  740. $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  741. $node->stock_id = $chado_stock->stock_id;
  742. }
  743. // remove any previous alias
  744. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  745. // set the URL for this stock page
  746. $url_alias = tripal_stock_get_stock_url($node);
  747. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  748. path_save($path_alias);
  749. break;
  750. }
  751. }
  752. /**
  753. * @ingroup tripal_stock
  754. */
  755. function tripal_stock_node_update($node) {
  756. switch ($node->type) {
  757. case 'chado_stock':
  758. // remove any previous alias
  759. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  760. // set the URL for this stock page
  761. $url_alias = tripal_stock_get_stock_url($node);
  762. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  763. path_save($path_alias);
  764. break;
  765. }
  766. }
  767. /**
  768. * @ingroup tripal_stock
  769. */
  770. function tripal_stock_node_view($node, $view_mode, $langcode) {
  771. switch ($node->type) {
  772. case 'chado_stock':
  773. if ($view_mode == 'full') {
  774. $node->content['tripal_stock_base'] = array(
  775. '#markup' => theme('tripal_stock_base', array('node' => $node)),
  776. '#tripal_toc_id' => 'base',
  777. '#tripal_toc_title' => 'Details',
  778. '#weight' => -100,
  779. );
  780. $node->content['tripal_stock_collections'] = array(
  781. '#markup' => theme('tripal_stock_collections', array('node' => $node)),
  782. '#tripal_toc_id' => 'collections',
  783. '#tripal_toc_title' => 'Stock Collections',
  784. );
  785. $node->content['tripal_stock_properties'] = array(
  786. '#markup' => theme('tripal_stock_properties', array('node' => $node)),
  787. '#tripal_toc_id' => 'properties',
  788. '#tripal_toc_title' => 'Properties',
  789. );
  790. $node->content['tripal_stock_references'] = array(
  791. '#markup' => theme('tripal_stock_references', array('node' => $node)),
  792. '#tripal_toc_id' => 'references',
  793. '#tripal_toc_title' => 'References',
  794. );
  795. $node->content['tripal_stock_relationships'] = array(
  796. '#markup' => theme('tripal_stock_relationships', array('node' => $node)),
  797. '#tripal_toc_id' => 'relationships',
  798. '#tripal_toc_title' => 'Relationships',
  799. );
  800. $node->content['tripal_stock_synonyms'] = array(
  801. '#markup' => theme('tripal_stock_synonyms', array('node' => $node)),
  802. '#tripal_toc_id' => 'synonyms',
  803. '#tripal_toc_title' => 'Synonyms',
  804. );
  805. $node->content['tripal_stock_publications'] = array(
  806. '#markup' => theme('tripal_stock_publications', array('node' => $node)),
  807. '#tripal_toc_id' => 'publications',
  808. '#tripal_toc_title' => 'Publications',
  809. );
  810. }
  811. if ($view_mode == 'teaser') {
  812. $node->content['tripal_stock_teaser'] = array(
  813. '#markup' => theme('tripal_stock_teaser', array('node' => $node)),
  814. );
  815. }
  816. break;
  817. case 'chado_organism':
  818. if ($view_mode == 'full') {
  819. $node->content['tripal_organism_stocks'] = array(
  820. '#markup' => theme('tripal_organism_stocks', array('node' => $node)),
  821. '#tripal_toc_id' => 'stocks',
  822. '#tripal_toc_title' => 'Stocks',
  823. );
  824. }
  825. break;
  826. }
  827. }