tripal_stock.module 32 KB

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