tripal_stock.module 28 KB

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