tripal_stock.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. //Displaying stocks----------------------------
  56. $items['stocks'] = array(
  57. 'menu_name' => ('primary-links'),
  58. 'title' => 'Stocks',
  59. 'page callback' => 'tripal_stock_show_stocks',
  60. 'access arguments' => array('access chado_stock content'),
  61. 'type' => MENU_NORMAL_ITEM,
  62. );
  63. // Adding Secondary Properties-----------------
  64. $items['node/%cs_node/properties'] = array(
  65. 'title' => 'Add Properties & Synonyms',
  66. 'description' => 'Settings for Chado Stocks',
  67. 'page callback' => 'tripal_stock_add_ALL_property_page',
  68. 'page arguments' => array(1),
  69. 'access arguments' => array('create chado_stock content'),
  70. 'type' => MENU_CALLBACK
  71. );
  72. $items['node/%cs_node/db_references'] = array(
  73. 'title' => 'Add Database References',
  74. 'description' => 'Settings for Chado Stocks',
  75. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  76. 'page arguments' => array(1),
  77. 'access arguments' => array('create chado_stock content'),
  78. 'type' => MENU_CALLBACK
  79. );
  80. $items['node/%cs_node/relationships'] = array(
  81. 'title' => 'Add Relationships',
  82. 'description' => 'Settings for Chado Stocks',
  83. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  84. 'page arguments' => array(1),
  85. 'access arguments' => array('create chado_stock content'),
  86. 'type' => MENU_CALLBACK
  87. );
  88. //Edit/Deleting Secondary Properties-------------
  89. $items['node/%cs_node/edit_properties'] = array(
  90. 'title' => 'Edit Properties',
  91. 'description' => 'Settings for Chado Stocks',
  92. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  93. 'page arguments' => array(1),
  94. 'access arguments' => array('edit chado_stock content'),
  95. 'type' => MENU_LOCAL_TASK,
  96. 'weight' => 8,
  97. );
  98. $items['node/%cs_node/edit_relationships'] = array(
  99. 'title' => 'Edit Relationships',
  100. 'description' => 'Settings for Chado Stocks',
  101. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  102. 'page arguments' => array(1),
  103. 'access arguments' => array('edit chado_stock content'),
  104. 'type' => MENU_LOCAL_TASK,
  105. 'weight' => 9,
  106. );
  107. $items['node/%cs_node/edit_db_references'] = array(
  108. 'title' => 'Edit DB References',
  109. 'description' => 'Settings for Chado Stocks',
  110. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  111. 'page arguments' => array(1),
  112. 'access arguments' => array('edit chado_stock content'),
  113. 'type' => MENU_LOCAL_TASK,
  114. 'weight' => 10,
  115. );
  116. return $items;
  117. }
  118. /**
  119. * Implements Menu wildcard_load hook
  120. *
  121. * Purpose: Allows the node ID of a chado stock to be dynamically
  122. * pulled from the path. The node is loaded from this node ID
  123. * and supplied to the page as an arguement. This is an example
  124. * of dynamic argument replacement using wildcards in the path.
  125. *
  126. * @param $nid
  127. * The node ID passed in from the path
  128. *
  129. * @return
  130. * The node object with the passed in nid
  131. *
  132. * @ingroup tripal_stock
  133. */
  134. function cs_node_load($nid) {
  135. if (is_numeric($nid)) {
  136. $node = node_load($nid);
  137. if ($node->type == 'chado_stock') {
  138. return $node;
  139. }
  140. }
  141. return FALSE;
  142. }
  143. /**
  144. * Implementation of hook_perm().
  145. *
  146. * Purpose: Set the permission types that the chado stock module uses
  147. *
  148. * @return
  149. * Listing of the possible permission catagories
  150. *
  151. * @ingroup tripal_stock
  152. */
  153. function tripal_stock_perm() {
  154. return array(
  155. 'access chado_stock content',
  156. 'create chado_stock content',
  157. 'edit chado_stock content',
  158. 'delete chado_stock content'
  159. );
  160. }
  161. /**
  162. * Implements hook_access(): Maps permission catagories to actions
  163. *
  164. * @param $op
  165. * The operation current operation: one of create, update, delete
  166. * @param $node
  167. * The node object the current operation is being performed on
  168. * @param $account
  169. * A user object representing the user for whom the operation is to be performed
  170. *
  171. * @return
  172. * TRUE grants access; FALSE denies it
  173. *
  174. * @ingroup tripal_stock
  175. */
  176. function chado_stock_access($op, $node, $account) {
  177. if ($op == 'create') {
  178. if (!user_access('create chado_stock content', $account)) {
  179. return FALSE;
  180. }
  181. }
  182. if ($op == 'update') {
  183. if (!user_access('edit chado_stock content', $account)) {
  184. return FALSE;
  185. }
  186. }
  187. if ($op == 'delete') {
  188. if (!user_access('delete chado_stock content', $account)) {
  189. return FALSE;
  190. }
  191. }
  192. if ($op == 'view') {
  193. if (!user_access('access chado_stock content', $account)) {
  194. return FALSE;
  195. }
  196. }
  197. return NULL;
  198. }
  199. /**
  200. * Implements hook_views_api()
  201. *
  202. * Purpose: Essentially this hook tells drupal that there is views support for
  203. * for this module which then includes tripal_stock.views.inc where all the
  204. * views integration code is
  205. *
  206. * @return
  207. * An array with fields important for views integration
  208. *
  209. * @ingroup tripal_stock
  210. */
  211. function tripal_stock_views_api() {
  212. return array(
  213. 'api' => 2.0,
  214. );
  215. }
  216. /**
  217. * Implements hook_theme(): Register themeing functions for this module
  218. *
  219. * @return
  220. * An array of themeing functions to register
  221. *
  222. * @ingroup tripal_stock
  223. */
  224. function tripal_stock_theme() {
  225. return array(
  226. 'tripal_stock_stock_table' => array(
  227. 'arguments' => array('stocks'),
  228. ),
  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. );
  264. }
  265. /**
  266. * Purpose: show stocks stored in drupals chado_stock table
  267. *
  268. * This function provides the default html representation of the stock table. This
  269. * representation can be overridden using Drupal Views2 to create more flexible tables
  270. * listing stocks.
  271. *
  272. * @return
  273. * HTML representation of a table of stocks
  274. *
  275. * @ingroup tripal_stock
  276. */
  277. function tripal_stock_show_stocks() {
  278. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  279. $no_stocks = db_result(db_query($sql));
  280. if ($no_stocks != 0) {
  281. $stocks = tripal_stock_get_all_stocks();
  282. if ($no_stocks != count($stocks)) {
  283. drupal_set_message(t("Synchronization needed."));
  284. }
  285. return theme('tripal_stock_stock_table', &$stocks);
  286. }
  287. else {
  288. return t("No Stocks exists. Please contact administrators to " .
  289. "synchronize stocks.");
  290. }
  291. }
  292. /**
  293. * A themeing funtion for the default tripal stock table
  294. *
  295. * @param $stocks
  296. * An array of all stock nodes
  297. *
  298. * @return
  299. * HTML representation of a table of stocks
  300. *
  301. * @ingroup tripal_stock
  302. */
  303. function theme_tripal_stock_stock_table(&$stocks) {
  304. // cycle through the stocks and build the stocks page
  305. $header = array('Name', 'Type', 'Organism', 'Description');
  306. $rows = array();
  307. foreach ($stocks as $node) {
  308. $output .= "<tr>";
  309. $rows[]= array(
  310. l($node->stock->name, "node/" . $node->nid),
  311. $node->stock->type_id->name,
  312. $node->stock->organism_id->common_name,
  313. $node->stock->description
  314. );
  315. }
  316. return theme('table', $header, $rows);
  317. }
  318. /**
  319. * Implements hook_node_info(): registers a stock node type
  320. *
  321. * @return
  322. * An array describing various details of the node
  323. *
  324. * @ingroup tripal_stock
  325. */
  326. function tripal_stock_node_info() {
  327. return array(
  328. 'chado_stock' => array(
  329. 'name' => t('Stock'),
  330. 'module' => 'chado_stock',
  331. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  332. 'has_title' => TRUE,
  333. 'has_body' => FALSE,
  334. ),
  335. );
  336. }
  337. /**
  338. * Implements hook_load(): Prepares the chado_stock node
  339. *
  340. * @param $node
  341. * The basic node containing all variables common to all nodes
  342. *
  343. * @return
  344. * A stock node containing all the variables from the basic node and all stock specific variables
  345. *
  346. * @ingroup tripal_stock
  347. */
  348. function chado_stock_load($node) {
  349. // Get stock_id from chado_stock linking table
  350. $map = db_fetch_object(db_query(
  351. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  352. $node->vid
  353. ));
  354. // Get stock content and add to node
  355. $stock = tripal_core_generate_chado_var('stock', array('stock_id' => $map->stock_id));
  356. //
  357. // move expandable fields downwards
  358. $node->expandable_fields = $stock->expandable_fields;
  359. unset($stock->expandable_fields);
  360. $node->expandable_tables = $stock->expandable_tables;
  361. unset($stock->expandable_tables);
  362. $node->expandable_nodes = $stock->expandable_nodes;
  363. unset($stock->expandable_nodes);
  364. $node->stock = $stock;
  365. return $node;
  366. }
  367. /**
  368. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  369. *
  370. * Parts to be added by this form
  371. * name,
  372. * uniquename,
  373. * description,
  374. * type => select from cvterm with key cvterm_id,
  375. * organism => select from available with key organism_id
  376. * main_db_reference => accession, version, description, db_name(select from dropdown)
  377. *
  378. * @param $node
  379. * An empty node object on insert OR the current stock node object on update
  380. * @param $form_state
  381. * The current state of the form
  382. *
  383. * @return
  384. * A description of the form to be rendered by drupal_get_form()
  385. *
  386. * @ingroup tripal_stock
  387. */
  388. function chado_stock_form($node, $form_state) {
  389. // Expand all fields needed
  390. $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');
  391. foreach ($fields_needed as $field_name) {
  392. // Check to see if it's excluded and expand it if so
  393. if ($node->expandable_fields) {
  394. if (in_array($field_name, $node->expandable_fields)) {
  395. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  396. }
  397. }
  398. }
  399. // This defines the path for the next step in a simulated multipart form
  400. // NOTE: The %node gets replaced with the nid in insert
  401. $form['next_step_path'] = array(
  402. '#type' => 'hidden',
  403. '#value' => 'node/%node/properties'
  404. );
  405. // If you don't want a multipart form set this to false
  406. // Will then do default redirect (to new node) on submit
  407. $form['simulate_multipart'] = array(
  408. '#type' => 'textfield',
  409. '#attributes' => array('style' => "display:none"),
  410. '#default_value' => TRUE
  411. );
  412. if (!isset($node->stock->uniquename)) {
  413. $form['progress'] = array(
  414. '#type' => 'item',
  415. '#value' => tripal_stock_add_chado_properties_progress('main')
  416. );
  417. }
  418. $form['names'] = array(
  419. '#type' => 'fieldset',
  420. '#title' => t('Stock Name')
  421. );
  422. $form['names']['title'] = array(
  423. '#type' => 'textfield',
  424. '#title' => t('Name'),
  425. '#default_value' => $node->stock->name,
  426. '#required' => TRUE
  427. );
  428. $form['names']['uniquename'] = array(
  429. '#type' => 'textfield',
  430. '#title' => t('Unique Name'),
  431. '#default_value' => $node->stock->uniquename,
  432. '#required' => TRUE
  433. );
  434. $form['names']['stock_id'] = array(
  435. '#type' => 'hidden',
  436. '#value' => $node->stock->stock_id
  437. );
  438. $form['details'] = array(
  439. '#type' => 'fieldset',
  440. '#title' => t('Stock Details')
  441. );
  442. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  443. $type_options[0] = 'Select a Type';
  444. if ($node->nid == '') {
  445. $type_default = 0; }
  446. else { $type_default = $node->stock->type_id->cvterm_id; }
  447. $form['details']['type_id'] = array(
  448. '#type' => 'select',
  449. '#title' => t('Type of Stock'),
  450. '#options' => $type_options,
  451. '#default_value' => $type_default,
  452. '#required' => TRUE,
  453. );
  454. $stock_oganism_options = tripal_organism_get_organism_options();
  455. $stock_oganism_options[0] = 'Select An Organism';
  456. if ($node->nid == '') {
  457. $organism_default = 0; }
  458. else { $organism_default = $node->stock->organism_id->organism_id; }
  459. $form['details']['organism_id'] = array(
  460. '#type' => 'select',
  461. '#title' => t('Source Organism for stock'),
  462. '#default_value' => $organism_default,
  463. '#options' => $stock_oganism_options,
  464. '#required' => TRUE
  465. );
  466. $form['details']['stock_description'] = array(
  467. '#type' => 'textarea',
  468. '#title' => t('Notes'),
  469. '#default_value' => $node->stock->description,
  470. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  471. );
  472. $form['database_reference'] = array(
  473. '#type' => 'fieldset',
  474. '#title' => t('Stock Database Reference')
  475. );
  476. $form['database_reference']['accession'] = array(
  477. '#type' => 'textfield',
  478. '#title' => t('Accession'),
  479. '#default_value' => $node->stock->dbxref_id->accession
  480. );
  481. $form['database_reference']['db_description'] = array(
  482. '#type' => 'textarea',
  483. '#title' => t('Description of Database Reference'),
  484. '#default_value' => $node->stock->dbxref_id->description,
  485. '#description' => t('Optionally enter a description about the database accession.')
  486. );
  487. $db_options = tripal_db_get_db_options();
  488. $db_options[0] = 'Select a Database';
  489. if ($node->nid == '') {
  490. $db_default = 0; }
  491. else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  492. $form['database_reference']['database'] = array(
  493. '#type' => 'select',
  494. '#title' => t('Database'),
  495. '#options' => $db_options,
  496. '#default_value' => $db_default
  497. );
  498. return $form;
  499. }
  500. /**
  501. * Implements hook_validate(): Validate the input from the chado_stock node form
  502. *
  503. * @param $node
  504. * The current node including fields with the form element names and submitted values
  505. * @param $form
  506. * A description of the form to be rendered by drupal_get_form()
  507. *
  508. * @ingroup tripal_stock
  509. */
  510. function chado_stock_validate($node, &$form) {
  511. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  512. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  513. // Validate Uniquename only if add
  514. if (empty($node->stock_id)) {
  515. $previous_db = tripal_db_set_active('chado');
  516. $chado_row = db_fetch_object(db_query("SELECT * FROM {stock} WHERE uniquename='" . $node->uniquename . "'"));
  517. tripal_db_set_active($previous_db);
  518. if (!empty($chado_row->stock_id)) {
  519. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=" . $chado_row->stock_id));
  520. if (!empty($drupal_row->nid)) {
  521. $link = l('node/' . $drupal_row->nid, $node->uniquename);
  522. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  523. }
  524. else {
  525. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  526. }
  527. }
  528. }
  529. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  530. if ( $node->type_id == 0) {
  531. form_set_error('type_id', 'Please select a type of stock.');
  532. }
  533. else {
  534. $previous_db = tripal_db_set_active('chado');
  535. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  536. tripal_db_set_active($previous_db);
  537. if ( $num_rows->count != 1) {
  538. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  539. }
  540. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  541. if ( $node->organism_id == 0) {
  542. form_set_error('organism_id', 'Please select a source organism for this stock');
  543. }
  544. else {
  545. $previous_db = tripal_db_set_active('chado');
  546. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  547. tripal_db_set_active($previous_db);
  548. if ( $num_rows->count != 1 ) {
  549. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  550. }
  551. // Check if Accession also database
  552. if ($node->accession != '') {
  553. if ($node->database == 0) {
  554. // there is an accession but no database selected
  555. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  556. }
  557. }
  558. else {
  559. if ($node->database > 0) {
  560. // there is a database selected but no accession
  561. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  562. }
  563. }
  564. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  565. if ( $node->database > 0) {
  566. $previous_db = tripal_db_set_active('chado');
  567. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  568. tripal_db_set_active($previous_db);
  569. if ($num_rows->count != 1) {
  570. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  571. }
  572. }
  573. /**
  574. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  575. *
  576. * @param $node
  577. * The current node including fields with the form element names and submitted values
  578. *
  579. * @return
  580. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  581. *
  582. * @ingroup tripal_stock
  583. */
  584. function chado_stock_insert($node) {
  585. //If the chado stock exists
  586. // then don't create but simply link to node
  587. if ($node->chado_stock_exists) {
  588. if (!empty($node->stock_id)) {
  589. db_query(
  590. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  591. ."VALUES (%d, %d, %d)",
  592. $node->nid,
  593. $node->vid,
  594. $node->stock_id
  595. );
  596. }
  597. return $node;
  598. }
  599. // create dbxref
  600. if ( !empty($node->accession) ) {
  601. if ( !empty($node->database) ) {
  602. $values = array(
  603. 'db_id' => $node->database,
  604. 'accession' => $node->accession,
  605. );
  606. if (!tripal_core_chado_select('dbxref', array(dbxref_id), $values)) {
  607. $values['description'] = $node->db_description;
  608. $values['version'] = '1';
  609. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  610. if (!$dbxref_status) {
  611. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  612. watchdog('tripal_stock',
  613. 'Insert Stock: Unable to create dbxref where values:%values',
  614. array('%values' => print_r($values, TRUE)),
  615. WATCHDOG_WARNING
  616. );
  617. }
  618. }
  619. else { $dbxref_status = 1; }
  620. }
  621. }
  622. // create stock
  623. if ($dbxref_status) {
  624. $values = array(
  625. 'dbxref_id' => array(
  626. 'db_id' => $node->database,
  627. 'accession' => $node->accession
  628. ),
  629. 'organism_id' => $node->organism_id,
  630. 'name' => $node->title,
  631. 'uniquename' => $node->uniquename,
  632. 'description' => $node->stock_description,
  633. 'type_id' => $node->type_id
  634. );
  635. $stock_status = tripal_core_chado_insert('stock', $values);
  636. }
  637. else {
  638. $values = array(
  639. 'organism_id' => $node->organism_id,
  640. 'name' => $node->title,
  641. 'uniquename' => $node->uniquename,
  642. 'description' => $node->stock_description,
  643. 'type_id' => $node->type_id
  644. );
  645. $stock_status = tripal_core_chado_insert('stock', $values);
  646. }
  647. // create drupal chado_stock entry
  648. if ($stock_status) {
  649. $values = array(
  650. 'organism_id' => $node->organism_id,
  651. 'uniquename' => $node->uniquename,
  652. 'type_id' => $node->type_id
  653. );
  654. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  655. if (!empty($chado_stock[0]->stock_id)) {
  656. db_query(
  657. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  658. ."VALUES (%d, %d, %d)",
  659. $node->nid,
  660. $node->vid,
  661. $chado_stock[0]->stock_id
  662. );
  663. //Move on to next stage of Stock Creation based on next_stage_path field
  664. if ($node->simulate_multipart) {
  665. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  666. $_REQUEST['destination'] = $next_stage_path;
  667. }
  668. }
  669. else {
  670. drupal_set_message(t('Error during stock creation.'), 'error');
  671. watchdog('tripal_stock',
  672. 'Insert Stock: Unable to find newly created stock where values:%values',
  673. array('%values' => print_r($values, TRUE)),
  674. WATCHDOG_ERROR
  675. );
  676. return FALSE;
  677. }
  678. }
  679. else {
  680. drupal_set_message(t('Error during stock creation.'), 'error');
  681. watchdog('tripal_stock',
  682. 'Insert Stock: Unable to create stock where values:%values',
  683. array('%values' => print_r($values, TRUE)),
  684. WATCHDOG_WARNING
  685. );
  686. return FALSE;
  687. }
  688. }
  689. /**
  690. * Implements hook_update(): Handles Editing/Updating of main stock info
  691. *
  692. * NOTE: Currently just writes over all old data
  693. *
  694. * @param $node
  695. * The current node including fields with the form element names and submitted values
  696. *
  697. * @return
  698. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  699. *
  700. * @ingroup tripal_stock
  701. */
  702. function chado_stock_update($node) {
  703. if ($node->revision) {
  704. chado_stock_insert($node);
  705. }
  706. else {
  707. //update dbxref
  708. if ($node->database) {
  709. if ($node->accession) {
  710. $dbxref_mode = '';
  711. $stock = tripal_core_chado_select(
  712. 'stock',
  713. array('dbxref_id', 'type_id'),
  714. array('stock_id' => $node->stock_id)
  715. );
  716. if ($stock[0]->dbxref_id) {
  717. $values = array(
  718. 'db_id' => $node->database,
  719. 'accession' => $node->accession,
  720. 'description' => $node->db_description
  721. );
  722. $dbxref_status = tripal_core_chado_update(
  723. 'dbxref',
  724. array('dbxref_id' => $stock[0]->dbxref_id),
  725. $values
  726. );
  727. $dbxref_mode = 'Update';
  728. }
  729. else {
  730. if ($stock[0]->type_id) {
  731. //create the dbxref
  732. //used the type_id as a control to check we have a stock but not a dbxref
  733. $values = array(
  734. 'db_id' => $node->database,
  735. 'accession' => $node->accession,
  736. 'description' => $node->db_description,
  737. 'version' => '1',
  738. );
  739. $dbxref_status = tripal_core_chado_insert(
  740. 'dbxref',
  741. $values
  742. );
  743. $dbxref_mode = 'Create';
  744. }
  745. else {
  746. drupal_set_message(t('Unable to find stock to Update'), 'error');
  747. watchdog(
  748. 'tripal_stock',
  749. 'Stock Update: Unable to find stock to update using values: %values',
  750. array('%values', print_r($values, TRUE)),
  751. WATCHDOG_ERROR
  752. );
  753. return FALSE;
  754. }
  755. }
  756. }
  757. }
  758. if (!$dbxref_status) {
  759. watchdog(
  760. 'tripal_stock',
  761. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  762. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  763. WATCHDOG_WARNING
  764. );
  765. }
  766. //can't change stock id which is all thats stored in drupal thus only update chado
  767. $update_values = array(
  768. 'organism_id' => $node->organism_id,
  769. 'name' => $node->title,
  770. 'uniquename' => $node->uniquename,
  771. 'description' => $node->stock_description,
  772. 'type_id' => $node->type_id,
  773. );
  774. if ($dbxref_status) {
  775. $update_values['dbxref_id'] = array(
  776. 'db_id' => $node->database,
  777. 'accession' => $node->accession
  778. );
  779. }
  780. $status = tripal_core_chado_update(
  781. 'stock',
  782. array('stock_id' => $node->stock_id),
  783. $update_values
  784. );
  785. if (!$status) {
  786. drupal_set_message(t('Unable to update stock'), 'error');
  787. watchdog(
  788. 'tripal_stock',
  789. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  790. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  791. WATCHDOG_ERROR
  792. );
  793. }
  794. }
  795. }
  796. /**
  797. * Implements hook_delete(): Handles deleting of chado_stocks
  798. *
  799. * NOTE: Currently deletes data -no undo or record-keeping functionality
  800. *
  801. * @param $node
  802. * The current node including fields with the form element names and submitted values
  803. *
  804. * @return
  805. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  806. *
  807. * @ingroup tripal_stock
  808. */
  809. function chado_stock_delete($node) {
  810. // Set stock in chado: is_obsolete = TRUE
  811. $previous_db = tripal_db_set_active('chado');
  812. db_query(
  813. "DELETE FROM {stock} WHERE stock_id=%d",
  814. $node->stock->stock_id
  815. );
  816. tripal_db_set_active($previous_db);
  817. //remove drupal node and all revisions
  818. db_query(
  819. "DELETE FROM {chado_stock} WHERE nid=%d",
  820. $node->nid
  821. );
  822. }
  823. /**
  824. * Purpose: Implement Blocks relating to stock content
  825. *
  826. * @param $op
  827. * What kind of information to retrieve about the block or blocks.
  828. * Possible values include list, configure, save, view.
  829. * @param $delta
  830. * Which block to return (not applicable if $op is 'list').
  831. * @param $edit
  832. * If $op is 'save', the submitted form data from the configuration form.
  833. *
  834. * @return
  835. * One of the following depending on $op: An array of block descriptions (list), the configuration
  836. * form (configure), nothing (save), an array defining subject and content for the block indexed
  837. * by $delta (view)
  838. *
  839. * @ingroup tripal_stock
  840. */
  841. function tripal_stock_block($op = 'list', $delta = 0, $edit=array()) {
  842. switch ($op) {
  843. case 'list':
  844. $blocks['base']['info'] = t('Tripal Stock Details');
  845. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  846. $blocks['properties']['info'] = t('Tripal Stock Properties');
  847. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  848. $blocks['references']['info'] = t('Tripal Stock References');
  849. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  850. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  851. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  852. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  853. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  854. return $blocks;
  855. case 'view':
  856. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  857. $nid = arg(1);
  858. $node = node_load($nid);
  859. $block = array();
  860. switch ($delta) {
  861. case 'base':
  862. $block['subject'] = t('Stock Details');
  863. $block['content'] = theme('tripal_stock_base', $node);
  864. break;
  865. case 'properties':
  866. $block['subject'] = t('Properties');
  867. $block['content'] = theme('tripal_stock_properties', $node);
  868. break;
  869. case 'references':
  870. $block['subject'] = t('References');
  871. $block['content'] = theme('tripal_stock_references', $node);
  872. break;
  873. case 'relationships':
  874. $block['subject'] = t('Relationships');
  875. $block['content'] = theme('tripal_stock_relationships', $node);
  876. break;
  877. case 'synonyms':
  878. $block['subject'] = t('Synonyms');
  879. $block['content'] = theme('tripal_stock_synonyms', $node);
  880. break;
  881. }
  882. return $block;
  883. }
  884. }
  885. }