tripal_stock.module 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. <?php
  2. // $Id$
  3. require("tripal_stock-administration.inc");
  4. require("tripal_stock.api.inc");
  5. require("other_module_api_functions.inc");
  6. require("tripal_stock-secondary_tables.inc");
  7. require("tripal_stock-properties.inc");
  8. require("tripal_stock-relationships.inc");
  9. require("tripal_stock-db_references.inc");
  10. /*************************************************************************
  11. * Implementation of hook_menu()
  12. * Purpose: Add menu items for this module
  13. * Note: A Menu item for the Create chado_stock form is automatically
  14. * added to the 'Create Content' Navigation menu
  15. *
  16. * @return and array for menu descriptions
  17. */
  18. function tripal_stock_menu() {
  19. $items = array();
  20. //Administrative settings menu-----------------
  21. $items['admin/tripal/tripal_stock'] = array(
  22. 'title' => t('Stocks'),
  23. 'description' => t('Basic Description of Tripal Stock Module Functionality'),
  24. 'page callback' => 'tripal_stock_module_description_page',
  25. 'access arguments' => array('administer site configuration'),
  26. 'type' => MENU_NORMAL_ITEM
  27. );
  28. $items['admin/tripal/tripal_stock/configuration'] = array(
  29. 'title' => t('Configuration'),
  30. 'description' => t('Settings for Chado Stocks'),
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('tripal_stock_admin'),
  33. 'access arguments' => array('administer site configuration'),
  34. 'type' => MENU_NORMAL_ITEM
  35. );
  36. //Displaying stocks----------------------------
  37. $items['stocks'] = array(
  38. 'menu_name' => ('primary-links'),
  39. 'title' => t('Stocks'),
  40. 'page callback' => 'tripal_stock_show_stocks',
  41. 'access arguments' => array('access chado_stock content'),
  42. 'type' => MENU_NORMAL_ITEM,
  43. );
  44. // Adding Secondary Properties-----------------
  45. $items['node/%cs_node/properties'] = array(
  46. 'title' => t('Add Properties & Synonyms'),
  47. 'description' => t('Settings for Chado Stocks'),
  48. 'page callback' => 'tripal_stock_add_ALL_property_page',
  49. 'page arguments' => array(1),
  50. 'access arguments' => array('create chado_stock content'),
  51. 'type' => MENU_CALLBACK
  52. );
  53. $items['node/%cs_node/db_references'] = array(
  54. 'title' => t('Add Database References'),
  55. 'description' => t('Settings for Chado Stocks'),
  56. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  57. 'page arguments' => array(1),
  58. 'access arguments' => array('create chado_stock content'),
  59. 'type' => MENU_CALLBACK
  60. );
  61. $items['node/%cs_node/relationships'] = array(
  62. 'title' => t('Add Relationships'),
  63. 'description' => t('Settings for Chado Stocks'),
  64. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  65. 'page arguments' => array(1),
  66. 'access arguments' => array('create chado_stock content'),
  67. 'type' => MENU_CALLBACK
  68. );
  69. //Edit/Deleting Secondary Properties-------------
  70. $items['node/%cs_node/edit_properties'] = array(
  71. 'title' => t('Edit Properties'),
  72. 'description' => t('Settings for Chado Stocks'),
  73. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  74. 'page arguments' => array(1),
  75. 'access arguments' => array('edit chado_stock content'),
  76. 'type' => MENU_LOCAL_TASK,
  77. 'weight' => 8,
  78. );
  79. $items['node/%cs_node/edit_relationships'] = array(
  80. 'title' => t('Edit Relationships'),
  81. 'description' => t('Settings for Chado Stocks'),
  82. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  83. 'page arguments' => array(1),
  84. 'access arguments' => array('edit chado_stock content'),
  85. 'type' => MENU_LOCAL_TASK,
  86. 'weight' => 9,
  87. );
  88. $items['node/%cs_node/edit_db_references'] = array(
  89. 'title' => t('Edit DB References'),
  90. 'description' => t('Settings for Chado Stocks'),
  91. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  92. 'page arguments' => array(1),
  93. 'access arguments' => array('edit chado_stock content'),
  94. 'type' => MENU_LOCAL_TASK,
  95. 'weight' => 10,
  96. );
  97. return $items;
  98. }
  99. /*************************************************************************
  100. * Implements Menu wildcard_load hook
  101. * Purpose: Allows the node ID of a chado stock to be dynamically
  102. * pulled from the path. The node is loaded from this node ID
  103. * and supplied to the page as an arguement
  104. */
  105. function cs_node_load($nid) {
  106. if (is_numeric($nid)) {
  107. $node = node_load($nid);
  108. if ($node->type == 'chado_stock') {
  109. return $node;
  110. }
  111. }
  112. return FALSE;
  113. }
  114. /*************************************************************************
  115. * Implementation of hook_perm()
  116. * Purpose: Set the permission types that the chado stock module uses
  117. *
  118. * @return an array listing the possible permission catagories
  119. */
  120. function tripal_stock_perm() {
  121. return array(
  122. 'access chado_stock content',
  123. 'create chado_stock content',
  124. 'edit chado_stock content',
  125. 'delete chado_stock content'
  126. );
  127. }
  128. /*************************************************************************
  129. * Implements hook_access()
  130. * Purpose: Maps permission catagories to actions;
  131. * TRUE grants access; FALSE denies it
  132. */
  133. function chado_stock_access($op, $node, $account) {
  134. if ($op == 'create') {
  135. return user_access('create chado_stock content', $account);
  136. }
  137. if ($op == 'update') {
  138. if (user_access('edit chado_stock content', $account)) {
  139. return TRUE;
  140. }
  141. }
  142. if ($op == 'delete') {
  143. if (user_access('delete chado_stock content', $account)) {
  144. return TRUE;
  145. }
  146. }
  147. if ($op == 'view') {
  148. if (user_access('access chado_stock content', $account)) {
  149. return TRUE;
  150. }
  151. }
  152. return FALSE;
  153. }
  154. /*************************************************************************
  155. * Implements hook_views_api()
  156. * Purpose: Essentially this hook tells drupal that there is views support for
  157. * for this module which then includes tripal_stock.views.inc where all the
  158. * views integration code is
  159. */
  160. function tripal_stock_views_api() {
  161. return array(
  162. 'api' => 2.0,
  163. );
  164. }
  165. /*************************************************************************
  166. * Implements hook_theme()
  167. * Purpose: Register themeing functions for this module
  168. *
  169. * @return an array of themeing functions to register
  170. */
  171. function tripal_stock_theme() {
  172. return array(
  173. 'tripal_stock_stock_table' => array (
  174. 'arguments' => array('stocks'),
  175. ),
  176. // Property edit forms--------------------------
  177. 'tripal_stock_edit_ALL_properties_form' => array(
  178. 'arguments' => array('form'),
  179. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  180. ),
  181. 'tripal_stock_edit_ALL_db_references_form' => array(
  182. 'arguments' => array('form'),
  183. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  184. ),
  185. 'tripal_stock_edit_ALL_relationships_form' => array(
  186. 'arguments' => array('form'),
  187. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  188. ),
  189. // 'tripal_stock_base' => array (
  190. // 'arguments' => array('node'=> null),
  191. // 'template' => 'tripal_stock_base',
  192. // ),
  193. );
  194. }
  195. /*************************************************************************
  196. * Purpose: Implement Blocks relating to stock content
  197. */
  198. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  199. switch($op) {
  200. case 'list':
  201. $blocks['base']['info'] = t('Tripal Stock Details');
  202. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  203. return $blocks;
  204. case 'view':
  205. if(user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  206. $nid = arg(1);
  207. $node = node_load($nid);
  208. $block = array();
  209. switch($delta){
  210. case 'base':
  211. $block['subject'] = t('Stock Details');
  212. // $block['content'] = theme('tripal_stock_base',$node);
  213. break;
  214. }
  215. return $blocks;
  216. }
  217. }
  218. }
  219. /*************************************************************************
  220. * Purpose:
  221. */
  222. function tripal_stock_nodeapi (&$node, $op, $teaser, $page) {
  223. if ($node->type == 'chado_stock') {
  224. switch ($op) {
  225. case 'view':
  226. $node->content['tripal_stock_base'] = array(
  227. // '#value' => theme('tripal_stock_base', $node),
  228. '#weight' => 0
  229. );
  230. }
  231. }
  232. }
  233. /*************************************************************************
  234. * Purpose: show stocks stored in drupals chado_stock table
  235. */
  236. function tripal_stock_show_stocks () {
  237. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  238. $no_stocks = db_result(db_query($sql));
  239. if($no_stocks != 0) {
  240. $stocks = tripal_stock_get_all_stocks();
  241. if($no_stocks != count($stocks)) {
  242. drupal_set_message("Synchronization needed.");
  243. }
  244. return theme('tripal_stock_stock_table',&$stocks);
  245. } else {
  246. return t("No Stocks exists. Please contact administrators to ".
  247. "synchronize stocks.");
  248. }
  249. }
  250. function theme_tripal_stock_stock_table (&$stocks) {
  251. // cycle through the stocks and build the stocks page
  252. $output = "<div id=\"stocks\">";
  253. $output .= '<table>';
  254. $output .= "<tr>";
  255. $output .= "<th>Name</th>";
  256. $output .= "<th>Type</th>";
  257. $output .= "<th>Organism</th>";
  258. $output .= "<th>Description</th>";
  259. $output .= "</tr>";
  260. foreach($stocks as $stock){
  261. $output .= "<tr>";
  262. $output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
  263. $output .= "<td>".$stock->stock_type."</td>";
  264. $output .= "<td nowrap>".$stock->organism->common_name."</td>";
  265. $output .= "<td>".$stock->description."</td>";
  266. $output .= "</tr>";
  267. }
  268. $output .= "</table>";
  269. $output .= "</div>";
  270. return $output;
  271. }
  272. /*************************************************************************
  273. * @section Implementation of stock Node
  274. * including node_load, form, insert, update, delete(by nid and vid)
  275. *************************************************************************/
  276. /*************************************************************************
  277. * Implements hook_node_info()
  278. */
  279. function tripal_stock_node_info() {
  280. return array(
  281. 'chado_stock' => array(
  282. 'name' => t('Stock'),
  283. 'module' => 'chado_stock',
  284. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  285. 'has_title' => TRUE,
  286. 'has_body' => FALSE,
  287. ),
  288. );
  289. }
  290. /*************************************************************************
  291. * Implements hook_load()
  292. * Everytime a chado_stock node is viewed or manipulated this hook is called first to prepare the data
  293. */
  294. function chado_stock_load($node) {
  295. // Gets the stock_id from the drupal chado_stock table---------------------------------------
  296. $map = db_fetch_object(db_query(
  297. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  298. $node->vid
  299. ));
  300. $node->stock_id = $map->stock_id;
  301. //Get the main stock information from the chado stock table-----------------------------------
  302. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  303. $values = array('stock_id' => $node->stock_id);
  304. $results = tripal_core_chado_select('stock', $columns, $values);
  305. $node->stock_name = $results[0]->name;
  306. $node->uniquename = $results[0]->uniquename;
  307. $node->description = $results[0]->description;
  308. $node->stock_type_id = $results[0]->type_id;
  309. $node->organism->organism_id = $results[0]->organism_id;
  310. $node->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  311. if (preg_match('/t/', $results[0]->is_obsolete)) {
  312. $node->is_obsolete = TRUE;
  313. } else {
  314. $node->is_obsolete = FALSE;
  315. }
  316. // Get type for current stock------------------------------------------------------------------
  317. $columns = array('name');
  318. $values = array('cvterm_id' => $node->stock_type_id);
  319. $results = tripal_core_chado_select('cvterm', $columns, $values);
  320. $node->stock_type = $results[0]->name;
  321. // Get organism details from chado & add to node-----------------------------------------------
  322. // get organism if for current stock
  323. // pull all data from chado for the organism and assign it to the current node
  324. $node->organism = tripal_organism_get_organism_by_organism_id($node->organism->organism_id);
  325. // Add Synonyms for stock----------------------------------------------------------------------
  326. // These are a special case of property (stockprop table) where cvterm='synonym'
  327. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  328. $values = array(
  329. 'stock_id' => $node->stock_id,
  330. 'type_id' => array(
  331. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  332. 'name' => 'synonym'
  333. )
  334. );
  335. $results = tripal_core_chado_select('stockprop', $columns, $values);
  336. if (!empty($results)) {
  337. foreach ($results as $r) {
  338. $r->type = 'synonym';
  339. $node->synonyms[] = $r;
  340. }
  341. }
  342. // Add properties for stock (not including synonyms)-------------------------------------------
  343. // $node->properties is an array of objects where each object describes a single property and has a type and value
  344. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  345. $values = array(
  346. 'stock_id' => $node->stock_id,
  347. );
  348. $results = tripal_core_chado_select('stockprop', $columns, $values);
  349. foreach ($results as $r) {
  350. $columns = array('name');
  351. $values = array('cvterm_id' => $r->type_id);
  352. $type_results = tripal_core_chado_select('cvterm', $columns, $values);
  353. $r->type = $type_results[0]->name;
  354. $node->properties[] = $r;
  355. }
  356. // Add in main db reference-----------------------------------------------------------------------
  357. // this is the dbxref_id in the stock table
  358. if (!empty($node->main_db_reference->dbxref_id)) {
  359. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  360. $values = array('dbxref_id' => $node->main_db_reference->dbxref_id);
  361. $results = tripal_core_chado_select('dbxref', $columns, $values);
  362. $node->main_db_reference = $results[0];
  363. //get db info
  364. $columns = array('name', 'description', 'url', 'urlprefix');
  365. $values = array('db_id' => $node->main_db_reference->db_id);
  366. $results = tripal_core_chado_select('db', $columns, $values);
  367. $node->main_db_reference->db_name = $results[0]->name;
  368. $node->main_db_reference->db_description = $results[0]->description;
  369. $node->main_db_reference->db_url = $results[0]->url;
  370. $node->main_db_reference->db_urlprefix = $results[0]->urlprefix;
  371. }
  372. // Add in extra references to external databases--------------------------------------------------
  373. // this includes all the dbxref entries in stock_dbxref
  374. $columns = array('dbxref_id');
  375. $values = array('stock_id' => $node->stock_id);
  376. $results = tripal_core_chado_select('stock_dbxref',$columns,$values);
  377. $node->db_references = array();
  378. foreach ($results as $r) {
  379. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  380. $values = array('dbxref_id' => $r->dbxref_id);
  381. $sub_results = tripal_core_chado_select('dbxref', $columns, $values);
  382. $dbxref = $sub_results[0];
  383. //get db info
  384. $columns = array('name', 'description', 'url', 'urlprefix');
  385. $values = array('db_id' => $dbxref->db_id);
  386. $results = tripal_core_chado_select('db', $columns, $values);
  387. $dbxref->db_name = $results[0]->name;
  388. $dbxref->db_description = $results[0]->description;
  389. $dbxref->db_url = $results[0]->url;
  390. $dbxref->db_urlprefix = $results[0]->urlprefix;
  391. $node->db_references[] = $dbxref;
  392. }
  393. // Add relationships for stock--------------------------------------------------------------------
  394. // Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
  395. // and those where the current stock is the object (other details stored in $node->subject_relationships)
  396. // fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
  397. // where current is subject.............................
  398. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  399. $values = array('subject_id' => $node->stock_id);
  400. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  401. $node->object_relationships = array();
  402. foreach ($results as $r) {
  403. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  404. $values = array('stock_id' => $r->object_id);
  405. $results = tripal_core_chado_select('stock', $columns, $values);
  406. // Type
  407. $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
  408. $r->type = $type_results[0]->name;
  409. // Object
  410. $r->object->stock_id = $r->object_id;
  411. unset($r->object_id);
  412. $r->object->stock_name = $results[0]->name;
  413. $r->object->uniquename = $results[0]->uniquename;
  414. $r->object->description = $results[0]->description;
  415. $r->object->stock_type_id = $results[0]->type_id;
  416. $r->object->organism->organism_id = $results[0]->organism_id;
  417. $r->object->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  418. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  419. $object_node = db_fetch_object(db_query($sql, $r->object->stock_id));
  420. $r->object->nid = $object_node->nid;
  421. $node->object_relationships[] = $r;
  422. }
  423. // where current is object.............................
  424. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  425. $values = array('object_id' => $node->stock_id);
  426. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  427. $node->subject_relationships = array();
  428. foreach ($results as $r) {
  429. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  430. $values = array('stock_id' => $r->subject_id);
  431. $results = tripal_core_chado_select('stock', $columns, $values);
  432. // Type
  433. $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
  434. $r->type = $type_results[0]->name;
  435. // Subject
  436. $r->subject->stock_id = $r->subject_id;
  437. unset($r->subject_id);
  438. $r->subject->stock_name = $results[0]->name;
  439. $r->subject->uniquename = $results[0]->uniquename;
  440. $r->subject->description = $results[0]->description;
  441. $r->subject->stock_type_id = $results[0]->type_id;
  442. $r->subject->organism->organism_id = $results[0]->organism_id;
  443. $r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  444. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  445. $subject_node = db_fetch_object(db_query($sql, $r->subject->stock_id));
  446. $r->subject->nid = $subject_node->nid;
  447. $node->subject_relationships[] = $r;
  448. }
  449. return $node;
  450. }
  451. /*************************************************************************
  452. * Implements hook_form()
  453. * Creates the main Add/Edit/Delete Form for chado stocks
  454. *
  455. * Parts to be added by this form
  456. * name,
  457. * uniquename,
  458. * description,
  459. * type => select from cvterm with key cvterm_id,
  460. * organism => select from available with key organism_id
  461. * main_db_reference => accession, version, description, db_name(select from dropdown)
  462. */
  463. function chado_stock_form($node, $form_state) {
  464. $type = node_get_types('type', $node);
  465. // This defines the path for the next step in a simulated multipart form
  466. // NOTE: The %node gets replaced with the nid in insert
  467. $form['next_step_path'] = array(
  468. '#type' => 'hidden',
  469. '#value' => 'node/%node/properties'
  470. );
  471. // If you don't want a multipart form set this to false
  472. // Will then do default redirect (to new node) on submit
  473. $form['simulate_multipart'] = array(
  474. '#type' => 'textfield',
  475. '#attributes'=>array('style'=>"display:none"),
  476. '#default_value' => TRUE
  477. );
  478. if (!isset($node->uniquename)) {
  479. $form['progress'] = array(
  480. '#type' => 'item',
  481. '#value' => tripal_stock_add_chado_properties_progress('main')
  482. );
  483. }
  484. $form['names'] = array(
  485. '#type' => 'fieldset',
  486. '#title' => t('Stock Name')
  487. );
  488. $form['names']['title'] = array(
  489. '#type' => 'textfield',
  490. '#title' => t('Name'),
  491. '#default_value' => $node->title,
  492. '#required' => TRUE
  493. );
  494. $form['names']['uniquename'] = array(
  495. '#type' => 'textfield',
  496. '#title' => t('Unique Name'),
  497. '#default_value' => $node->uniquename,
  498. '#required' => TRUE
  499. );
  500. $form['names']['stock_id'] = array(
  501. '#type' => 'hidden',
  502. '#value' => $node->stock_id
  503. );
  504. $form['details'] = array(
  505. '#type' => 'fieldset',
  506. '#title' => t('Stock Details')
  507. );
  508. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  509. $type_options[0] = 'Select a Type';
  510. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
  511. $form['details']['type_id'] = array(
  512. '#type' => 'select',
  513. '#title' => t('Type of Stock'),
  514. '#options' => $type_options,
  515. '#default_value' => $type_default,
  516. '#required' => TRUE,
  517. );
  518. $stock_oganism_options = tripal_organism_get_organism_options();
  519. $stock_oganism_options[0] = 'Select An Organism';
  520. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
  521. $form['details']['organism_id'] = array(
  522. '#type' => 'select',
  523. '#title' => t('Source Organism for stock'),
  524. '#default_value' => $organism_default,
  525. '#options' => $stock_oganism_options,
  526. '#required' => TRUE
  527. );
  528. $form['details']['stock_description'] = array(
  529. '#type' => 'textarea',
  530. '#title' => t('Notes'),
  531. '#default_value' => $node->description,
  532. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  533. );
  534. $form['database_reference'] = array(
  535. '#type' => 'fieldset',
  536. '#title' => t('Stock Database Reference')
  537. );
  538. $form['database_reference']['accession'] = array(
  539. '#type' => 'textfield',
  540. '#title' => t('Accession'),
  541. '#default_value' => $node->main_db_reference->accession
  542. );
  543. $form['database_reference']['db_description'] = array(
  544. '#type' => 'textarea',
  545. '#title' => t('Description of Database Reference'),
  546. '#default_value' => $node->main_db_reference->db_description,
  547. '#description' => t('Optionally enter a description about the database accession.')
  548. );
  549. $db_options = tripal_db_get_db_options();
  550. $db_options[0] = 'Select a Database';
  551. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
  552. $form['database_reference']['database'] = array(
  553. '#type' => 'select',
  554. '#title' => t('Database'),
  555. '#options' => $db_options,
  556. '#default_value' => $db_default
  557. );
  558. return $form;
  559. }
  560. /*************************************************************************
  561. * Implements hook_validate()
  562. */
  563. function chado_stock_validate($node, &$form) {
  564. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  565. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  566. // Validate Uniquename only if add
  567. if (empty($node->stock_id)) {
  568. $previous_db = tripal_db_set_active('chado');
  569. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  570. tripal_db_set_active($previous_db);
  571. if(!empty($chado_row->stock_id)) {
  572. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  573. if (!empty($drupal_row->nid)) {
  574. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  575. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  576. } else {
  577. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  578. }
  579. }
  580. }
  581. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  582. if ( $node->type_id == 0) {
  583. form_set_error('type_id', 'Please select a type of stock.');
  584. } else {
  585. $previous_db = tripal_db_set_active('chado');
  586. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  587. tripal_db_set_active($previous_db);
  588. if ( $num_rows->count != 1) {
  589. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  590. }
  591. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  592. if ( $node->organism_id == 0) {
  593. form_set_error('organism_id', 'Please select a source organism for this stock');
  594. } else {
  595. $previous_db = tripal_db_set_active('chado');
  596. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  597. tripal_db_set_active($previous_db);
  598. if ( $num_rows->count != 1 ) {
  599. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  600. }
  601. // Check if Accession also database
  602. if ($node->accession != '') {
  603. if ($node->database == 0) {
  604. // there is an accession but no database selected
  605. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  606. }
  607. } else {
  608. if ($node->database > 0) {
  609. // there is a database selected but no accession
  610. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  611. }
  612. }
  613. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  614. if ( $node->database > 0) {
  615. $previous_db = tripal_db_set_active('chado');
  616. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  617. tripal_db_set_active($previous_db);
  618. if ($num_rows->count != 1) {
  619. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  620. }
  621. // Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
  622. if ( $node->accession != '') {
  623. $previous_db = tripal_db_set_active('chado');
  624. $num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
  625. tripal_db_set_active($previous_db);
  626. if ($num_rows->count > 0) {
  627. form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
  628. }
  629. }
  630. /*************************************************************************
  631. * Implements hook_insert()
  632. * Inserts data from chado_stock_form() into drupal and chado
  633. */
  634. function chado_stock_insert($node) {
  635. // create dbxref
  636. if ( !empty($node->accession) ) {
  637. if ( !empty($node->database) ) {
  638. $values = array(
  639. 'db_id' => $node->database,
  640. 'accession' => $node->accession,
  641. );
  642. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  643. $values['description'] = $node->db_description;
  644. $values['version'] = '1';
  645. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  646. if (!$dbxref_status) {
  647. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  648. watchdog('tripal_stock',
  649. 'Insert Stock: Unable to create dbxref where values:%values',
  650. array('%values' => print_r($values, TRUE)),
  651. WATCHDOG_WARNING
  652. );
  653. }
  654. } else { $dbxref_status = 1; }
  655. }
  656. }
  657. // create stock
  658. if($dbxref_status) {
  659. $values = array(
  660. 'dbxref_id' => array(
  661. 'db_id' => $node->database,
  662. 'accession' => $node->accession
  663. ),
  664. 'organism_id' => $node->organism_id,
  665. 'name' => $node->title,
  666. 'uniquename' => $node->uniquename,
  667. 'description' => $node->description,
  668. 'type_id' => $node->type_id
  669. );
  670. $stock_status = tripal_core_chado_insert('stock', $values);
  671. } else {
  672. $values = array(
  673. 'organism_id' => $node->organism_id,
  674. 'name' => $node->title,
  675. 'uniquename' => $node->uniquename,
  676. 'description' => $node->description,
  677. 'type_id' => $node->type_id
  678. );
  679. $stock_status = tripal_core_chado_insert('stock', $values);
  680. }
  681. // create drupal chado_stock entry
  682. if ($stock_status) {
  683. $values = array(
  684. 'organism_id' => $node->organism_id,
  685. 'uniquename' => $node->uniquename,
  686. 'type_id' => $node->type_id
  687. );
  688. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  689. if (!empty($chado_stock[0]->stock_id)) {
  690. db_query(
  691. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  692. ."VALUES (%d, %d, %d)",
  693. $node->nid,
  694. $node->vid,
  695. $chado_stock[0]->stock_id
  696. );
  697. //Move on to next stage of Stock Creation based on next_stage_path field
  698. if ($node->simulate_multipart) {
  699. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  700. $_REQUEST['destination'] = $next_stage_path;
  701. }
  702. } else {
  703. drupal_set_message('Error during stock creation.', 'error');
  704. watchdog('tripal_stock',
  705. 'Insert Stock: Unable to find newly created stock where values:%values',
  706. array('%values' => print_r($values, TRUE)),
  707. WATCHDOG_ERROR
  708. );
  709. return FALSE;
  710. }
  711. } else {
  712. drupal_set_message('Error during stock creation.', 'error');
  713. watchdog('tripal_stock',
  714. 'Insert Stock: Unable to create stock where values:%values',
  715. array('%values' => print_r($values, TRUE)),
  716. WATCHDOG_WARNING
  717. );
  718. return FALSE;
  719. }
  720. }
  721. /*************************************************************************
  722. * Implements hook_update()
  723. * Handles Editing/Updating of main stock info (that in the chado stock table)
  724. * Currently just writes over all old data
  725. */
  726. function chado_stock_update($node) {
  727. if ($node->revision) {
  728. chado_stock_insert($node);
  729. } else {
  730. //update dbxref
  731. if ($node->database) {
  732. if($node->accession) {
  733. $dbxref_mode = '';
  734. $stock = tripal_core_chado_select(
  735. 'stock',
  736. array('dbxref_id', 'type_id'),
  737. array('stock_id' => $node->stock_id)
  738. );
  739. if ($stock[0]->dbxref_id) {
  740. $values = array(
  741. 'db_id' => $node->database,
  742. 'accession' => $node->accession,
  743. 'description' => $node->db_description
  744. );
  745. $dbxref_status = tripal_core_chado_update(
  746. 'dbxref',
  747. array('dbxref_id' => $stock[0]->dbxref_id),
  748. $values
  749. );
  750. $dbxref_mode = 'Update';
  751. } else {
  752. if ($stock[0]->type_id) {
  753. //create the dbxref
  754. //used the type_id as a control to check we have a stock but not a dbxref
  755. $values = array(
  756. 'db_id' => $node->database,
  757. 'accession' => $node->accession,
  758. 'description' => $node->db_description,
  759. 'version' => '1',
  760. );
  761. $dbxref_status = tripal_core_chado_insert(
  762. 'dbxref',
  763. $values
  764. );
  765. $dbxref_mode = 'Create';
  766. } else {
  767. drupal_set_message('Unable to find stock to Update', 'error');
  768. watchdog(
  769. 'tripal_stock',
  770. 'Stock Update: Unable to find stock to update using values: %values',
  771. array('%values', print_r($values, TRUE)),
  772. WATCHDOG_ERROR
  773. );
  774. return FALSE;
  775. }
  776. }
  777. }
  778. }
  779. if (!$dbxref_status) {
  780. watchdog(
  781. 'tripal_stock',
  782. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  783. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  784. WATCHDOG_WARNING
  785. );
  786. }
  787. //can't change stock id which is all thats stored in drupal thus only update chado
  788. $update_values = array(
  789. 'organism_id' => $node->organism_id,
  790. 'name' => $node->title,
  791. 'uniquename' => $node->uniquename,
  792. 'description' => $node->description,
  793. 'type_id' => $node->type_id,
  794. );
  795. if ($dbxref_status) {
  796. $update_values['dbxref_id'] = array(
  797. 'db_id' => $node->database,
  798. 'accession' => $node->accession
  799. );
  800. }
  801. $status = tripal_core_chado_update(
  802. 'stock',
  803. array('stock_id' => $node->stock_id),
  804. $update_values
  805. );
  806. if (!$status) {
  807. drupal_set_message('Unable to update stock', 'error');
  808. watchdog(
  809. 'tripal_stock',
  810. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  811. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  812. WATCHDOG_ERROR
  813. );
  814. }
  815. }
  816. }
  817. /*************************************************************************
  818. * Implements hook_delete()
  819. * Handles deleting of chado_stocks
  820. */
  821. function chado_stock_delete($node) {
  822. //remove drupal node and all revisions
  823. db_query(
  824. "DELETE FROM {chado_stock} WHERE nid=%d",
  825. $node->nid
  826. );
  827. // Set stock in chado: is_obsolete = TRUE
  828. $previous_db = tripal_db_set_active('chado');
  829. db_query(
  830. "DELETE FROM stock WHERE stock_id=%d",
  831. $node->stock_id
  832. );
  833. tripal_db_set_active($previous_db);
  834. }