tripal_stock.module 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 $node){
  315. $output .= "<tr>";
  316. $output .= "<td>".l($node->stock->name, "node/".$node->nid)."</td>";
  317. $output .= "<td>".$node->stock->type_id->name."</td>";
  318. $output .= "<td nowrap>".$node->stock->organism_id->common_name."</td>";
  319. $output .= "<td>".$node->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. //
  365. // move expandable fields downwards
  366. $node->expandable_fields = $stock->expandable_fields;
  367. unset($stock->expandable_fields);
  368. $node->expandable_tables = $stock->expandable_tables;
  369. unset($stock->expandable_tables);
  370. $node->expandable_nodes = $stock->expandable_nodes;
  371. unset($stock->expandable_nodes);
  372. $node->stock = $stock;
  373. return $node;
  374. }
  375. /**
  376. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  377. *
  378. * Parts to be added by this form
  379. * name,
  380. * uniquename,
  381. * description,
  382. * type => select from cvterm with key cvterm_id,
  383. * organism => select from available with key organism_id
  384. * main_db_reference => accession, version, description, db_name(select from dropdown)
  385. *
  386. * @param $node
  387. * An empty node object on insert OR the current stock node object on update
  388. * @param $form_state
  389. * The current state of the form
  390. *
  391. * @return
  392. * A description of the form to be rendered by drupal_get_form()
  393. *
  394. * @ingroup tripal_stock
  395. */
  396. function chado_stock_form($node, $form_state) {
  397. // Expand all fields needed
  398. $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');
  399. foreach ($fields_needed as $field_name) {
  400. // Check to see if it's excluded and expand it if so
  401. if ($node->expandable_fields) {
  402. if (in_array($field_name, $node->expandable_fields)) {
  403. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  404. }
  405. }
  406. }
  407. // This defines the path for the next step in a simulated multipart form
  408. // NOTE: The %node gets replaced with the nid in insert
  409. $form['next_step_path'] = array(
  410. '#type' => 'hidden',
  411. '#value' => 'node/%node/properties'
  412. );
  413. // If you don't want a multipart form set this to false
  414. // Will then do default redirect (to new node) on submit
  415. $form['simulate_multipart'] = array(
  416. '#type' => 'textfield',
  417. '#attributes'=>array('style'=>"display:none"),
  418. '#default_value' => TRUE
  419. );
  420. if (!isset($node->stock->uniquename)) {
  421. $form['progress'] = array(
  422. '#type' => 'item',
  423. '#value' => tripal_stock_add_chado_properties_progress('main')
  424. );
  425. }
  426. $form['names'] = array(
  427. '#type' => 'fieldset',
  428. '#title' => t('Stock Name')
  429. );
  430. $form['names']['title'] = array(
  431. '#type' => 'textfield',
  432. '#title' => t('Name'),
  433. '#default_value' => $node->stock->name,
  434. '#required' => TRUE
  435. );
  436. $form['names']['uniquename'] = array(
  437. '#type' => 'textfield',
  438. '#title' => t('Unique Name'),
  439. '#default_value' => $node->stock->uniquename,
  440. '#required' => TRUE
  441. );
  442. $form['names']['stock_id'] = array(
  443. '#type' => 'hidden',
  444. '#value' => $node->stock->stock_id
  445. );
  446. $form['details'] = array(
  447. '#type' => 'fieldset',
  448. '#title' => t('Stock Details')
  449. );
  450. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  451. $type_options[0] = 'Select a Type';
  452. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock->type_id->cvterm_id; }
  453. $form['details']['type_id'] = array(
  454. '#type' => 'select',
  455. '#title' => t('Type of Stock'),
  456. '#options' => $type_options,
  457. '#default_value' => $type_default,
  458. '#required' => TRUE,
  459. );
  460. $stock_oganism_options = tripal_organism_get_organism_options();
  461. $stock_oganism_options[0] = 'Select An Organism';
  462. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->stock->organism_id->organism_id; }
  463. $form['details']['organism_id'] = array(
  464. '#type' => 'select',
  465. '#title' => t('Source Organism for stock'),
  466. '#default_value' => $organism_default,
  467. '#options' => $stock_oganism_options,
  468. '#required' => TRUE
  469. );
  470. $form['details']['stock_description'] = array(
  471. '#type' => 'textarea',
  472. '#title' => t('Notes'),
  473. '#default_value' => $node->stock->description,
  474. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  475. );
  476. $form['database_reference'] = array(
  477. '#type' => 'fieldset',
  478. '#title' => t('Stock Database Reference')
  479. );
  480. $form['database_reference']['accession'] = array(
  481. '#type' => 'textfield',
  482. '#title' => t('Accession'),
  483. '#default_value' => $node->stock->dbxref_id->accession
  484. );
  485. $form['database_reference']['db_description'] = array(
  486. '#type' => 'textarea',
  487. '#title' => t('Description of Database Reference'),
  488. '#default_value' => $node->stock->dbxref_id->description,
  489. '#description' => t('Optionally enter a description about the database accession.')
  490. );
  491. $db_options = tripal_db_get_db_options();
  492. $db_options[0] = 'Select a Database';
  493. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  494. $form['database_reference']['database'] = array(
  495. '#type' => 'select',
  496. '#title' => t('Database'),
  497. '#options' => $db_options,
  498. '#default_value' => $db_default
  499. );
  500. return $form;
  501. }
  502. /**
  503. * Implements hook_validate(): Validate the input from the chado_stock node form
  504. *
  505. * @param $node
  506. * The current node including fields with the form element names and submitted values
  507. * @param $form
  508. * A description of the form to be rendered by drupal_get_form()
  509. *
  510. * @ingroup tripal_stock
  511. */
  512. function chado_stock_validate($node, &$form) {
  513. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  514. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  515. // Validate Uniquename only if add
  516. if (empty($node->stock_id)) {
  517. $previous_db = tripal_db_set_active('chado');
  518. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  519. tripal_db_set_active($previous_db);
  520. if(!empty($chado_row->stock_id)) {
  521. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  522. if (!empty($drupal_row->nid)) {
  523. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  524. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  525. } else {
  526. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  527. }
  528. }
  529. }
  530. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  531. if ( $node->type_id == 0) {
  532. form_set_error('type_id', 'Please select a type of stock.');
  533. } else {
  534. $previous_db = tripal_db_set_active('chado');
  535. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  536. tripal_db_set_active($previous_db);
  537. if ( $num_rows->count != 1) {
  538. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  539. }
  540. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  541. if ( $node->organism_id == 0) {
  542. form_set_error('organism_id', 'Please select a source organism for this stock');
  543. } else {
  544. $previous_db = tripal_db_set_active('chado');
  545. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  546. tripal_db_set_active($previous_db);
  547. if ( $num_rows->count != 1 ) {
  548. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  549. }
  550. // Check if Accession also database
  551. if ($node->accession != '') {
  552. if ($node->database == 0) {
  553. // there is an accession but no database selected
  554. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  555. }
  556. } else {
  557. if ($node->database > 0) {
  558. // there is a database selected but no accession
  559. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  560. }
  561. }
  562. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  563. if ( $node->database > 0) {
  564. $previous_db = tripal_db_set_active('chado');
  565. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  566. tripal_db_set_active($previous_db);
  567. if ($num_rows->count != 1) {
  568. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  569. }
  570. }
  571. /**
  572. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  573. *
  574. * @param $node
  575. * The current node including fields with the form element names and submitted values
  576. *
  577. * @return
  578. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  579. *
  580. * @ingroup tripal_stock
  581. */
  582. function chado_stock_insert($node) {
  583. // create dbxref
  584. if ( !empty($node->accession) ) {
  585. if ( !empty($node->database) ) {
  586. $values = array(
  587. 'db_id' => $node->database,
  588. 'accession' => $node->accession,
  589. );
  590. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  591. $values['description'] = $node->db_description;
  592. $values['version'] = '1';
  593. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  594. if (!$dbxref_status) {
  595. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  596. watchdog('tripal_stock',
  597. 'Insert Stock: Unable to create dbxref where values:%values',
  598. array('%values' => print_r($values, TRUE)),
  599. WATCHDOG_WARNING
  600. );
  601. }
  602. } else { $dbxref_status = 1; }
  603. }
  604. }
  605. // create stock
  606. if($dbxref_status) {
  607. $values = array(
  608. 'dbxref_id' => array(
  609. 'db_id' => $node->database,
  610. 'accession' => $node->accession
  611. ),
  612. 'organism_id' => $node->organism_id,
  613. 'name' => $node->title,
  614. 'uniquename' => $node->uniquename,
  615. 'description' => $node->stock_description,
  616. 'type_id' => $node->type_id
  617. );
  618. $stock_status = tripal_core_chado_insert('stock', $values);
  619. } else {
  620. $values = array(
  621. 'organism_id' => $node->organism_id,
  622. 'name' => $node->title,
  623. 'uniquename' => $node->uniquename,
  624. 'description' => $node->stock_description,
  625. 'type_id' => $node->type_id
  626. );
  627. $stock_status = tripal_core_chado_insert('stock', $values);
  628. }
  629. // create drupal chado_stock entry
  630. if ($stock_status) {
  631. $values = array(
  632. 'organism_id' => $node->organism_id,
  633. 'uniquename' => $node->uniquename,
  634. 'type_id' => $node->type_id
  635. );
  636. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  637. if (!empty($chado_stock[0]->stock_id)) {
  638. db_query(
  639. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  640. ."VALUES (%d, %d, %d)",
  641. $node->nid,
  642. $node->vid,
  643. $chado_stock[0]->stock_id
  644. );
  645. //Move on to next stage of Stock Creation based on next_stage_path field
  646. if ($node->simulate_multipart) {
  647. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  648. $_REQUEST['destination'] = $next_stage_path;
  649. }
  650. } else {
  651. drupal_set_message('Error during stock creation.', 'error');
  652. watchdog('tripal_stock',
  653. 'Insert Stock: Unable to find newly created stock where values:%values',
  654. array('%values' => print_r($values, TRUE)),
  655. WATCHDOG_ERROR
  656. );
  657. return FALSE;
  658. }
  659. } else {
  660. drupal_set_message('Error during stock creation.', 'error');
  661. watchdog('tripal_stock',
  662. 'Insert Stock: Unable to create stock where values:%values',
  663. array('%values' => print_r($values, TRUE)),
  664. WATCHDOG_WARNING
  665. );
  666. return FALSE;
  667. }
  668. }
  669. /**
  670. * Implements hook_update(): Handles Editing/Updating of main stock info
  671. *
  672. * NOTE: Currently just writes over all old data
  673. *
  674. * @param $node
  675. * The current node including fields with the form element names and submitted values
  676. *
  677. * @return
  678. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  679. *
  680. * @ingroup tripal_stock
  681. */
  682. function chado_stock_update($node) {
  683. if ($node->revision) {
  684. chado_stock_insert($node);
  685. } else {
  686. //update dbxref
  687. if ($node->database) {
  688. if($node->accession) {
  689. $dbxref_mode = '';
  690. $stock = tripal_core_chado_select(
  691. 'stock',
  692. array('dbxref_id', 'type_id'),
  693. array('stock_id' => $node->stock_id)
  694. );
  695. if ($stock[0]->dbxref_id) {
  696. $values = array(
  697. 'db_id' => $node->database,
  698. 'accession' => $node->accession,
  699. 'description' => $node->db_description
  700. );
  701. $dbxref_status = tripal_core_chado_update(
  702. 'dbxref',
  703. array('dbxref_id' => $stock[0]->dbxref_id),
  704. $values
  705. );
  706. $dbxref_mode = 'Update';
  707. } else {
  708. if ($stock[0]->type_id) {
  709. //create the dbxref
  710. //used the type_id as a control to check we have a stock but not a dbxref
  711. $values = array(
  712. 'db_id' => $node->database,
  713. 'accession' => $node->accession,
  714. 'description' => $node->db_description,
  715. 'version' => '1',
  716. );
  717. $dbxref_status = tripal_core_chado_insert(
  718. 'dbxref',
  719. $values
  720. );
  721. $dbxref_mode = 'Create';
  722. } else {
  723. drupal_set_message('Unable to find stock to Update', 'error');
  724. watchdog(
  725. 'tripal_stock',
  726. 'Stock Update: Unable to find stock to update using values: %values',
  727. array('%values', print_r($values, TRUE)),
  728. WATCHDOG_ERROR
  729. );
  730. return FALSE;
  731. }
  732. }
  733. }
  734. }
  735. if (!$dbxref_status) {
  736. watchdog(
  737. 'tripal_stock',
  738. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  739. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  740. WATCHDOG_WARNING
  741. );
  742. }
  743. //can't change stock id which is all thats stored in drupal thus only update chado
  744. $update_values = array(
  745. 'organism_id' => $node->organism_id,
  746. 'name' => $node->title,
  747. 'uniquename' => $node->uniquename,
  748. 'description' => $node->stock_description,
  749. 'type_id' => $node->type_id,
  750. );
  751. if ($dbxref_status) {
  752. $update_values['dbxref_id'] = array(
  753. 'db_id' => $node->database,
  754. 'accession' => $node->accession
  755. );
  756. }
  757. $status = tripal_core_chado_update(
  758. 'stock',
  759. array('stock_id' => $node->stock_id),
  760. $update_values
  761. );
  762. if (!$status) {
  763. drupal_set_message('Unable to update stock', 'error');
  764. watchdog(
  765. 'tripal_stock',
  766. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  767. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  768. WATCHDOG_ERROR
  769. );
  770. }
  771. }
  772. }
  773. /**
  774. * Implements hook_delete(): Handles deleting of chado_stocks
  775. *
  776. * NOTE: Currently deletes data -no undo or record-keeping functionality
  777. *
  778. * @param $node
  779. * The current node including fields with the form element names and submitted values
  780. *
  781. * @return
  782. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  783. *
  784. * @ingroup tripal_stock
  785. */
  786. function chado_stock_delete($node) {
  787. //remove drupal node and all revisions
  788. db_query(
  789. "DELETE FROM {chado_stock} WHERE nid=%d",
  790. $node->nid
  791. );
  792. // Set stock in chado: is_obsolete = TRUE
  793. $previous_db = tripal_db_set_active('chado');
  794. db_query(
  795. "DELETE FROM stock WHERE stock_id=%d",
  796. $node->stock_id
  797. );
  798. tripal_db_set_active($previous_db);
  799. }
  800. /**
  801. * Purpose: Implement Blocks relating to stock content
  802. *
  803. * @param $op
  804. * What kind of information to retrieve about the block or blocks.
  805. * Possible values include list, configure, save, view.
  806. * @param $delta
  807. * Which block to return (not applicable if $op is 'list').
  808. * @param $edit
  809. * If $op is 'save', the submitted form data from the configuration form.
  810. *
  811. * @return
  812. * One of the following depending on $op: An array of block descriptions (list), the configuration
  813. * form (configure), nothing (save), an array defining subject and content for the block indexed
  814. * by $delta (view)
  815. *
  816. * @ingroup tripal_stock
  817. */
  818. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  819. switch($op) {
  820. case 'list':
  821. $blocks['base']['info'] = t('Tripal Stock Details');
  822. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  823. $blocks['properties']['info'] = t('Tripal Stock Properties');
  824. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  825. $blocks['references']['info'] = t('Tripal Stock References');
  826. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  827. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
  828. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  829. $blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
  830. $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
  831. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  832. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  833. return $blocks;
  834. case 'view':
  835. if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  836. $nid = arg(1);
  837. $node = node_load($nid);
  838. $block = array();
  839. switch($delta){
  840. case 'base':
  841. $block['subject'] = t('Stock Details');
  842. $block['content'] = theme('tripal_stock_base',$node);
  843. break;
  844. case 'properties':
  845. $block['subject'] = t('Properties');
  846. $block['content'] = theme('tripal_stock_properties',$node);
  847. break;
  848. case 'references':
  849. $block['subject'] = t('References');
  850. $block['content'] = theme('tripal_stock_references',$node);
  851. break;
  852. case 'relationships_as_object':
  853. $block['subject'] = t('Object Relationships');
  854. $block['content'] = theme('tripal_stock_relationships_as_object',$node);
  855. break;
  856. case 'relationships_as_subject':
  857. $block['subject'] = t('Subject Relationships');
  858. $block['content'] = theme('tripal_stock_relationships_as_subject',$node);
  859. break;
  860. case 'synonyms':
  861. $block['subject'] = t('Synonyms');
  862. $block['content'] = theme('tripal_stock_synonyms',$node);
  863. break;
  864. }
  865. return $block;
  866. }
  867. }
  868. }
  869. /**
  870. *
  871. *
  872. * @ingroup tripal_stock
  873. */
  874. function tripal_stock_preprocess(&$variables){
  875. // if the template file is the default node template file then we want
  876. // to add all of our variables.
  877. if($variables['template_files'][0] == 'node-chado_stock'){
  878. // stock properties
  879. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stockprop');
  880. // stock synonyms
  881. $variables['node']->stock->stock_synonyms = tripal_core_generate_chado_var(
  882. 'stockprop',
  883. array(
  884. 'stock_id'=>$variables['node']->stock->stock_id,
  885. 'type_id' => array(
  886. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  887. 'name' => 'synonym'
  888. ),
  889. )
  890. );
  891. // Stock database references
  892. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stock_dbxref');
  893. // Stock Object Relationships
  894. $variables['node']->stock->stock_object_relationships = tripal_core_generate_chado_var(
  895. 'stock_relationship',
  896. array('subject_id'=>$variables['node']->stock->stock_id)
  897. );
  898. // Stock Subject Relationships
  899. $variables['node']->stock->stock_subject_relationships = tripal_core_generate_chado_var(
  900. 'stock_relationship',
  901. array('object_id'=>$variables['node']->stock->stock_id)
  902. );
  903. }
  904. }
  905. /**
  906. *
  907. *
  908. * @ingroup tripal_stock
  909. */
  910. function tripal_stock_preprocess_tripal_stock_properties(&$variables){
  911. // stock properties
  912. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stockprop');
  913. }
  914. /**
  915. *
  916. *
  917. * @ingroup tripal_stock
  918. */
  919. function tripal_stock_preprocess_tripal_stock_synonyms(&$variables){
  920. // stock synonyms
  921. $variables['node']->stock->stock_synonyms = tripal_core_generate_chado_var(
  922. 'stockprop',
  923. array(
  924. 'stock_id'=>$variables['node']->stock->stock_id,
  925. 'type_id' => array(
  926. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  927. 'name' => 'synonym'
  928. ),
  929. )
  930. );
  931. }
  932. /**
  933. *
  934. *
  935. * @ingroup tripal_stock
  936. */
  937. function tripal_stock_preprocess_tripal_stock_references(&$variables){
  938. // Stock database references
  939. $variables['node'] = tripal_core_expand_chado_vars($variables['node'], 'table', 'stock_dbxref');
  940. }
  941. /**
  942. *
  943. *
  944. * @ingroup tripal_stock
  945. */
  946. function tripal_stock_preprocess_tripal_stock_relationships_as_object(&$variables){
  947. // Stock Object Relationships
  948. $variables['node']->stock->stock_object_relationships = tripal_core_generate_chado_var(
  949. 'stock_relationship',
  950. array('subject_id'=>$variables['node']->stock->stock_id)
  951. );
  952. }
  953. /**
  954. *
  955. *
  956. * @ingroup tripal_stock
  957. */
  958. function tripal_stock_preprocess_tripal_stock_relationships_as_subject(&$variables){
  959. // Stock Subject Relationships
  960. $variables['node']->stock->stock_subject_relationships = tripal_core_generate_chado_var(
  961. 'stock_relationship',
  962. array('object_id'=>$variables['node']->stock->stock_id)
  963. );
  964. }