tripal_stock.module 35 KB

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