tripal_stock.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. if(!user_access('create chado_stock content', $account)){
  180. return FALSE;
  181. }
  182. }
  183. if ($op == 'update') {
  184. if (!user_access('edit chado_stock content', $account)) {
  185. return FALSE;
  186. }
  187. }
  188. if ($op == 'delete') {
  189. if (!user_access('delete chado_stock content', $account)) {
  190. return FALSE;
  191. }
  192. }
  193. if ($op == 'view') {
  194. if (!user_access('access chado_stock content', $account)) {
  195. return FALSE;
  196. }
  197. }
  198. return NULL;
  199. }
  200. /**
  201. * Implements hook_views_api()
  202. *
  203. * Purpose: Essentially this hook tells drupal that there is views support for
  204. * for this module which then includes tripal_stock.views.inc where all the
  205. * views integration code is
  206. *
  207. * @return
  208. * An array with fields important for views integration
  209. *
  210. * @ingroup tripal_stock
  211. */
  212. function tripal_stock_views_api() {
  213. return array(
  214. 'api' => 2.0,
  215. );
  216. }
  217. /**
  218. * Implements hook_theme(): Register themeing functions for this module
  219. *
  220. * @return
  221. * An array of themeing functions to register
  222. *
  223. * @ingroup tripal_stock
  224. */
  225. function tripal_stock_theme() {
  226. return array(
  227. 'tripal_stock_stock_table' => array (
  228. 'arguments' => array('stocks'),
  229. ),
  230. // Property edit forms--------------------------
  231. 'tripal_stock_edit_ALL_properties_form' => array(
  232. 'arguments' => array('form'),
  233. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  234. ),
  235. 'tripal_stock_edit_ALL_db_references_form' => array(
  236. 'arguments' => array('form'),
  237. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  238. ),
  239. 'tripal_stock_edit_ALL_relationships_form' => array(
  240. 'arguments' => array('form'),
  241. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  242. ),
  243. // Block Templates------------------------------
  244. 'tripal_stock_base' => array (
  245. 'arguments' => array('node'=> null),
  246. 'template' => 'tripal_stock_base',
  247. ),
  248. 'tripal_stock_properties' => array (
  249. 'arguments' => array('node'=> null),
  250. 'template' => 'tripal_stock_properties',
  251. ),
  252. 'tripal_stock_references' => array (
  253. 'arguments' => array('node'=> null),
  254. 'template' => 'tripal_stock_references',
  255. ),
  256. 'tripal_stock_relationships' => array (
  257. 'arguments' => array('node'=> null),
  258. 'template' => 'tripal_stock_relationships',
  259. ),
  260. 'tripal_stock_synonyms' => array (
  261. 'arguments' => array('node'=> null),
  262. 'template' => 'tripal_stock_synonyms',
  263. ),
  264. );
  265. }
  266. /**
  267. * Purpose: show stocks stored in drupals chado_stock table
  268. *
  269. * This function provides the default html representation of the stock table. This
  270. * representation can be overridden using Drupal Views2 to create more flexible tables
  271. * listing stocks.
  272. *
  273. * @return
  274. * HTML representation of a table of stocks
  275. *
  276. * @ingroup tripal_stock
  277. */
  278. function tripal_stock_show_stocks () {
  279. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  280. $no_stocks = db_result(db_query($sql));
  281. if($no_stocks != 0) {
  282. $stocks = tripal_stock_get_all_stocks();
  283. if($no_stocks != count($stocks)) {
  284. drupal_set_message("Synchronization needed.");
  285. }
  286. return theme('tripal_stock_stock_table',&$stocks);
  287. } else {
  288. return t("No Stocks exists. Please contact administrators to ".
  289. "synchronize stocks.");
  290. }
  291. }
  292. /**
  293. * A themeing funtion for the default tripal stock table
  294. *
  295. * @param $stocks
  296. * An array of all stock nodes
  297. *
  298. * @return
  299. * HTML representation of a table of stocks
  300. *
  301. * @ingroup tripal_stock
  302. */
  303. function theme_tripal_stock_stock_table (&$stocks) {
  304. // cycle through the stocks and build the stocks page
  305. $output = "<div id=\"stocks\">";
  306. $output .= '<table>';
  307. $output .= "<tr>";
  308. $output .= "<th>Name</th>";
  309. $output .= "<th>Type</th>";
  310. $output .= "<th>Organism</th>";
  311. $output .= "<th>Description</th>";
  312. $output .= "</tr>";
  313. foreach($stocks as $node){
  314. $output .= "<tr>";
  315. $output .= "<td>".l($node->stock->name, "node/".$node->nid)."</td>";
  316. $output .= "<td>".$node->stock->type_id->name."</td>";
  317. $output .= "<td nowrap>".$node->stock->organism_id->common_name."</td>";
  318. $output .= "<td>".$node->stock->description."</td>";
  319. $output .= "</tr>";
  320. }
  321. $output .= "</table>";
  322. $output .= "</div>";
  323. return $output;
  324. }
  325. /**
  326. * Implements hook_node_info(): registers a stock node type
  327. *
  328. * @return
  329. * An array describing various details of the node
  330. *
  331. * @ingroup tripal_stock
  332. */
  333. function tripal_stock_node_info() {
  334. return array(
  335. 'chado_stock' => array(
  336. 'name' => t('Stock'),
  337. 'module' => 'chado_stock',
  338. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  339. 'has_title' => TRUE,
  340. 'has_body' => FALSE,
  341. ),
  342. );
  343. }
  344. /**
  345. * Implements hook_load(): Prepares the chado_stock node
  346. *
  347. * @param $node
  348. * The basic node containing all variables common to all nodes
  349. *
  350. * @return
  351. * A stock node containing all the variables from the basic node and all stock specific variables
  352. *
  353. * @ingroup tripal_stock
  354. */
  355. function chado_stock_load($node) {
  356. // Get stock_id from chado_stock linking table
  357. $map = db_fetch_object(db_query(
  358. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  359. $node->vid
  360. ));
  361. // Get stock content and add to node
  362. $stock = tripal_core_generate_chado_var('stock', array('stock_id'=>$map->stock_id));
  363. //
  364. // move expandable fields downwards
  365. $node->expandable_fields = $stock->expandable_fields;
  366. unset($stock->expandable_fields);
  367. $node->expandable_tables = $stock->expandable_tables;
  368. unset($stock->expandable_tables);
  369. $node->expandable_nodes = $stock->expandable_nodes;
  370. unset($stock->expandable_nodes);
  371. $node->stock = $stock;
  372. return $node;
  373. }
  374. /**
  375. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  376. *
  377. * Parts to be added by this form
  378. * name,
  379. * uniquename,
  380. * description,
  381. * type => select from cvterm with key cvterm_id,
  382. * organism => select from available with key organism_id
  383. * main_db_reference => accession, version, description, db_name(select from dropdown)
  384. *
  385. * @param $node
  386. * An empty node object on insert OR the current stock node object on update
  387. * @param $form_state
  388. * The current state of the form
  389. *
  390. * @return
  391. * A description of the form to be rendered by drupal_get_form()
  392. *
  393. * @ingroup tripal_stock
  394. */
  395. function chado_stock_form($node, $form_state) {
  396. // Expand all fields needed
  397. $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');
  398. foreach ($fields_needed as $field_name) {
  399. // Check to see if it's excluded and expand it if so
  400. if ($node->expandable_fields) {
  401. if (in_array($field_name, $node->expandable_fields)) {
  402. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  403. }
  404. }
  405. }
  406. // This defines the path for the next step in a simulated multipart form
  407. // NOTE: The %node gets replaced with the nid in insert
  408. $form['next_step_path'] = array(
  409. '#type' => 'hidden',
  410. '#value' => 'node/%node/properties'
  411. );
  412. // If you don't want a multipart form set this to false
  413. // Will then do default redirect (to new node) on submit
  414. $form['simulate_multipart'] = array(
  415. '#type' => 'textfield',
  416. '#attributes'=>array('style'=>"display:none"),
  417. '#default_value' => TRUE
  418. );
  419. if (!isset($node->stock->uniquename)) {
  420. $form['progress'] = array(
  421. '#type' => 'item',
  422. '#value' => tripal_stock_add_chado_properties_progress('main')
  423. );
  424. }
  425. $form['names'] = array(
  426. '#type' => 'fieldset',
  427. '#title' => t('Stock Name')
  428. );
  429. $form['names']['title'] = array(
  430. '#type' => 'textfield',
  431. '#title' => t('Name'),
  432. '#default_value' => $node->stock->name,
  433. '#required' => TRUE
  434. );
  435. $form['names']['uniquename'] = array(
  436. '#type' => 'textfield',
  437. '#title' => t('Unique Name'),
  438. '#default_value' => $node->stock->uniquename,
  439. '#required' => TRUE
  440. );
  441. $form['names']['stock_id'] = array(
  442. '#type' => 'hidden',
  443. '#value' => $node->stock->stock_id
  444. );
  445. $form['details'] = array(
  446. '#type' => 'fieldset',
  447. '#title' => t('Stock Details')
  448. );
  449. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  450. $type_options[0] = 'Select a Type';
  451. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock->type_id->cvterm_id; }
  452. $form['details']['type_id'] = array(
  453. '#type' => 'select',
  454. '#title' => t('Type of Stock'),
  455. '#options' => $type_options,
  456. '#default_value' => $type_default,
  457. '#required' => TRUE,
  458. );
  459. $stock_oganism_options = tripal_organism_get_organism_options();
  460. $stock_oganism_options[0] = 'Select An Organism';
  461. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->stock->organism_id->organism_id; }
  462. $form['details']['organism_id'] = array(
  463. '#type' => 'select',
  464. '#title' => t('Source Organism for stock'),
  465. '#default_value' => $organism_default,
  466. '#options' => $stock_oganism_options,
  467. '#required' => TRUE
  468. );
  469. $form['details']['stock_description'] = array(
  470. '#type' => 'textarea',
  471. '#title' => t('Notes'),
  472. '#default_value' => $node->stock->description,
  473. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  474. );
  475. $form['database_reference'] = array(
  476. '#type' => 'fieldset',
  477. '#title' => t('Stock Database Reference')
  478. );
  479. $form['database_reference']['accession'] = array(
  480. '#type' => 'textfield',
  481. '#title' => t('Accession'),
  482. '#default_value' => $node->stock->dbxref_id->accession
  483. );
  484. $form['database_reference']['db_description'] = array(
  485. '#type' => 'textarea',
  486. '#title' => t('Description of Database Reference'),
  487. '#default_value' => $node->stock->dbxref_id->description,
  488. '#description' => t('Optionally enter a description about the database accession.')
  489. );
  490. $db_options = tripal_db_get_db_options();
  491. $db_options[0] = 'Select a Database';
  492. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  493. $form['database_reference']['database'] = array(
  494. '#type' => 'select',
  495. '#title' => t('Database'),
  496. '#options' => $db_options,
  497. '#default_value' => $db_default
  498. );
  499. return $form;
  500. }
  501. /**
  502. * Implements hook_validate(): Validate the input from the chado_stock node form
  503. *
  504. * @param $node
  505. * The current node including fields with the form element names and submitted values
  506. * @param $form
  507. * A description of the form to be rendered by drupal_get_form()
  508. *
  509. * @ingroup tripal_stock
  510. */
  511. function chado_stock_validate($node, &$form) {
  512. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  513. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  514. // Validate Uniquename only if add
  515. if (empty($node->stock_id)) {
  516. $previous_db = tripal_db_set_active('chado');
  517. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  518. tripal_db_set_active($previous_db);
  519. if(!empty($chado_row->stock_id)) {
  520. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  521. if (!empty($drupal_row->nid)) {
  522. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  523. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  524. } else {
  525. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  526. }
  527. }
  528. }
  529. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  530. if ( $node->type_id == 0) {
  531. form_set_error('type_id', 'Please select a type of stock.');
  532. } else {
  533. $previous_db = tripal_db_set_active('chado');
  534. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  535. tripal_db_set_active($previous_db);
  536. if ( $num_rows->count != 1) {
  537. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  538. }
  539. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  540. if ( $node->organism_id == 0) {
  541. form_set_error('organism_id', 'Please select a source organism for this stock');
  542. } else {
  543. $previous_db = tripal_db_set_active('chado');
  544. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  545. tripal_db_set_active($previous_db);
  546. if ( $num_rows->count != 1 ) {
  547. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  548. }
  549. // Check if Accession also database
  550. if ($node->accession != '') {
  551. if ($node->database == 0) {
  552. // there is an accession but no database selected
  553. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  554. }
  555. } else {
  556. if ($node->database > 0) {
  557. // there is a database selected but no accession
  558. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  559. }
  560. }
  561. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  562. if ( $node->database > 0) {
  563. $previous_db = tripal_db_set_active('chado');
  564. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  565. tripal_db_set_active($previous_db);
  566. if ($num_rows->count != 1) {
  567. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  568. }
  569. }
  570. /**
  571. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  572. *
  573. * @param $node
  574. * The current node including fields with the form element names and submitted values
  575. *
  576. * @return
  577. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  578. *
  579. * @ingroup tripal_stock
  580. */
  581. function chado_stock_insert($node) {
  582. //If the chado stock exists
  583. // then don't create but simply link to node
  584. if ($node->chado_stock_exists) {
  585. if (!empty($node->stock_id)) {
  586. db_query(
  587. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  588. ."VALUES (%d, %d, %d)",
  589. $node->nid,
  590. $node->vid,
  591. $node->stock_id
  592. );
  593. }
  594. return $node;
  595. }
  596. // create dbxref
  597. if ( !empty($node->accession) ) {
  598. if ( !empty($node->database) ) {
  599. $values = array(
  600. 'db_id' => $node->database,
  601. 'accession' => $node->accession,
  602. );
  603. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  604. $values['description'] = $node->db_description;
  605. $values['version'] = '1';
  606. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  607. if (!$dbxref_status) {
  608. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  609. watchdog('tripal_stock',
  610. 'Insert Stock: Unable to create dbxref where values:%values',
  611. array('%values' => print_r($values, TRUE)),
  612. WATCHDOG_WARNING
  613. );
  614. }
  615. } else { $dbxref_status = 1; }
  616. }
  617. }
  618. // create stock
  619. if($dbxref_status) {
  620. $values = array(
  621. 'dbxref_id' => array(
  622. 'db_id' => $node->database,
  623. 'accession' => $node->accession
  624. ),
  625. 'organism_id' => $node->organism_id,
  626. 'name' => $node->title,
  627. 'uniquename' => $node->uniquename,
  628. 'description' => $node->stock_description,
  629. 'type_id' => $node->type_id
  630. );
  631. $stock_status = tripal_core_chado_insert('stock', $values);
  632. } else {
  633. $values = array(
  634. 'organism_id' => $node->organism_id,
  635. 'name' => $node->title,
  636. 'uniquename' => $node->uniquename,
  637. 'description' => $node->stock_description,
  638. 'type_id' => $node->type_id
  639. );
  640. $stock_status = tripal_core_chado_insert('stock', $values);
  641. }
  642. // create drupal chado_stock entry
  643. if ($stock_status) {
  644. $values = array(
  645. 'organism_id' => $node->organism_id,
  646. 'uniquename' => $node->uniquename,
  647. 'type_id' => $node->type_id
  648. );
  649. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  650. if (!empty($chado_stock[0]->stock_id)) {
  651. db_query(
  652. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  653. ."VALUES (%d, %d, %d)",
  654. $node->nid,
  655. $node->vid,
  656. $chado_stock[0]->stock_id
  657. );
  658. //Move on to next stage of Stock Creation based on next_stage_path field
  659. if ($node->simulate_multipart) {
  660. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  661. $_REQUEST['destination'] = $next_stage_path;
  662. }
  663. } else {
  664. drupal_set_message('Error during stock creation.', 'error');
  665. watchdog('tripal_stock',
  666. 'Insert Stock: Unable to find newly created stock where values:%values',
  667. array('%values' => print_r($values, TRUE)),
  668. WATCHDOG_ERROR
  669. );
  670. return FALSE;
  671. }
  672. } else {
  673. drupal_set_message('Error during stock creation.', 'error');
  674. watchdog('tripal_stock',
  675. 'Insert Stock: Unable to create stock where values:%values',
  676. array('%values' => print_r($values, TRUE)),
  677. WATCHDOG_WARNING
  678. );
  679. return FALSE;
  680. }
  681. }
  682. /**
  683. * Implements hook_update(): Handles Editing/Updating of main stock info
  684. *
  685. * NOTE: Currently just writes over all old data
  686. *
  687. * @param $node
  688. * The current node including fields with the form element names and submitted values
  689. *
  690. * @return
  691. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  692. *
  693. * @ingroup tripal_stock
  694. */
  695. function chado_stock_update($node) {
  696. if ($node->revision) {
  697. chado_stock_insert($node);
  698. } else {
  699. //update dbxref
  700. if ($node->database) {
  701. if($node->accession) {
  702. $dbxref_mode = '';
  703. $stock = tripal_core_chado_select(
  704. 'stock',
  705. array('dbxref_id', 'type_id'),
  706. array('stock_id' => $node->stock_id)
  707. );
  708. if ($stock[0]->dbxref_id) {
  709. $values = array(
  710. 'db_id' => $node->database,
  711. 'accession' => $node->accession,
  712. 'description' => $node->db_description
  713. );
  714. $dbxref_status = tripal_core_chado_update(
  715. 'dbxref',
  716. array('dbxref_id' => $stock[0]->dbxref_id),
  717. $values
  718. );
  719. $dbxref_mode = 'Update';
  720. } else {
  721. if ($stock[0]->type_id) {
  722. //create the dbxref
  723. //used the type_id as a control to check we have a stock but not a dbxref
  724. $values = array(
  725. 'db_id' => $node->database,
  726. 'accession' => $node->accession,
  727. 'description' => $node->db_description,
  728. 'version' => '1',
  729. );
  730. $dbxref_status = tripal_core_chado_insert(
  731. 'dbxref',
  732. $values
  733. );
  734. $dbxref_mode = 'Create';
  735. } else {
  736. drupal_set_message('Unable to find stock to Update', 'error');
  737. watchdog(
  738. 'tripal_stock',
  739. 'Stock Update: Unable to find stock to update using values: %values',
  740. array('%values', print_r($values, TRUE)),
  741. WATCHDOG_ERROR
  742. );
  743. return FALSE;
  744. }
  745. }
  746. }
  747. }
  748. if (!$dbxref_status) {
  749. watchdog(
  750. 'tripal_stock',
  751. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  752. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  753. WATCHDOG_WARNING
  754. );
  755. }
  756. //can't change stock id which is all thats stored in drupal thus only update chado
  757. $update_values = array(
  758. 'organism_id' => $node->organism_id,
  759. 'name' => $node->title,
  760. 'uniquename' => $node->uniquename,
  761. 'description' => $node->stock_description,
  762. 'type_id' => $node->type_id,
  763. );
  764. if ($dbxref_status) {
  765. $update_values['dbxref_id'] = array(
  766. 'db_id' => $node->database,
  767. 'accession' => $node->accession
  768. );
  769. }
  770. $status = tripal_core_chado_update(
  771. 'stock',
  772. array('stock_id' => $node->stock_id),
  773. $update_values
  774. );
  775. if (!$status) {
  776. drupal_set_message('Unable to update stock', 'error');
  777. watchdog(
  778. 'tripal_stock',
  779. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  780. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  781. WATCHDOG_ERROR
  782. );
  783. }
  784. }
  785. }
  786. /**
  787. * Implements hook_delete(): Handles deleting of chado_stocks
  788. *
  789. * NOTE: Currently deletes data -no undo or record-keeping functionality
  790. *
  791. * @param $node
  792. * The current node including fields with the form element names and submitted values
  793. *
  794. * @return
  795. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  796. *
  797. * @ingroup tripal_stock
  798. */
  799. function chado_stock_delete($node) {
  800. // Set stock in chado: is_obsolete = TRUE
  801. $previous_db = tripal_db_set_active('chado');
  802. db_query(
  803. "DELETE FROM stock WHERE stock_id=%d",
  804. $node->stock->stock_id
  805. );
  806. tripal_db_set_active($previous_db);
  807. //remove drupal node and all revisions
  808. db_query(
  809. "DELETE FROM {chado_stock} WHERE nid=%d",
  810. $node->nid
  811. );
  812. }
  813. /**
  814. * Purpose: Implement Blocks relating to stock content
  815. *
  816. * @param $op
  817. * What kind of information to retrieve about the block or blocks.
  818. * Possible values include list, configure, save, view.
  819. * @param $delta
  820. * Which block to return (not applicable if $op is 'list').
  821. * @param $edit
  822. * If $op is 'save', the submitted form data from the configuration form.
  823. *
  824. * @return
  825. * One of the following depending on $op: An array of block descriptions (list), the configuration
  826. * form (configure), nothing (save), an array defining subject and content for the block indexed
  827. * by $delta (view)
  828. *
  829. * @ingroup tripal_stock
  830. */
  831. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  832. switch($op) {
  833. case 'list':
  834. $blocks['base']['info'] = t('Tripal Stock Details');
  835. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  836. $blocks['properties']['info'] = t('Tripal Stock Properties');
  837. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  838. $blocks['references']['info'] = t('Tripal Stock References');
  839. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  840. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  841. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  842. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  843. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  844. return $blocks;
  845. case 'view':
  846. if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  847. $nid = arg(1);
  848. $node = node_load($nid);
  849. $block = array();
  850. switch($delta){
  851. case 'base':
  852. $block['subject'] = t('Stock Details');
  853. $block['content'] = theme('tripal_stock_base',$node);
  854. break;
  855. case 'properties':
  856. $block['subject'] = t('Properties');
  857. $block['content'] = theme('tripal_stock_properties',$node);
  858. break;
  859. case 'references':
  860. $block['subject'] = t('References');
  861. $block['content'] = theme('tripal_stock_references',$node);
  862. break;
  863. case 'relationships':
  864. $block['subject'] = t('Relationships');
  865. $block['content'] = theme('tripal_stock_relationships',$node);
  866. break;
  867. case 'synonyms':
  868. $block['subject'] = t('Synonyms');
  869. $block['content'] = theme('tripal_stock_synonyms',$node);
  870. break;
  871. }
  872. return $block;
  873. }
  874. }
  875. }