tripal_stock.module 35 KB

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