tripal_stock.chado_node.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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', $node->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['manually_edit_chado'] = array(
  191. '#type' => 'hidden',
  192. '#value' => TRUE,
  193. );
  194. $form['names'] = array(
  195. '#type' => 'fieldset',
  196. '#title' => t('Stock Name')
  197. );
  198. $form['names']['sname'] = array(
  199. '#type' => 'textfield',
  200. '#title' => t('Name'),
  201. '#default_value' => $sname,
  202. '#required' => TRUE
  203. );
  204. $form['names']['uniquename'] = array(
  205. '#type' => 'textfield',
  206. '#title' => t('Unique Name'),
  207. '#default_value' => $uniquename,
  208. '#required' => TRUE
  209. );
  210. $form['names']['stock_id'] = array(
  211. '#type' => 'hidden',
  212. '#value' => $stock_id,
  213. );
  214. $form['details'] = array(
  215. '#type' => 'fieldset',
  216. '#title' => t('Stock Details')
  217. );
  218. $type_options = tripal_get_cvterm_default_options('stock', 'type_id', 'stock types');
  219. $type_options[0] = 'Select a Type';
  220. $form['details']['type_id'] = array(
  221. '#type' => 'select',
  222. '#title' => t('Type of Stock'),
  223. '#options' => $type_options,
  224. '#default_value' => $type_id,
  225. '#required' => TRUE,
  226. );
  227. // get the list of organisms
  228. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  229. $org_rset = chado_query($sql);
  230. $organisms = array();
  231. $organisms[''] = '';
  232. while ($organism = $org_rset->fetchObject()) {
  233. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  234. }
  235. $form['details']['organism_id'] = array(
  236. '#type' => 'select',
  237. '#title' => t('Source Organism for stock'),
  238. '#default_value' => $organism_id,
  239. '#options' => $organisms,
  240. '#required' => TRUE
  241. );
  242. $form['details']['stock_description'] = array(
  243. '#type' => 'textarea',
  244. '#title' => t('Notes'),
  245. '#default_value' => $sdescription,
  246. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  247. );
  248. $form['database_reference'] = array(
  249. '#type' => 'fieldset',
  250. '#title' => t('Stock Database Reference')
  251. );
  252. $form['database_reference']['accession'] = array(
  253. '#type' => 'textfield',
  254. '#title' => t('Accession'),
  255. '#default_value' => $dbxref_accession,
  256. );
  257. $form['database_reference']['db_description'] = array(
  258. '#type' => 'textarea',
  259. '#title' => t('Description of Database Reference'),
  260. '#default_value' => $dbxref_description,
  261. '#description' => t('Optionally enter a description about the database accession.')
  262. );
  263. $db_options = tripal_db_get_db_options();
  264. $db_options[0] = 'Select a Database';
  265. $form['database_reference']['database'] = array(
  266. '#type' => 'select',
  267. '#title' => t('Database'),
  268. '#options' => $db_options,
  269. '#default_value' => $dbxref_database
  270. );
  271. // PROPERTIES FORM
  272. //---------------------------------------------
  273. $prop_cv = tripal_get_default_cv('stockprop', 'type_id');
  274. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  275. $details = array(
  276. 'property_table' => 'stockprop',
  277. 'chado_id' => $stock_id,
  278. 'cv_id' => $cv_id
  279. );
  280. chado_add_node_form_properties($form, $form_state, $details);
  281. // ADDITIONAL DBXREFS FORM
  282. //---------------------------------------------
  283. $details = array(
  284. 'linking_table' => 'stock_dbxref',
  285. 'base_foreign_key' => 'stock_id',
  286. 'base_key_value' => $stock_id
  287. );
  288. chado_add_node_form_dbxrefs($form, $form_state, $details);
  289. // RELATIONSHIPS FORM
  290. //---------------------------------------------
  291. $relationship_cv = tripal_get_default_cv('stock_relationship', 'type_id');
  292. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  293. $details = array(
  294. 'relationship_table' => 'stock_relationship',
  295. 'base_table' => 'stock',
  296. 'base_foreign_key' => 'stock_id',
  297. 'base_key_value' => $stock_id,
  298. 'nodetype' => 'stock',
  299. 'cv_id' => $cv_id
  300. );
  301. chado_add_node_form_relationships($form, $form_state, $details);
  302. return $form;
  303. }
  304. /**
  305. * Implements hook_validate().
  306. * Validate the input from the chado_stock node form
  307. *
  308. * @param $node
  309. * The current node including fields with the form element names and submitted values
  310. * @param $form
  311. * A description of the form to be rendered by drupal_get_form()
  312. *
  313. * @ingroup tripal_stock
  314. */
  315. function chado_stock_validate(&$node, $form, &$form_state) {
  316. // if this is a delete then don't validate
  317. if($node->op == 'Delete') {
  318. return;
  319. }
  320. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  321. // need to validate during syncing so just skip it.
  322. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  323. return;
  324. }
  325. // remove surrounding whitespace
  326. $node->uniquename = trim($node->uniquename);
  327. $node->sname = trim($node->sname);
  328. $node->stock_description = trim($node->stock_description);
  329. $node->accession = trim($node->accession);
  330. $node->db_description = trim($node->db_description);
  331. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  332. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  333. // if this is an update, we want to make sure that a different stock for
  334. // the organism doesn't already have this uniquename. We don't want to give
  335. // two sequences the same uniquename
  336. if (property_exists($node, 'nid')) {
  337. $sql = "
  338. SELECT *
  339. FROM {stock} S
  340. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  341. WHERE
  342. uniquename = :uname AND organism_id = :organism_id AND
  343. CVT.name = :cvtname AND NOT stock_id = :stock_id
  344. ";
  345. $result = chado_query($sql, array(':uname' => $node->uniquename,
  346. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  347. ':stock_id' => $node->stock_id))->fetchObject();
  348. if ($result) {
  349. 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."));
  350. }
  351. }
  352. // if this is an insert then we just need to make sure this name doesn't
  353. // already exist for this organism if it does then we need to throw an error
  354. else {
  355. $sql = "
  356. SELECT *
  357. FROM {Stock} S
  358. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  359. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  360. $result = chado_query($sql, array(':uname' => $node->uniquename,
  361. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  362. if ($result) {
  363. 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."));
  364. }
  365. }
  366. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  367. if ($node->type_id == 0) {
  368. form_set_error('type_id', 'Please select a type of stock.');
  369. }
  370. else {
  371. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  372. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  373. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  374. if ( $num_rows->count != 1) {
  375. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  376. }
  377. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  378. if ( $node->organism_id == 0) {
  379. form_set_error('organism_id', 'Please select a source organism for this stock');
  380. }
  381. else {
  382. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  383. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  384. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  385. if ( $num_rows->count != 1 ) {
  386. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  387. }
  388. // Check if Accession also database
  389. if ($node->accession != '') {
  390. if ($node->database == 0) {
  391. // there is an accession but no database selected
  392. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  393. }
  394. }
  395. else {
  396. if ($node->database > 0) {
  397. // there is a database selected but no accession
  398. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  399. }
  400. }
  401. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  402. if ( $node->database > 0) {
  403. $replace = array(':table' => 'db', ':column' => 'db_id');
  404. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  405. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  406. if ($num_rows->count != 1) {
  407. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  408. }
  409. }
  410. /**
  411. * Implements hook_insert().
  412. * Inserts data from chado_stock_form() into drupal and chado
  413. *
  414. * @param $node
  415. * The current node including fields with the form element names and submitted values
  416. *
  417. * @return
  418. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  419. *
  420. * @ingroup tripal_stock
  421. */
  422. function chado_stock_insert($node) {
  423. $node->uniquename = trim($node->uniquename);
  424. $node->sname = trim($node->sname);
  425. // if there is an stock_id in the $node object then this must be a sync so
  426. // we can skip adding the stock to chado as it is already there, although
  427. // we do need to proceed with the rest of the insert
  428. if (property_exists($node, 'manually_edit_chado')) {
  429. // before we can add the stock, we must add the dbxref if one has been
  430. // provided by the user.
  431. $dbxref_status = 0;
  432. if (!empty($node->accession) ) {
  433. if (!empty($node->database) ) {
  434. $values = array(
  435. 'db_id' => $node->database,
  436. 'accession' => $node->accession,
  437. );
  438. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  439. $values['description'] = $node->db_description;
  440. $values['version'] = '1';
  441. $dbxref_status = chado_insert_record('dbxref', $values);
  442. if (!$dbxref_status) {
  443. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  444. tripal_report_error('tripal_stock', TRIPAL_WARNING,
  445. 'Insert Stock: Unable to create dbxref where values:%values',
  446. array('%values' => print_r($values, TRUE)));
  447. }
  448. }
  449. else {
  450. $dbxref_status = 1;
  451. }
  452. }
  453. }
  454. // create stock including the dbxref
  455. $stock = '';
  456. if ($dbxref_status) {
  457. $values = array(
  458. 'dbxref_id' => array(
  459. 'db_id' => $node->database,
  460. 'accession' => $node->accession
  461. ),
  462. 'organism_id' => $node->organism_id,
  463. 'name' => $node->sname,
  464. 'uniquename' => $node->uniquename,
  465. 'description' => $node->stock_description,
  466. 'type_id' => $node->type_id
  467. );
  468. $stock = chado_insert_record('stock', $values);
  469. }
  470. // create a stock without a dbxref
  471. else {
  472. $values = array(
  473. 'organism_id' => $node->organism_id,
  474. 'name' => $node->sname,
  475. 'uniquename' => $node->uniquename,
  476. 'description' => $node->stock_description,
  477. 'type_id' => $node->type_id
  478. );
  479. $stock = chado_insert_record('stock', $values);
  480. }
  481. if (is_array($stock)) {
  482. $stock_added = TRUE;
  483. $stock_id = $stock['stock_id'];
  484. }
  485. else {
  486. $stock_added = FALSE;
  487. }
  488. if ($stock_added) {
  489. // Now add properties
  490. $details = array(
  491. 'property_table' => 'stockprop',
  492. 'base_table' => 'stock',
  493. 'foreignkey_name' => 'stock_id',
  494. 'foreignkey_value' => $stock_id
  495. );
  496. chado_update_node_form_properties($node, $details);
  497. // Now add the additional references
  498. $details = array(
  499. 'linking_table' => 'stock_dbxref',
  500. 'foreignkey_name' => 'stock_id',
  501. 'foreignkey_value' => $stock_id
  502. );
  503. chado_update_node_form_dbxrefs($node, $details);
  504. // Now add in relationships
  505. $details = array(
  506. 'relationship_table' => 'stock_relationship',
  507. 'foreignkey_value' => $node->stock_id
  508. );
  509. chado_update_node_form_relationships($node, $details);
  510. }
  511. } //end of adding stock to chado
  512. else {
  513. // stock already exists since this is a sync
  514. $stock_added = TRUE;
  515. $stock['stock_id'] = $node->stock_id;
  516. }
  517. // if the stock creation was succesful then add the URL and the entry in the
  518. // chado_stock table
  519. if ($stock_added) {
  520. // add the entry to the chado_stock table
  521. db_insert('chado_stock')->fields(array(
  522. 'nid' => (int) $node->nid,
  523. 'vid' => (int) $node->vid,
  524. 'stock_id' => (int) $stock['stock_id']
  525. ))->execute();
  526. }
  527. else {
  528. drupal_set_message(t('Error during stock creation.'), 'error');
  529. tripal_report_error('tripal_stock', TRIPAL_WARNING,
  530. 'Insert Stock: Unable to create stock where values:%values',
  531. array('%values' => print_r($values, TRUE)));
  532. return FALSE;
  533. }
  534. }
  535. /**
  536. * Implements hook_update().
  537. * Handles Editing/Updating of main stock info
  538. *
  539. * NOTE: Currently just writes over all old data
  540. *
  541. * @param $node
  542. * The current node including fields with the form element names and submitted values
  543. *
  544. * @return
  545. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  546. *
  547. * @ingroup tripal_stock
  548. */
  549. function chado_stock_update($node) {
  550. $node->uniquename = trim($node->uniquename);
  551. $node->sname = trim($node->sname);
  552. if ($node->revision) {
  553. // there is no way to handle revisions in Chado but leave
  554. // this here just to make not we've addressed it.
  555. }
  556. //update dbxref
  557. $dbxref_status = NULL;
  558. $dbxref_present = FALSE;
  559. if ($node->database) {
  560. $dbxref_present = TRUE;
  561. if ($node->accession) {
  562. $dbxref_mode = '';
  563. $stock = chado_select_record(
  564. 'stock',
  565. array('dbxref_id', 'type_id'),
  566. array('stock_id' => $node->stock_id)
  567. );
  568. if ($stock[0]->dbxref_id) {
  569. $values = array(
  570. 'db_id' => $node->database,
  571. 'accession' => $node->accession,
  572. 'description' => $node->db_description
  573. );
  574. $dbxref_status = chado_update_record(
  575. 'dbxref',
  576. array('dbxref_id' => $stock[0]->dbxref_id),
  577. $values
  578. );
  579. $dbxref_mode = 'Update';
  580. }
  581. else {
  582. if ($stock[0]->type_id) {
  583. //create the dbxref
  584. //used the type_id as a control to check we have a stock but not a dbxref
  585. $values = array(
  586. 'db_id' => $node->database,
  587. 'accession' => $node->accession,
  588. 'description' => $node->db_description,
  589. 'version' => '1',
  590. );
  591. $dbxref_status = chado_insert_record(
  592. 'dbxref',
  593. $values
  594. );
  595. $dbxref_mode = 'Create';
  596. }
  597. else {
  598. drupal_set_message(t('Unable to find stock to Update'), 'error');
  599. tripal_report_error('tripal_stock', TRIPAL_ERROR,
  600. 'Stock Update: Unable to find stock to update using values: %values',
  601. array('%values', print_r($values, TRUE))
  602. );
  603. return FALSE;
  604. }
  605. }
  606. }
  607. if (!$dbxref_status) {
  608. tripal_report_error('tripal_stock', TRIPAL_WARNING,
  609. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  610. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode));
  611. }
  612. }
  613. //can't change stock id which is all thats stored in drupal thus only update chado
  614. $update_values = array(
  615. 'organism_id' => $node->organism_id,
  616. 'name' => $node->sname,
  617. 'uniquename' => $node->uniquename,
  618. 'description' => $node->stock_description,
  619. 'type_id' => $node->type_id,
  620. );
  621. if ($dbxref_present) {
  622. if ($dbxref_status) {
  623. $update_values['dbxref_id'] = array(
  624. 'db_id' => $node->database,
  625. 'accession' => $node->accession
  626. );
  627. }
  628. }
  629. $status = chado_update_record('stock', array('stock_id' => $node->stock_id), $update_values);
  630. if (!$status) {
  631. drupal_set_message(t('Unable to update stock'), 'error');
  632. tripal_report_error('tripal_stock', TRIPAL_ERROR,
  633. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  634. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE))
  635. );
  636. }
  637. else {
  638. // set the URL for this stock page
  639. $values = array('stock_id' => $node->stock_id);
  640. $stock = chado_select_record('stock', array('*'), $values);
  641. }
  642. // now update the properties
  643. if ($node->stock_id > 0) {
  644. $details = array(
  645. 'property_table' => 'stockprop',
  646. 'base_table' => 'stock',
  647. 'foreignkey_name' => 'stock_id',
  648. 'foreignkey_value' => $node->stock_id
  649. );
  650. chado_update_node_form_properties($node, $details);
  651. }
  652. // now update the additional dbxrefs
  653. if ($node->stock_id > 0) {
  654. $details = array(
  655. 'linking_table' => 'stock_dbxref',
  656. 'foreignkey_name' => 'stock_id',
  657. 'foreignkey_value' => $node->stock_id
  658. );
  659. chado_update_node_form_dbxrefs($node, $details);
  660. }
  661. // now update relationships
  662. if ($node->stock_id > 0) {
  663. $details = array(
  664. 'relationship_table' => 'stock_relationship',
  665. 'foreignkey_value' => $node->stock_id
  666. );
  667. chado_update_node_form_relationships($node, $details);
  668. }
  669. }
  670. /**
  671. * Implements hook_delete().
  672. * Handles deleting of chado_stocks
  673. *
  674. * NOTE: Currently deletes data -no undo or record-keeping functionality
  675. *
  676. * @param $node
  677. * The current node including fields with the form element names and submitted values
  678. *
  679. * @return
  680. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  681. *
  682. * @ingroup tripal_stock
  683. */
  684. function chado_stock_delete($node) {
  685. // Set stock in chado: is_obsolete = TRUE
  686. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  687. //remove drupal node and all revisions
  688. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  689. }
  690. /**
  691. * Used by Tripal Chado Node API during sync'ing of nodes
  692. *
  693. * @ingroup tripal_stock
  694. */
  695. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  696. $new_node->organism_id = $record->organism_id;
  697. $new_node->sname = $record->name;
  698. $new_node->uniquename = $record->uniquename;
  699. $new_node->type_id = $record->type_id;
  700. return $new_node;
  701. }
  702. /**
  703. * Implements hook_node_presave(). Acts on all content types.
  704. *
  705. * @ingroup tripal_stock
  706. */
  707. function tripal_stock_node_presave($node) {
  708. switch ($node->type) {
  709. case 'chado_stock':
  710. // for a form submission the fields part of the node object
  711. // but for a sync the fields are in an object of the node
  712. $organism_id = null;
  713. $sname = '';
  714. $uniquename = '';
  715. $type = '';
  716. if(property_exists($node, 'organism_id')) {
  717. $organism_id = $node->organism_id;
  718. $sname = $node->sname;
  719. $uniquename = $node->uniquename;
  720. $type_id = $node->type_id;
  721. $values = array('cvterm_id' => $node->type_id);
  722. $cvterm = chado_select_record('cvterm', array('name'), $values);
  723. $type = $cvterm[0]->name;
  724. }
  725. else if (property_exists($node, 'stock')) {
  726. $organism_id = $node->stock->organism_id;
  727. $sname = $node->stock->name;
  728. $uniquename = $node->stock->uniquename;
  729. $type = $node->stock->type_id->name;
  730. }
  731. $values = array('organism_id' => $organism_id);
  732. $organism = chado_select_record('organism', array('genus','species'), $values);
  733. $node->title = "$sname, $uniquename ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  734. if ($sname == $uniquename) {
  735. $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  736. }
  737. break;
  738. }
  739. }
  740. /**
  741. * Implements hook_node_view(). Acts on all content types.
  742. *
  743. * @ingroup tripal_stock
  744. */
  745. function tripal_stock_node_view($node, $view_mode, $langcode) {
  746. switch ($node->type) {
  747. case 'chado_stock':
  748. if ($view_mode == 'full') {
  749. $node->content['tripal_stock_base'] = array(
  750. '#markup' => theme('tripal_stock_base', array('node' => $node)),
  751. '#tripal_toc_id' => 'base',
  752. '#tripal_toc_title' => 'Overview',
  753. '#weight' => -100,
  754. );
  755. $node->content['tripal_stock_collections'] = array(
  756. '#markup' => theme('tripal_stock_collections', array('node' => $node)),
  757. '#tripal_toc_id' => 'collections',
  758. '#tripal_toc_title' => 'Stock Collections',
  759. );
  760. $node->content['tripal_stock_properties'] = array(
  761. '#markup' => theme('tripal_stock_properties', array('node' => $node)),
  762. '#tripal_toc_id' => 'properties',
  763. '#tripal_toc_title' => 'Properties',
  764. );
  765. $node->content['tripal_stock_references'] = array(
  766. '#markup' => theme('tripal_stock_references', array('node' => $node)),
  767. '#tripal_toc_id' => 'references',
  768. '#tripal_toc_title' => 'Cross References',
  769. );
  770. $node->content['tripal_stock_relationships'] = array(
  771. '#markup' => theme('tripal_stock_relationships', array('node' => $node)),
  772. '#tripal_toc_id' => 'relationships',
  773. '#tripal_toc_title' => 'Relationships',
  774. );
  775. $node->content['tripal_stock_synonyms'] = array(
  776. '#markup' => theme('tripal_stock_synonyms', array('node' => $node)),
  777. '#tripal_toc_id' => 'synonyms',
  778. '#tripal_toc_title' => 'Synonyms',
  779. );
  780. $node->content['tripal_stock_publications'] = array(
  781. '#markup' => theme('tripal_stock_publications', array('node' => $node)),
  782. '#tripal_toc_id' => 'publications',
  783. '#tripal_toc_title' => 'Publications',
  784. );
  785. }
  786. if ($view_mode == 'teaser') {
  787. $node->content['tripal_stock_teaser'] = array(
  788. '#markup' => theme('tripal_stock_teaser', array('node' => $node)),
  789. );
  790. }
  791. break;
  792. case 'chado_organism':
  793. if ($view_mode == 'full') {
  794. $node->content['tripal_organism_stocks'] = array(
  795. '#markup' => theme('tripal_organism_stocks', array('node' => $node)),
  796. '#tripal_toc_id' => 'stocks',
  797. '#tripal_toc_title' => 'Stocks',
  798. );
  799. }
  800. break;
  801. }
  802. }
  803. /**
  804. * Implements hook_node_insert().
  805. * Acts on all content types.
  806. *
  807. * @ingroup tripal_stock
  808. */
  809. function tripal_stock_node_insert($node) {
  810. // set the URL path after inserting. We do it here because we do not
  811. // know the stock_id in the presave
  812. switch ($node->type) {
  813. case 'chado_stock':
  814. // remove any previous alias
  815. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  816. // set the URL for this stock page
  817. $url_alias = tripal_stock_get_stock_url($node);
  818. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  819. path_save($path_alias);
  820. break;
  821. }
  822. }
  823. /**
  824. * Implements hook_node_update().
  825. * Acts on all content types.
  826. *
  827. * @ingroup tripal_stock
  828. */
  829. function tripal_stock_node_update($node) {
  830. // add items to other nodes, build index and search results
  831. switch ($node->type) {
  832. case 'chado_stock':
  833. // remove any previous alias
  834. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  835. // set the URL for this stock page
  836. $url_alias = tripal_stock_get_stock_url($node);
  837. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  838. path_save($path_alias);
  839. break;
  840. }
  841. }
  842. /**
  843. * Return the url alias for a stock
  844. *
  845. * @param $node
  846. * A node object containing at least the stock_id and nid
  847. * @param $url_alias
  848. * Optional. This should be the URL alias syntax string that contains
  849. * placeholders such as [id], [genus], [species], [name], [uniquename],
  850. * and [type]. These placeholders will be substituted for actual values.
  851. * If this parameter is not provided then the value of the
  852. * chado_stock_url_string Drupal variable will be used.
  853. *
  854. * @ingroup tripal_stock
  855. */
  856. function tripal_stock_get_stock_url($node, $url_alias = NULL) {
  857. // get the starting URL alias
  858. if(!$url_alias) {
  859. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  860. if (!$url_alias) {
  861. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  862. }
  863. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  864. }
  865. // get the stock
  866. $values = array('stock_id' => $node->stock_id);
  867. $stock = chado_select_record('stock', array('*'), $values);
  868. if (!$stock) {
  869. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  870. return FALSE;
  871. }
  872. $stock = (object) $stock[0];
  873. // get the organism
  874. $values = array('organism_id' => $stock->organism_id);
  875. $organism = chado_select_record('organism', array('*'), $values);
  876. if (!$organism) {
  877. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  878. return FALSE;
  879. }
  880. $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
  881. $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
  882. // get the type
  883. $values = array('cvterm_id' => $stock->type_id);
  884. $cvterm = chado_select_record('cvterm', array('name'), $values);
  885. if (!$cvterm) {
  886. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  887. return FALSE;
  888. }
  889. $type = preg_replace('/\s/', '_', $cvterm[0]->name);
  890. // now substitute in the values
  891. $url_alias = preg_replace('/\[id\]/', $stock->stock_id, $url_alias);
  892. $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
  893. $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
  894. $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
  895. $url_alias = preg_replace('/\[name\]/', $stock->name, $url_alias);
  896. $url_alias = preg_replace('/\[uniquename\]/', $stock->uniquename, $url_alias);
  897. // the dst field of the url_alias table is only 128 characters long.
  898. // if this is the case then simply return the node URL, we can't set this one
  899. if (strlen($url_alias) > 128) {
  900. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  901. return "node/" . $node->nid;
  902. }
  903. return $url_alias;
  904. }