tripal_stock.module 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Implements Tripal Stock Module hooks
  6. */
  7. /**
  8. * @defgroup tripal_stock Stock Module
  9. * @{
  10. * Provides functions for managing chado stocks including creating details pages for each stock
  11. *
  12. * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado
  13. * stocks. The stock module was designed to store information about stock collections in a
  14. * laboratory. What is called a stock could also be called a strain or an accession. There is a lot
  15. * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all
  16. * come from some taxon, have genotypes, physical locations in the lab, some conceivable
  17. * relationship with a publication, some conceivable relationship with a sequence feature (such as a
  18. * transgene), and could be described by some ontology term. For more information about the chado
  19. * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
  20. * @}
  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' => t('Stocks'),
  42. 'description' => t('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' => t('Configuration'),
  49. 'description' => t('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' => t('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' => t('Add Properties & Synonyms'),
  66. 'description' => t('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' => t('Add Database References'),
  74. 'description' => t('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' => t('Add Relationships'),
  82. 'description' => t('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' => t('Edit Properties'),
  91. 'description' => t('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' => t('Edit Relationships'),
  100. 'description' => t('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' => t('Edit DB References'),
  109. 'description' => t('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. return user_access('create chado_stock content', $account);
  179. }
  180. if ($op == 'update') {
  181. if (user_access('edit chado_stock content', $account)) {
  182. return TRUE;
  183. }
  184. }
  185. if ($op == 'delete') {
  186. if (user_access('delete chado_stock content', $account)) {
  187. return TRUE;
  188. }
  189. }
  190. if ($op == 'view') {
  191. if (user_access('access chado_stock content', $account)) {
  192. return TRUE;
  193. }
  194. }
  195. return FALSE;
  196. }
  197. /**
  198. * Implements hook_views_api()
  199. *
  200. * Purpose: Essentially this hook tells drupal that there is views support for
  201. * for this module which then includes tripal_stock.views.inc where all the
  202. * views integration code is
  203. *
  204. * @return
  205. * An array with fields important for views integration
  206. *
  207. * @ingroup tripal_stock
  208. */
  209. function tripal_stock_views_api() {
  210. return array(
  211. 'api' => 2.0,
  212. );
  213. }
  214. /**
  215. * Implements hook_theme(): Register themeing functions for this module
  216. *
  217. * @return
  218. * An array of themeing functions to register
  219. *
  220. * @ingroup tripal_stock
  221. */
  222. function tripal_stock_theme() {
  223. return array(
  224. 'tripal_stock_stock_table' => array (
  225. 'arguments' => array('stocks'),
  226. ),
  227. // Property edit forms--------------------------
  228. 'tripal_stock_edit_ALL_properties_form' => array(
  229. 'arguments' => array('form'),
  230. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  231. ),
  232. 'tripal_stock_edit_ALL_db_references_form' => array(
  233. 'arguments' => array('form'),
  234. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  235. ),
  236. 'tripal_stock_edit_ALL_relationships_form' => array(
  237. 'arguments' => array('form'),
  238. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  239. ),
  240. // Block Templates------------------------------
  241. 'tripal_stock_base' => array (
  242. 'arguments' => array('node'=> null),
  243. 'template' => 'tripal_stock_base',
  244. ),
  245. 'tripal_stock_properties' => array (
  246. 'arguments' => array('node'=> null),
  247. 'template' => 'tripal_stock_properties',
  248. ),
  249. 'tripal_stock_references' => array (
  250. 'arguments' => array('node'=> null),
  251. 'template' => 'tripal_stock_references',
  252. ),
  253. 'tripal_stock_relationships_as_object' => array (
  254. 'arguments' => array('node'=> null),
  255. 'template' => 'tripal_stock_relationships_as_object',
  256. ),
  257. 'tripal_stock_relationships_as_subject' => array (
  258. 'arguments' => array('node'=> null),
  259. 'template' => 'tripal_stock_relationships_as_subject',
  260. ),
  261. 'tripal_stock_synonyms' => array (
  262. 'arguments' => array('node'=> null),
  263. 'template' => 'tripal_stock_synonyms',
  264. ),
  265. );
  266. }
  267. /**
  268. * Purpose: show stocks stored in drupals chado_stock table
  269. *
  270. * This function provides the default html representation of the stock table. This
  271. * representation can be overridden using Drupal Views2 to create more flexible tables
  272. * listing stocks.
  273. *
  274. * @return
  275. * HTML representation of a table of stocks
  276. *
  277. * @ingroup tripal_stock
  278. */
  279. function tripal_stock_show_stocks () {
  280. $sql = "SELECT COUNT(stock_id) FROM {chado_stock}";
  281. $no_stocks = db_result(db_query($sql));
  282. if($no_stocks != 0) {
  283. $stocks = tripal_stock_get_all_stocks();
  284. if($no_stocks != count($stocks)) {
  285. drupal_set_message("Synchronization needed.");
  286. }
  287. return theme('tripal_stock_stock_table',&$stocks);
  288. } else {
  289. return t("No Stocks exists. Please contact administrators to ".
  290. "synchronize stocks.");
  291. }
  292. }
  293. /**
  294. * A themeing funtion for the default tripal stock table
  295. *
  296. * @param $stocks
  297. * An array of all stock nodes
  298. *
  299. * @return
  300. * HTML representation of a table of stocks
  301. *
  302. * @ingroup tripal_stock
  303. */
  304. function theme_tripal_stock_stock_table (&$stocks) {
  305. // cycle through the stocks and build the stocks page
  306. $output = "<div id=\"stocks\">";
  307. $output .= '<table>';
  308. $output .= "<tr>";
  309. $output .= "<th>Name</th>";
  310. $output .= "<th>Type</th>";
  311. $output .= "<th>Organism</th>";
  312. $output .= "<th>Description</th>";
  313. $output .= "</tr>";
  314. foreach($stocks as $stock){
  315. $output .= "<tr>";
  316. $output .= "<td>".l($stock->stock_name, "node/".$stock->nid)."</td>";
  317. $output .= "<td>".$stock->stock_type."</td>";
  318. $output .= "<td nowrap>".$stock->organism->common_name."</td>";
  319. $output .= "<td>".$stock->description."</td>";
  320. $output .= "</tr>";
  321. }
  322. $output .= "</table>";
  323. $output .= "</div>";
  324. return $output;
  325. }
  326. /**
  327. * Implements hook_node_info(): registers a stock node type
  328. *
  329. * @return
  330. * An array describing various details of the node
  331. *
  332. * @ingroup tripal_stock
  333. */
  334. function tripal_stock_node_info() {
  335. return array(
  336. 'chado_stock' => array(
  337. 'name' => t('Stock'),
  338. 'module' => 'chado_stock',
  339. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  340. 'has_title' => TRUE,
  341. 'has_body' => FALSE,
  342. ),
  343. );
  344. }
  345. /**
  346. * Implements hook_load(): Prepares the chado_stock node
  347. *
  348. * @param $node
  349. * The basic node containing all variables common to all nodes
  350. *
  351. * @return
  352. * A stock node containing all the variables from the basic node and all stock specific variables
  353. *
  354. * @ingroup tripal_stock
  355. */
  356. function chado_stock_load($node) {
  357. // Gets the stock_id from the drupal chado_stock table---------------------------------------
  358. $map = db_fetch_object(db_query(
  359. "SELECT stock_id as stock_id FROM {chado_stock} WHERE vid=%d",
  360. $node->vid
  361. ));
  362. $node->stock_id = $map->stock_id;
  363. //Get the main stock information from the chado stock table-----------------------------------
  364. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  365. $values = array('stock_id' => $node->stock_id);
  366. $results = tripal_core_chado_select('stock', $columns, $values);
  367. $node->stock_name = $results[0]->name;
  368. $node->uniquename = $results[0]->uniquename;
  369. $node->description = $results[0]->description;
  370. $node->stock_type_id = $results[0]->type_id;
  371. $node->organism->organism_id = $results[0]->organism_id;
  372. $node->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  373. if (preg_match('/t/', $results[0]->is_obsolete)) {
  374. $node->is_obsolete = TRUE;
  375. } else {
  376. $node->is_obsolete = FALSE;
  377. }
  378. // Get type for current stock------------------------------------------------------------------
  379. $columns = array('name');
  380. $values = array('cvterm_id' => $node->stock_type_id);
  381. $results = tripal_core_chado_select('cvterm', $columns, $values);
  382. $node->stock_type = $results[0]->name;
  383. // Get organism details from chado & add to node-----------------------------------------------
  384. // get organism if for current stock
  385. // pull all data from chado for the organism and assign it to the current node
  386. $node->organism = tripal_organism_get_organism_by_organism_id($node->organism->organism_id);
  387. // Add Synonyms for stock----------------------------------------------------------------------
  388. // These are a special case of property (stockprop table) where cvterm='synonym'
  389. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  390. $values = array(
  391. 'stock_id' => $node->stock_id,
  392. 'type_id' => array(
  393. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  394. 'name' => 'synonym'
  395. )
  396. );
  397. $results = tripal_core_chado_select('stockprop', $columns, $values);
  398. if (!empty($results)) {
  399. foreach ($results as $r) {
  400. $r->type = 'synonym';
  401. $node->synonyms[] = $r;
  402. }
  403. }
  404. // Add properties for stock (not including synonyms)-------------------------------------------
  405. // $node->properties is an array of objects where each object describes a single property and has a type and value
  406. $columns = array('stockprop_id', 'type_id', 'value', 'rank');
  407. $values = array(
  408. 'stock_id' => $node->stock_id,
  409. );
  410. $results = tripal_core_chado_select('stockprop', $columns, $values);
  411. foreach ($results as $r) {
  412. $columns = array('name');
  413. $values = array('cvterm_id' => $r->type_id);
  414. $type_results = tripal_core_chado_select('cvterm', $columns, $values);
  415. $r->type = $type_results[0]->name;
  416. $node->properties[] = $r;
  417. }
  418. // Add in main db reference-----------------------------------------------------------------------
  419. // this is the dbxref_id in the stock table
  420. if (!empty($node->main_db_reference->dbxref_id)) {
  421. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  422. $values = array('dbxref_id' => $node->main_db_reference->dbxref_id);
  423. $results = tripal_core_chado_select('dbxref', $columns, $values);
  424. $node->main_db_reference = $results[0];
  425. //get db info
  426. $columns = array('name', 'description', 'url', 'urlprefix');
  427. $values = array('db_id' => $node->main_db_reference->db_id);
  428. $results = tripal_core_chado_select('db', $columns, $values);
  429. $node->main_db_reference->db_name = $results[0]->name;
  430. $node->main_db_reference->db_description = $results[0]->description;
  431. $node->main_db_reference->db_url = $results[0]->url;
  432. $node->main_db_reference->db_urlprefix = $results[0]->urlprefix;
  433. }
  434. // Add in extra references to external databases--------------------------------------------------
  435. // this includes all the dbxref entries in stock_dbxref
  436. $columns = array('dbxref_id');
  437. $values = array('stock_id' => $node->stock_id);
  438. $results = tripal_core_chado_select('stock_dbxref',$columns,$values);
  439. $node->db_references = array();
  440. foreach ($results as $r) {
  441. $columns = array('dbxref_id', 'accession', 'version', 'description', 'db_id');
  442. $values = array('dbxref_id' => $r->dbxref_id);
  443. $sub_results = tripal_core_chado_select('dbxref', $columns, $values);
  444. $dbxref = $sub_results[0];
  445. //get db info
  446. $columns = array('name', 'description', 'url', 'urlprefix');
  447. $values = array('db_id' => $dbxref->db_id);
  448. $results = tripal_core_chado_select('db', $columns, $values);
  449. $dbxref->db_name = $results[0]->name;
  450. $dbxref->db_description = $results[0]->description;
  451. $dbxref->db_url = $results[0]->url;
  452. $dbxref->db_urlprefix = $results[0]->urlprefix;
  453. $node->db_references[] = $dbxref;
  454. }
  455. // Add relationships for stock--------------------------------------------------------------------
  456. // Relationships are broken down into those where the current stock is the subject (other details stored in $node->object_relationships)
  457. // and those where the current stock is the object (other details stored in $node->subject_relationships)
  458. // fields available to each include object/subject_name, object/subject_id (stock_id in chado) and object/subject_nid (node id in drupal)
  459. // where current is subject.............................
  460. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  461. $values = array('subject_id' => $node->stock_id);
  462. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  463. $node->object_relationships = array();
  464. foreach ($results as $r) {
  465. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  466. $values = array('stock_id' => $r->object_id);
  467. $results = tripal_core_chado_select('stock', $columns, $values);
  468. // Type
  469. $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
  470. $r->type = $type_results[0]->name;
  471. // Object
  472. $r->object->stock_id = $r->object_id;
  473. unset($r->object_id);
  474. $r->object->stock_name = $results[0]->name;
  475. $r->object->uniquename = $results[0]->uniquename;
  476. $r->object->description = $results[0]->description;
  477. $r->object->stock_type_id = $results[0]->type_id;
  478. $r->object->organism->organism_id = $results[0]->organism_id;
  479. $r->object->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  480. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  481. $object_node = db_fetch_object(db_query($sql, $r->object->stock_id));
  482. $r->object->nid = $object_node->nid;
  483. $node->object_relationships[] = $r;
  484. }
  485. // where current is object.............................
  486. $columns = array('stock_relationship_id', 'subject_id', 'type_id', 'object_id', 'value', 'rank');
  487. $values = array('object_id' => $node->stock_id);
  488. $results = tripal_core_chado_select('stock_relationship', $columns, $values);
  489. $node->subject_relationships = array();
  490. foreach ($results as $r) {
  491. $columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
  492. $values = array('stock_id' => $r->subject_id);
  493. $results = tripal_core_chado_select('stock', $columns, $values);
  494. // Type
  495. $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
  496. $r->type = $type_results[0]->name;
  497. // Subject
  498. $r->subject->stock_id = $r->subject_id;
  499. unset($r->subject_id);
  500. $r->subject->stock_name = $results[0]->name;
  501. $r->subject->uniquename = $results[0]->uniquename;
  502. $r->subject->description = $results[0]->description;
  503. $r->subject->stock_type_id = $results[0]->type_id;
  504. $r->subject->organism->organism_id = $results[0]->organism_id;
  505. $r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
  506. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  507. $subject_node = db_fetch_object(db_query($sql, $r->subject->stock_id));
  508. $r->subject->nid = $subject_node->nid;
  509. $node->subject_relationships[] = $r;
  510. }
  511. return $node;
  512. }
  513. /**
  514. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  515. *
  516. * Parts to be added by this form
  517. * name,
  518. * uniquename,
  519. * description,
  520. * type => select from cvterm with key cvterm_id,
  521. * organism => select from available with key organism_id
  522. * main_db_reference => accession, version, description, db_name(select from dropdown)
  523. *
  524. * @param $node
  525. * An empty node object on insert OR the current stock node object on update
  526. * @param $form_state
  527. * The current state of the form
  528. *
  529. * @return
  530. * A description of the form to be rendered by drupal_get_form()
  531. *
  532. * @ingroup tripal_stock
  533. */
  534. function chado_stock_form($node, $form_state) {
  535. $type = node_get_types('type', $node);
  536. // This defines the path for the next step in a simulated multipart form
  537. // NOTE: The %node gets replaced with the nid in insert
  538. $form['next_step_path'] = array(
  539. '#type' => 'hidden',
  540. '#value' => 'node/%node/properties'
  541. );
  542. // If you don't want a multipart form set this to false
  543. // Will then do default redirect (to new node) on submit
  544. $form['simulate_multipart'] = array(
  545. '#type' => 'textfield',
  546. '#attributes'=>array('style'=>"display:none"),
  547. '#default_value' => TRUE
  548. );
  549. if (!isset($node->uniquename)) {
  550. $form['progress'] = array(
  551. '#type' => 'item',
  552. '#value' => tripal_stock_add_chado_properties_progress('main')
  553. );
  554. }
  555. $form['names'] = array(
  556. '#type' => 'fieldset',
  557. '#title' => t('Stock Name')
  558. );
  559. $form['names']['title'] = array(
  560. '#type' => 'textfield',
  561. '#title' => t('Name'),
  562. '#default_value' => $node->title,
  563. '#required' => TRUE
  564. );
  565. $form['names']['uniquename'] = array(
  566. '#type' => 'textfield',
  567. '#title' => t('Unique Name'),
  568. '#default_value' => $node->uniquename,
  569. '#required' => TRUE
  570. );
  571. $form['names']['stock_id'] = array(
  572. '#type' => 'hidden',
  573. '#value' => $node->stock_id
  574. );
  575. $form['details'] = array(
  576. '#type' => 'fieldset',
  577. '#title' => t('Stock Details')
  578. );
  579. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'null') );
  580. $type_options[0] = 'Select a Type';
  581. if ($node->nid == '') { $type_default = 0; } else { $type_default = $node->stock_type_id; }
  582. $form['details']['type_id'] = array(
  583. '#type' => 'select',
  584. '#title' => t('Type of Stock'),
  585. '#options' => $type_options,
  586. '#default_value' => $type_default,
  587. '#required' => TRUE,
  588. );
  589. $stock_oganism_options = tripal_organism_get_organism_options();
  590. $stock_oganism_options[0] = 'Select An Organism';
  591. if ($node->nid == '') { $organism_default = 0; } else { $organism_default = $node->organism->organism_id; }
  592. $form['details']['organism_id'] = array(
  593. '#type' => 'select',
  594. '#title' => t('Source Organism for stock'),
  595. '#default_value' => $organism_default,
  596. '#options' => $stock_oganism_options,
  597. '#required' => TRUE
  598. );
  599. $form['details']['stock_description'] = array(
  600. '#type' => 'textarea',
  601. '#title' => t('Notes'),
  602. '#default_value' => $node->description,
  603. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  604. );
  605. $form['database_reference'] = array(
  606. '#type' => 'fieldset',
  607. '#title' => t('Stock Database Reference')
  608. );
  609. $form['database_reference']['accession'] = array(
  610. '#type' => 'textfield',
  611. '#title' => t('Accession'),
  612. '#default_value' => $node->main_db_reference->accession
  613. );
  614. $form['database_reference']['db_description'] = array(
  615. '#type' => 'textarea',
  616. '#title' => t('Description of Database Reference'),
  617. '#default_value' => $node->main_db_reference->db_description,
  618. '#description' => t('Optionally enter a description about the database accession.')
  619. );
  620. $db_options = tripal_db_get_db_options();
  621. $db_options[0] = 'Select a Database';
  622. if ($node->nid == '') { $db_default = 0; } else { $db_default = $node->main_db_reference->db_id; }
  623. $form['database_reference']['database'] = array(
  624. '#type' => 'select',
  625. '#title' => t('Database'),
  626. '#options' => $db_options,
  627. '#default_value' => $db_default
  628. );
  629. return $form;
  630. }
  631. /**
  632. * Implements hook_validate(): Validate the input from the chado_stock node form
  633. *
  634. * @param $node
  635. * The current node including fields with the form element names and submitted values
  636. * @param $form
  637. * A description of the form to be rendered by drupal_get_form()
  638. *
  639. * @ingroup tripal_stock
  640. */
  641. function chado_stock_validate($node, &$form) {
  642. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  643. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  644. // Validate Uniquename only if add
  645. if (empty($node->stock_id)) {
  646. $previous_db = tripal_db_set_active('chado');
  647. $chado_row = db_fetch_object(db_query("SELECT * FROM stock WHERE uniquename='".$node->uniquename."'"));
  648. tripal_db_set_active($previous_db);
  649. if(!empty($chado_row->stock_id)) {
  650. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=".$chado_row->stock_id));
  651. if (!empty($drupal_row->nid)) {
  652. $link = l('node/'.$drupal_row->nid, $node->uniquename);
  653. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  654. } else {
  655. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  656. }
  657. }
  658. }
  659. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  660. if ( $node->type_id == 0) {
  661. form_set_error('type_id', 'Please select a type of stock.');
  662. } else {
  663. $previous_db = tripal_db_set_active('chado');
  664. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  665. tripal_db_set_active($previous_db);
  666. if ( $num_rows->count != 1) {
  667. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  668. }
  669. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  670. if ( $node->organism_id == 0) {
  671. form_set_error('organism_id', 'Please select a source organism for this stock');
  672. } else {
  673. $previous_db = tripal_db_set_active('chado');
  674. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  675. tripal_db_set_active($previous_db);
  676. if ( $num_rows->count != 1 ) {
  677. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  678. }
  679. // Check if Accession also database
  680. if ($node->accession != '') {
  681. if ($node->database == 0) {
  682. // there is an accession but no database selected
  683. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  684. }
  685. } else {
  686. if ($node->database > 0) {
  687. // there is a database selected but no accession
  688. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  689. }
  690. }
  691. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  692. if ( $node->database > 0) {
  693. $previous_db = tripal_db_set_active('chado');
  694. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  695. tripal_db_set_active($previous_db);
  696. if ($num_rows->count != 1) {
  697. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  698. }
  699. // Check Accession is unique for database ( $form['values']['database_reference']['accession'] )
  700. if ( $node->accession != '') {
  701. $previous_db = tripal_db_set_active('chado');
  702. $num_rows = db_fetch_object(db_query($string_in_chado_sql, 'dbxref', 'accession', $node->accession));
  703. tripal_db_set_active($previous_db);
  704. if ($num_rows->count > 0) {
  705. form_set_error('accession', 'This accession ('.$node->accession.') has already been assigned to another stock.'); }
  706. }
  707. }
  708. /**
  709. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  710. *
  711. * @param $node
  712. * The current node including fields with the form element names and submitted values
  713. *
  714. * @return
  715. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  716. *
  717. * @ingroup tripal_stock
  718. */
  719. function chado_stock_insert($node) {
  720. // create dbxref
  721. if ( !empty($node->accession) ) {
  722. if ( !empty($node->database) ) {
  723. $values = array(
  724. 'db_id' => $node->database,
  725. 'accession' => $node->accession,
  726. );
  727. if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
  728. $values['description'] = $node->db_description;
  729. $values['version'] = '1';
  730. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  731. if (!$dbxref_status) {
  732. drupal_set_message('Unable to add database reference to this stock.', 'warning');
  733. watchdog('tripal_stock',
  734. 'Insert Stock: Unable to create dbxref where values:%values',
  735. array('%values' => print_r($values, TRUE)),
  736. WATCHDOG_WARNING
  737. );
  738. }
  739. } else { $dbxref_status = 1; }
  740. }
  741. }
  742. // create stock
  743. if($dbxref_status) {
  744. $values = array(
  745. 'dbxref_id' => array(
  746. 'db_id' => $node->database,
  747. 'accession' => $node->accession
  748. ),
  749. 'organism_id' => $node->organism_id,
  750. 'name' => $node->title,
  751. 'uniquename' => $node->uniquename,
  752. 'description' => $node->description,
  753. 'type_id' => $node->type_id
  754. );
  755. $stock_status = tripal_core_chado_insert('stock', $values);
  756. } else {
  757. $values = array(
  758. 'organism_id' => $node->organism_id,
  759. 'name' => $node->title,
  760. 'uniquename' => $node->uniquename,
  761. 'description' => $node->description,
  762. 'type_id' => $node->type_id
  763. );
  764. $stock_status = tripal_core_chado_insert('stock', $values);
  765. }
  766. // create drupal chado_stock entry
  767. if ($stock_status) {
  768. $values = array(
  769. 'organism_id' => $node->organism_id,
  770. 'uniquename' => $node->uniquename,
  771. 'type_id' => $node->type_id
  772. );
  773. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  774. if (!empty($chado_stock[0]->stock_id)) {
  775. db_query(
  776. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  777. ."VALUES (%d, %d, %d)",
  778. $node->nid,
  779. $node->vid,
  780. $chado_stock[0]->stock_id
  781. );
  782. //Move on to next stage of Stock Creation based on next_stage_path field
  783. if ($node->simulate_multipart) {
  784. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  785. $_REQUEST['destination'] = $next_stage_path;
  786. }
  787. } else {
  788. drupal_set_message('Error during stock creation.', 'error');
  789. watchdog('tripal_stock',
  790. 'Insert Stock: Unable to find newly created stock where values:%values',
  791. array('%values' => print_r($values, TRUE)),
  792. WATCHDOG_ERROR
  793. );
  794. return FALSE;
  795. }
  796. } else {
  797. drupal_set_message('Error during stock creation.', 'error');
  798. watchdog('tripal_stock',
  799. 'Insert Stock: Unable to create stock where values:%values',
  800. array('%values' => print_r($values, TRUE)),
  801. WATCHDOG_WARNING
  802. );
  803. return FALSE;
  804. }
  805. }
  806. /**
  807. * Implements hook_update(): Handles Editing/Updating of main stock info
  808. *
  809. * NOTE: Currently just writes over all old data
  810. *
  811. * @param $node
  812. * The current node including fields with the form element names and submitted values
  813. *
  814. * @return
  815. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  816. *
  817. * @ingroup tripal_stock
  818. */
  819. function chado_stock_update($node) {
  820. if ($node->revision) {
  821. chado_stock_insert($node);
  822. } else {
  823. //update dbxref
  824. if ($node->database) {
  825. if($node->accession) {
  826. $dbxref_mode = '';
  827. $stock = tripal_core_chado_select(
  828. 'stock',
  829. array('dbxref_id', 'type_id'),
  830. array('stock_id' => $node->stock_id)
  831. );
  832. if ($stock[0]->dbxref_id) {
  833. $values = array(
  834. 'db_id' => $node->database,
  835. 'accession' => $node->accession,
  836. 'description' => $node->db_description
  837. );
  838. $dbxref_status = tripal_core_chado_update(
  839. 'dbxref',
  840. array('dbxref_id' => $stock[0]->dbxref_id),
  841. $values
  842. );
  843. $dbxref_mode = 'Update';
  844. } else {
  845. if ($stock[0]->type_id) {
  846. //create the dbxref
  847. //used the type_id as a control to check we have a stock but not a dbxref
  848. $values = array(
  849. 'db_id' => $node->database,
  850. 'accession' => $node->accession,
  851. 'description' => $node->db_description,
  852. 'version' => '1',
  853. );
  854. $dbxref_status = tripal_core_chado_insert(
  855. 'dbxref',
  856. $values
  857. );
  858. $dbxref_mode = 'Create';
  859. } else {
  860. drupal_set_message('Unable to find stock to Update', 'error');
  861. watchdog(
  862. 'tripal_stock',
  863. 'Stock Update: Unable to find stock to update using values: %values',
  864. array('%values', print_r($values, TRUE)),
  865. WATCHDOG_ERROR
  866. );
  867. return FALSE;
  868. }
  869. }
  870. }
  871. }
  872. if (!$dbxref_status) {
  873. watchdog(
  874. 'tripal_stock',
  875. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  876. array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
  877. WATCHDOG_WARNING
  878. );
  879. }
  880. //can't change stock id which is all thats stored in drupal thus only update chado
  881. $update_values = array(
  882. 'organism_id' => $node->organism_id,
  883. 'name' => $node->title,
  884. 'uniquename' => $node->uniquename,
  885. 'description' => $node->description,
  886. 'type_id' => $node->type_id,
  887. );
  888. if ($dbxref_status) {
  889. $update_values['dbxref_id'] = array(
  890. 'db_id' => $node->database,
  891. 'accession' => $node->accession
  892. );
  893. }
  894. $status = tripal_core_chado_update(
  895. 'stock',
  896. array('stock_id' => $node->stock_id),
  897. $update_values
  898. );
  899. if (!$status) {
  900. drupal_set_message('Unable to update stock', 'error');
  901. watchdog(
  902. 'tripal_stock',
  903. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  904. array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
  905. WATCHDOG_ERROR
  906. );
  907. }
  908. }
  909. }
  910. /**
  911. * Implements hook_delete(): Handles deleting of chado_stocks
  912. *
  913. * NOTE: Currently deletes data -no undo or record-keeping functionality
  914. *
  915. * @param $node
  916. * The current node including fields with the form element names and submitted values
  917. *
  918. * @return
  919. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  920. *
  921. * @ingroup tripal_stock
  922. */
  923. function chado_stock_delete($node) {
  924. //remove drupal node and all revisions
  925. db_query(
  926. "DELETE FROM {chado_stock} WHERE nid=%d",
  927. $node->nid
  928. );
  929. // Set stock in chado: is_obsolete = TRUE
  930. $previous_db = tripal_db_set_active('chado');
  931. db_query(
  932. "DELETE FROM stock WHERE stock_id=%d",
  933. $node->stock_id
  934. );
  935. tripal_db_set_active($previous_db);
  936. }
  937. /**
  938. * Purpose: Implement Blocks relating to stock content
  939. *
  940. * @param $op
  941. * What kind of information to retrieve about the block or blocks.
  942. * Possible values include list, configure, save, view.
  943. * @param $delta
  944. * Which block to return (not applicable if $op is 'list').
  945. * @param $edit
  946. * If $op is 'save', the submitted form data from the configuration form.
  947. *
  948. * @return
  949. * One of the following depending on $op: An array of block descriptions (list), the configuration
  950. * form (configure), nothing (save), an array defining subject and content for the block indexed
  951. * by $delta (view)
  952. *
  953. * @ingroup tripal_stock
  954. */
  955. function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
  956. switch($op) {
  957. case 'list':
  958. $blocks['base']['info'] = t('Tripal Stock Details');
  959. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  960. $blocks['properties']['info'] = t('Tripal Stock Properties');
  961. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  962. $blocks['references']['info'] = t('Tripal Stock References');
  963. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  964. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships as Object');
  965. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  966. $blocks['relationships_as_subject']['info'] = t('Tripal Stock Relationships as Subject');
  967. $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
  968. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  969. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  970. return $blocks;
  971. case 'view':
  972. if(user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  973. $nid = arg(1);
  974. $node = node_load($nid);
  975. $block = array();
  976. switch($delta){
  977. case 'base':
  978. $block['subject'] = t('Stock Details');
  979. $block['content'] = theme('tripal_stock_base',$node);
  980. break;
  981. case 'properties':
  982. $block['subject'] = t('Properties');
  983. $block['content'] = theme('tripal_stock_properties',$node);
  984. break;
  985. case 'references':
  986. $block['subject'] = t('References');
  987. $block['content'] = theme('tripal_stock_references',$node);
  988. break;
  989. case 'relationships_as_object':
  990. $block['subject'] = t('Object Relationships');
  991. $block['content'] = theme('tripal_stock_relationships_as_object',$node);
  992. break;
  993. case 'relationships_as_subject':
  994. $block['subject'] = t('Subject Relationships');
  995. $block['content'] = theme('tripal_stock_relationships_as_subject',$node);
  996. break;
  997. case 'synonyms':
  998. $block['subject'] = t('Synonyms');
  999. $block['content'] = theme('tripal_stock_synonyms',$node);
  1000. break;
  1001. }
  1002. return $block;
  1003. }
  1004. }
  1005. }