tripal_stock.module 30 KB

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