tripal_stock.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Implements Tripal Stock Module hooks
  6. */
  7. /**
  8. * @defgroup tripal_stock Stock Module
  9. * @{
  10. * Provides functions for managing chado stocks including creating details pages for each stock
  11. *
  12. * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado
  13. * stocks. The stock module was designed to store information about stock collections in a
  14. * laboratory. What is called a stock could also be called a strain or an accession. There is a lot
  15. * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all
  16. * come from some taxon, have genotypes, physical locations in the lab, some conceivable
  17. * relationship with a publication, some conceivable relationship with a sequence feature (such as a
  18. * transgene), and could be described by some ontology term. For more information about the chado
  19. * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
  20. * @}
  21. * @ingroup tripal_modules
  22. */
  23. require("tripal_stock-administration.inc");
  24. require("tripal_stock.api.inc");
  25. require("other_module_api_functions.inc");
  26. require("tripal_stock-secondary_tables.inc");
  27. require("tripal_stock-properties.inc");
  28. require("tripal_stock-relationships.inc");
  29. require("tripal_stock-db_references.inc");
  30. /**
  31. * Implements hook_menu(): Adds menu items for the tripal_stock
  32. *
  33. * @return
  34. * Menu definitions for the tripal_stock
  35. *
  36. * @ingroup tripal_stock
  37. */
  38. function tripal_stock_menu() {
  39. $items = array();
  40. //Administrative settings menu-----------------
  41. $items['admin/tripal/tripal_stock'] = array(
  42. 'title' => t('Stocks'),
  43. 'description' => t('Basic Description of Tripal Stock Module Functionality'),
  44. 'page callback' => 'tripal_stock_module_description_page',
  45. 'access arguments' => array('administer site configuration'),
  46. 'type' => MENU_NORMAL_ITEM
  47. );
  48. $items['admin/tripal/tripal_stock/configuration'] = array(
  49. 'title' => t('Configuration'),
  50. 'description' => t('Settings for Chado Stocks'),
  51. 'page callback' => 'drupal_get_form',
  52. 'page arguments' => array('tripal_stock_admin'),
  53. 'access arguments' => array('administer site configuration'),
  54. 'type' => MENU_NORMAL_ITEM
  55. );
  56. //Displaying stocks----------------------------
  57. $items['stocks'] = array(
  58. 'menu_name' => ('primary-links'),
  59. 'title' => t('Stocks'),
  60. 'page callback' => 'tripal_stock_show_stocks',
  61. 'access arguments' => array('access chado_stock content'),
  62. 'type' => MENU_NORMAL_ITEM,
  63. );
  64. // Adding Secondary Properties-----------------
  65. $items['node/%cs_node/properties'] = array(
  66. 'title' => t('Add Properties & Synonyms'),
  67. 'description' => t('Settings for Chado Stocks'),
  68. 'page callback' => 'tripal_stock_add_ALL_property_page',
  69. 'page arguments' => array(1),
  70. 'access arguments' => array('create chado_stock content'),
  71. 'type' => MENU_CALLBACK
  72. );
  73. $items['node/%cs_node/db_references'] = array(
  74. 'title' => t('Add Database References'),
  75. 'description' => t('Settings for Chado Stocks'),
  76. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  77. 'page arguments' => array(1),
  78. 'access arguments' => array('create chado_stock content'),
  79. 'type' => MENU_CALLBACK
  80. );
  81. $items['node/%cs_node/relationships'] = array(
  82. 'title' => t('Add Relationships'),
  83. 'description' => t('Settings for Chado Stocks'),
  84. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  85. 'page arguments' => array(1),
  86. 'access arguments' => array('create chado_stock content'),
  87. 'type' => MENU_CALLBACK
  88. );
  89. //Edit/Deleting Secondary Properties-------------
  90. $items['node/%cs_node/edit_properties'] = array(
  91. 'title' => t('Edit Properties'),
  92. 'description' => t('Settings for Chado Stocks'),
  93. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  94. 'page arguments' => array(1),
  95. 'access arguments' => array('edit chado_stock content'),
  96. 'type' => MENU_LOCAL_TASK,
  97. 'weight' => 8,
  98. );
  99. $items['node/%cs_node/edit_relationships'] = array(
  100. 'title' => t('Edit Relationships'),
  101. 'description' => t('Settings for Chado Stocks'),
  102. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  103. 'page arguments' => array(1),
  104. 'access arguments' => array('edit chado_stock content'),
  105. 'type' => MENU_LOCAL_TASK,
  106. 'weight' => 9,
  107. );
  108. $items['node/%cs_node/edit_db_references'] = array(
  109. 'title' => t('Edit DB References'),
  110. 'description' => t('Settings for Chado Stocks'),
  111. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  112. 'page arguments' => array(1),
  113. 'access arguments' => array('edit chado_stock content'),
  114. 'type' => MENU_LOCAL_TASK,
  115. 'weight' => 10,
  116. );
  117. return $items;
  118. }
  119. /**
  120. * Implements Menu wildcard_load hook
  121. *
  122. * Purpose: Allows the node ID of a chado stock to be dynamically
  123. * pulled from the path. The node is loaded from this node ID
  124. * and supplied to the page as an arguement. This is an example
  125. * of dynamic argument replacement using wildcards in the path.
  126. *
  127. * @param $nid
  128. * The node ID passed in from the path
  129. *
  130. * @return
  131. * The node object with the passed in nid
  132. *
  133. * @ingroup tripal_stock
  134. */
  135. function cs_node_load($nid) {
  136. if (is_numeric($nid)) {
  137. $node = node_load($nid);
  138. if ($node->type == 'chado_stock') {
  139. return $node;
  140. }
  141. }
  142. return FALSE;
  143. }
  144. /**
  145. * Implementation of hook_perm()
  146. *
  147. * Purpose: Set the permission types that the chado stock module uses
  148. *
  149. * @return
  150. * Listing of the possible permission catagories
  151. *
  152. * @ingroup tripal_stock
  153. */
  154. function tripal_stock_perm() {
  155. return array(
  156. 'access chado_stock content',
  157. 'create chado_stock content',
  158. 'edit chado_stock content',
  159. 'delete chado_stock content'
  160. );
  161. }
  162. /**
  163. * Implements hook_access(): Maps permission catagories to actions
  164. *
  165. * @param $op
  166. * The operation current operation: one of create, update, delete
  167. * @param $node
  168. * The node object the current operation is being performed on
  169. * @param $account
  170. * A user object representing the user for whom the operation is to be performed
  171. *
  172. * @return
  173. * TRUE grants access; FALSE denies it
  174. *
  175. * @ingroup tripal_stock
  176. */
  177. function chado_stock_access($op, $node, $account) {
  178. if ($op == 'create') {
  179. return user_access('create chado_stock content', $account);
  180. }
  181. if ($op == 'update') {
  182. if (user_access('edit chado_stock content', $account)) {
  183. return TRUE;
  184. }
  185. }
  186. if ($op == 'delete') {
  187. if (user_access('delete chado_stock content', $account)) {
  188. return TRUE;
  189. }
  190. }
  191. if ($op == 'view') {
  192. if (user_access('access chado_stock content', $account)) {
  193. return TRUE;
  194. }
  195. }
  196. return FALSE;
  197. }
  198. /**
  199. * Implements hook_views_api()
  200. *
  201. * Purpose: Essentially this hook tells drupal that there is views support for
  202. * for this module which then includes tripal_stock.views.inc where all the
  203. * views integration code is
  204. *
  205. * @return
  206. * An array with fields important for views integration
  207. *
  208. * @ingroup tripal_stock
  209. */
  210. function tripal_stock_views_api() {
  211. return array(
  212. 'api' => 2.0,
  213. );
  214. }
  215. /**
  216. * Implements hook_theme(): Register themeing functions for this module
  217. *
  218. * @return
  219. * An array of themeing functions to register
  220. *
  221. * @ingroup tripal_stock
  222. */
  223. function tripal_stock_theme() {
  224. return array(
  225. 'tripal_stock_stock_table' => array (
  226. 'arguments' => array('stocks'),
  227. ),
  228. // Property edit forms--------------------------
  229. 'tripal_stock_edit_ALL_properties_form' => array(
  230. 'arguments' => array('form'),
  231. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  232. ),
  233. 'tripal_stock_edit_ALL_db_references_form' => array(
  234. 'arguments' => array('form'),
  235. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  236. ),
  237. 'tripal_stock_edit_ALL_relationships_form' => array(
  238. 'arguments' => array('form'),
  239. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  240. ),
  241. // Block Templates------------------------------
  242. 'tripal_stock_base' => array (
  243. 'arguments' => array('node'=> null),
  244. 'template' => 'tripal_stock_base',
  245. ),
  246. 'tripal_stock_properties' => array (
  247. 'arguments' => array('node'=> null),
  248. 'template' => 'tripal_stock_properties',
  249. ),
  250. 'tripal_stock_references' => array (
  251. 'arguments' => array('node'=> null),
  252. 'template' => 'tripal_stock_references',
  253. ),
  254. 'tripal_stock_relationships_as_object' => array (
  255. 'arguments' => array('node'=> null),
  256. 'template' => 'tripal_stock_relationships_as_object',
  257. ),
  258. 'tripal_stock_relationships_as_subject' => array (
  259. 'arguments' => array('node'=> null),
  260. 'template' => 'tripal_stock_relationships_as_subject',
  261. ),
  262. 'tripal_stock_synonyms' => array (
  263. 'arguments' => array('node'=> null),
  264. 'template' => 'tripal_stock_synonyms',
  265. ),
  266. );
  267. }
  268. /**
  269. * Purpose: show stocks stored in drupals chado_stock table
  270. *
  271. * This function provides the default html representation of the stock table. This
  272. * representation can be overridden using Drupal Views2 to create more flexible tables
  273. * listing stocks.
  274. *
  275. * @return
  276. * HTML representation of a table of stocks
  277. *
  278. * @ingroup tripal_stock
  279. */
  280. function tripal_stock_show_stocks () {
  281. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  282. $no_stocks = db_result(db_query($sql));
  283. if($no_stocks != 0) {
  284. $stocks = tripal_stock_get_all_stocks();
  285. if($no_stocks != count($stocks)) {
  286. drupal_set_message("Synchronization needed.");
  287. }
  288. return theme('tripal_stock_stock_table',&$stocks);
  289. } else {
  290. return t("No Stocks exists. Please contact administrators to ".
  291. "synchronize stocks.");
  292. }
  293. }
  294. /**
  295. * A themeing funtion for the default tripal stock table
  296. *
  297. * @param $stocks
  298. * An array of all stock nodes
  299. *
  300. * @return
  301. * HTML representation of a table of stocks
  302. *
  303. * @ingroup tripal_stock
  304. */
  305. function theme_tripal_stock_stock_table (&$stocks) {
  306. // cycle through the stocks and build the stocks page
  307. $output = "<div id=\"stocks\">";
  308. $output .= '<table>';
  309. $output .= "<tr>";
  310. $output .= "<th>Name</th>";
  311. $output .= "<th>Type</th>";
  312. $output .= "<th>Organism</th>";
  313. $output .= "<th>Description</th>";
  314. $output .= "</tr>";
  315. foreach($stocks as $node){
  316. $output .= "<tr>";
  317. $output .= "<td>".l($node->stock->name, "node/".$node->nid)."</td>";
  318. $output .= "<td>".$node->stock->type_id->name."</td>";
  319. $output .= "<td nowrap>".$node->stock->organism_id->common_name."</td>";
  320. $output .= "<td>".$node->stock->description."</td>";
  321. $output .= "</tr>";
  322. }
  323. $output .= "</table>";
  324. $output .= "</div>";
  325. return $output;
  326. }
  327. /**
  328. * Implements hook_node_info(): registers a stock node type
  329. *
  330. * @return
  331. * An array describing various details of the node
  332. *
  333. * @ingroup tripal_stock
  334. */
  335. function tripal_stock_node_info() {
  336. return array(
  337. 'chado_stock' => array(
  338. 'name' => t('Stock'),
  339. 'module' => 'chado_stock',
  340. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  341. 'has_title' => TRUE,
  342. 'has_body' => FALSE,
  343. ),
  344. );
  345. }
  346. /**
  347. * Implements hook_load(): Prepares the chado_stock node
  348. *
  349. * @param $node
  350. * The basic node containing all variables common to all nodes
  351. *
  352. * @return
  353. * A stock node containing all the variables from the basic node and all stock specific variables
  354. *
  355. * @ingroup tripal_stock
  356. */
  357. function chado_stock_load($node) {
  358. // Get stock_id from chado_stock linking table
  359. $map = db_fetch_object(db_query(
  360. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  361. $node->vid
  362. ));
  363. // Get stock content and add to node
  364. $stock = tripal_core_generate_chado_var('stock', array('stock_id'=>$map->stock_id));
  365. //
  366. // move expandable fields downwards
  367. $node->expandable_fields = $stock->expandable_fields;
  368. unset($stock->expandable_fields);
  369. $node->expandable_tables = $stock->expandable_tables;
  370. unset($stock->expandable_tables);
  371. $node->expandable_nodes = $stock->expandable_nodes;
  372. unset($stock->expandable_nodes);
  373. $node->stock = $stock;
  374. return $node;
  375. }
  376. /**
  377. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  378. *
  379. * Parts to be added by this form
  380. * name,
  381. * uniquename,
  382. * description,
  383. * type => select from cvterm with key cvterm_id,
  384. * organism => select from available with key organism_id
  385. * main_db_reference => accession, version, description, db_name(select from dropdown)
  386. *
  387. * @param $node
  388. * An empty node object on insert OR the current stock node object on update
  389. * @param $form_state
  390. * The current state of the form
  391. *
  392. * @return
  393. * A description of the form to be rendered by drupal_get_form()
  394. *
  395. * @ingroup tripal_stock
  396. */
  397. function chado_stock_form($node, $form_state) {
  398. // Expand all fields needed
  399. $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
  400. foreach ($fields_needed as $field_name) {
  401. // Check to see if it's excluded and expand it if so
  402. if ($node->expandable_fields) {
  403. if (in_array($field_name, $node->expandable_fields)) {
  404. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  405. }
  406. }
  407. }
  408. // This defines the path for the next step in a simulated multipart form
  409. // NOTE: The %node gets replaced with the nid in insert
  410. $form['next_step_path'] = array(
  411. '#type' => 'hidden',
  412. '#value' => 'node/%node/properties'
  413. );
  414. // If you don't want a multipart form set this to false
  415. // Will then do default redirect (to new node) on submit
  416. $form['simulate_multipart'] = array(
  417. '#type' => 'textfield',
  418. '#attributes'=>array('style'=>"display:none"),
  419. '#default_value' => TRUE
  420. );
  421. if (!isset($node->stock->uniquename)) {
  422. $form['progress'] = array(
  423. '#type' => 'item',
  424. '#value' => tripal_stock_add_chado_properties_progress('main')
  425. );
  426. }
  427. $form['names'] = array(
  428. '#type' => 'fieldset',
  429. '#title' => t('Stock Name')
  430. );
  431. $form['names']['title'] = array(
  432. '#type' => 'textfield',
  433. '#title' => t('Name'),
  434. '#default_value' => $node->stock->name,
  435. '#required' => TRUE
  436. );
  437. $form['names']['uniquename'] = array(
  438. '#type' => 'textfield',
  439. '#title' => t('Unique Name'),
  440. '#default_value' => $node->stock->uniquename,
  441. '#required' => TRUE
  442. );
  443. $form['names']['stock_id'] = array(
  444. '#type' => 'hidden',
  445. '#value' => $node->stock->stock_id
  446. );
  447. $form['details'] = array(
  448. '#type' => 'fieldset',
  449. '#title' => t('Stock Details')
  450. );
  451. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  452. $type_options[0] = 'Select a Type';
  453. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock->type_id->cvterm_id; }
  454. $form['details']['type_id'] = array(
  455. '#type' => 'select',
  456. '#title' => t('Type of Stock'),
  457. '#options' => $type_options,
  458. '#default_value' => $type_default,
  459. '#required' => TRUE,
  460. );
  461. $stock_oganism_options = tripal_organism_get_organism_options();
  462. $stock_oganism_options[0] = 'Select An Organism';
  463. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->stock->organism_id->organism_id; }
  464. $form['details']['organism_id'] = array(
  465. '#type' => 'select',
  466. '#title' => t('Source Organism for stock'),
  467. '#default_value' => $organism_default,
  468. '#options' => $stock_oganism_options,
  469. '#required' => TRUE
  470. );
  471. $form['details']['stock_description'] = array(
  472. '#type' => 'textarea',
  473. '#title' => t('Notes'),
  474. '#default_value' => $node->stock->description,
  475. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  476. );
  477. $form['database_reference'] = array(
  478. '#type' => 'fieldset',
  479. '#title' => t('Stock Database Reference')
  480. );
  481. $form['database_reference']['accession'] = array(
  482. '#type' => 'textfield',
  483. '#title' => t('Accession'),
  484. '#default_value' => $node->stock->dbxref_id->accession
  485. );
  486. $form['database_reference']['db_description'] = array(
  487. '#type' => 'textarea',
  488. '#title' => t('Description of Database Reference'),
  489. '#default_value' => $node->stock->dbxref_id->description,
  490. '#description' => t('Optionally enter a description about the database accession.')
  491. );
  492. $db_options = tripal_db_get_db_options();
  493. $db_options[0] = 'Select a Database';
  494. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  495. $form['database_reference']['database'] = array(
  496. '#type' => 'select',
  497. '#title' => t('Database'),
  498. '#options' => $db_options,
  499. '#default_value' => $db_default
  500. );
  501. return $form;
  502. }
  503. /**
  504. * Implements hook_validate(): Validate the input from the chado_stock node form
  505. *
  506. * @param $node
  507. * The current node including fields with the form element names and submitted values
  508. * @param $form
  509. * A description of the form to be rendered by drupal_get_form()
  510. *
  511. * @ingroup tripal_stock
  512. */
  513. function chado_stock_validate($node, &$form) {
  514. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  515. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  516. // Validate Uniquename only if add
  517. if (empty($node->stock_id)) {
  518. $previous_db = tripal_db_set_active('chado');
  519. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  520. tripal_db_set_active($previous_db);
  521. if(!empty($chado_row->stock_id)) {
  522. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  523. if (!empty($drupal_row->nid)) {
  524. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  525. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  526. } else {
  527. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  528. }
  529. }
  530. }
  531. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  532. if ( $node->type_id == 0) {
  533. form_set_error('type_id', 'Please select a type of stock.');
  534. } else {
  535. $previous_db = tripal_db_set_active('chado');
  536. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  537. tripal_db_set_active($previous_db);
  538. if ( $num_rows->count != 1) {
  539. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  540. }
  541. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  542. if ( $node->organism_id == 0) {
  543. form_set_error('organism_id', 'Please select a source organism for this stock');
  544. } else {
  545. $previous_db = tripal_db_set_active('chado');
  546. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  547. tripal_db_set_active($previous_db);
  548. if ( $num_rows->count != 1 ) {
  549. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  550. }
  551. // Check if Accession also database
  552. if ($node->accession != '') {
  553. if ($node->database == 0) {
  554. // there is an accession but no database selected
  555. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  556. }
  557. } else {
  558. if ($node->database > 0) {
  559. // there is a database selected but no accession
  560. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  561. }
  562. }
  563. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  564. if ( $node->database > 0) {
  565. $previous_db = tripal_db_set_active('chado');
  566. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  567. tripal_db_set_active($previous_db);
  568. if ($num_rows->count != 1) {
  569. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  570. }
  571. }
  572. /**
  573. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  574. *
  575. * @param $node
  576. * The current node including fields with the form element names and submitted values
  577. *
  578. * @return
  579. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  580. *
  581. * @ingroup tripal_stock
  582. */
  583. function chado_stock_insert($node) {
  584. // create dbxref
  585. if ( !empty($node->accession) ) {
  586. if ( !empty($node->database) ) {
  587. $values = array(
  588. 'db_id' => $node->database,
  589. 'accession' => $node->accession,
  590. );
  591. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  592. $values['description'] = $node->db_description;
  593. $values['version'] = '1';
  594. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  595. if (!$dbxref_status) {
  596. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  597. watchdog('tripal_stock',
  598. 'Insert Stock: Unable to create dbxref where values:%values',
  599. array('%values' => print_r($values, TRUE)),
  600. WATCHDOG_WARNING
  601. );
  602. }
  603. } else { $dbxref_status = 1; }
  604. }
  605. }
  606. // create stock
  607. if($dbxref_status) {
  608. $values = array(
  609. 'dbxref_id' => array(
  610. 'db_id' => $node->database,
  611. 'accession' => $node->accession
  612. ),
  613. 'organism_id' => $node->organism_id,
  614. 'name' => $node->title,
  615. 'uniquename' => $node->uniquename,
  616. 'description' => $node->stock_description,
  617. 'type_id' => $node->type_id
  618. );
  619. $stock_status = tripal_core_chado_insert('stock', $values);
  620. } else {
  621. $values = array(
  622. 'organism_id' => $node->organism_id,
  623. 'name' => $node->title,
  624. 'uniquename' => $node->uniquename,
  625. 'description' => $node->stock_description,
  626. 'type_id' => $node->type_id
  627. );
  628. $stock_status = tripal_core_chado_insert('stock', $values);
  629. }
  630. // create drupal chado_stock entry
  631. if ($stock_status) {
  632. $values = array(
  633. 'organism_id' => $node->organism_id,
  634. 'uniquename' => $node->uniquename,
  635. 'type_id' => $node->type_id
  636. );
  637. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  638. if (!empty($chado_stock[0]->stock_id)) {
  639. db_query(
  640. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  641. ."VALUES (%d, %d, %d)",
  642. $node->nid,
  643. $node->vid,
  644. $chado_stock[0]->stock_id
  645. );
  646. //Move on to next stage of Stock Creation based on next_stage_path field
  647. if ($node->simulate_multipart) {
  648. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  649. $_REQUEST['destination'] = $next_stage_path;
  650. }
  651. } else {
  652. drupal_set_message('Error during stock creation.', 'error');
  653. watchdog('tripal_stock',
  654. 'Insert Stock: Unable to find newly created stock where values:%values',
  655. array('%values' => print_r($values, TRUE)),
  656. WATCHDOG_ERROR
  657. );
  658. return FALSE;
  659. }
  660. } else {
  661. drupal_set_message('Error during stock creation.', 'error');
  662. watchdog('tripal_stock',
  663. 'Insert Stock: Unable to create stock where values:%values',
  664. array('%values' => print_r($values, TRUE)),
  665. WATCHDOG_WARNING
  666. );
  667. return FALSE;
  668. }
  669. }
  670. /**
  671. * Implements hook_update(): Handles Editing/Updating of main stock info
  672. *
  673. * NOTE: Currently just writes over all old data
  674. *
  675. * @param $node
  676. * The current node including fields with the form element names and submitted values
  677. *
  678. * @return
  679. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  680. *
  681. * @ingroup tripal_stock
  682. */
  683. function chado_stock_update($node) {
  684. if ($node->revision) {
  685. chado_stock_insert($node);
  686. } else {
  687. //update dbxref
  688. if ($node->database) {
  689. if($node->accession) {
  690. $dbxref_mode = '';
  691. $stock = tripal_core_chado_select(
  692. 'stock',
  693. array('dbxref_id', 'type_id'),
  694. array('stock_id' => $node->stock_id)
  695. );
  696. if ($stock[0]->dbxref_id) {
  697. $values = array(
  698. 'db_id' => $node->database,
  699. 'accession' => $node->accession,
  700. 'description' => $node->db_description
  701. );
  702. $dbxref_status = tripal_core_chado_update(
  703. 'dbxref',
  704. array('dbxref_id' => $stock[0]->dbxref_id),
  705. $values
  706. );
  707. $dbxref_mode = 'Update';
  708. } else {
  709. if ($stock[0]->type_id) {
  710. //create the dbxref
  711. //used the type_id as a control to check we have a stock but not a dbxref
  712. $values = array(
  713. 'db_id' => $node->database,
  714. 'accession' => $node->accession,
  715. 'description' => $node->db_description,
  716. 'version' => '1',
  717. );
  718. $dbxref_status = tripal_core_chado_insert(
  719. 'dbxref',
  720. $values
  721. );
  722. $dbxref_mode = 'Create';
  723. } else {
  724. drupal_set_message('Unable to find stock to Update', 'error');
  725. watchdog(
  726. 'tripal_stock',
  727. 'Stock Update: Unable to find stock to update using values: %values',
  728. array('%values', print_r($values, TRUE)),
  729. WATCHDOG_ERROR
  730. );
  731. return FALSE;
  732. }
  733. }
  734. }
  735. }
  736. if (!$dbxref_status) {
  737. watchdog(
  738. 'tripal_stock',
  739. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  740. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  741. WATCHDOG_WARNING
  742. );
  743. }
  744. //can't change stock id which is all thats stored in drupal thus only update chado
  745. $update_values = array(
  746. 'organism_id' => $node->organism_id,
  747. 'name' => $node->title,
  748. 'uniquename' => $node->uniquename,
  749. 'description' => $node->stock_description,
  750. 'type_id' => $node->type_id,
  751. );
  752. if ($dbxref_status) {
  753. $update_values['dbxref_id'] = array(
  754. 'db_id' => $node->database,
  755. 'accession' => $node->accession
  756. );
  757. }
  758. $status = tripal_core_chado_update(
  759. 'stock',
  760. array('stock_id' => $node->stock_id),
  761. $update_values
  762. );
  763. if (!$status) {
  764. drupal_set_message('Unable to update stock', 'error');
  765. watchdog(
  766. 'tripal_stock',
  767. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  768. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  769. WATCHDOG_ERROR
  770. );
  771. }
  772. }
  773. }
  774. /**
  775. * Implements hook_delete(): Handles deleting of chado_stocks
  776. *
  777. * NOTE: Currently deletes data -no undo or record-keeping functionality
  778. *
  779. * @param $node
  780. * The current node including fields with the form element names and submitted values
  781. *
  782. * @return
  783. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  784. *
  785. * @ingroup tripal_stock
  786. */
  787. function chado_stock_delete($node) {
  788. //remove drupal node and all revisions
  789. db_query(
  790. "DELETE FROM {chado_stock} WHERE nid=%d",
  791. $node->nid
  792. );
  793. // Set stock in chado: is_obsolete = TRUE
  794. $previous_db = tripal_db_set_active('chado');
  795. db_query(
  796. "DELETE FROM stock WHERE stock_id=%d",
  797. $node->stock_id
  798. );
  799. tripal_db_set_active($previous_db);
  800. }
  801. /**
  802. * Purpose: Implement Blocks relating to stock content
  803. *
  804. * @param $op
  805. * What kind of information to retrieve about the block or blocks.
  806. * Possible values include list, configure, save, view.
  807. * @param $delta
  808. * Which block to return (not applicable if $op is 'list').
  809. * @param $edit
  810. * If $op is 'save', the submitted form data from the configuration form.
  811. *
  812. * @return
  813. * One of the following depending on $op: An array of block descriptions (list), the configuration
  814. * form (configure), nothing (save), an array defining subject and content for the block indexed
  815. * by $delta (view)
  816. *
  817. * @ingroup tripal_stock
  818. */
  819. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  820. switch($op) {
  821. case 'list':
  822. $blocks['base']['info'] = t('Tripal Stock Details');
  823. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  824. $blocks['properties']['info'] = t('Tripal Stock Properties');
  825. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  826. $blocks['references']['info'] = t('Tripal Stock References');
  827. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  828. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
  829. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  830. $blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
  831. $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
  832. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  833. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  834. return $blocks;
  835. case 'view':
  836. if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  837. $nid = arg(1);
  838. $node = node_load($nid);
  839. $block = array();
  840. switch($delta){
  841. case 'base':
  842. $block['subject'] = t('Stock Details');
  843. $block['content'] = theme('tripal_stock_base',$node);
  844. break;
  845. case 'properties':
  846. $block['subject'] = t('Properties');
  847. $block['content'] = theme('tripal_stock_properties',$node);
  848. break;
  849. case 'references':
  850. $block['subject'] = t('References');
  851. $block['content'] = theme('tripal_stock_references',$node);
  852. break;
  853. case 'relationships_as_object':
  854. $block['subject'] = t('Object Relationships');
  855. $block['content'] = theme('tripal_stock_relationships_as_object',$node);
  856. break;
  857. case 'relationships_as_subject':
  858. $block['subject'] = t('Subject Relationships');
  859. $block['content'] = theme('tripal_stock_relationships_as_subject',$node);
  860. break;
  861. case 'synonyms':
  862. $block['subject'] = t('Synonyms');
  863. $block['content'] = theme('tripal_stock_synonyms',$node);
  864. break;
  865. }
  866. return $block;
  867. }
  868. }
  869. }