tripal_stock.module 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. // Block Templates------------------------------
  190. 'tripal_stock_base' => array (
  191. 'arguments' => array('node'=> null),
  192. 'template' => 'tripal_stock_base',
  193. ),
  194. 'tripal_stock_properties' => array (
  195. 'arguments' => array('node'=> null),
  196. 'template' => 'tripal_stock_properties',
  197. ),
  198. 'tripal_stock_references' => array (
  199. 'arguments' => array('node'=> null),
  200. 'template' => 'tripal_stock_references',
  201. ),
  202. 'tripal_stock_relationships_as_object' => array (
  203. 'arguments' => array('node'=> null),
  204. 'template' => 'tripal_stock_relationships_as_object',
  205. ),
  206. 'tripal_stock_relationships_as_subject' => array (
  207. 'arguments' => array('node'=> null),
  208. 'template' => 'tripal_stock_relationships_as_subject',
  209. ),
  210. 'tripal_stock_synonyms' => array (
  211. 'arguments' => array('node'=> null),
  212. 'template' => 'tripal_stock_synonyms',
  213. ),
  214. );
  215. }
  216. /*************************************************************************
  217. * Purpose: show stocks stored in drupals chado_stock table
  218. */
  219. function tripal_stock_show_stocks () {
  220. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  221. $no_stocks = db_result(db_query($sql));
  222. if($no_stocks != 0) {
  223. $stocks = tripal_stock_get_all_stocks();
  224. if($no_stocks != count($stocks)) {
  225. drupal_set_message("Synchronization needed.");
  226. }
  227. return theme('tripal_stock_stock_table',&$stocks);
  228. } else {
  229. return t("No Stocks exists. Please contact administrators to ".
  230. "synchronize stocks.");
  231. }
  232. }
  233. function theme_tripal_stock_stock_table (&$stocks) {
  234. // cycle through the stocks and build the stocks page
  235. $output = "<div id=\"stocks\">";
  236. $output .= '<table>';
  237. $output .= "<tr>";
  238. $output .= "<th>Name</th>";
  239. $output .= "<th>Type</th>";
  240. $output .= "<th>Organism</th>";
  241. $output .= "<th>Description</th>";
  242. $output .= "</tr>";
  243. foreach($stocks as $stock){
  244. $output .= "<tr>";
  245. $output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
  246. $output .= "<td>".$stock->stock_type."</td>";
  247. $output .= "<td nowrap>".$stock->organism->common_name."</td>";
  248. $output .= "<td>".$stock->description."</td>";
  249. $output .= "</tr>";
  250. }
  251. $output .= "</table>";
  252. $output .= "</div>";
  253. return $output;
  254. }
  255. /*************************************************************************
  256. * @section Implementation of stock Node
  257. * including node_load, form, insert, update, delete(by nid and vid)
  258. *************************************************************************/
  259. /*************************************************************************
  260. * Implements hook_node_info()
  261. */
  262. function tripal_stock_node_info() {
  263. return array(
  264. 'chado_stock' => array(
  265. 'name' => t('Stock'),
  266. 'module' => 'chado_stock',
  267. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  268. 'has_title' => TRUE,
  269. 'has_body' => FALSE,
  270. ),
  271. );
  272. }
  273. /*************************************************************************
  274. * Implements hook_load()
  275. * Everytime a chado_stock node is viewed or manipulated this hook is called first to prepare the data
  276. */
  277. function chado_stock_load($node) {
  278. // Gets the stock_id from the drupal chado_stock table---------------------------------------
  279. $map = db_fetch_object(db_query(
  280. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  281. $node->vid
  282. ));
  283. $node->stock_id = $map->stock_id;
  284. //Get the main stock information from the chado stock table-----------------------------------
  285. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  286. $values = array('stock_id' => $node->stock_id);
  287. $results = tripal_core_chado_select('stock', $columns, $values);
  288. $node->stock_name = $results[0]->name;
  289. $node->uniquename = $results[0]->uniquename;
  290. $node->description = $results[0]->description;
  291. $node->stock_type_id = $results[0]->type_id;
  292. $node->organism->organism_id = $results[0]->organism_id;
  293. $node->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  294. if (preg_match('/t/', $results[0]->is_obsolete)) {
  295. $node->is_obsolete = TRUE;
  296. } else {
  297. $node->is_obsolete = FALSE;
  298. }
  299. // Get type for current stock------------------------------------------------------------------
  300. $columns = array('name');
  301. $values = array('cvterm_id' => $node->stock_type_id);
  302. $results = tripal_core_chado_select('cvterm', $columns, $values);
  303. $node->stock_type = $results[0]->name;
  304. // Get organism details from chado & add to node-----------------------------------------------
  305. // get organism if for current stock
  306. // pull all data from chado for the organism and assign it to the current node
  307. $node->organism = tripal_organism_get_organism_by_organism_id($node->organism->organism_id);
  308. // Add Synonyms for stock----------------------------------------------------------------------
  309. // These are a special case of property (stockprop table) where cvterm='synonym'
  310. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  311. $values = array(
  312. 'stock_id' => $node->stock_id,
  313. 'type_id' => array(
  314. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  315. 'name' => 'synonym'
  316. )
  317. );
  318. $results = tripal_core_chado_select('stockprop', $columns, $values);
  319. if (!empty($results)) {
  320. foreach ($results as $r) {
  321. $r->type = 'synonym';
  322. $node->synonyms[] = $r;
  323. }
  324. }
  325. // Add properties for stock (not including synonyms)-------------------------------------------
  326. // $node->properties is an array of objects where each object describes a single property and has a type and value
  327. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  328. $values = array(
  329. 'stock_id' => $node->stock_id,
  330. );
  331. $results = tripal_core_chado_select('stockprop', $columns, $values);
  332. foreach ($results as $r) {
  333. $columns = array('name');
  334. $values = array('cvterm_id' => $r->type_id);
  335. $type_results = tripal_core_chado_select('cvterm', $columns, $values);
  336. $r->type = $type_results[0]->name;
  337. $node->properties[] = $r;
  338. }
  339. // Add in main db reference-----------------------------------------------------------------------
  340. // this is the dbxref_id in the stock table
  341. if (!empty($node->main_db_reference->dbxref_id)) {
  342. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  343. $values = array('dbxref_id' => $node->main_db_reference->dbxref_id);
  344. $results = tripal_core_chado_select('dbxref', $columns, $values);
  345. $node->main_db_reference = $results[0];
  346. //get db info
  347. $columns = array('name', 'description', 'url', 'urlprefix');
  348. $values = array('db_id' => $node->main_db_reference->db_id);
  349. $results = tripal_core_chado_select('db', $columns, $values);
  350. $node->main_db_reference->db_name = $results[0]->name;
  351. $node->main_db_reference->db_description = $results[0]->description;
  352. $node->main_db_reference->db_url = $results[0]->url;
  353. $node->main_db_reference->db_urlprefix = $results[0]->urlprefix;
  354. }
  355. // Add in extra references to external databases--------------------------------------------------
  356. // this includes all the dbxref entries in stock_dbxref
  357. $columns = array('dbxref_id');
  358. $values = array('stock_id' => $node->stock_id);
  359. $results = tripal_core_chado_select('stock_dbxref',$columns,$values);
  360. $node->db_references = array();
  361. foreach ($results as $r) {
  362. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  363. $values = array('dbxref_id' => $r->dbxref_id);
  364. $sub_results = tripal_core_chado_select('dbxref', $columns, $values);
  365. $dbxref = $sub_results[0];
  366. //get db info
  367. $columns = array('name', 'description', 'url', 'urlprefix');
  368. $values = array('db_id' => $dbxref->db_id);
  369. $results = tripal_core_chado_select('db', $columns, $values);
  370. $dbxref->db_name = $results[0]->name;
  371. $dbxref->db_description = $results[0]->description;
  372. $dbxref->db_url = $results[0]->url;
  373. $dbxref->db_urlprefix = $results[0]->urlprefix;
  374. $node->db_references[] = $dbxref;
  375. }
  376. // Add relationships for stock--------------------------------------------------------------------
  377. // Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
  378. // and those where the current stock is the object (other details stored in $node->subject_relationships)
  379. // fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
  380. // where current is subject.............................
  381. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  382. $values = array('subject_id' => $node->stock_id);
  383. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  384. $node->object_relationships = array();
  385. foreach ($results as $r) {
  386. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  387. $values = array('stock_id' => $r->object_id);
  388. $results = tripal_core_chado_select('stock', $columns, $values);
  389. // Type
  390. $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
  391. $r->type = $type_results[0]->name;
  392. // Object
  393. $r->object->stock_id = $r->object_id;
  394. unset($r->object_id);
  395. $r->object->stock_name = $results[0]->name;
  396. $r->object->uniquename = $results[0]->uniquename;
  397. $r->object->description = $results[0]->description;
  398. $r->object->stock_type_id = $results[0]->type_id;
  399. $r->object->organism->organism_id = $results[0]->organism_id;
  400. $r->object->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  401. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  402. $object_node = db_fetch_object(db_query($sql, $r->object->stock_id));
  403. $r->object->nid = $object_node->nid;
  404. $node->object_relationships[] = $r;
  405. }
  406. // where current is object.............................
  407. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  408. $values = array('object_id' => $node->stock_id);
  409. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  410. $node->subject_relationships = array();
  411. foreach ($results as $r) {
  412. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  413. $values = array('stock_id' => $r->subject_id);
  414. $results = tripal_core_chado_select('stock', $columns, $values);
  415. // Type
  416. $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
  417. $r->type = $type_results[0]->name;
  418. // Subject
  419. $r->subject->stock_id = $r->subject_id;
  420. unset($r->subject_id);
  421. $r->subject->stock_name = $results[0]->name;
  422. $r->subject->uniquename = $results[0]->uniquename;
  423. $r->subject->description = $results[0]->description;
  424. $r->subject->stock_type_id = $results[0]->type_id;
  425. $r->subject->organism->organism_id = $results[0]->organism_id;
  426. $r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  427. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  428. $subject_node = db_fetch_object(db_query($sql, $r->subject->stock_id));
  429. $r->subject->nid = $subject_node->nid;
  430. $node->subject_relationships[] = $r;
  431. }
  432. return $node;
  433. }
  434. /*************************************************************************
  435. * Implements hook_form()
  436. * Creates the main Add/Edit/Delete Form for chado stocks
  437. *
  438. * Parts to be added by this form
  439. * name,
  440. * uniquename,
  441. * description,
  442. * type => select from cvterm with key cvterm_id,
  443. * organism => select from available with key organism_id
  444. * main_db_reference => accession, version, description, db_name(select from dropdown)
  445. */
  446. function chado_stock_form($node, $form_state) {
  447. $type = node_get_types('type', $node);
  448. // This defines the path for the next step in a simulated multipart form
  449. // NOTE: The %node gets replaced with the nid in insert
  450. $form['next_step_path'] = array(
  451. '#type' => 'hidden',
  452. '#value' => 'node/%node/properties'
  453. );
  454. // If you don't want a multipart form set this to false
  455. // Will then do default redirect (to new node) on submit
  456. $form['simulate_multipart'] = array(
  457. '#type' => 'textfield',
  458. '#attributes'=>array('style'=>"display:none"),
  459. '#default_value' => TRUE
  460. );
  461. if (!isset($node->uniquename)) {
  462. $form['progress'] = array(
  463. '#type' => 'item',
  464. '#value' => tripal_stock_add_chado_properties_progress('main')
  465. );
  466. }
  467. $form['names'] = array(
  468. '#type' => 'fieldset',
  469. '#title' => t('Stock Name')
  470. );
  471. $form['names']['title'] = array(
  472. '#type' => 'textfield',
  473. '#title' => t('Name'),
  474. '#default_value' => $node->title,
  475. '#required' => TRUE
  476. );
  477. $form['names']['uniquename'] = array(
  478. '#type' => 'textfield',
  479. '#title' => t('Unique Name'),
  480. '#default_value' => $node->uniquename,
  481. '#required' => TRUE
  482. );
  483. $form['names']['stock_id'] = array(
  484. '#type' => 'hidden',
  485. '#value' => $node->stock_id
  486. );
  487. $form['details'] = array(
  488. '#type' => 'fieldset',
  489. '#title' => t('Stock Details')
  490. );
  491. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  492. $type_options[0] = 'Select a Type';
  493. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
  494. $form['details']['type_id'] = array(
  495. '#type' => 'select',
  496. '#title' => t('Type of Stock'),
  497. '#options' => $type_options,
  498. '#default_value' => $type_default,
  499. '#required' => TRUE,
  500. );
  501. $stock_oganism_options = tripal_organism_get_organism_options();
  502. $stock_oganism_options[0] = 'Select An Organism';
  503. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
  504. $form['details']['organism_id'] = array(
  505. '#type' => 'select',
  506. '#title' => t('Source Organism for stock'),
  507. '#default_value' => $organism_default,
  508. '#options' => $stock_oganism_options,
  509. '#required' => TRUE
  510. );
  511. $form['details']['stock_description'] = array(
  512. '#type' => 'textarea',
  513. '#title' => t('Notes'),
  514. '#default_value' => $node->description,
  515. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  516. );
  517. $form['database_reference'] = array(
  518. '#type' => 'fieldset',
  519. '#title' => t('Stock Database Reference')
  520. );
  521. $form['database_reference']['accession'] = array(
  522. '#type' => 'textfield',
  523. '#title' => t('Accession'),
  524. '#default_value' => $node->main_db_reference->accession
  525. );
  526. $form['database_reference']['db_description'] = array(
  527. '#type' => 'textarea',
  528. '#title' => t('Description of Database Reference'),
  529. '#default_value' => $node->main_db_reference->db_description,
  530. '#description' => t('Optionally enter a description about the database accession.')
  531. );
  532. $db_options = tripal_db_get_db_options();
  533. $db_options[0] = 'Select a Database';
  534. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
  535. $form['database_reference']['database'] = array(
  536. '#type' => 'select',
  537. '#title' => t('Database'),
  538. '#options' => $db_options,
  539. '#default_value' => $db_default
  540. );
  541. return $form;
  542. }
  543. /*************************************************************************
  544. * Implements hook_validate()
  545. */
  546. function chado_stock_validate($node, &$form) {
  547. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  548. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  549. // Validate Uniquename only if add
  550. if (empty($node->stock_id)) {
  551. $previous_db = tripal_db_set_active('chado');
  552. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  553. tripal_db_set_active($previous_db);
  554. if(!empty($chado_row->stock_id)) {
  555. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  556. if (!empty($drupal_row->nid)) {
  557. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  558. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  559. } else {
  560. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  561. }
  562. }
  563. }
  564. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  565. if ( $node->type_id == 0) {
  566. form_set_error('type_id', 'Please select a type of stock.');
  567. } else {
  568. $previous_db = tripal_db_set_active('chado');
  569. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  570. tripal_db_set_active($previous_db);
  571. if ( $num_rows->count != 1) {
  572. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  573. }
  574. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  575. if ( $node->organism_id == 0) {
  576. form_set_error('organism_id', 'Please select a source organism for this stock');
  577. } else {
  578. $previous_db = tripal_db_set_active('chado');
  579. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  580. tripal_db_set_active($previous_db);
  581. if ( $num_rows->count != 1 ) {
  582. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  583. }
  584. // Check if Accession also database
  585. if ($node->accession != '') {
  586. if ($node->database == 0) {
  587. // there is an accession but no database selected
  588. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  589. }
  590. } else {
  591. if ($node->database > 0) {
  592. // there is a database selected but no accession
  593. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  594. }
  595. }
  596. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  597. if ( $node->database > 0) {
  598. $previous_db = tripal_db_set_active('chado');
  599. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  600. tripal_db_set_active($previous_db);
  601. if ($num_rows->count != 1) {
  602. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  603. }
  604. // Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
  605. if ( $node->accession != '') {
  606. $previous_db = tripal_db_set_active('chado');
  607. $num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
  608. tripal_db_set_active($previous_db);
  609. if ($num_rows->count > 0) {
  610. form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
  611. }
  612. }
  613. /*************************************************************************
  614. * Implements hook_insert()
  615. * Inserts data from chado_stock_form() into drupal and chado
  616. */
  617. function chado_stock_insert($node) {
  618. // create dbxref
  619. if ( !empty($node->accession) ) {
  620. if ( !empty($node->database) ) {
  621. $values = array(
  622. 'db_id' => $node->database,
  623. 'accession' => $node->accession,
  624. );
  625. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  626. $values['description'] = $node->db_description;
  627. $values['version'] = '1';
  628. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  629. if (!$dbxref_status) {
  630. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  631. watchdog('tripal_stock',
  632. 'Insert Stock: Unable to create dbxref where values:%values',
  633. array('%values' => print_r($values, TRUE)),
  634. WATCHDOG_WARNING
  635. );
  636. }
  637. } else { $dbxref_status = 1; }
  638. }
  639. }
  640. // create stock
  641. if($dbxref_status) {
  642. $values = array(
  643. 'dbxref_id' => array(
  644. 'db_id' => $node->database,
  645. 'accession' => $node->accession
  646. ),
  647. 'organism_id' => $node->organism_id,
  648. 'name' => $node->title,
  649. 'uniquename' => $node->uniquename,
  650. 'description' => $node->description,
  651. 'type_id' => $node->type_id
  652. );
  653. $stock_status = tripal_core_chado_insert('stock', $values);
  654. } else {
  655. $values = array(
  656. 'organism_id' => $node->organism_id,
  657. 'name' => $node->title,
  658. 'uniquename' => $node->uniquename,
  659. 'description' => $node->description,
  660. 'type_id' => $node->type_id
  661. );
  662. $stock_status = tripal_core_chado_insert('stock', $values);
  663. }
  664. // create drupal chado_stock entry
  665. if ($stock_status) {
  666. $values = array(
  667. 'organism_id' => $node->organism_id,
  668. 'uniquename' => $node->uniquename,
  669. 'type_id' => $node->type_id
  670. );
  671. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  672. if (!empty($chado_stock[0]->stock_id)) {
  673. db_query(
  674. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  675. ."VALUES (%d, %d, %d)",
  676. $node->nid,
  677. $node->vid,
  678. $chado_stock[0]->stock_id
  679. );
  680. //Move on to next stage of Stock Creation based on next_stage_path field
  681. if ($node->simulate_multipart) {
  682. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  683. $_REQUEST['destination'] = $next_stage_path;
  684. }
  685. } else {
  686. drupal_set_message('Error during stock creation.', 'error');
  687. watchdog('tripal_stock',
  688. 'Insert Stock: Unable to find newly created stock where values:%values',
  689. array('%values' => print_r($values, TRUE)),
  690. WATCHDOG_ERROR
  691. );
  692. return FALSE;
  693. }
  694. } else {
  695. drupal_set_message('Error during stock creation.', 'error');
  696. watchdog('tripal_stock',
  697. 'Insert Stock: Unable to create stock where values:%values',
  698. array('%values' => print_r($values, TRUE)),
  699. WATCHDOG_WARNING
  700. );
  701. return FALSE;
  702. }
  703. }
  704. /*************************************************************************
  705. * Implements hook_update()
  706. * Handles Editing/Updating of main stock info (that in the chado stock table)
  707. * Currently just writes over all old data
  708. */
  709. function chado_stock_update($node) {
  710. if ($node->revision) {
  711. chado_stock_insert($node);
  712. } else {
  713. //update dbxref
  714. if ($node->database) {
  715. if($node->accession) {
  716. $dbxref_mode = '';
  717. $stock = tripal_core_chado_select(
  718. 'stock',
  719. array('dbxref_id', 'type_id'),
  720. array('stock_id' => $node->stock_id)
  721. );
  722. if ($stock[0]->dbxref_id) {
  723. $values = array(
  724. 'db_id' => $node->database,
  725. 'accession' => $node->accession,
  726. 'description' => $node->db_description
  727. );
  728. $dbxref_status = tripal_core_chado_update(
  729. 'dbxref',
  730. array('dbxref_id' => $stock[0]->dbxref_id),
  731. $values
  732. );
  733. $dbxref_mode = 'Update';
  734. } else {
  735. if ($stock[0]->type_id) {
  736. //create the dbxref
  737. //used the type_id as a control to check we have a stock but not a dbxref
  738. $values = array(
  739. 'db_id' => $node->database,
  740. 'accession' => $node->accession,
  741. 'description' => $node->db_description,
  742. 'version' => '1',
  743. );
  744. $dbxref_status = tripal_core_chado_insert(
  745. 'dbxref',
  746. $values
  747. );
  748. $dbxref_mode = 'Create';
  749. } else {
  750. drupal_set_message('Unable to find stock to Update', 'error');
  751. watchdog(
  752. 'tripal_stock',
  753. 'Stock Update: Unable to find stock to update using values: %values',
  754. array('%values', print_r($values, TRUE)),
  755. WATCHDOG_ERROR
  756. );
  757. return FALSE;
  758. }
  759. }
  760. }
  761. }
  762. if (!$dbxref_status) {
  763. watchdog(
  764. 'tripal_stock',
  765. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  766. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  767. WATCHDOG_WARNING
  768. );
  769. }
  770. //can't change stock id which is all thats stored in drupal thus only update chado
  771. $update_values = array(
  772. 'organism_id' => $node->organism_id,
  773. 'name' => $node->title,
  774. 'uniquename' => $node->uniquename,
  775. 'description' => $node->description,
  776. 'type_id' => $node->type_id,
  777. );
  778. if ($dbxref_status) {
  779. $update_values['dbxref_id'] = array(
  780. 'db_id' => $node->database,
  781. 'accession' => $node->accession
  782. );
  783. }
  784. $status = tripal_core_chado_update(
  785. 'stock',
  786. array('stock_id' => $node->stock_id),
  787. $update_values
  788. );
  789. if (!$status) {
  790. drupal_set_message('Unable to update stock', 'error');
  791. watchdog(
  792. 'tripal_stock',
  793. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  794. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  795. WATCHDOG_ERROR
  796. );
  797. }
  798. }
  799. }
  800. /*************************************************************************
  801. * Implements hook_delete()
  802. * Handles deleting of chado_stocks
  803. */
  804. function chado_stock_delete($node) {
  805. //remove drupal node and all revisions
  806. db_query(
  807. "DELETE FROM {chado_stock} WHERE nid=%d",
  808. $node->nid
  809. );
  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_id
  815. );
  816. tripal_db_set_active($previous_db);
  817. }
  818. /**************************************************************************
  819. * @section Tripal Stock Blocks and Template Preprocessor Functions
  820. **************************************************************************/
  821. /*************************************************************************
  822. * Purpose: Implement Blocks relating to stock content
  823. */
  824. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  825. switch($op) {
  826. case 'list':
  827. $blocks['base']['info'] = t('Tripal Stock Details');
  828. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  829. $blocks['properties']['info'] = t('Tripal Stock Properties');
  830. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  831. $blocks['references']['info'] = t('Tripal Stock References');
  832. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  833. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
  834. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  835. $blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
  836. $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
  837. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  838. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  839. return $blocks;
  840. case 'view':
  841. if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  842. $nid = arg(1);
  843. $node = node_load($nid);
  844. $block = array();
  845. switch($delta){
  846. case 'base':
  847. $block['subject'] = t('Stock Details');
  848. $block['content'] = theme('tripal_stock_base',$node);
  849. break;
  850. case 'properties':
  851. $block['subject'] = t('Properties');
  852. $block['content'] = theme('tripal_stock_properties',$node);
  853. break;
  854. case 'references':
  855. $block['subject'] = t('References');
  856. $block['content'] = theme('tripal_stock_references',$node);
  857. break;
  858. case 'relationships_as_object':
  859. $block['subject'] = t('Object Relationships');
  860. $block['content'] = theme('tripal_stock_relationships_as_object',$node);
  861. break;
  862. case 'relationships_as_subject':
  863. $block['subject'] = t('Subject Relationships');
  864. $block['content'] = theme('tripal_stock_relationships_as_subject',$node);
  865. break;
  866. case 'synonyms':
  867. $block['subject'] = t('Synonyms');
  868. $block['content'] = theme('tripal_stock_synonyms',$node);
  869. break;
  870. }
  871. return $block;
  872. }
  873. }
  874. }
  875. function tripal_stock_preprocess_node_chado_stock (&$variables) {
  876. $variables['testing1'] = 'test';
  877. drupal_set_message('here');
  878. }