tripal_stock.module 41 KB

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