tripal_stock.chado_node.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. 'locked' => TRUE,
  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', $nid);
  56. // if the nid does not have a matching record then skip this node.
  57. // this can happen with orphaned nodes.
  58. if (!$stock_id) {
  59. continue;
  60. }
  61. // build the variable with all the stock details
  62. $values = array('stock_id' => $stock_id);
  63. $stock = chado_generate_var('stock', $values);
  64. // add in the uniquename and the description as these are both text fields
  65. $stock = chado_expand_var($stock, 'field', 'stock.uniquename');
  66. $stock = chado_expand_var($stock, 'field', 'stock.description');
  67. // by default, the titles are saved using the unique constraint. We will
  68. // keep it the same, but remove the duplicate name if the unique name and name
  69. // are identical
  70. $title_type = variable_get('chado_stock_title', 'unique_constraint');
  71. if($title_type == 'unique_constraint') {
  72. if (strcmp($stock->name, $stock->uniquename)==0) {
  73. $node->title = $stock->name . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  74. }
  75. // in previous version of Tripal, the stock title was simply the unique name.
  76. // so, we recreate the title just to be sure all of our stock pages are consistent
  77. else {
  78. $node->title = $stock->name . ", " . $stock->uniquename . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  79. }
  80. }
  81. // set the title to be the stock name or uniquename as configured
  82. if($title_type == 'stock_name') {
  83. $node->title = $stock->name;
  84. }
  85. if($title_type == 'stock_unique_name') {
  86. $node->title = $stock->uniquename;
  87. }
  88. // add this to the node
  89. $node->stock = $stock;
  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. /* I don't think we need this... commenting out but leaving just in case
  118. // If existing stock then expand all fields needed using the chado API
  119. if (isset($node->nid)) {
  120. $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');
  121. foreach ($fields_needed as $field_name) {
  122. // Check to see if it's excluded and expand it if so
  123. if (isset($node->expandable_fields)) {
  124. if (in_array($field_name, $node->expandable_fields)) {
  125. $node = chado_expand_var($node, 'field', $field_name);
  126. }
  127. }
  128. }
  129. }
  130. */
  131. //TODO: @lacey can you take a look at the above code?
  132. // Default values can come in the following ways:
  133. //
  134. // 1) as elements of the $node object. This occurs when editing an existing stock
  135. // 2) in the $form_state['values'] array which occurs on a failed validation or
  136. // ajax callbacks from non submit form elements
  137. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  138. // form elements and the form is being rebuilt
  139. //
  140. // set form field defaults
  141. $sname = '';
  142. $uniquename = '';
  143. $stock_id = 0;
  144. $type_id = 0;
  145. $organism_id = 0;
  146. $sdescription = '';
  147. $dbxref_accession = '';
  148. $dbxref_description = '';
  149. $dbxref_database = 0;
  150. // 1) if we are editing an existing node then the stock is already part of the node
  151. if (property_exists($node, 'stock')) {
  152. $sname = $node->stock->name;
  153. $uniquename = $node->stock->uniquename;
  154. $stock_id = $node->stock->stock_id;
  155. $type_id = $node->stock->type_id->cvterm_id;
  156. $organism_id = $node->stock->organism_id->organism_id;
  157. $sdescription = $node->stock->description;
  158. if (isset($node->stock->dbxref_id->db_id)) {
  159. $dbxref_accession = $node->stock->dbxref_id->accession;
  160. $dbxref_description = $node->stock->dbxref_id->description;
  161. $dbxref_database = $node->stock->dbxref_id->db_id->db_id;
  162. }
  163. }
  164. // 2) if we are re constructing the form from a failed validation or ajax callback
  165. // then use the $form_state['values'] values
  166. if (array_key_exists('values', $form_state)) {
  167. $sname = $form_state['values']['sname'];
  168. $uniquename = $form_state['values']['uniquename'];
  169. $stock_id = $form_state['values']['stock_id'];
  170. $type_id = $form_state['values']['type_id'];
  171. $organism_id = $form_state['values']['organism_id'];
  172. $sdescription = $form_state['values']['description'];
  173. $dbxref_accession = $form_state['values']['accession'];
  174. $dbxref_description = $form_state['values']['db_description'];
  175. $dbxref_database = $form_state['values']['database'];
  176. }
  177. // 3) if we are re building the form from after submission (from ajax call) then
  178. // the values are in the $form_state['input'] array
  179. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  180. $sname = $form_state['input']['sname'];
  181. $uniquename = $form_state['input']['uniquename'];
  182. $stock_id = $form_state['input']['stock_id'];
  183. $type_id = $form_state['input']['type_id'];
  184. $organism_id = $form_state['input']['organism_id'];
  185. $sdescription = $form_state['input']['description'];
  186. $dbxref_accession = $form_state['input']['accession'];
  187. $dbxref_description = $form_state['input']['db_description'];
  188. $dbxref_database = $form_state['input']['database'];
  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_get_cvterm_default_select_options('stock', 'type_id', 'stock types');
  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. $prop_cv = tripal_get_default_cv('stockprop', 'type_id');
  270. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  271. $details = array(
  272. 'property_table' => 'stockprop',
  273. 'chado_id' => $stock_id,
  274. 'cv_id' => $cv_id
  275. );
  276. chado_add_node_form_properties($form, $form_state, $details);
  277. // ADDITIONAL DBXREFS FORM
  278. //---------------------------------------------
  279. $details = array(
  280. 'linking_table' => 'stock_dbxref',
  281. 'base_foreign_key' => 'stock_id',
  282. 'base_key_value' => $stock_id
  283. );
  284. chado_add_node_form_dbxrefs($form, $form_state, $details);
  285. // RELATIONSHIPS FORM
  286. //---------------------------------------------
  287. $relationship_cv = tripal_get_default_cv('stock_relationship', 'type_id');
  288. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  289. $details = array(
  290. 'relationship_table' => 'stock_relationship',
  291. 'base_table' => 'stock',
  292. 'base_foreign_key' => 'stock_id',
  293. 'base_key_value' => $stock_id,
  294. 'nodetype' => 'stock',
  295. 'cv_id' => $cv_id
  296. );
  297. chado_add_node_form_relationships($form, $form_state, $details);
  298. return $form;
  299. }
  300. /**
  301. * Implements hook_validate().
  302. * Validate the input from the chado_stock node form
  303. *
  304. * @param $node
  305. * The current node including fields with the form element names and submitted values
  306. * @param $form
  307. * A description of the form to be rendered by drupal_get_form()
  308. *
  309. * @ingroup tripal_stock
  310. */
  311. function chado_stock_validate(&$node, $form, &$form_state) {
  312. // if this is a delete then don't validate
  313. if($node->op == 'Delete') {
  314. return;
  315. }
  316. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  317. // need to validate during syncing so just skip it.
  318. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  319. return;
  320. }
  321. // remove surrounding whitespace
  322. $node->uniquename = trim($node->uniquename);
  323. $node->sname = trim($node->sname);
  324. $node->stock_description = trim($node->stock_description);
  325. $node->accession = trim($node->accession);
  326. $node->db_description = trim($node->db_description);
  327. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  328. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  329. // if this is an update, we want to make sure that a different stock for
  330. // the organism doesn't already have this uniquename. We don't want to give
  331. // two sequences the same uniquename
  332. if (property_exists($node, 'nid')) {
  333. $sql = "
  334. SELECT *
  335. FROM {stock} S
  336. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  337. WHERE
  338. uniquename = :uname AND organism_id = :organism_id AND
  339. CVT.name = :cvtname AND NOT stock_id = :stock_id
  340. ";
  341. $result = chado_query($sql, array(':uname' => $node->uniquename,
  342. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  343. ':stock_id' => $node->stock_id))->fetchObject();
  344. if ($result) {
  345. 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."));
  346. }
  347. }
  348. // if this is an insert then we just need to make sure this name doesn't
  349. // already exist for this organism if it does then we need to throw an error
  350. else {
  351. $sql = "
  352. SELECT *
  353. FROM {Stock} S
  354. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  355. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  356. $result = chado_query($sql, array(':uname' => $node->uniquename,
  357. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  358. if ($result) {
  359. 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."));
  360. }
  361. }
  362. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  363. if ($node->type_id == 0) {
  364. form_set_error('type_id', 'Please select a type of stock.');
  365. }
  366. else {
  367. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  368. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  369. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  370. if ( $num_rows->count != 1) {
  371. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  372. }
  373. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  374. if ( $node->organism_id == 0) {
  375. form_set_error('organism_id', 'Please select a source organism for this stock');
  376. }
  377. else {
  378. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  379. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  380. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  381. if ( $num_rows->count != 1 ) {
  382. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  383. }
  384. // Check if Accession also database
  385. if ($node->accession != '') {
  386. if ($node->database == 0) {
  387. // there is an accession but no database selected
  388. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  389. }
  390. }
  391. else {
  392. if ($node->database > 0) {
  393. // there is a database selected but no accession
  394. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  395. }
  396. }
  397. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  398. if ( $node->database > 0) {
  399. $replace = array(':table' => 'db', ':column' => 'db_id');
  400. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  401. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  402. if ($num_rows->count != 1) {
  403. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  404. }
  405. }
  406. /**
  407. * Implements hook_insert().
  408. * Inserts data from chado_stock_form() into drupal and chado
  409. *
  410. * @param $node
  411. * The current node including fields with the form element names and submitted values
  412. *
  413. * @return
  414. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  415. *
  416. * @ingroup tripal_stock
  417. */
  418. function chado_stock_insert($node) {
  419. $node->uniquename = trim($node->uniquename);
  420. $node->sname = trim($node->sname);
  421. // if there is an stock_id in the $node object then this must be a sync so
  422. // we can skip adding the stock to chado as it is already there, although
  423. // we do need to proceed with the rest of the insert
  424. if (!property_exists($node, 'stock_id')) {
  425. // before we can add the stock, we must add the dbxref if one has been
  426. // provided by the user.
  427. $dbxref_status = 0;
  428. if (!empty($node->accession) ) {
  429. if (!empty($node->database) ) {
  430. $values = array(
  431. 'db_id' => $node->database,
  432. 'accession' => $node->accession,
  433. );
  434. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  435. $values['description'] = $node->db_description;
  436. $values['version'] = '1';
  437. $dbxref_status = chado_insert_record('dbxref', $values);
  438. if (!$dbxref_status) {
  439. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  440. tripal_report_error('tripal_stock', TRIPAL_WARNING,
  441. 'Insert Stock: Unable to create dbxref where values:%values',
  442. array('%values' => print_r($values, TRUE)));
  443. }
  444. }
  445. else {
  446. $dbxref_status = 1;
  447. }
  448. }
  449. }
  450. // create stock including the dbxref
  451. $stock = '';
  452. if ($dbxref_status) {
  453. $values = array(
  454. 'dbxref_id' => array(
  455. 'db_id' => $node->database,
  456. 'accession' => $node->accession
  457. ),
  458. 'organism_id' => $node->organism_id,
  459. 'name' => $node->sname,
  460. 'uniquename' => $node->uniquename,
  461. 'description' => $node->stock_description,
  462. 'type_id' => $node->type_id
  463. );
  464. $stock = chado_insert_record('stock', $values);
  465. }
  466. // create a stock without a dbxref
  467. else {
  468. $values = array(
  469. 'organism_id' => $node->organism_id,
  470. 'name' => $node->sname,
  471. 'uniquename' => $node->uniquename,
  472. 'description' => $node->stock_description,
  473. 'type_id' => $node->type_id
  474. );
  475. $stock = chado_insert_record('stock', $values);
  476. }
  477. if (is_array($stock)) {
  478. $stock_added = TRUE;
  479. $stock_id = $stock['stock_id'];
  480. }
  481. else {
  482. $stock_added = FALSE;
  483. }
  484. if ($stock_added) {
  485. // Now add properties
  486. $details = array(
  487. 'property_table' => 'stockprop',
  488. 'base_table' => 'stock',
  489. 'foreignkey_name' => 'stock_id',
  490. 'foreignkey_value' => $stock_id
  491. );
  492. chado_update_node_form_properties($node, $details);
  493. // Now add the additional references
  494. $details = array(
  495. 'linking_table' => 'stock_dbxref',
  496. 'foreignkey_name' => 'stock_id',
  497. 'foreignkey_value' => $stock_id
  498. );
  499. chado_update_node_form_dbxrefs($node, $details);
  500. // Now add in relationships
  501. $details = array(
  502. 'relationship_table' => 'stock_relationship',
  503. 'foreignkey_value' => $node->stock_id
  504. );
  505. chado_update_node_form_relationships($node, $details);
  506. }
  507. } //end of adding stock to chado
  508. else {
  509. // stock already exists since this is a sync
  510. $stock_added = TRUE;
  511. $stock['stock_id'] = $node->stock_id;
  512. }
  513. // if the stock creation was succesful then add the URL and the entry in the
  514. // chado_stock table
  515. if ($stock_added) {
  516. // add the entry to the chado_stock table
  517. db_insert('chado_stock')->fields(array(
  518. 'nid' => (int) $node->nid,
  519. 'vid' => (int) $node->vid,
  520. 'stock_id' => (int) $stock['stock_id']
  521. ))->execute();
  522. }
  523. else {
  524. drupal_set_message(t('Error during stock creation.'), 'error');
  525. tripal_report_error('tripal_stock', TRIPAL_WARNING,
  526. 'Insert Stock: Unable to create stock where values:%values',
  527. array('%values' => print_r($values, TRUE)));
  528. return FALSE;
  529. }
  530. }
  531. /**
  532. * Implements hook_update().
  533. * Handles Editing/Updating of main stock info
  534. *
  535. * NOTE: Currently just writes over all old data
  536. *
  537. * @param $node
  538. * The current node including fields with the form element names and submitted values
  539. *
  540. * @return
  541. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  542. *
  543. * @ingroup tripal_stock
  544. */
  545. function chado_stock_update($node) {
  546. $node->uniquename = trim($node->uniquename);
  547. $node->sname = trim($node->sname);
  548. if ($node->revision) {
  549. // there is no way to handle revisions in Chado but leave
  550. // this here just to make not we've addressed it.
  551. }
  552. //update dbxref
  553. $dbxref_status = NULL;
  554. $dbxref_present = FALSE;
  555. if ($node->database) {
  556. $dbxref_present = TRUE;
  557. if ($node->accession) {
  558. $dbxref_mode = '';
  559. $stock = chado_select_record(
  560. 'stock',
  561. array('dbxref_id', 'type_id'),
  562. array('stock_id' => $node->stock_id)
  563. );
  564. if ($stock[0]->dbxref_id) {
  565. $values = array(
  566. 'db_id' => $node->database,
  567. 'accession' => $node->accession,
  568. 'description' => $node->db_description
  569. );
  570. $dbxref_status = chado_update_record(
  571. 'dbxref',
  572. array('dbxref_id' => $stock[0]->dbxref_id),
  573. $values
  574. );
  575. $dbxref_mode = 'Update';
  576. }
  577. else {
  578. if ($stock[0]->type_id) {
  579. //create the dbxref
  580. //used the type_id as a control to check we have a stock but not a dbxref
  581. $values = array(
  582. 'db_id' => $node->database,
  583. 'accession' => $node->accession,
  584. 'description' => $node->db_description,
  585. 'version' => '1',
  586. );
  587. $dbxref_status = chado_insert_record(
  588. 'dbxref',
  589. $values
  590. );
  591. $dbxref_mode = 'Create';
  592. }
  593. else {
  594. drupal_set_message(t('Unable to find stock to Update'), 'error');
  595. tripal_report_error('tripal_stock', TRIPAL_ERROR,
  596. 'Stock Update: Unable to find stock to update using values: %values',
  597. array('%values', print_r($values, TRUE))
  598. );
  599. return FALSE;
  600. }
  601. }
  602. }
  603. if (!$dbxref_status) {
  604. tripal_report_error('tripal_stock', TRIPAL_WARNING,
  605. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  606. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode));
  607. }
  608. }
  609. //can't change stock id which is all thats stored in drupal thus only update chado
  610. $update_values = array(
  611. 'organism_id' => $node->organism_id,
  612. 'name' => $node->sname,
  613. 'uniquename' => $node->uniquename,
  614. 'description' => $node->stock_description,
  615. 'type_id' => $node->type_id,
  616. );
  617. if ($dbxref_present) {
  618. if ($dbxref_status) {
  619. $update_values['dbxref_id'] = array(
  620. 'db_id' => $node->database,
  621. 'accession' => $node->accession
  622. );
  623. }
  624. }
  625. $status = chado_update_record('stock', array('stock_id' => $node->stock_id), $update_values);
  626. if (!$status) {
  627. drupal_set_message(t('Unable to update stock'), 'error');
  628. tripal_report_error('tripal_stock', TRIPAL_ERROR,
  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. );
  632. }
  633. else {
  634. // set the URL for this stock page
  635. $values = array('stock_id' => $node->stock_id);
  636. $stock = chado_select_record('stock', array('*'), $values);
  637. }
  638. // now update the properties
  639. if ($node->stock_id > 0) {
  640. $details = array(
  641. 'property_table' => 'stockprop',
  642. 'base_table' => 'stock',
  643. 'foreignkey_name' => 'stock_id',
  644. 'foreignkey_value' => $node->stock_id
  645. );
  646. chado_update_node_form_properties($node, $details);
  647. }
  648. // now update the additional dbxrefs
  649. if ($node->stock_id > 0) {
  650. $details = array(
  651. 'linking_table' => 'stock_dbxref',
  652. 'foreignkey_name' => 'stock_id',
  653. 'foreignkey_value' => $node->stock_id
  654. );
  655. chado_update_node_form_dbxrefs($node, $details);
  656. }
  657. // now update relationships
  658. if ($node->stock_id > 0) {
  659. $details = array(
  660. 'relationship_table' => 'stock_relationship',
  661. 'foreignkey_value' => $node->stock_id
  662. );
  663. chado_update_node_form_relationships($node, $details);
  664. }
  665. }
  666. /**
  667. * Implements hook_delete().
  668. * 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. * @ingroup tripal_stock
  690. */
  691. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  692. $new_node->organism_id = $record->organism_id;
  693. $new_node->sname = $record->name;
  694. $new_node->uniquename = $record->uniquename;
  695. $new_node->type_id = $record->type_id;
  696. return $new_node;
  697. }
  698. /**
  699. * Implements hook_node_presave(). Acts on all content types.
  700. *
  701. * @ingroup tripal_stock
  702. */
  703. function tripal_stock_node_presave($node) {
  704. switch ($node->type) {
  705. case 'chado_stock':
  706. // for a form submission the fields part of the node object
  707. // but for a sync the fields are in an object of the node
  708. $organism_id = null;
  709. $sname = '';
  710. $uniquename = '';
  711. $type = '';
  712. if(property_exists($node, 'organism_id')) {
  713. $organism_id = $node->organism_id;
  714. $sname = $node->sname;
  715. $uniquename = $node->uniquename;
  716. $type_id = $node->type_id;
  717. $values = array('cvterm_id' => $node->type_id);
  718. $cvterm = chado_select_record('cvterm', array('name'), $values);
  719. $type = $cvterm[0]->name;
  720. }
  721. else if (property_exists($node, 'stock')) {
  722. $organism_id = $node->stock->organism_id;
  723. $sname = $node->stock->name;
  724. $uniquename = $node->stock->uniquename;
  725. $type = $node->stock->type_id->name;
  726. }
  727. $values = array('organism_id' => $organism_id);
  728. $organism = chado_select_record('organism', array('genus','species'), $values);
  729. $node->title = "$sname, $uniquename ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  730. if ($sname == $uniquename) {
  731. $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  732. }
  733. break;
  734. }
  735. }
  736. /**
  737. * Implements hook_node_view(). Acts on all content types.
  738. *
  739. * @ingroup tripal_stock
  740. */
  741. function tripal_stock_node_view($node, $view_mode, $langcode) {
  742. switch ($node->type) {
  743. case 'chado_stock':
  744. if ($view_mode == 'full') {
  745. $node->content['tripal_stock_base'] = array(
  746. '#markup' => theme('tripal_stock_base', array('node' => $node)),
  747. '#tripal_toc_id' => 'base',
  748. '#tripal_toc_title' => 'Overview',
  749. '#weight' => -100,
  750. );
  751. $node->content['tripal_stock_collections'] = array(
  752. '#markup' => theme('tripal_stock_collections', array('node' => $node)),
  753. '#tripal_toc_id' => 'collections',
  754. '#tripal_toc_title' => 'Stock Collections',
  755. );
  756. $node->content['tripal_stock_properties'] = array(
  757. '#markup' => theme('tripal_stock_properties', array('node' => $node)),
  758. '#tripal_toc_id' => 'properties',
  759. '#tripal_toc_title' => 'Properties',
  760. );
  761. $node->content['tripal_stock_references'] = array(
  762. '#markup' => theme('tripal_stock_references', array('node' => $node)),
  763. '#tripal_toc_id' => 'references',
  764. '#tripal_toc_title' => 'Cross References',
  765. );
  766. $node->content['tripal_stock_relationships'] = array(
  767. '#markup' => theme('tripal_stock_relationships', array('node' => $node)),
  768. '#tripal_toc_id' => 'relationships',
  769. '#tripal_toc_title' => 'Relationships',
  770. );
  771. $node->content['tripal_stock_synonyms'] = array(
  772. '#markup' => theme('tripal_stock_synonyms', array('node' => $node)),
  773. '#tripal_toc_id' => 'synonyms',
  774. '#tripal_toc_title' => 'Synonyms',
  775. );
  776. $node->content['tripal_stock_publications'] = array(
  777. '#markup' => theme('tripal_stock_publications', array('node' => $node)),
  778. '#tripal_toc_id' => 'publications',
  779. '#tripal_toc_title' => 'Publications',
  780. );
  781. }
  782. if ($view_mode == 'teaser') {
  783. $node->content['tripal_stock_teaser'] = array(
  784. '#markup' => theme('tripal_stock_teaser', array('node' => $node)),
  785. );
  786. }
  787. break;
  788. case 'chado_organism':
  789. if ($view_mode == 'full') {
  790. $node->content['tripal_organism_stocks'] = array(
  791. '#markup' => theme('tripal_organism_stocks', array('node' => $node)),
  792. '#tripal_toc_id' => 'stocks',
  793. '#tripal_toc_title' => 'Stocks',
  794. );
  795. }
  796. break;
  797. }
  798. }
  799. /**
  800. * Implements hook_node_insert().
  801. * Acts on all content types.
  802. *
  803. * @ingroup tripal_stock
  804. */
  805. function tripal_stock_node_insert($node) {
  806. // set the URL path after inserting. We do it here because we do not
  807. // know the stock_id in the presave
  808. switch ($node->type) {
  809. case 'chado_stock':
  810. // remove any previous alias
  811. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  812. // set the URL for this stock page
  813. $url_alias = tripal_stock_get_stock_url($node);
  814. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  815. path_save($path_alias);
  816. break;
  817. }
  818. }
  819. /**
  820. * Implements hook_node_update().
  821. * Acts on all content types.
  822. *
  823. * @ingroup tripal_stock
  824. */
  825. function tripal_stock_node_update($node) {
  826. // add items to other nodes, build index and search results
  827. switch ($node->type) {
  828. case 'chado_stock':
  829. // remove any previous alias
  830. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  831. // set the URL for this stock page
  832. $url_alias = tripal_stock_get_stock_url($node);
  833. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  834. path_save($path_alias);
  835. break;
  836. }
  837. }
  838. /**
  839. * Return the url alias for a stock
  840. *
  841. * @param $node
  842. * A node object containing at least the stock_id and nid
  843. * @param $url_alias
  844. * Optional. This should be the URL alias syntax string that contains
  845. * placeholders such as [id], [genus], [species], [name], [uniquename],
  846. * and [type]. These placeholders will be substituted for actual values.
  847. * If this parameter is not provided then the value of the
  848. * chado_stock_url_string Drupal variable will be used.
  849. *
  850. * @ingroup tripal_stock
  851. */
  852. function tripal_stock_get_stock_url($node, $url_alias = NULL) {
  853. // get the starting URL alias
  854. if(!$url_alias) {
  855. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  856. if (!$url_alias) {
  857. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  858. }
  859. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  860. }
  861. // get the stock
  862. $values = array('stock_id' => $node->stock_id);
  863. $stock = chado_select_record('stock', array('*'), $values);
  864. if (!$stock) {
  865. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  866. return FALSE;
  867. }
  868. $stock = (object) $stock[0];
  869. // get the organism
  870. $values = array('organism_id' => $stock->organism_id);
  871. $organism = chado_select_record('organism', array('*'), $values);
  872. if (!$organism) {
  873. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  874. return FALSE;
  875. }
  876. $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
  877. $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
  878. // get the type
  879. $values = array('cvterm_id' => $stock->type_id);
  880. $cvterm = chado_select_record('cvterm', array('name'), $values);
  881. if (!$cvterm) {
  882. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  883. return FALSE;
  884. }
  885. $type = preg_replace('/\s/', '_', $cvterm[0]->name);
  886. // now substitute in the values
  887. $url_alias = preg_replace('/\[id\]/', $stock->stock_id, $url_alias);
  888. $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
  889. $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
  890. $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
  891. $url_alias = preg_replace('/\[name\]/', $stock->name, $url_alias);
  892. $url_alias = preg_replace('/\[uniquename\]/', $stock->uniquename, $url_alias);
  893. // the dst field of the url_alias table is only 128 characters long.
  894. // if this is the case then simply return the node URL, we can't set this one
  895. if (strlen($url_alias) > 128) {
  896. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  897. return "node/" . $node->nid;
  898. }
  899. return $url_alias;
  900. }