tripal_stock.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. );
  253. }
  254. /**
  255. * Implements hook_node_info(): registers a stock node type
  256. *
  257. * @return
  258. * An array describing various details of the node
  259. *
  260. * @ingroup tripal_stock
  261. */
  262. function tripal_stock_node_info() {
  263. return array(
  264. 'chado_stock' => array(
  265. 'name' => t('Stock'),
  266. 'module' => 'chado_stock',
  267. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  268. 'has_title' => TRUE,
  269. 'has_body' => FALSE,
  270. ),
  271. );
  272. }
  273. /**
  274. * Implements hook_load(): Prepares the chado_stock node
  275. *
  276. * @param $node
  277. * The basic node containing all variables common to all nodes
  278. *
  279. * @return
  280. * A stock node containing all the variables from the basic node and all stock specific variables
  281. *
  282. * @ingroup tripal_stock
  283. */
  284. function chado_stock_load($node) {
  285. // Get stock_id from chado_stock linking table
  286. $map = db_fetch_object(db_query(
  287. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  288. $node->vid
  289. ));
  290. // Get stock content and add to node
  291. $stock = tripal_core_generate_chado_var('stock', array('stock_id' => $map->stock_id));
  292. //
  293. // move expandable fields downwards
  294. $node->expandable_fields = $stock->expandable_fields;
  295. unset($stock->expandable_fields);
  296. $node->expandable_tables = $stock->expandable_tables;
  297. unset($stock->expandable_tables);
  298. $node->expandable_nodes = $stock->expandable_nodes;
  299. unset($stock->expandable_nodes);
  300. $node->stock = $stock;
  301. return $node;
  302. }
  303. /**
  304. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  305. *
  306. * Parts to be added by this form
  307. * name,
  308. * uniquename,
  309. * description,
  310. * type => select from cvterm with key cvterm_id,
  311. * organism => select from available with key organism_id
  312. * main_db_reference => accession, version, description, db_name(select from dropdown)
  313. *
  314. * @param $node
  315. * An empty node object on insert OR the current stock node object on update
  316. * @param $form_state
  317. * The current state of the form
  318. *
  319. * @return
  320. * A description of the form to be rendered by drupal_get_form()
  321. *
  322. * @ingroup tripal_stock
  323. */
  324. function chado_stock_form($node, $form_state) {
  325. // Expand all fields needed
  326. $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');
  327. foreach ($fields_needed as $field_name) {
  328. // Check to see if it's excluded and expand it if so
  329. if ($node->expandable_fields) {
  330. if (in_array($field_name, $node->expandable_fields)) {
  331. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  332. }
  333. }
  334. }
  335. // This defines the path for the next step in a simulated multipart form
  336. // NOTE: The %node gets replaced with the nid in insert
  337. $form['next_step_path'] = array(
  338. '#type' => 'hidden',
  339. '#value' => 'node/%node/properties'
  340. );
  341. // If you don't want a multipart form set this to false
  342. // Will then do default redirect (to new node) on submit
  343. $form['simulate_multipart'] = array(
  344. '#type' => 'textfield',
  345. '#attributes' => array('style' => "display:none"),
  346. '#default_value' => TRUE
  347. );
  348. if (!isset($node->stock->uniquename)) {
  349. $form['progress'] = array(
  350. '#type' => 'item',
  351. '#value' => tripal_stock_add_chado_properties_progress('main')
  352. );
  353. }
  354. $form['names'] = array(
  355. '#type' => 'fieldset',
  356. '#title' => t('Stock Name')
  357. );
  358. $form['names']['title'] = array(
  359. '#type' => 'textfield',
  360. '#title' => t('Name'),
  361. '#default_value' => $node->stock->name,
  362. '#required' => TRUE
  363. );
  364. $form['names']['uniquename'] = array(
  365. '#type' => 'textfield',
  366. '#title' => t('Unique Name'),
  367. '#default_value' => $node->stock->uniquename,
  368. '#required' => TRUE
  369. );
  370. $form['names']['stock_id'] = array(
  371. '#type' => 'hidden',
  372. '#value' => $node->stock->stock_id
  373. );
  374. $form['details'] = array(
  375. '#type' => 'fieldset',
  376. '#title' => t('Stock Details')
  377. );
  378. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  379. $type_options[0] = 'Select a Type';
  380. if ($node->nid == '') {
  381. $type_default = 0; }
  382. else { $type_default = $node->stock->type_id->cvterm_id; }
  383. $form['details']['type_id'] = array(
  384. '#type' => 'select',
  385. '#title' => t('Type of Stock'),
  386. '#options' => $type_options,
  387. '#default_value' => $type_default,
  388. '#required' => TRUE,
  389. );
  390. $stock_oganism_options = tripal_organism_get_organism_options();
  391. $stock_oganism_options[0] = 'Select An Organism';
  392. if ($node->nid == '') {
  393. $organism_default = 0; }
  394. else { $organism_default = $node->stock->organism_id->organism_id; }
  395. $form['details']['organism_id'] = array(
  396. '#type' => 'select',
  397. '#title' => t('Source Organism for stock'),
  398. '#default_value' => $organism_default,
  399. '#options' => $stock_oganism_options,
  400. '#required' => TRUE
  401. );
  402. $form['details']['stock_description'] = array(
  403. '#type' => 'textarea',
  404. '#title' => t('Notes'),
  405. '#default_value' => $node->stock->description,
  406. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  407. );
  408. $form['database_reference'] = array(
  409. '#type' => 'fieldset',
  410. '#title' => t('Stock Database Reference')
  411. );
  412. $form['database_reference']['accession'] = array(
  413. '#type' => 'textfield',
  414. '#title' => t('Accession'),
  415. '#default_value' => $node->stock->dbxref_id->accession
  416. );
  417. $form['database_reference']['db_description'] = array(
  418. '#type' => 'textarea',
  419. '#title' => t('Description of Database Reference'),
  420. '#default_value' => $node->stock->dbxref_id->description,
  421. '#description' => t('Optionally enter a description about the database accession.')
  422. );
  423. $db_options = tripal_db_get_db_options();
  424. $db_options[0] = 'Select a Database';
  425. if ($node->nid == '') {
  426. $db_default = 0; }
  427. else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  428. $form['database_reference']['database'] = array(
  429. '#type' => 'select',
  430. '#title' => t('Database'),
  431. '#options' => $db_options,
  432. '#default_value' => $db_default
  433. );
  434. return $form;
  435. }
  436. /**
  437. * Implements hook_validate(): Validate the input from the chado_stock node form
  438. *
  439. * @param $node
  440. * The current node including fields with the form element names and submitted values
  441. * @param $form
  442. * A description of the form to be rendered by drupal_get_form()
  443. *
  444. * @ingroup tripal_stock
  445. */
  446. function chado_stock_validate($node, &$form) {
  447. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  448. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  449. // Validate Uniquename only if add
  450. if (empty($node->stock_id)) {
  451. $previous_db = tripal_db_set_active('chado');
  452. $chado_row = db_fetch_object(db_query("SELECT * FROM {stock} WHERE uniquename='" . $node->uniquename . "'"));
  453. tripal_db_set_active($previous_db);
  454. if (!empty($chado_row->stock_id)) {
  455. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=" . $chado_row->stock_id));
  456. if (!empty($drupal_row->nid)) {
  457. $link = l('node/' . $drupal_row->nid, $node->uniquename);
  458. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  459. }
  460. else {
  461. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  462. }
  463. }
  464. }
  465. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  466. if ( $node->type_id == 0) {
  467. form_set_error('type_id', 'Please select a type of stock.');
  468. }
  469. else {
  470. $previous_db = tripal_db_set_active('chado');
  471. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  472. tripal_db_set_active($previous_db);
  473. if ( $num_rows->count != 1) {
  474. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  475. }
  476. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  477. if ( $node->organism_id == 0) {
  478. form_set_error('organism_id', 'Please select a source organism for this stock');
  479. }
  480. else {
  481. $previous_db = tripal_db_set_active('chado');
  482. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  483. tripal_db_set_active($previous_db);
  484. if ( $num_rows->count != 1 ) {
  485. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  486. }
  487. // Check if Accession also database
  488. if ($node->accession != '') {
  489. if ($node->database == 0) {
  490. // there is an accession but no database selected
  491. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  492. }
  493. }
  494. else {
  495. if ($node->database > 0) {
  496. // there is a database selected but no accession
  497. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  498. }
  499. }
  500. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  501. if ( $node->database > 0) {
  502. $previous_db = tripal_db_set_active('chado');
  503. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  504. tripal_db_set_active($previous_db);
  505. if ($num_rows->count != 1) {
  506. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  507. }
  508. }
  509. /**
  510. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  511. *
  512. * @param $node
  513. * The current node including fields with the form element names and submitted values
  514. *
  515. * @return
  516. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  517. *
  518. * @ingroup tripal_stock
  519. */
  520. function chado_stock_insert($node) {
  521. //If the chado stock exists
  522. // then don't create but simply link to node
  523. if ($node->chado_stock_exists) {
  524. if (!empty($node->stock_id)) {
  525. db_query(
  526. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  527. ."VALUES (%d, %d, %d)",
  528. $node->nid,
  529. $node->vid,
  530. $node->stock_id
  531. );
  532. }
  533. return $node;
  534. }
  535. // create dbxref
  536. if ( !empty($node->accession) ) {
  537. if ( !empty($node->database) ) {
  538. $values = array(
  539. 'db_id' => $node->database,
  540. 'accession' => $node->accession,
  541. );
  542. if (!tripal_core_chado_select('dbxref', array(dbxref_id), $values)) {
  543. $values['description'] = $node->db_description;
  544. $values['version'] = '1';
  545. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  546. if (!$dbxref_status) {
  547. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  548. watchdog('tripal_stock',
  549. 'Insert Stock: Unable to create dbxref where values:%values',
  550. array('%values' => print_r($values, TRUE)),
  551. WATCHDOG_WARNING
  552. );
  553. }
  554. }
  555. else { $dbxref_status = 1; }
  556. }
  557. }
  558. // create stock
  559. if ($dbxref_status) {
  560. $values = array(
  561. 'dbxref_id' => array(
  562. 'db_id' => $node->database,
  563. 'accession' => $node->accession
  564. ),
  565. 'organism_id' => $node->organism_id,
  566. 'name' => $node->title,
  567. 'uniquename' => $node->uniquename,
  568. 'description' => $node->stock_description,
  569. 'type_id' => $node->type_id
  570. );
  571. $stock_status = tripal_core_chado_insert('stock', $values);
  572. }
  573. else {
  574. $values = array(
  575. 'organism_id' => $node->organism_id,
  576. 'name' => $node->title,
  577. 'uniquename' => $node->uniquename,
  578. 'description' => $node->stock_description,
  579. 'type_id' => $node->type_id
  580. );
  581. $stock_status = tripal_core_chado_insert('stock', $values);
  582. }
  583. // create drupal chado_stock entry
  584. if ($stock_status) {
  585. $values = array(
  586. 'organism_id' => $node->organism_id,
  587. 'uniquename' => $node->uniquename,
  588. 'type_id' => $node->type_id
  589. );
  590. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  591. if (!empty($chado_stock[0]->stock_id)) {
  592. db_query(
  593. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  594. ."VALUES (%d, %d, %d)",
  595. $node->nid,
  596. $node->vid,
  597. $chado_stock[0]->stock_id
  598. );
  599. //Move on to next stage of Stock Creation based on next_stage_path field
  600. if ($node->simulate_multipart) {
  601. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  602. $_REQUEST['destination'] = $next_stage_path;
  603. }
  604. }
  605. else {
  606. drupal_set_message(t('Error during stock creation.'), 'error');
  607. watchdog('tripal_stock',
  608. 'Insert Stock: Unable to find newly created stock where values:%values',
  609. array('%values' => print_r($values, TRUE)),
  610. WATCHDOG_ERROR
  611. );
  612. return FALSE;
  613. }
  614. }
  615. else {
  616. drupal_set_message(t('Error during stock creation.'), 'error');
  617. watchdog('tripal_stock',
  618. 'Insert Stock: Unable to create stock where values:%values',
  619. array('%values' => print_r($values, TRUE)),
  620. WATCHDOG_WARNING
  621. );
  622. return FALSE;
  623. }
  624. }
  625. /**
  626. * Implements hook_update(): Handles Editing/Updating of main stock info
  627. *
  628. * NOTE: Currently just writes over all old data
  629. *
  630. * @param $node
  631. * The current node including fields with the form element names and submitted values
  632. *
  633. * @return
  634. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  635. *
  636. * @ingroup tripal_stock
  637. */
  638. function chado_stock_update($node) {
  639. if ($node->revision) {
  640. chado_stock_insert($node);
  641. }
  642. else {
  643. //update dbxref
  644. if ($node->database) {
  645. if ($node->accession) {
  646. $dbxref_mode = '';
  647. $stock = tripal_core_chado_select(
  648. 'stock',
  649. array('dbxref_id', 'type_id'),
  650. array('stock_id' => $node->stock_id)
  651. );
  652. if ($stock[0]->dbxref_id) {
  653. $values = array(
  654. 'db_id' => $node->database,
  655. 'accession' => $node->accession,
  656. 'description' => $node->db_description
  657. );
  658. $dbxref_status = tripal_core_chado_update(
  659. 'dbxref',
  660. array('dbxref_id' => $stock[0]->dbxref_id),
  661. $values
  662. );
  663. $dbxref_mode = 'Update';
  664. }
  665. else {
  666. if ($stock[0]->type_id) {
  667. //create the dbxref
  668. //used the type_id as a control to check we have a stock but not a dbxref
  669. $values = array(
  670. 'db_id' => $node->database,
  671. 'accession' => $node->accession,
  672. 'description' => $node->db_description,
  673. 'version' => '1',
  674. );
  675. $dbxref_status = tripal_core_chado_insert(
  676. 'dbxref',
  677. $values
  678. );
  679. $dbxref_mode = 'Create';
  680. }
  681. else {
  682. drupal_set_message(t('Unable to find stock to Update'), 'error');
  683. watchdog(
  684. 'tripal_stock',
  685. 'Stock Update: Unable to find stock to update using values: %values',
  686. array('%values', print_r($values, TRUE)),
  687. WATCHDOG_ERROR
  688. );
  689. return FALSE;
  690. }
  691. }
  692. }
  693. }
  694. if (!$dbxref_status) {
  695. watchdog(
  696. 'tripal_stock',
  697. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  698. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  699. WATCHDOG_WARNING
  700. );
  701. }
  702. //can't change stock id which is all thats stored in drupal thus only update chado
  703. $update_values = array(
  704. 'organism_id' => $node->organism_id,
  705. 'name' => $node->title,
  706. 'uniquename' => $node->uniquename,
  707. 'description' => $node->stock_description,
  708. 'type_id' => $node->type_id,
  709. );
  710. if ($dbxref_status) {
  711. $update_values['dbxref_id'] = array(
  712. 'db_id' => $node->database,
  713. 'accession' => $node->accession
  714. );
  715. }
  716. $status = tripal_core_chado_update(
  717. 'stock',
  718. array('stock_id' => $node->stock_id),
  719. $update_values
  720. );
  721. if (!$status) {
  722. drupal_set_message(t('Unable to update stock'), 'error');
  723. watchdog(
  724. 'tripal_stock',
  725. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  726. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  727. WATCHDOG_ERROR
  728. );
  729. }
  730. }
  731. }
  732. /**
  733. * Implements hook_delete(): Handles deleting of chado_stocks
  734. *
  735. * NOTE: Currently deletes data -no undo or record-keeping functionality
  736. *
  737. * @param $node
  738. * The current node including fields with the form element names and submitted values
  739. *
  740. * @return
  741. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  742. *
  743. * @ingroup tripal_stock
  744. */
  745. function chado_stock_delete($node) {
  746. // Set stock in chado: is_obsolete = TRUE
  747. $previous_db = tripal_db_set_active('chado');
  748. db_query(
  749. "DELETE FROM {stock} WHERE stock_id=%d",
  750. $node->stock->stock_id
  751. );
  752. tripal_db_set_active($previous_db);
  753. //remove drupal node and all revisions
  754. db_query(
  755. "DELETE FROM {chado_stock} WHERE nid=%d",
  756. $node->nid
  757. );
  758. }
  759. /**
  760. * Purpose: Implement Blocks relating to stock content
  761. *
  762. * @param $op
  763. * What kind of information to retrieve about the block or blocks.
  764. * Possible values include list, configure, save, view.
  765. * @param $delta
  766. * Which block to return (not applicable if $op is 'list').
  767. * @param $edit
  768. * If $op is 'save', the submitted form data from the configuration form.
  769. *
  770. * @return
  771. * One of the following depending on $op: An array of block descriptions (list), the configuration
  772. * form (configure), nothing (save), an array defining subject and content for the block indexed
  773. * by $delta (view)
  774. *
  775. * @ingroup tripal_stock
  776. */
  777. function tripal_stock_block($op = 'list', $delta = 0, $edit=array()) {
  778. switch ($op) {
  779. case 'list':
  780. $blocks['base']['info'] = t('Tripal Stock Details');
  781. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  782. $blocks['properties']['info'] = t('Tripal Stock Properties');
  783. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  784. $blocks['references']['info'] = t('Tripal Stock References');
  785. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  786. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  787. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  788. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  789. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  790. return $blocks;
  791. case 'view':
  792. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  793. $nid = arg(1);
  794. $node = node_load($nid);
  795. $block = array();
  796. switch ($delta) {
  797. case 'base':
  798. $block['subject'] = t('Stock Details');
  799. $block['content'] = theme('tripal_stock_base', $node);
  800. break;
  801. case 'properties':
  802. $block['subject'] = t('Properties');
  803. $block['content'] = theme('tripal_stock_properties', $node);
  804. break;
  805. case 'references':
  806. $block['subject'] = t('References');
  807. $block['content'] = theme('tripal_stock_references', $node);
  808. break;
  809. case 'relationships':
  810. $block['subject'] = t('Relationships');
  811. $block['content'] = theme('tripal_stock_relationships', $node);
  812. break;
  813. case 'synonyms':
  814. $block['subject'] = t('Synonyms');
  815. $block['content'] = theme('tripal_stock_synonyms', $node);
  816. break;
  817. }
  818. return $block;
  819. }
  820. }
  821. }