tripal_stock.module 34 KB

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