tripal_stock.module 34 KB

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