tripal_stock.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. // $Id$
  3. require("tripal_stock-administration.inc");
  4. require("tripal_stock.api.inc");
  5. require("other_module_api_functions.inc");
  6. require("tripal_stock-secondary_tables.inc");
  7. require("tripal_stock-properties.inc");
  8. require("tripal_stock-relationships.inc");
  9. require("tripal_stock-db_references.inc");
  10. /*************************************************************************
  11. * Implementation of hook_menu()
  12. * Purpose: Add menu items for this module
  13. * Note: A Menu item for the Create chado_stock form is automatically
  14. * added to the 'Create Content' Navigation menu
  15. *
  16. * @return and array for menu descriptions
  17. */
  18. function tripal_stock_menu() {
  19. $items = array();
  20. //Administrative settings menu-----------------
  21. $items['admin/tripal/tripal_stock'] = array(
  22. 'title' => t('Stocks'),
  23. 'description' => t('Settings for Chado Stocks'),
  24. 'page callback' => 'drupal_get_form',
  25. 'page arguments' => array('tripal_stock_admin'),
  26. 'access arguments' => array('administer site configuration'),
  27. 'type' => MENU_NORMAL_ITEM
  28. );
  29. //Displaying stocks----------------------------
  30. $items['stocks'] = array(
  31. 'menu_name' => ('primary-links'),
  32. 'title' => t('Stocks'),
  33. 'page callback' => 'tripal_stock_show_stocks',
  34. 'access arguments' => array('access chado_stock content'),
  35. 'type' => MENU_NORMAL_ITEM,
  36. );
  37. // Adding Secondary Properties-----------------
  38. $items['node/%cs_node/properties'] = array(
  39. 'title' => t('Add Properties & Synonyms'),
  40. 'description' => t('Settings for Chado Stocks'),
  41. 'page callback' => 'tripal_stock_add_ALL_property_page',
  42. 'page arguments' => array(1),
  43. 'access arguments' => array('create chado_stock content'),
  44. 'type' => MENU_CALLBACK
  45. );
  46. $items['node/%cs_node/db_references'] = array(
  47. 'title' => t('Add Database References'),
  48. 'description' => t('Settings for Chado Stocks'),
  49. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  50. 'page arguments' => array(1),
  51. 'access arguments' => array('create chado_stock content'),
  52. 'type' => MENU_CALLBACK
  53. );
  54. $items['node/%cs_node/relationships'] = array(
  55. 'title' => t('Add Relationships'),
  56. 'description' => t('Settings for Chado Stocks'),
  57. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  58. 'page arguments' => array(1),
  59. 'access arguments' => array('create chado_stock content'),
  60. 'type' => MENU_CALLBACK
  61. );
  62. //Edit/Deleting Secondary Properties-------------
  63. $items['node/%cs_node/edit_properties'] = array(
  64. 'title' => t('Edit Properties'),
  65. 'description' => t('Settings for Chado Stocks'),
  66. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  67. 'page arguments' => array(1),
  68. 'access arguments' => array('edit chado_stock content'),
  69. 'type' => MENU_LOCAL_TASK,
  70. 'weight' => 8,
  71. );
  72. $items['node/%cs_node/edit_relationships'] = array(
  73. 'title' => t('Edit Relationships'),
  74. 'description' => t('Settings for Chado Stocks'),
  75. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  76. 'page arguments' => array(1),
  77. 'access arguments' => array('edit chado_stock content'),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'weight' => 9,
  80. );
  81. $items['node/%cs_node/edit_db_references'] = array(
  82. 'title' => t('Edit DB References'),
  83. 'description' => t('Settings for Chado Stocks'),
  84. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  85. 'page arguments' => array(1),
  86. 'access arguments' => array('edit chado_stock content'),
  87. 'type' => MENU_LOCAL_TASK,
  88. 'weight' => 10,
  89. );
  90. return $items;
  91. }
  92. /*************************************************************************
  93. * Implements Menu wildcard_load hook
  94. * Purpose: Allows the node ID of a chado stock to be dynamically
  95. * pulled from the path. The node is loaded from this node ID
  96. * and supplied to the page as an arguement
  97. */
  98. function cs_node_load($nid) {
  99. if (is_numeric($nid)) {
  100. $node = node_load($nid);
  101. if ($node->type == 'chado_stock') {
  102. return $node;
  103. }
  104. }
  105. return FALSE;
  106. }
  107. /*************************************************************************
  108. * Implementation of hook_perm()
  109. * Purpose: Set the permission types that the chado stock module uses
  110. *
  111. * @return an array listing the possible permission catagories
  112. */
  113. function tripal_stock_perm() {
  114. return array(
  115. 'access chado_stock content',
  116. 'create chado_stock content',
  117. 'edit chado_stock content',
  118. 'delete chado_stock content'
  119. );
  120. }
  121. /*************************************************************************
  122. * Implements hook_access()
  123. * Purpose: Maps permission catagories to actions;
  124. * TRUE grants access; FALSE denies it
  125. */
  126. function chado_stock_access($op, $node, $account) {
  127. if ($op == 'create') {
  128. return user_access('create chado_stock content', $account);
  129. }
  130. if ($op == 'update') {
  131. if (user_access('edit chado_stock content', $account)) {
  132. return TRUE;
  133. }
  134. }
  135. if ($op == 'delete') {
  136. if (user_access('delete chado_stock content', $account)) {
  137. return TRUE;
  138. }
  139. }
  140. if ($op == 'view') {
  141. if (user_access('access chado_stock content', $account)) {
  142. return TRUE;
  143. }
  144. }
  145. return FALSE;
  146. }
  147. /*************************************************************************
  148. * Implements hook_views_api()
  149. * Purpose: Essentially this hook tells drupal that there is views support for
  150. * for this module which then includes tripal_stock.views.inc where all the
  151. * views integration code is
  152. */
  153. function tripal_stock_views_api() {
  154. return array(
  155. 'api' => 2.0,
  156. );
  157. }
  158. /*************************************************************************
  159. * Implements hook_theme()
  160. * Purpose: Register themeing functions for this module
  161. *
  162. * @return an array of themeing functions to register
  163. */
  164. function tripal_stock_theme() {
  165. return array(
  166. 'tripal_stock_stock_table' => array (
  167. 'arguments' => array('stocks'),
  168. ),
  169. // Property edit forms--------------------------
  170. 'tripal_stock_edit_ALL_properties_form' => array(
  171. 'arguments' => array('form'),
  172. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  173. ),
  174. 'tripal_stock_edit_ALL_db_references_form' => array(
  175. 'arguments' => array('form'),
  176. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  177. ),
  178. 'tripal_stock_edit_ALL_relationships_form' => array(
  179. 'arguments' => array('form'),
  180. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  181. ),
  182. );
  183. }
  184. /*************************************************************************
  185. * Purpose: show stocks stored in drupals chado_stock table
  186. */
  187. function tripal_stock_show_stocks () {
  188. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  189. $no_stocks = db_result(db_query($sql));
  190. if($no_stocks != 0) {
  191. $stocks = tripal_stock_get_all_stocks();
  192. if($no_stocks != count($stocks)) {
  193. drupal_set_message("Synchronization needed.");
  194. }
  195. return theme('tripal_stock_stock_table',&$stocks);
  196. } else {
  197. return t("No Stocks exists. Please contact administrators to ".
  198. "synchronize stocks.");
  199. }
  200. }
  201. function theme_tripal_stock_stock_table (&$stocks) {
  202. // cycle through the stocks and build the stocks page
  203. $output = "<div id=\"stocks\">";
  204. $output .= '<table>';
  205. $output .= "<tr>";
  206. $output .= "<th>Name</th>";
  207. $output .= "<th>Type</th>";
  208. $output .= "<th>Organism</th>";
  209. $output .= "<th>Description</th>";
  210. $output .= "</tr>";
  211. foreach($stocks as $stock){
  212. $output .= "<tr>";
  213. $output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
  214. $output .= "<td>".$stock->stock_type."</td>";
  215. $output .= "<td nowrap>".$stock->organism->common_name."</td>";
  216. $output .= "<td>".$stock->description."</td>";
  217. $output .= "</tr>";
  218. }
  219. $output .= "</table>";
  220. $output .= "</div>";
  221. return $output;
  222. }
  223. /*************************************************************************
  224. * @section Implementation of stock Node
  225. * including node_load, form, insert, update, delete(by nid and vid)
  226. *************************************************************************/
  227. /*************************************************************************
  228. * Implements hook_node_info()
  229. */
  230. function tripal_stock_node_info() {
  231. return array(
  232. 'chado_stock' => array(
  233. 'name' => t('Stock'),
  234. 'module' => 'chado_stock',
  235. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  236. 'has_title' => TRUE,
  237. 'has_body' => FALSE,
  238. ),
  239. );
  240. }
  241. /*************************************************************************
  242. * Implements hook_load()
  243. * Everytime a chado_stock node is viewed or manipulated this hook is called first to prepare the data
  244. */
  245. function chado_stock_load($node) {
  246. // Gets the stock_id from the drupal chado_stock table---------------------------------------
  247. $map = db_fetch_object(db_query(
  248. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  249. $node->vid
  250. ));
  251. $node->stock_id = $map->stock_id;
  252. //Get the main stock information from the chado stock table-----------------------------------
  253. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  254. $values = array('stock_id' => $node->stock_id);
  255. $results = tripal_core_chado_select('stock', $columns, $values);
  256. $node->stock_name = $results[0]->name;
  257. $node->uniquename = $results[0]->uniquename;
  258. $node->description = $results[0]->description;
  259. $node->stock_type_id = $results[0]->type_id;
  260. $node->organism->organism_id = $results[0]->organism_id;
  261. $node->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  262. if (preg_match('/t/', $results[0]->is_obsolete)) {
  263. $node->is_obsolete = TRUE;
  264. } else {
  265. $node->is_obsolete = FALSE;
  266. }
  267. // Get type for current stock------------------------------------------------------------------
  268. $columns = array('name');
  269. $values = array('cvterm_id' => $node->stock_type_id);
  270. $results = tripal_core_chado_select('cvterm', $columns, $values);
  271. $node->stock_type = $results[0]->name;
  272. // Get organism details from chado & add to node-----------------------------------------------
  273. // get organism if for current stock
  274. // pull all data from chado for the organism and assign it to the current node
  275. $node->organism = tripal_organism_get_organism_by_organism_id($node->organism->organism_id);
  276. // Add Synonyms for stock----------------------------------------------------------------------
  277. // These are a special case of property (stockprop table) where cvterm='synonym'
  278. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  279. $values = array(
  280. 'stock_id' => $node->stock_id,
  281. 'type_id' => array(
  282. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  283. 'name' => 'synonym'
  284. )
  285. );
  286. $results = tripal_core_chado_select('stockprop', $columns, $values);
  287. foreach ($results as $r) {
  288. $r->type = 'synonym';
  289. $node->synonyms[] = $r;
  290. }
  291. // Add properties for stock (not including synonyms)-------------------------------------------
  292. // $node->properties is an array of objects where each object describes a single property and has a type and value
  293. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  294. $values = array(
  295. 'stock_id' => $node->stock_id,
  296. );
  297. $results = tripal_core_chado_select('stockprop', $columns, $values);
  298. foreach ($results as $r) {
  299. $columns = array('name');
  300. $values = array('cvterm_id' => $r->type_id);
  301. $type_results = tripal_core_chado_select('cvterm', $columns, $values);
  302. $r->type = $type_results[0]->name;
  303. $node->properties[] = $r;
  304. }
  305. // Add in main db reference-----------------------------------------------------------------------
  306. // this is the dbxref_id in the stock table
  307. if (!empty($node->main_db_reference->dbxref_id)) {
  308. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  309. $values = array('dbxref_id' => $node->main_db_reference->dbxref_id);
  310. $results = tripal_core_chado_select('dbxref', $columns, $values);
  311. $node->main_db_reference = $results[0];
  312. //get db info
  313. $columns = array('name', 'description', 'url', 'urlprefix');
  314. $values = array('db_id' => $node->main_db_reference->db_id);
  315. $results = tripal_core_chado_select('db', $columns, $values);
  316. $node->main_db_reference->db_name = $results[0]->name;
  317. $node->main_db_reference->db_description = $results[0]->description;
  318. $node->main_db_reference->db_url = $results[0]->url;
  319. $node->main_db_reference->db_urlprefix = $results[0]->urlprefix;
  320. }
  321. // Add in extra references to external databases--------------------------------------------------
  322. // this includes all the dbxref entries in stock_dbxref
  323. $columns = array('dbxref_id');
  324. $values = array('stock_id' => $node->stock_id);
  325. $results = tripal_core_chado_select('stock_dbxref',$columns,$values);
  326. $node->db_references = array();
  327. foreach ($results as $r) {
  328. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  329. $values = array('dbxref_id' => $r->dbxref_id);
  330. $sub_results = tripal_core_chado_select('dbxref', $columns, $values);
  331. $dbxref = $sub_results[0];
  332. //get db info
  333. $columns = array('name', 'description', 'url', 'urlprefix');
  334. $values = array('db_id' => $dbxref->db_id);
  335. $results = tripal_core_chado_select('db', $columns, $values);
  336. $dbxref->db_name = $results[0]->name;
  337. $dbxref->db_description = $results[0]->description;
  338. $dbxref->db_url = $results[0]->url;
  339. $dbxref->db_urlprefix = $results[0]->urlprefix;
  340. $node->db_references[] = $dbxref;
  341. }
  342. // Add relationships for stock--------------------------------------------------------------------
  343. // Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
  344. // and those where the current stock is the object (other details stored in $node->subject_relationships)
  345. // fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
  346. // where current is subject.............................
  347. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  348. $values = array('subject_id' => $node->stock_id);
  349. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  350. $node->object_relationships = array();
  351. foreach ($results as $r) {
  352. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  353. $values = array('stock_id' => $r->object_id);
  354. $results = tripal_core_chado_select('stock', $columns, $values);
  355. $r->object->stock_id = $r->object_id;
  356. unset($r->object_id);
  357. $r->object->stock_name = $results[0]->name;
  358. $r->object->uniquename = $results[0]->uniquename;
  359. $r->object->description = $results[0]->description;
  360. $r->object->stock_type_id = $results[0]->type_id;
  361. $r->object->organism->organism_id = $results[0]->organism_id;
  362. $r->object->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  363. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  364. $object_node = db_fetch_object(db_query($sql, $r->object->stock_id));
  365. $r->object->nid = $object_node->nid;
  366. $node->object_relationships[] = $r;
  367. }
  368. // where current is object.............................
  369. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  370. $values = array('object_id' => $node->stock_id);
  371. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  372. $node->subject_relationships = array();
  373. foreach ($results as $r) {
  374. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  375. $values = array('stock_id' => $r->subject_id);
  376. $results = tripal_core_chado_select('stock', $columns, $values);
  377. $r->subject->stock_id = $r->subject_id;
  378. unset($r->subject_id);
  379. $r->subject->stock_name = $results[0]->name;
  380. $r->subject->uniquename = $results[0]->uniquename;
  381. $r->subject->description = $results[0]->description;
  382. $r->subject->stock_type_id = $results[0]->type_id;
  383. $r->subject->organism->organism_id = $results[0]->organism_id; $r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  384. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  385. $subject_node = db_fetch_object(db_query($sql, $r->subject->stock_id));
  386. $r->subject->nid = $subject_node->nid;
  387. $node->subject_relationships[] = $r;
  388. }
  389. return $node;
  390. }
  391. /*************************************************************************
  392. * Implements hook_form()
  393. * Creates the main Add/Edit/Delete Form for chado stocks
  394. *
  395. * Parts to be added by this form
  396. * name,
  397. * uniquename,
  398. * description,
  399. * type => select from cvterm with key cvterm_id,
  400. * organism => select from available with key organism_id
  401. * main_db_reference => accession, version, description, db_name(select from dropdown)
  402. */
  403. function chado_stock_form($node, $form_state) {
  404. $type = node_get_types('type', $node);
  405. // This defines the path for the next step in a simulated multipart form
  406. // NOTE: The %node gets replaced with the nid in insert
  407. $form['next_step_path'] = array(
  408. '#type' => 'hidden',
  409. '#value' => 'node/%node/properties'
  410. );
  411. // If you don't want a multipart form set this to false
  412. // Will then do default redirect (to new node) on submit
  413. $form['simulate_multipart'] = array(
  414. '#type' => 'textfield',
  415. '#attributes'=>array('style'=>"display:none"),
  416. '#default_value' => TRUE
  417. );
  418. if (!isset($node->uniquename)) {
  419. $form['progress'] = array(
  420. '#type' => 'item',
  421. '#value' => tripal_stock_add_chado_properties_progress('main')
  422. );
  423. }
  424. $form['names'] = array(
  425. '#type' => 'fieldset',
  426. '#title' => t('Stock Name')
  427. );
  428. $form['names']['title'] = array(
  429. '#type' => 'textfield',
  430. '#title' => t('Name'),
  431. '#default_value' => $node->title,
  432. '#required' => TRUE
  433. );
  434. $form['names']['uniquename'] = array(
  435. '#type' => 'textfield',
  436. '#title' => t('Unique Name'),
  437. '#default_value' => $node->uniquename,
  438. '#required' => TRUE
  439. );
  440. $form['names']['stock_id'] = array(
  441. '#type' => 'hidden',
  442. '#value' => $node->stock_id
  443. );
  444. $form['details'] = array(
  445. '#type' => 'fieldset',
  446. '#title' => t('Stock Details')
  447. );
  448. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  449. $type_options[0] = 'Select a Type';
  450. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
  451. $form['details']['type_id'] = array(
  452. '#type' => 'select',
  453. '#title' => t('Type of Stock'),
  454. '#options' => $type_options,
  455. '#default_value' => $type_default,
  456. '#required' => TRUE,
  457. );
  458. $stock_oganism_options = tripal_organism_get_organism_options();
  459. $stock_oganism_options[0] = 'Select An Organism';
  460. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
  461. $form['details']['organism_id'] = array(
  462. '#type' => 'select',
  463. '#title' => t('Source Organism for stock'),
  464. '#default_value' => $organism_default,
  465. '#options' => $stock_oganism_options,
  466. '#required' => TRUE
  467. );
  468. $form['details']['stock_description'] = array(
  469. '#type' => 'textarea',
  470. '#title' => t('Notes'),
  471. '#default_value' => $node->description,
  472. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  473. );
  474. $form['database_reference'] = array(
  475. '#type' => 'fieldset',
  476. '#title' => t('Stock Database Reference')
  477. );
  478. $form['database_reference']['accession'] = array(
  479. '#type' => 'textfield',
  480. '#title' => t('Accession'),
  481. '#default_value' => $node->main_db_reference->accession
  482. );
  483. $form['database_reference']['db_description'] = array(
  484. '#type' => 'textarea',
  485. '#title' => t('Description of Database Reference'),
  486. '#default_value' => $node->main_db_reference->db_description,
  487. '#description' => t('Optionally enter a description about the database accession.')
  488. );
  489. $db_options = tripal_db_get_db_options();
  490. $db_options[0] = 'Select a Database';
  491. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
  492. $form['database_reference']['database'] = array(
  493. '#type' => 'select',
  494. '#title' => t('Database'),
  495. '#options' => $db_options,
  496. '#default_value' => $db_default
  497. );
  498. return $form;
  499. }
  500. /*************************************************************************
  501. * Implements hook_validate()
  502. */
  503. function chado_stock_validate($node, &$form) {
  504. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  505. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  506. // Validate Uniquename only if add
  507. if (empty($node->stock_id)) {
  508. $previous_db = tripal_db_set_active('chado');
  509. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  510. tripal_db_set_active($previous_db);
  511. if(!empty($chado_row->stock_id)) {
  512. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  513. if (!empty($drupal_row->nid)) {
  514. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  515. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  516. } else {
  517. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  518. }
  519. }
  520. }
  521. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  522. if ( $node->type_id == 0) {
  523. form_set_error('type_id', 'Please select a type of stock.');
  524. } else {
  525. $previous_db = tripal_db_set_active('chado');
  526. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  527. tripal_db_set_active($previous_db);
  528. if ( $num_rows->count != 1) {
  529. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  530. }
  531. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  532. if ( $node->organism_id == 0) {
  533. form_set_error('organism_id', 'Please select a source organism for this stock');
  534. } else {
  535. $previous_db = tripal_db_set_active('chado');
  536. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  537. tripal_db_set_active($previous_db);
  538. if ( $num_rows->count != 1 ) {
  539. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  540. }
  541. // Check if Accession also database
  542. if ($node->accession != '') {
  543. if ($node->database == 0) {
  544. // there is an accession but no database selected
  545. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  546. }
  547. } else {
  548. if ($node->database > 0) {
  549. // there is a database selected but no accession
  550. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  551. }
  552. }
  553. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  554. if ( $node->database > 0) {
  555. $previous_db = tripal_db_set_active('chado');
  556. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  557. tripal_db_set_active($previous_db);
  558. if ($num_rows->count != 1) {
  559. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  560. }
  561. // Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
  562. if ( $node->accession != '') {
  563. $previous_db = tripal_db_set_active('chado');
  564. $num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
  565. tripal_db_set_active($previous_db);
  566. if ($num_rows->count > 0) {
  567. form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
  568. }
  569. }
  570. /*************************************************************************
  571. * Implements hook_insert()
  572. * Inserts data from chado_stock_form() into drupal and chado
  573. */
  574. function chado_stock_insert($node) {
  575. $previous_db = tripal_db_set_active('chado');
  576. // create dbxref
  577. if ( !empty($node->accession) ) {
  578. if ( !empty($node->database) ) {
  579. db_query(
  580. "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')",
  581. $node->database,
  582. $node->accession,
  583. $node->db_description
  584. );
  585. $dbxref = tripal_db_get_dbxref_by_accession($node->accession, $node->database);
  586. }
  587. }
  588. // create stock
  589. if($dbxref->dbxref_id != 0) {
  590. db_query(
  591. "INSERT INTO stock (dbxref_id, organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, %d, '%s', '%s', '%s', %d, 'f')",
  592. $dbxref->dbxref_id,
  593. $node->organism_id,
  594. $node->title,
  595. $node->uniquename,
  596. $node->description,
  597. $node->type_id
  598. );
  599. } else {
  600. db_query(
  601. "INSERT INTO stock (organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, '%s', '%s', '%s', %d, 'f')",
  602. $node->organism_id,
  603. $node->title,
  604. $node->uniquename,
  605. $node->description,
  606. $node->type_id
  607. );
  608. }
  609. tripal_db_set_active($previous_db);
  610. // create drupal chado_stock entry
  611. $previous_db = tripal_db_set_active('chado');
  612. $chado_stock = db_fetch_object(db_query("SELECT stock_id FROM stock WHERE uniquename='".$node->uniquename."' AND type_id=".$node->type_id.' AND organism_id='.$node->organism_id));
  613. tripal_db_set_active($previous_db);
  614. if (!empty($chado_stock->stock_id)) {
  615. db_query(
  616. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  617. ."VALUES (%d, %d, %d)",
  618. $node->nid,
  619. $node->vid,
  620. $chado_stock->stock_id
  621. );
  622. //Move on to next stage of Stock Creation based on next_stage_path field
  623. if ($node->simulate_multipart) {
  624. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  625. $_REQUEST['destination'] = $next_stage_path;
  626. }
  627. } else {
  628. drupal_set_message('Error during stock creation.', 'error');
  629. }
  630. }
  631. /*************************************************************************
  632. * Implements hook_update()
  633. * Handles Editing/Updating of main stock info (that in the chado stock table)
  634. * Currently just writes over all old data
  635. */
  636. function chado_stock_update($node) {
  637. if ($node->revision) {
  638. chado_stock_insert($node);
  639. } else {
  640. //can't change stock id which is all thats stored in drupal thus only update chado
  641. $previous_db = tripal_db_set_active('chado');
  642. db_query(
  643. "UPDATE stock SET organism_id=%d, name='%s', uniquename='%s', description='%s', type_id=%d WHERE stock_id=%d",
  644. $node->organism_id,
  645. $node->title,
  646. $node->uniquename,
  647. $node->description,
  648. $node->type_id,
  649. $node->stock_id
  650. );
  651. tripal_db_set_active($previous_db);
  652. }
  653. }
  654. /*************************************************************************
  655. * Implements hook_delete()
  656. * Handles deleting of chado_stocks
  657. */
  658. function chado_stock_delete($node) {
  659. //remove drupal node and all revisions
  660. db_query(
  661. "DELETE FROM {chado_stock} WHERE nid=%d",
  662. $node->nid
  663. );
  664. // Set stock in chado: is_obsolete = TRUE
  665. $previous_db = tripal_db_set_active('chado');
  666. db_query(
  667. "DELETE FROM stock WHERE stock_id=%d",
  668. $node->stock_id
  669. );
  670. tripal_db_set_active($previous_db);
  671. }