tripal_stock.module 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. <?php
  2. /**
  3. * @file
  4. * Implements Tripal Stock Module hooks
  5. */
  6. /**
  7. * @defgroup tripal_stock Stock Module
  8. * @ingroup tripal_modules
  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_once("includes/tripal_stock.admin.inc");
  23. require_once("includes/tripal_stock.sync_stocks.inc");
  24. require_once("includes/other_module_api_functions.inc");
  25. require_once("includes/tripal_stock-secondary_tables.inc");
  26. require_once("includes/tripal_stock-properties.inc");
  27. require_once("includes/tripal_stock-relationships.inc");
  28. require_once("includes/tripal_stock-db_references.inc");
  29. require_once("api/tripal_stock.api.inc");
  30. /**
  31. * Implements hook_menu(): Adds menu items for the tripal_stock
  32. *
  33. * @return
  34. * Menu definitions for the tripal_stock
  35. *
  36. * @ingroup tripal_stock
  37. */
  38. function tripal_stock_menu() {
  39. $items = array();
  40. //Administrative settings menu-----------------
  41. $items['admin/tripal/chado/tripal_stock'] = array(
  42. 'title' => 'Stocks',
  43. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  44. 'page callback' => 'tripal_stock_admin_stock_view',
  45. 'access arguments' => array('administer tripal stocks'),
  46. 'type' => MENU_NORMAL_ITEM
  47. );
  48. $items['admin/tripal/chado/tripal_stock/configuration'] = array(
  49. 'title' => 'Settings',
  50. 'description' => 'Settings for Chado Stocks',
  51. 'page callback' => 'drupal_get_form',
  52. 'page arguments' => array('tripal_stock_admin'),
  53. 'access arguments' => array('administer tripal stocks'),
  54. 'type' => MENU_LOCAL_TASK,
  55. 'weight' => 5
  56. );
  57. $items['admin/tripal/chado/tripal_stock/sync'] = array(
  58. 'title' => ' Sync',
  59. 'description' => 'Sync stocks from Chado with Drupal',
  60. 'page callback' => 'drupal_get_form',
  61. 'page arguments' => array('tripal_stock_sync_form'),
  62. 'access arguments' => array('administer tripal stocks'),
  63. 'type' => MENU_LOCAL_TASK,
  64. 'weight' => 0
  65. );
  66. $items['admin/tripal/chado/tripal_stock/help'] = array(
  67. 'title' => 'Help',
  68. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  69. 'page callback' => 'theme',
  70. 'page arguments' => array('tripal_stock_help'),
  71. 'access arguments' => array('administer tripal stocks'),
  72. 'type' => MENU_LOCAL_TASK,
  73. 'weight' => 10
  74. );
  75. // Adding Secondary Properties-----------------
  76. $items['node/%cs_node/properties'] = array(
  77. 'title' => 'Add Properties & Synonyms',
  78. 'description' => 'Settings for Chado Stocks',
  79. 'page callback' => 'tripal_stock_add_ALL_property_page',
  80. 'page arguments' => array(1),
  81. 'access arguments' => array('create chado_stock content'),
  82. 'type' => MENU_CALLBACK
  83. );
  84. $items['node/%cs_node/db_references'] = array(
  85. 'title' => 'Add Database References',
  86. 'description' => 'Settings for Chado Stocks',
  87. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  88. 'page arguments' => array(1),
  89. 'access arguments' => array('create chado_stock content'),
  90. 'type' => MENU_CALLBACK
  91. );
  92. $items['node/%cs_node/relationships'] = array(
  93. 'title' => 'Add Relationships',
  94. 'description' => 'Settings for Chado Stocks',
  95. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  96. 'page arguments' => array(1),
  97. 'access arguments' => array('create chado_stock content'),
  98. 'type' => MENU_CALLBACK
  99. );
  100. //Edit/Deleting Secondary Properties-------------
  101. $items['node/%cs_node/edit_properties'] = array(
  102. 'title' => 'Edit Properties',
  103. 'description' => 'Settings for Chado Stocks',
  104. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  105. 'page arguments' => array(1),
  106. 'access arguments' => array('edit chado_stock content'),
  107. 'type' => MENU_LOCAL_TASK,
  108. 'weight' => 8,
  109. );
  110. $items['node/%cs_node/edit_relationships'] = array(
  111. 'title' => 'Edit Relationships',
  112. 'description' => 'Settings for Chado Stocks',
  113. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  114. 'page arguments' => array(1),
  115. 'access arguments' => array('edit chado_stock content'),
  116. 'type' => MENU_LOCAL_TASK,
  117. 'weight' => 9,
  118. );
  119. $items['node/%cs_node/edit_db_references'] = array(
  120. 'title' => 'Edit DB References',
  121. 'description' => 'Settings for Chado Stocks',
  122. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  123. 'page arguments' => array(1),
  124. 'access arguments' => array('edit chado_stock content'),
  125. 'type' => MENU_LOCAL_TASK,
  126. 'weight' => 10,
  127. );
  128. // the menu link for addressing any stock (by name, uniquename, synonym)
  129. $items['stock/%'] = array(
  130. 'page callback' => 'tripal_stock_match_stocks_page',
  131. 'page arguments' => array(1),
  132. 'access arguments' => array('access chado_stock content'),
  133. 'type' => MENU_LOCAL_TASK,
  134. );
  135. return $items;
  136. }
  137. /**
  138. * Implements Menu wildcard_load hook
  139. *
  140. * Purpose: Allows the node ID of a chado stock to be dynamically
  141. * pulled from the path. The node is loaded from this node ID
  142. * and supplied to the page as an arguement. This is an example
  143. * of dynamic argument replacement using wildcards in the path.
  144. *
  145. * @param $nid
  146. * The node ID passed in from the path
  147. *
  148. * @return
  149. * The node object with the passed in nid
  150. *
  151. * @ingroup tripal_stock
  152. */
  153. function cs_node_load($nid) {
  154. if (is_numeric($nid)) {
  155. $node = node_load($nid);
  156. if ($node->type == 'chado_stock') {
  157. return $node;
  158. }
  159. }
  160. return FALSE;
  161. }
  162. /**
  163. * Implementation of hook_perm().
  164. *
  165. * Purpose: Set the permission types that the chado stock module uses
  166. *
  167. * @return
  168. * Listing of the possible permission catagories
  169. *
  170. * @ingroup tripal_stock
  171. */
  172. function tripal_stock_permissions() {
  173. return array(
  174. 'access chado_stock content' => array(
  175. 'title' => t('View Stocks'),
  176. 'description' => t('Allow users to view stock pages.'),
  177. ),
  178. 'create chado_stock content' => array(
  179. 'title' => t('Create Stocks'),
  180. 'description' => t('Allow users to create new stock pages.'),
  181. ),
  182. 'delete chado_stock content' => array(
  183. 'title' => t('Delete Stocks'),
  184. 'description' => t('Allow users to delete stock pages.'),
  185. ),
  186. 'edit chado_stock content' => array(
  187. 'title' => t('Edit Stocks'),
  188. 'description' => t('Allow users to edit stock pages.'),
  189. ),
  190. 'adminster tripal stock' => array(
  191. 'title' => t('Administer Stocks'),
  192. 'description' => t('Allow users to administer all stocks.'),
  193. ),
  194. );
  195. }
  196. /**
  197. * Implement hook_access().
  198. *
  199. * This hook allows node modules to limit access to the node types they define.
  200. *
  201. * @param $node
  202. * The node on which the operation is to be performed, or, if it does not yet exist, the
  203. * type of node to be created
  204. *
  205. * @param $op
  206. * The operation to be performed
  207. *
  208. * @param $account
  209. * A user object representing the user for whom the operation is to be performed
  210. *
  211. * @return
  212. * If the permission for the specified operation is not set then return FALSE. If the
  213. * permission is set then return NULL as this allows other modules to disable
  214. * access. The only exception is when the $op == 'create'. We will always
  215. * return TRUE if the permission is set.
  216. *
  217. * @ingroup tripal_stock
  218. */
  219. function chado_stock_node_access($node, $op, $account) {
  220. if ($op == 'create') {
  221. if (!user_access('create chado_stock content', $account)) {
  222. return FALSE;
  223. }
  224. return TRUE;
  225. }
  226. if ($op == 'update') {
  227. if (!user_access('edit chado_stock content', $account)) {
  228. return FALSE;
  229. }
  230. }
  231. if ($op == 'delete') {
  232. if (!user_access('delete chado_stock content', $account)) {
  233. return FALSE;
  234. }
  235. }
  236. if ($op == 'view') {
  237. if (!user_access('access chado_stock content', $account)) {
  238. return FALSE;
  239. }
  240. }
  241. return NULL;
  242. }
  243. /**
  244. * Implements hook_views_api()
  245. *
  246. * Purpose: Essentially this hook tells drupal that there is views support for
  247. * for this module which then includes tripal_stock.views.inc where all the
  248. * views integration code is
  249. *
  250. * @return
  251. * An array with fields important for views integration
  252. *
  253. * @ingroup tripal_stock
  254. */
  255. function tripal_stock_views_api() {
  256. return array(
  257. 'api' => 2.0,
  258. );
  259. }
  260. /**
  261. * Implements hook_theme(): Register themeing functions for this module
  262. *
  263. * @return
  264. * An array of themeing functions to register
  265. *
  266. * @ingroup tripal_stock
  267. */
  268. function tripal_stock_theme() {
  269. $theme_path = drupal_get_path('module', 'tripal_stock') . '/theme';
  270. $items = array(
  271. // property edit forms function templates
  272. 'tripal_stock_edit_ALL_properties_form' => array(
  273. 'arguments' => array('form'),
  274. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  275. ),
  276. 'tripal_stock_edit_ALL_db_references_form' => array(
  277. 'arguments' => array('form'),
  278. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  279. ),
  280. 'tripal_stock_edit_ALL_relationships_form' => array(
  281. 'arguments' => array('form'),
  282. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  283. ),
  284. // tripal_stock templates
  285. 'node__chado_stock' => array(
  286. 'template' => 'node--chado-generic',
  287. 'path' => drupal_get_path('module', 'tripal_core') . '/theme',
  288. 'render element' => 'node',
  289. 'base hook' => 'node',
  290. ),
  291. 'tripal_stock_base' => array(
  292. 'arguments' => array('node' => NULL),
  293. 'template' => 'tripal_stock_base',
  294. 'path' => "$theme_path/tripal_stock",
  295. ),
  296. 'tripal_stock_properties' => array(
  297. 'arguments' => array('node' => NULL),
  298. 'template' => 'tripal_stock_properties',
  299. 'path' => "$theme_path/tripal_stock",
  300. ),
  301. 'tripal_stock_references' => array(
  302. 'arguments' => array('node' => NULL),
  303. 'template' => 'tripal_stock_references',
  304. 'path' => "$theme_path/tripal_stock",
  305. ),
  306. 'tripal_stock_relationships' => array(
  307. 'arguments' => array('node' => NULL),
  308. 'template' => 'tripal_stock_relationships',
  309. 'path' => "$theme_path/tripal_stock",
  310. ),
  311. 'tripal_stock_synonyms' => array(
  312. 'arguments' => array('node' => NULL),
  313. 'template' => 'tripal_stock_synonyms',
  314. 'path' => "$theme_path/tripal_stock",
  315. ),
  316. 'tripal_stock_collections' => array(
  317. 'arguments' => array('node' => NULL),
  318. 'template' => 'tripal_stock_collections',
  319. 'path' => "$theme_path/tripal_stock",
  320. ),
  321. 'tripal_stock_collections' => array(
  322. 'arguments' => array('node' => NULL),
  323. 'template' => 'tripal_stock_collections',
  324. 'path' => "$theme_path/tripal_stock",
  325. ),
  326. 'tripal_stock_phenotypes' => array(
  327. 'arguments' => array('node' => NULL),
  328. 'template' => 'tripal_stock_phenotypes',
  329. 'path' => "$theme_path/tripal_stock",
  330. ),
  331. 'tripal_stock_locations' => array(
  332. 'arguments' => array('node' => NULL),
  333. 'template' => 'tripal_stock_locations',
  334. 'path' => "$theme_path/tripal_stock",
  335. ),
  336. // tripal_organism templates
  337. 'tripal_organism_stocks' => array(
  338. 'arguments' => array('node' => NULL),
  339. 'template' => 'tripal_organism_stocks',
  340. 'path' => "$theme_path/tripal_organism",
  341. ),
  342. // help template
  343. 'tripal_stock_help' => array(
  344. 'template' => 'tripal_stock_help',
  345. 'arguments' => array(NULL),
  346. 'path' => $theme_path,
  347. ),
  348. );
  349. return $items;
  350. }
  351. /**
  352. * Implements hook_help()
  353. * Purpose: Adds a help page to the module list
  354. */
  355. function tripal_stock_help ($path, $arg) {
  356. if ($path == 'admin/help#tripal_stock') {
  357. return theme('tripal_stock_help', array());
  358. }
  359. }
  360. /**
  361. * Implements hook_node_info(): registers a stock node type
  362. *
  363. * @return
  364. * An array describing various details of the node
  365. *
  366. * @ingroup tripal_stock
  367. */
  368. function tripal_stock_node_info() {
  369. return array(
  370. 'chado_stock' => array(
  371. 'name' => t('Stock'),
  372. 'base' => 'chado_stock',
  373. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  374. 'has_title' => TRUE,
  375. 'has_body' => FALSE,
  376. ),
  377. );
  378. }
  379. /**
  380. * Implements hook_load(): Prepares the chado_stock node
  381. *
  382. * @param $node
  383. * The basic node containing all variables common to all nodes
  384. *
  385. * @return
  386. * A stock node containing all the variables from the basic node and all stock specific variables
  387. *
  388. * D7 @todo: Make optimizations to take advantage of $nodes
  389. *
  390. * @ingroup tripal_stock
  391. */
  392. function chado_stock_load($nodes) {
  393. $new_nodes = array();
  394. foreach ($nodes as $nid => $node) {
  395. // get the stock details from chado
  396. $stock_id = chado_get_id_for_node('stock', $node->nid);
  397. if (empty($stock_id)) {
  398. tripal_core_report_error(
  399. 'tripal_stock',
  400. TRIPAL_ERROR,
  401. 'Unable to retrieve stock_id for %title (NID = %nid).',
  402. array('%title' => $node->title, '%nid' => $node->nid)
  403. );
  404. }
  405. else {
  406. // build the variable with all the stock details
  407. $values = array('stock_id' => $stock_id);
  408. $stock = tripal_core_generate_chado_var('stock', $values);
  409. // by default, the titles are saved using the unique constraint. We will
  410. // keep it the same, but remove the duplicate name if the unique name and name
  411. // are identical
  412. $title_type = variable_get('chado_stock_title', 'unique_constraint');
  413. if($title_type == 'unique_constraint') {
  414. if (strcmp($stock->name, $stock->uniquename)==0) {
  415. $node->title = $stock->name . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  416. }
  417. // in previous version of Tripal, the stock title was simply the unique name.
  418. // so, we recreate the title just to be sure all of our stock pages are consistent
  419. else {
  420. $node->title = $stock->name . ", " . $stock->uniquename . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  421. }
  422. }
  423. // set the title to be the stock name or uniquename as configured
  424. if($title_type == 'stock_name') {
  425. $node->title = $stock->name;
  426. }
  427. if($title_type == 'stock_unique_name') {
  428. $node->title = $stock->uniquename;
  429. }
  430. // add this to the node
  431. $node->stock = $stock;
  432. }
  433. $new_nodes[$nid] = $node;
  434. }
  435. ddl($new_nodes, 'nodes at end of load');
  436. return $new_nodes;
  437. }
  438. /**
  439. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  440. *
  441. * Parts to be added by this form
  442. * name,
  443. * uniquename,
  444. * description,
  445. * type => select from cvterm with key cvterm_id,
  446. * organism => select from available with key organism_id
  447. * main_db_reference => accession, version, description, db_name(select from dropdown)
  448. *
  449. * @param $node
  450. * An empty node object on insert OR the current stock node object on update
  451. * @param $form_state
  452. * The current state of the form
  453. *
  454. * @return
  455. * A description of the form to be rendered by drupal_get_form()
  456. *
  457. * @ingroup tripal_stock
  458. */
  459. function chado_stock_form($node, $form_state) {
  460. ddl($node, 'node');
  461. if (isset($node->nid)) {
  462. // Expand all fields needed
  463. $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
  464. foreach ($fields_needed as $field_name) {
  465. // Check to see if it's excluded and expand it if so
  466. if (isset($node->expandable_fields)) {
  467. if (in_array($field_name, $node->expandable_fields)) {
  468. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  469. }
  470. }
  471. }
  472. }
  473. // This defines the path for the next step in a simulated multipart form
  474. // NOTE: The %node gets replaced with the nid in insert
  475. $form['next_step_path'] = array(
  476. '#type' => 'hidden',
  477. '#value' => 'node/%node/properties'
  478. );
  479. // If you don't want a multipart form set this to false
  480. // Will then do default redirect (to new node) on submit
  481. $form['simulate_multipart'] = array(
  482. '#type' => 'textfield',
  483. '#attributes' => array('style' => "display:none"),
  484. '#default_value' => TRUE
  485. );
  486. if (!isset($node->stock->uniquename)) {
  487. $form['progress'] = array(
  488. '#type' => 'item',
  489. '#value' => tripal_stock_add_chado_properties_progress('main')
  490. );
  491. }
  492. $form['names'] = array(
  493. '#type' => 'fieldset',
  494. '#title' => t('Stock Name')
  495. );
  496. $form['names']['sname'] = array(
  497. '#type' => 'textfield',
  498. '#title' => t('Name'),
  499. '#default_value' => (isset($node->stock->name)) ? $node->stock->name : '',
  500. '#required' => TRUE
  501. );
  502. $form['names']['uniquename'] = array(
  503. '#type' => 'textfield',
  504. '#title' => t('Unique Name'),
  505. '#default_value' => (isset($node->stock->uniquename)) ? $node->stock->uniquename : '',
  506. '#required' => TRUE
  507. );
  508. $form['names']['stock_id'] = array(
  509. '#type' => 'hidden',
  510. '#value' => (isset($node->stock->stock_id)) ? $node->stock->stock_id : NULL
  511. );
  512. $form['details'] = array(
  513. '#type' => 'fieldset',
  514. '#title' => t('Stock Details')
  515. );
  516. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  517. $type_options[0] = 'Select a Type';
  518. if (!isset($node->nid)) {
  519. $type_default = 0;
  520. }
  521. else {
  522. $type_default = $node->stock->type_id->cvterm_id;
  523. }
  524. $form['details']['type_id'] = array(
  525. '#type' => 'select',
  526. '#title' => t('Type of Stock'),
  527. '#options' => $type_options,
  528. '#default_value' => $type_default,
  529. '#required' => TRUE,
  530. );
  531. // get the list of organisms
  532. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  533. $org_rset = chado_query($sql);
  534. $organisms = array();
  535. $organisms[''] = '';
  536. while ($organism = $org_rset->fetchObject()) {
  537. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  538. }
  539. $form['details']['organism_id'] = array(
  540. '#type' => 'select',
  541. '#title' => t('Source Organism for stock'),
  542. '#default_value' => (isset($node->stock->organism_id->organism_id)) ? $node->stock->organism_id->organism_id : '',
  543. '#options' => $organisms,
  544. '#required' => TRUE
  545. );
  546. $form['details']['stock_description'] = array(
  547. '#type' => 'textarea',
  548. '#title' => t('Notes'),
  549. '#default_value' => (isset($node->stock->description)) ? $node->stock->description : '',
  550. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  551. );
  552. $form['database_reference'] = array(
  553. '#type' => 'fieldset',
  554. '#title' => t('Stock Database Reference')
  555. );
  556. $form['database_reference']['accession'] = array(
  557. '#type' => 'textfield',
  558. '#title' => t('Accession'),
  559. '#default_value' => (isset($node->stock->dbxref_id->accession)) ? $node->stock->dbxref_id->accession : ''
  560. );
  561. $form['database_reference']['db_description'] = array(
  562. '#type' => 'textarea',
  563. '#title' => t('Description of Database Reference'),
  564. '#default_value' => (isset($node->stock->dbxref_id->description)) ? $node->stock->dbxref_id->description : '',
  565. '#description' => t('Optionally enter a description about the database accession.')
  566. );
  567. $db_options = tripal_db_get_db_options();
  568. $db_options[0] = 'Select a Database';
  569. if (!isset($node->nid)) {
  570. $db_default = 0;
  571. }
  572. else {
  573. $db_default = $node->stock->dbxref_id->db_id->db_id;
  574. }
  575. $form['database_reference']['database'] = array(
  576. '#type' => 'select',
  577. '#title' => t('Database'),
  578. '#options' => $db_options,
  579. '#default_value' => $db_default
  580. );
  581. return $form;
  582. }
  583. /**
  584. * Implements hook_validate(): Validate the input from the chado_stock node form
  585. *
  586. * @param $node
  587. * The current node including fields with the form element names and submitted values
  588. * @param $form
  589. * A description of the form to be rendered by drupal_get_form()
  590. *
  591. * @ingroup tripal_stock
  592. */
  593. function chado_stock_validate($node, $node, $form, &$form_state) {
  594. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  595. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  596. // if this is an update, we want to make sure that a different stock for
  597. // the organism doesn't already have this uniquename. We don't want to give
  598. // two sequences the same uniquename
  599. if ($node->stock_id) {
  600. $sql = "
  601. SELECT *
  602. FROM {stock} S
  603. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  604. WHERE
  605. uniquename = :uname AND organism_id = :organism_id AND
  606. CVT.name = :cvtname AND NOT stock_id = :stock_id
  607. ";
  608. $result = chado_query($sql, array(':uname' => $node->uniquename,
  609. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  610. ':stock_id' => $node->stock_id))->fetchObject();
  611. if ($result) {
  612. form_set_error('uniquename', t("Stock update cannot proceed. The stock name '$node->uniquename' is not unique for this organism. Please provide a unique name for this stock."));
  613. }
  614. }
  615. // if this is an insert then we just need to make sure this name doesn't
  616. // already exist for this organism if it does then we need to throw an error
  617. else {
  618. $sql = "
  619. SELECT *
  620. FROM {Stock} S
  621. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  622. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  623. $result = chado_query($sql, array(':uname' => $node->uniquename,
  624. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  625. if ($result) {
  626. form_set_error('uniquename', t("Stock insert cannot proceed. The stock name '$node->uniquename' already exists for this organism. Please provide a unique name for this stock."));
  627. }
  628. }
  629. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  630. if ( $node->type_id == 0) {
  631. form_set_error('type_id', 'Please select a type of stock.');
  632. }
  633. else {
  634. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  635. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  636. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  637. if ( $num_rows->count != 1) {
  638. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  639. }
  640. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  641. if ( $node->organism_id == 0) {
  642. form_set_error('organism_id', 'Please select a source organism for this stock');
  643. }
  644. else {
  645. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  646. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  647. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  648. if ( $num_rows->count != 1 ) {
  649. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  650. }
  651. // Check if Accession also database
  652. if ($node->accession != '') {
  653. if ($node->database == 0) {
  654. // there is an accession but no database selected
  655. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  656. }
  657. }
  658. else {
  659. if ($node->database > 0) {
  660. // there is a database selected but no accession
  661. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  662. }
  663. }
  664. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  665. if ( $node->database > 0) {
  666. $replace = array(':table' => 'db', ':column' => 'db_id');
  667. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  668. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  669. if ($num_rows->count != 1) {
  670. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  671. }
  672. }
  673. /**
  674. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  675. *
  676. * @param $node
  677. * The current node including fields with the form element names and submitted values
  678. *
  679. * @return
  680. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  681. *
  682. * @ingroup tripal_stock
  683. */
  684. function chado_stock_insert($node) {
  685. // If the chado stock exists (e.g. this is only a syncing operation)
  686. // then don't create but simply link to node
  687. if (isset($node->chado_stock_exists)) {
  688. if ($node->chado_stock_exists) {
  689. if (!empty($node->stock_id)) {
  690. db_insert('chado_stock')->fields(array(
  691. 'nid' => $node->nid,
  692. 'vid' => $node->vid,
  693. 'stock_id' => $node->stock_id
  694. ));
  695. }
  696. return $node;
  697. }
  698. }
  699. // before we can add the stock, we must add the dbxref if one has been
  700. // provided by the user.
  701. $dbxref_status = 0;
  702. if (!empty($node->accession) ) {
  703. if (!empty($node->database) ) {
  704. $values = array(
  705. 'db_id' => $node->database,
  706. 'accession' => $node->accession,
  707. );
  708. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  709. $values['description'] = $node->db_description;
  710. $values['version'] = '1';
  711. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  712. if (!$dbxref_status) {
  713. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  714. watchdog('tripal_stock',
  715. 'Insert Stock: Unable to create dbxref where values:%values',
  716. array('%values' => print_r($values, TRUE)),
  717. WATCHDOG_WARNING
  718. );
  719. }
  720. }
  721. else {
  722. $dbxref_status = 1;
  723. }
  724. }
  725. }
  726. // create stock including the dbxref
  727. $stock = '';
  728. if ($dbxref_status) {
  729. $values = array(
  730. 'dbxref_id' => array(
  731. 'db_id' => $node->database,
  732. 'accession' => $node->accession
  733. ),
  734. 'organism_id' => $node->organism_id,
  735. 'name' => $node->sname,
  736. 'uniquename' => $node->uniquename,
  737. 'description' => $node->stock_description,
  738. 'type_id' => $node->type_id
  739. );
  740. $stock = tripal_core_chado_insert('stock', $values);
  741. }
  742. // create a stock without a dbxref
  743. else {
  744. $values = array(
  745. 'organism_id' => $node->organism_id,
  746. 'name' => $node->sname,
  747. 'uniquename' => $node->uniquename,
  748. 'description' => $node->stock_description,
  749. 'type_id' => $node->type_id
  750. );
  751. $stock = tripal_core_chado_insert('stock', $values);
  752. }
  753. ddl($stock, 'stock in insert');
  754. ddl($node, 'node in insert');
  755. // if the stock creation was succesful then add the URL and the entry in the
  756. // chado_stock table
  757. if (is_array($stock)) {
  758. // add the entry to the chado_stock table
  759. db_insert('chado_stock')->fields(array(
  760. 'nid' => (int) $node->nid,
  761. 'vid' => (int) $node->vid,
  762. 'stock_id' => (int) $stock['stock_id']
  763. ))->execute();
  764. // Move on to next stage of Stock Creation based on next_stage_path field
  765. if ($node->simulate_multipart) {
  766. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  767. $_REQUEST['destination'] = $next_stage_path;
  768. }
  769. }
  770. else {
  771. drupal_set_message(t('Error during stock creation.'), 'error');
  772. watchdog('tripal_stock',
  773. 'Insert Stock: Unable to create stock where values:%values',
  774. array('%values' => print_r($values, TRUE)),
  775. WATCHDOG_WARNING
  776. );
  777. return FALSE;
  778. }
  779. }
  780. /**
  781. * Implements hook_update(): Handles Editing/Updating of main stock info
  782. *
  783. * NOTE: Currently just writes over all old data
  784. *
  785. * @param $node
  786. * The current node including fields with the form element names and submitted values
  787. *
  788. * @return
  789. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  790. *
  791. * @ingroup tripal_stock
  792. */
  793. function chado_stock_update($node) {
  794. if ($node->revision) {
  795. // there is no way to handle revisions in Chado but leave
  796. // this here just to make not we've addressed it.
  797. }
  798. //update dbxref
  799. if ($node->database) {
  800. if ($node->accession) {
  801. $dbxref_mode = '';
  802. $stock = tripal_core_chado_select(
  803. 'stock',
  804. array('dbxref_id', 'type_id'),
  805. array('stock_id' => $node->stock_id)
  806. );
  807. if ($stock[0]->dbxref_id) {
  808. $values = array(
  809. 'db_id' => $node->database,
  810. 'accession' => $node->accession,
  811. 'description' => $node->db_description
  812. );
  813. $dbxref_status = tripal_core_chado_update(
  814. 'dbxref',
  815. array('dbxref_id' => $stock[0]->dbxref_id),
  816. $values
  817. );
  818. $dbxref_mode = 'Update';
  819. }
  820. else {
  821. if ($stock[0]->type_id) {
  822. //create the dbxref
  823. //used the type_id as a control to check we have a stock but not a dbxref
  824. $values = array(
  825. 'db_id' => $node->database,
  826. 'accession' => $node->accession,
  827. 'description' => $node->db_description,
  828. 'version' => '1',
  829. );
  830. $dbxref_status = tripal_core_chado_insert(
  831. 'dbxref',
  832. $values
  833. );
  834. $dbxref_mode = 'Create';
  835. }
  836. else {
  837. drupal_set_message(t('Unable to find stock to Update'), 'error');
  838. watchdog(
  839. 'tripal_stock',
  840. 'Stock Update: Unable to find stock to update using values: %values',
  841. array('%values', print_r($values, TRUE)),
  842. WATCHDOG_ERROR
  843. );
  844. return FALSE;
  845. }
  846. }
  847. }
  848. }
  849. if (!$dbxref_status) {
  850. watchdog(
  851. 'tripal_stock',
  852. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  853. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  854. WATCHDOG_WARNING
  855. );
  856. }
  857. //can't change stock id which is all thats stored in drupal thus only update chado
  858. $update_values = array(
  859. 'organism_id' => $node->organism_id,
  860. 'name' => $node->sname,
  861. 'uniquename' => $node->uniquename,
  862. 'description' => $node->stock_description,
  863. 'type_id' => $node->type_id,
  864. );
  865. if ($dbxref_status) {
  866. $update_values['dbxref_id'] = array(
  867. 'db_id' => $node->database,
  868. 'accession' => $node->accession
  869. );
  870. }
  871. $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
  872. if (!$status) {
  873. drupal_set_message(t('Unable to update stock'), 'error');
  874. watchdog(
  875. 'tripal_stock',
  876. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  877. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  878. WATCHDOG_ERROR
  879. );
  880. }
  881. else {
  882. // set the URL for this stock page
  883. $values = array('stock_id' => $node->stock_id);
  884. $stock = tripal_core_chado_select('stock', array('*'), $values);
  885. }
  886. }
  887. /**
  888. * Implements hook_delete(): Handles deleting of chado_stocks
  889. *
  890. * NOTE: Currently deletes data -no undo or record-keeping functionality
  891. *
  892. * @param $node
  893. * The current node including fields with the form element names and submitted values
  894. *
  895. * @return
  896. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  897. *
  898. * @ingroup tripal_stock
  899. */
  900. function chado_stock_delete($node) {
  901. // Set stock in chado: is_obsolete = TRUE
  902. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  903. //remove drupal node and all revisions
  904. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  905. }
  906. /**
  907. * @ingroup tripal_stock
  908. */
  909. function tripal_stock_block_info() {
  910. $blocks['base']['info'] = t('Tripal Stock Details');
  911. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  912. $blocks['properties']['info'] = t('Tripal Stock Properties');
  913. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  914. $blocks['references']['info'] = t('Tripal Stock References');
  915. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  916. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  917. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  918. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  919. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  920. $blocks['collections']['info'] = t('Tripal Stock Collections');
  921. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  922. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  923. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  924. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  925. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  926. $blocks['locations']['info'] = t('Tripal Stock Locations');
  927. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  928. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  929. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  930. return $blocks;
  931. }
  932. /**
  933. *
  934. * @ingroup tripal_stock
  935. */
  936. function tripal_stock_block_view($delta = '') {
  937. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  938. $nid = arg(1);
  939. $node = node_load($nid);
  940. $block = array();
  941. switch ($delta) {
  942. case 'base':
  943. $block['subject'] = t('Stock Details');
  944. $block['content'] = theme('tripal_stock_base', $node);
  945. break;
  946. case 'properties':
  947. $block['subject'] = t('Properties');
  948. $block['content'] = theme('tripal_stock_properties', $node);
  949. break;
  950. case 'references':
  951. $block['subject'] = t('References');
  952. $block['content'] = theme('tripal_stock_references', $node);
  953. break;
  954. case 'relationships':
  955. $block['subject'] = t('Relationships');
  956. $block['content'] = theme('tripal_stock_relationships', $node);
  957. break;
  958. case 'synonyms':
  959. $block['subject'] = t('Synonyms');
  960. $block['content'] = theme('tripal_stock_synonyms', $node);
  961. break;
  962. case 'collections':
  963. $block['subject'] = t('Stock Collections');
  964. $block['content'] = theme('tripal_stock_collections', $node);
  965. break;
  966. case 'phenotypes':
  967. $block['subject'] = t('Stock Phenotypes');
  968. $block['content'] = theme('tripal_stock_phenotypes', $node);
  969. break;
  970. case 'genotypes':
  971. $block['subject'] = t('Stock Genotypes');
  972. $block['content'] = theme('tripal_stock_genotypes', $node);
  973. break;
  974. case 'locations':
  975. $block['subject'] = t('Stock Locations');
  976. $block['content'] = theme('tripal_stock_locations', $node);
  977. break;
  978. case 'orgstocks':
  979. $block['subject'] = t('Organism Stocks');
  980. $block['content'] = theme('tripal_organism_stocks', $node);
  981. break;
  982. }
  983. return $block;
  984. }
  985. }
  986. /**
  987. *
  988. *
  989. * @ingroup tripal_stock
  990. */
  991. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  992. // we want to provide a new variable that contains the matched stocks.
  993. $stock = $variables['node']->stock;
  994. // normally we would use tripal_core_expand_chado_vars to expand our
  995. // organism object and add in the relationships, however whan a large
  996. // number of relationships are present this significantly slows the
  997. // query, therefore we will manually perform the query
  998. $sql = "
  999. SELECT
  1000. S.name, S.uniquename, S.stock_id, CS.nid,
  1001. CVT.name as rel_type, CVTs.name as obj_type,
  1002. SR.value
  1003. FROM {stock_relationship} SR
  1004. INNER JOIN {stock} S on SR.object_id = S.stock_id
  1005. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  1006. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  1007. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  1008. WHERE SR.subject_id = :stock_id
  1009. ";
  1010. $as_subject = chado_query($sql, array(':stock_id' => $stock->stock_id));
  1011. $sql = "
  1012. SELECT
  1013. S.name, S.uniquename, S.stock_id, CS.nid,
  1014. CVT.name as rel_type, CVTs.name as sub_type,
  1015. SR.value
  1016. FROM {stock_relationship} SR
  1017. INNER JOIN {stock} S on SR.subject_id = S.stock_id
  1018. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  1019. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  1020. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  1021. WHERE SR.object_id = :stock_id
  1022. ";
  1023. $as_object = chado_query($sql, array(':stock_id' => $stock->stock_id));
  1024. // combine both object and subject relationshisp into a single array
  1025. $relationships = array();
  1026. $relationships['object'] = array();
  1027. $relationships['subject'] = array();
  1028. // iterate through the object relationships
  1029. while ($relationship = $as_object->fetchObject()) {
  1030. // get the relationship and child types
  1031. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  1032. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  1033. if (!array_key_exists($rel_type, $relationships['object'])) {
  1034. $relationships['object'][$rel_type] = array();
  1035. }
  1036. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  1037. $relationships['object'][$rel_type][$sub_type] = array();
  1038. }
  1039. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  1040. }
  1041. // now add in the subject relationships
  1042. while ($relationship = $as_subject->fetchObject()) {
  1043. // get the relationship and child types
  1044. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  1045. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  1046. if (!array_key_exists($rel_type, $relationships['subject'])) {
  1047. $relationships['subject'][$rel_type] = array();
  1048. }
  1049. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  1050. $relationships['subject'][$rel_type][$obj_type] = array();
  1051. }
  1052. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  1053. }
  1054. $stock->all_relationships = $relationships;
  1055. }
  1056. /**
  1057. * @ingroup tripal_stock
  1058. */
  1059. function tripal_stock_node_presave($node) {
  1060. switch ($node->type) {
  1061. case 'chado_stock':
  1062. $values = array('organism_id' => $node->organism_id);
  1063. $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
  1064. $values = array('cvterm_id' => $node->type_id);
  1065. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  1066. $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  1067. break;
  1068. }
  1069. }
  1070. /**
  1071. * @ingroup tripal_stock
  1072. */
  1073. function tripal_stock_node_insert($node) {
  1074. switch ($node->type) {
  1075. case 'chado_stock':
  1076. if (!$node->stock_id) {
  1077. $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
  1078. $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  1079. $node->stock_id = $chado_stock->stock_id;
  1080. }
  1081. // remove any previous alias
  1082. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1083. // set the URL for this stock page
  1084. $url_alias = tripal_stock_get_stock_url($node);
  1085. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1086. path_save($path_alias);
  1087. break;
  1088. }
  1089. }
  1090. /**
  1091. * @ingroup tripal_stock
  1092. */
  1093. function tripal_stock_node_update($node) {
  1094. switch ($node->type) {
  1095. case 'chado_stock':
  1096. // remove any previous alias
  1097. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1098. // set the URL for this stock page
  1099. $url_alias = tripal_stock_get_stock_url($node);
  1100. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1101. path_save($path_alias);
  1102. break;
  1103. }
  1104. }
  1105. /**
  1106. * @ingroup tripal_stock
  1107. */
  1108. function tripal_stock_node_view($node, $view_mode, $langcode) {
  1109. switch ($node->type) {
  1110. case 'chado_organism':
  1111. if ($view_mode == 'full') {
  1112. // Show stock if the organism/feature is not at teaser view
  1113. $node->content['tripal_organism_stocks'] = array(
  1114. '#value' => theme('tripal_organism_stocks', $node),
  1115. );
  1116. }
  1117. break;
  1118. case 'chado_stock':
  1119. if ($view_mode == 'full') {
  1120. $node->content['tripal_stock_base'] = array(
  1121. '#value' => theme('tripal_stock_base', array('node' => $node)),
  1122. );
  1123. // Cross References
  1124. $node->content['tripal_stock_references'] = array(
  1125. '#value' => theme('tripal_stock_references', array('node' => $node)),
  1126. );
  1127. // Properties
  1128. $node->content['tripal_stock_properties'] = array(
  1129. '#value' => theme('tripal_stock_properties', array('node' => $node)),
  1130. );
  1131. // Synonyms
  1132. $node->content['tripal_stock_synonyms'] = array(
  1133. '#value' => theme('tripal_stock_synonyms', array('node' => $node)),
  1134. );
  1135. // Relationships
  1136. $node->content['tripal_stock_relationships'] = array(
  1137. '#value' => theme('tripal_stock_relationships', array('node' => $node)),
  1138. );
  1139. // Stock Collections
  1140. $node->content['tripal_stock_collections'] = array(
  1141. '#value' => theme('tripal_stock_collections', array('node' => $node)),
  1142. );
  1143. // Stock Genotypes
  1144. $node->content['tripal_stock_genotypes'] = array(
  1145. '#value' => theme('tripal_stock_genotypes', array('node' => $node)),
  1146. );
  1147. }
  1148. if ($view_mode == 'teaser') {
  1149. $node->content['tripal_stock_teaser'] = array(
  1150. '#value' => theme('tripal_stock_teaser', array('node' => $node)),
  1151. );
  1152. }
  1153. break;
  1154. }
  1155. }
  1156. /*
  1157. * Uses the value provided in the $id argument to find all stocks that match
  1158. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  1159. * stock it will redirect to that stock page, otherwise, a list of matching
  1160. * stocks is shown.
  1161. */
  1162. function tripal_stock_match_stocks_page($id) {
  1163. // if the URL alias configuration is set such that the URL
  1164. // always begins with 'stock' then we want to use the ID as it is and
  1165. // forward it on. Otherwise, try to find the matching stock.
  1166. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  1167. if (!$url_alias) {
  1168. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  1169. }
  1170. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  1171. if (preg_match('/^stock\//', $url_alias)) {
  1172. drupal_goto($id);
  1173. }
  1174. $sql = "
  1175. SELECT
  1176. S.name, S.uniquename, S.stock_id,
  1177. O.genus, O.species, O.organism_id,
  1178. CVT.cvterm_id, CVT.name as type_name,
  1179. CS.nid
  1180. FROM {stock} S
  1181. INNER JOIN {organism} O on S.organism_id = O.organism_id
  1182. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  1183. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  1184. WHERE
  1185. S.uniquename = :uname or S.name = :name
  1186. ";
  1187. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  1188. $num_matches = 0;
  1189. // iterate through the matches and build the table for showing matches
  1190. $header = array('Uniquename', 'Name', 'Type', 'Species');
  1191. $rows = array();
  1192. $curr_match;
  1193. while ($match = $results->fetchObject()) {
  1194. $curr_match = $match;
  1195. $rows[] = array(
  1196. $match->uniquename,
  1197. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  1198. $match->type_name,
  1199. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  1200. );
  1201. $num_matches++;
  1202. }
  1203. // if we have more than one match then generate the table, otherwise, redirect
  1204. // to the matched stock
  1205. if ($num_matches == 1) {
  1206. drupal_goto("node/" . $curr_match->nid);
  1207. }
  1208. if ($num_matches == 0) {
  1209. return "<p>No stocks matched the given name '$id'</p>";
  1210. }
  1211. $table_attrs = array(
  1212. 'class' => 'tripal-table tripal-table-horz'
  1213. );
  1214. $output = "<p>The following stocks match the name '$id'.</p>";
  1215. $output .= theme_table($header, $rows, $table_attrs, $caption);
  1216. return $output;
  1217. }
  1218. /**
  1219. * Implementation of hook_form_alter()
  1220. *
  1221. * @param $form
  1222. * @param $form_state
  1223. * @param $form_id
  1224. */
  1225. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  1226. // turn of preview button for insert/updates
  1227. if ($form_id == "chado_stock_node_form") {
  1228. $form['actions']['preview']['#access'] = FALSE;
  1229. }
  1230. }