tripal_stock.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. $previous_db = tripal_db_set_active('chado');
  254. $chado_stock = db_fetch_object(db_query(
  255. "SELECT s.name, s.uniquename, s.description, c.name as type, c.cvterm_id as type_id, s.is_obsolete, s.organism_id "
  256. ."FROM stock s, cvterm c WHERE s.type_id=c.cvterm_id AND stock_id=%d",
  257. $map->stock_id
  258. ));
  259. tripal_db_set_active($previous_db);
  260. //add to node
  261. $node->stock_name = $chado_stock->name;
  262. $node->uniquename = $chado_stock->uniquename;
  263. $node->description = $chado_stock->description;
  264. $node->stock_type = $chado_stock->type;
  265. $node->stock_type_id = $chado_stock->type_id;
  266. $node->is_obsolete = $chado_stock->is_obsolete;
  267. // Get organism details from chado & add to node-----------------------------------------------
  268. // get organism if for current stock
  269. // pull all data from chado for the organism and assign it to the current node
  270. $node->organism = tripal_organism_get_organism_by_organism_id($chado_stock->organism_id);
  271. // Add Synonyms for stock----------------------------------------------------------------------
  272. // These are a special case of property (stockprop table) where cvterm='synonym'
  273. $previous_db = tripal_db_set_active('chado');
  274. $results = db_query(
  275. "SELECT sp.stockprop_id, sp.value as value, sp.type_id, 'synonym' as type FROM stockprop sp, cvterm c "
  276. ."WHERE sp.stock_id=%d AND sp.type_id=c.cvterm_id AND c.name='synonym'",
  277. $map->stock_id
  278. );
  279. tripal_db_set_active($previous_db);
  280. $i=0;
  281. // Node->synonyms is an array of synonyms(strings)
  282. while ($r = db_fetch_object($results) ) {
  283. $synonyms[$i++] = $r;
  284. }
  285. $node->synonyms = $synonyms;
  286. // Add properties for stock (not including synonyms)-------------------------------------------
  287. // $node->properties is an array of objects where each object describes a single property and has a type and value
  288. $previous_db = tripal_db_set_active('chado');
  289. $results = db_query(
  290. "SELECT sp.stockprop_id, c.name as type, sp.value as value FROM stockprop sp, cvterm c "
  291. ."WHERE sp.stock_id=%d AND sp.type_id=c.cvterm_id AND c.name != 'synonym'",
  292. $map->stock_id
  293. );
  294. tripal_db_set_active($previous_db);
  295. $i=0;
  296. while ($r = db_fetch_object($results) ) {
  297. $properties[$i++] = $r;
  298. }
  299. $node->properties = $properties;
  300. // Add in main db reference-----------------------------------------------------------------------
  301. // this is the dbxref_id in the stock table
  302. //$node->main_db_reference is an object describing the dbxref and has an accession, version, description, db_name, db_description, & urlprefix
  303. $previous_db = tripal_db_set_active('chado');
  304. $db_reference = db_fetch_object(db_query(
  305. "SELECT dbx.dbxref_id, dbx.accession, dbx.version, dbx.description, db.db_id, db.name as db_name, db.description as db_description, db.urlprefix as db_urlprefix "
  306. ."FROM dbxref dbx, db db, stock s WHERE stock_id=%d AND s.dbxref_id=dbx.dbxref_id AND dbx.db_id=db.db_id",
  307. $map->stock_id
  308. ));
  309. tripal_db_set_active($previous_db);
  310. $node->main_db_reference = $db_reference;
  311. // Add in extra references to external databases---------------------------------------------------
  312. // this includes all the dbxref entries in stock_dbxref
  313. // $node->db_referencesi s an array of objects where each object describes a given dbxref and has the same fields as the main db references
  314. $previous_db = tripal_db_set_active('chado');
  315. $results = db_query(
  316. "SELECT dbx.dbxref_id, dbx.accession, dbx.version, dbx.description, db.db_id, db.name as db_name, db.description as db_description, db.urlprefix as db_urlprefix "
  317. ."FROM dbxref dbx, db db, stock_dbxref s WHERE stock_id=%d AND s.dbxref_id=dbx.dbxref_id AND dbx.db_id=db.db_id",
  318. $map->stock_id
  319. );
  320. tripal_db_set_active($previous_db);
  321. $i=0;
  322. while ($r = db_fetch_object($results) ) {
  323. $db_references[$i++] = $r;
  324. }
  325. $node->db_references = $db_references;
  326. // Add relationships for stock---------------------------------------------------------------------
  327. // Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
  328. // and those where the current stock is the object (other details stored in $node->subject_relationships)
  329. // fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
  330. // where current is subject.............................
  331. $previous_db = tripal_db_set_active('chado');
  332. $result = db_query(
  333. "SELECT sr.stock_relationship_id, c.name as relationship_type, sr.type_id as relationship_type_id, s.uniquename as object_uniquename, s.name as object_name, s.stock_id as object_id "
  334. . "FROM stock_relationship sr, stock s, cvterm c "
  335. . "WHERE sr.subject_id=%d AND sr.object_id=s.stock_id AND sr.type_id=c.cvterm_id",
  336. $map->stock_id
  337. );
  338. tripal_db_set_active($previous_db);
  339. $i=0;
  340. $relationships= array();
  341. while ($r = db_fetch_object($result) ) {
  342. //get nid
  343. $object = db_fetch_object(db_query("SELECT * FROM {node} n, {chado_stock} c WHERE c.nid=n.nid AND c.vid=n.vid AND c.stock_id=%d", $r->object_id ));
  344. $r->object_nid = $object->nid;
  345. $relationships[$i++] = $r;
  346. }
  347. $node->object_relationships = $relationships;
  348. // where current is object.............................
  349. $previous_db = tripal_db_set_active('chado');
  350. $result = db_query(
  351. "SELECT sr.stock_relationship_id, c.name as relationship_type, sr.type_id as relationship_type_id, s.uniquename as subject_uniquename, s.name as subject_name, s.stock_id as subject_id "
  352. . "FROM stock_relationship sr, stock s, cvterm c "
  353. . "WHERE sr.object_id=%d AND sr.subject_id=s.stock_id AND sr.type_id=c.cvterm_id",
  354. $map->stock_id
  355. );
  356. tripal_db_set_active($previous_db);
  357. $i=0;
  358. $relationships= array();
  359. while ($r = db_fetch_object($result) ) {
  360. //get nid
  361. $subject = db_fetch_object(db_query("SELECT * FROM {node} n, {chado_stock} c WHERE c.nid=n.nid AND c.vid=n.vid AND c.stock_id=%d", $r->subject_id ));
  362. $r->subject_nid = $subject->nid;
  363. $relationships[$i++] = $r;
  364. }
  365. $node->subject_relationships = $relationships;
  366. return $node;
  367. }
  368. /*************************************************************************
  369. * Implements hook_form()
  370. * Creates the main Add/Edit/Delete Form for chado stocks
  371. *
  372. * Parts to be added by this form
  373. * name,
  374. * uniquename,
  375. * description,
  376. * type => select from cvterm with key cvterm_id,
  377. * organism => select from available with key organism_id
  378. * main_db_reference => accession, version, description, db_name(select from dropdown)
  379. */
  380. function chado_stock_form($node, $form_state) {
  381. $type = node_get_types('type', $node);
  382. // This defines the path for the next step in a simulated multipart form
  383. // NOTE: The %node gets replaced with the nid in insert
  384. $form['next_step_path'] = array(
  385. '#type' => 'hidden',
  386. '#value' => 'node/%node/properties'
  387. );
  388. // If you don't want a multipart form set this to false
  389. // Will then do default redirect (to new node) on submit
  390. $form['simulate_multipart'] = array(
  391. '#type' => 'textfield',
  392. '#attributes'=>array('style'=>"display:none"),
  393. '#default_value' => TRUE
  394. );
  395. if (!isset($node->uniquename)) {
  396. $form['progress'] = array(
  397. '#type' => 'item',
  398. '#value' => tripal_stock_add_chado_properties_progress('main')
  399. );
  400. }
  401. $form['names'] = array(
  402. '#type' => 'fieldset',
  403. '#title' => t('Stock Name')
  404. );
  405. $form['names']['title'] = array(
  406. '#type' => 'textfield',
  407. '#title' => t('Name'),
  408. '#default_value' => $node->title,
  409. '#required' => TRUE
  410. );
  411. $form['names']['uniquename'] = array(
  412. '#type' => 'textfield',
  413. '#title' => t('Unique Name'),
  414. '#default_value' => $node->uniquename,
  415. '#required' => TRUE
  416. );
  417. $form['names']['stock_id'] = array(
  418. '#type' => 'hidden',
  419. '#value' => $node->stock_id
  420. );
  421. $form['details'] = array(
  422. '#type' => 'fieldset',
  423. '#title' => t('Stock Details')
  424. );
  425. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  426. $type_options[0] = 'Select a Type';
  427. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
  428. $form['details']['type_id'] = array(
  429. '#type' => 'select',
  430. '#title' => t('Type of Stock'),
  431. '#options' => $type_options,
  432. '#default_value' => $type_default,
  433. '#required' => TRUE,
  434. );
  435. $stock_oganism_options = tripal_organism_get_organism_options();
  436. $stock_oganism_options[0] = 'Select An Organism';
  437. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
  438. $form['details']['organism_id'] = array(
  439. '#type' => 'select',
  440. '#title' => t('Source Organism for stock'),
  441. '#default_value' => $organism_default,
  442. '#options' => $stock_oganism_options,
  443. '#required' => TRUE
  444. );
  445. $form['details']['stock_description'] = array(
  446. '#type' => 'textarea',
  447. '#title' => t('Notes'),
  448. '#default_value' => $node->description,
  449. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  450. );
  451. $form['database_reference'] = array(
  452. '#type' => 'fieldset',
  453. '#title' => t('Stock Database Reference')
  454. );
  455. $form['database_reference']['accession'] = array(
  456. '#type' => 'textfield',
  457. '#title' => t('Accession'),
  458. '#default_value' => $node->main_db_reference->accession
  459. );
  460. $form['database_reference']['db_description'] = array(
  461. '#type' => 'textarea',
  462. '#title' => t('Description of Database Reference'),
  463. '#default_value' => $node->main_db_reference->db_description,
  464. '#description' => t('Optionally enter a description about the database accession.')
  465. );
  466. $db_options = tripal_db_get_db_options();
  467. $db_options[0] = 'Select a Database';
  468. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
  469. $form['database_reference']['database'] = array(
  470. '#type' => 'select',
  471. '#title' => t('Database'),
  472. '#options' => $db_options,
  473. '#default_value' => $db_default
  474. );
  475. return $form;
  476. }
  477. /*************************************************************************
  478. * Implements hook_validate()
  479. */
  480. function chado_stock_validate($node, &$form) {
  481. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  482. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  483. // Validate Uniquename only if add
  484. if (empty($node->stock_id)) {
  485. $previous_db = tripal_db_set_active('chado');
  486. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  487. tripal_db_set_active($previous_db);
  488. if(!empty($chado_row->stock_id)) {
  489. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  490. if (!empty($drupal_row->nid)) {
  491. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  492. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  493. } else {
  494. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  495. }
  496. }
  497. }
  498. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  499. if ( $node->type_id == 0) {
  500. form_set_error('type_id', 'Please select a type of stock.');
  501. } else {
  502. $previous_db = tripal_db_set_active('chado');
  503. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  504. tripal_db_set_active($previous_db);
  505. if ( $num_rows->count != 1) {
  506. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  507. }
  508. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  509. if ( $node->organism_id == 0) {
  510. form_set_error('organism_id', 'Please select a source organism for this stock');
  511. } else {
  512. $previous_db = tripal_db_set_active('chado');
  513. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  514. tripal_db_set_active($previous_db);
  515. if ( $num_rows->count != 1 ) {
  516. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  517. }
  518. // Check if Accession also database
  519. if ($node->accession != '') {
  520. if ($node->database == 0) {
  521. // there is an accession but no database selected
  522. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  523. }
  524. } else {
  525. if ($node->database > 0) {
  526. // there is a database selected but no accession
  527. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  528. }
  529. }
  530. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  531. if ( $node->database > 0) {
  532. $previous_db = tripal_db_set_active('chado');
  533. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  534. tripal_db_set_active($previous_db);
  535. if ($num_rows->count != 1) {
  536. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  537. }
  538. // Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
  539. if ( $node->accession != '') {
  540. $previous_db = tripal_db_set_active('chado');
  541. $num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
  542. tripal_db_set_active($previous_db);
  543. if ($num_rows->count > 0) {
  544. form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
  545. }
  546. }
  547. /*************************************************************************
  548. * Implements hook_insert()
  549. * Inserts data from chado_stock_form() into drupal and chado
  550. */
  551. function chado_stock_insert($node) {
  552. $previous_db = tripal_db_set_active('chado');
  553. // create dbxref
  554. if ( !empty($node->accession) ) {
  555. if ( !empty($node->database) ) {
  556. db_query(
  557. "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')",
  558. $node->database,
  559. $node->accession,
  560. $node->db_description
  561. );
  562. $dbxref = tripal_db_get_dbxref_by_accession($node->accession, $node->database);
  563. }
  564. }
  565. // create stock
  566. if($dbxref->dbxref_id != 0) {
  567. db_query(
  568. "INSERT INTO stock (dbxref_id, organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, %d, '%s', '%s', '%s', %d, 'f')",
  569. $dbxref->dbxref_id,
  570. $node->organism_id,
  571. $node->title,
  572. $node->uniquename,
  573. $node->description,
  574. $node->type_id
  575. );
  576. } else {
  577. db_query(
  578. "INSERT INTO stock (organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, '%s', '%s', '%s', %d, 'f')",
  579. $node->organism_id,
  580. $node->title,
  581. $node->uniquename,
  582. $node->description,
  583. $node->type_id
  584. );
  585. }
  586. tripal_db_set_active($previous_db);
  587. // create drupal chado_stock entry
  588. $previous_db = tripal_db_set_active('chado');
  589. $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));
  590. tripal_db_set_active($previous_db);
  591. if (!empty($chado_stock->stock_id)) {
  592. db_query(
  593. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  594. ."VALUES (%d, %d, %d)",
  595. $node->nid,
  596. $node->vid,
  597. $chado_stock->stock_id
  598. );
  599. //Move on to next stage of Stock Creation based on next_stage_path field
  600. if ($node->simulate_multipart) {
  601. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  602. $_REQUEST['destination'] = $next_stage_path;
  603. }
  604. } else {
  605. drupal_set_message('Error during stock creation.', 'error');
  606. }
  607. }
  608. /*************************************************************************
  609. * Implements hook_update()
  610. * Handles Editing/Updating of main stock info (that in the chado stock table)
  611. * Currently just writes over all old data
  612. */
  613. function chado_stock_update($node) {
  614. if ($node->revision) {
  615. chado_stock_insert($node);
  616. } else {
  617. //can't change stock id which is all thats stored in drupal thus only update chado
  618. $previous_db = tripal_db_set_active('chado');
  619. db_query(
  620. "UPDATE stock SET organism_id=%d, name='%s', uniquename='%s', description='%s', type_id=%d WHERE stock_id=%d",
  621. $node->organism_id,
  622. $node->title,
  623. $node->uniquename,
  624. $node->description,
  625. $node->type_id,
  626. $node->stock_id
  627. );
  628. tripal_db_set_active($previous_db);
  629. }
  630. }
  631. /*************************************************************************
  632. * Implements hook_delete()
  633. * Handles deleting of chado_stocks
  634. */
  635. function chado_stock_delete($node) {
  636. //remove drupal node and all revisions
  637. db_query(
  638. "DELETE FROM {chado_stock} WHERE nid=%d",
  639. $node->nid
  640. );
  641. // Set stock in chado: is_obsolete = TRUE
  642. $previous_db = tripal_db_set_active('chado');
  643. db_query(
  644. "DELETE FROM stock WHERE stock_id=%d",
  645. $node->stock_id
  646. );
  647. tripal_db_set_active($previous_db);
  648. }