tripal_stock.module 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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("includes/tripal_stock.chado_node.inc");
  30. require_once("api/tripal_stock.api.inc");
  31. /**
  32. * Implements hook_menu(): Adds menu items for the tripal_stock
  33. *
  34. * @return
  35. * Menu definitions for the tripal_stock
  36. *
  37. * @ingroup tripal_stock
  38. */
  39. function tripal_stock_menu() {
  40. $items = array();
  41. //Administrative settings menu-----------------
  42. $items['admin/tripal/chado/tripal_stock'] = array(
  43. 'title' => 'Stocks',
  44. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  45. 'page callback' => 'tripal_stock_admin_stock_view',
  46. 'access arguments' => array('administer tripal stocks'),
  47. 'type' => MENU_NORMAL_ITEM
  48. );
  49. $items['admin/tripal/chado/tripal_stock/configuration'] = array(
  50. 'title' => 'Settings',
  51. 'description' => 'Settings for Chado Stocks',
  52. 'page callback' => 'drupal_get_form',
  53. 'page arguments' => array('tripal_stock_admin'),
  54. 'access arguments' => array('administer tripal stocks'),
  55. 'type' => MENU_LOCAL_TASK,
  56. 'weight' => 5
  57. );
  58. $items['admin/tripal/chado/tripal_stock/sync'] = array(
  59. 'title' => ' Sync',
  60. 'description' => 'Sync stocks from Chado with Drupal',
  61. 'page callback' => 'drupal_get_form',
  62. //'page arguments' => array('tripal_stock_sync_form'),
  63. 'page arguments' => array('tripal_core_chado_node_sync_form', 'tripal_stock', 'chado_stock'),
  64. 'access arguments' => array('administer tripal stocks'),
  65. 'type' => MENU_LOCAL_TASK,
  66. 'weight' => 0
  67. );
  68. $items['admin/tripal/chado/tripal_stock/help'] = array(
  69. 'title' => 'Help',
  70. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  71. 'page callback' => 'theme',
  72. 'page arguments' => array('tripal_stock_help'),
  73. 'access arguments' => array('administer tripal stocks'),
  74. 'type' => MENU_LOCAL_TASK,
  75. 'weight' => 10
  76. );
  77. // Adding Secondary Properties-----------------
  78. $items['node/%cs_node/properties'] = array(
  79. 'title' => 'Add Properties & Synonyms',
  80. 'description' => 'Settings for Chado Stocks',
  81. 'page callback' => 'tripal_stock_add_ALL_property_page',
  82. 'page arguments' => array(1),
  83. 'access arguments' => array('create chado_stock content'),
  84. 'type' => MENU_CALLBACK
  85. );
  86. $items['node/%cs_node/db_references'] = array(
  87. 'title' => 'Add Database References',
  88. 'description' => 'Settings for Chado Stocks',
  89. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  90. 'page arguments' => array(1),
  91. 'access arguments' => array('create chado_stock content'),
  92. 'type' => MENU_CALLBACK
  93. );
  94. $items['node/%cs_node/relationships'] = array(
  95. 'title' => 'Add Relationships',
  96. 'description' => 'Settings for Chado Stocks',
  97. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  98. 'page arguments' => array(1),
  99. 'access arguments' => array('create chado_stock content'),
  100. 'type' => MENU_CALLBACK
  101. );
  102. //Edit/Deleting Secondary Properties-------------
  103. $items['node/%cs_node/edit_properties'] = array(
  104. 'title' => 'Edit Properties',
  105. 'description' => 'Settings for Chado Stocks',
  106. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  107. 'page arguments' => array(1),
  108. 'access arguments' => array('edit chado_stock content'),
  109. 'type' => MENU_LOCAL_TASK,
  110. 'weight' => 8,
  111. );
  112. $items['node/%cs_node/edit_relationships'] = array(
  113. 'title' => 'Edit Relationships',
  114. 'description' => 'Settings for Chado Stocks',
  115. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  116. 'page arguments' => array(1),
  117. 'access arguments' => array('edit chado_stock content'),
  118. 'type' => MENU_LOCAL_TASK,
  119. 'weight' => 9,
  120. );
  121. $items['node/%cs_node/edit_db_references'] = array(
  122. 'title' => 'Edit DB References',
  123. 'description' => 'Settings for Chado Stocks',
  124. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  125. 'page arguments' => array(1),
  126. 'access arguments' => array('edit chado_stock content'),
  127. 'type' => MENU_LOCAL_TASK,
  128. 'weight' => 10,
  129. );
  130. // the menu link for addressing any stock (by name, uniquename, synonym)
  131. $items['stock/%'] = array(
  132. 'page callback' => 'tripal_stock_match_stocks_page',
  133. 'page arguments' => array(1),
  134. 'access arguments' => array('access chado_stock content'),
  135. 'type' => MENU_LOCAL_TASK,
  136. );
  137. return $items;
  138. }
  139. /**
  140. * Implements Menu wildcard_load hook
  141. *
  142. * Purpose: Allows the node ID of a chado stock to be dynamically
  143. * pulled from the path. The node is loaded from this node ID
  144. * and supplied to the page as an arguement. This is an example
  145. * of dynamic argument replacement using wildcards in the path.
  146. *
  147. * @param $nid
  148. * The node ID passed in from the path
  149. *
  150. * @return
  151. * The node object with the passed in nid
  152. *
  153. * @ingroup tripal_stock
  154. */
  155. function cs_node_load($nid) {
  156. if (is_numeric($nid)) {
  157. $node = node_load($nid);
  158. if ($node->type == 'chado_stock') {
  159. return $node;
  160. }
  161. }
  162. return FALSE;
  163. }
  164. /**
  165. * Implementation of hook_perm().
  166. *
  167. * Purpose: Set the permission types that the chado stock module uses
  168. *
  169. * @return
  170. * Listing of the possible permission catagories
  171. *
  172. * @ingroup tripal_stock
  173. */
  174. function tripal_stock_permissions() {
  175. return array(
  176. 'access chado_stock content' => array(
  177. 'title' => t('View Stocks'),
  178. 'description' => t('Allow users to view stock pages.'),
  179. ),
  180. 'create chado_stock content' => array(
  181. 'title' => t('Create Stocks'),
  182. 'description' => t('Allow users to create new stock pages.'),
  183. ),
  184. 'delete chado_stock content' => array(
  185. 'title' => t('Delete Stocks'),
  186. 'description' => t('Allow users to delete stock pages.'),
  187. ),
  188. 'edit chado_stock content' => array(
  189. 'title' => t('Edit Stocks'),
  190. 'description' => t('Allow users to edit stock pages.'),
  191. ),
  192. 'adminster tripal stock' => array(
  193. 'title' => t('Administer Stocks'),
  194. 'description' => t('Allow users to administer all stocks.'),
  195. ),
  196. );
  197. }
  198. /**
  199. * Implement hook_access().
  200. *
  201. * This hook allows node modules to limit access to the node types they define.
  202. *
  203. * @param $node
  204. * The node on which the operation is to be performed, or, if it does not yet exist, the
  205. * type of node to be created
  206. *
  207. * @param $op
  208. * The operation to be performed
  209. *
  210. * @param $account
  211. * A user object representing the user for whom the operation is to be performed
  212. *
  213. * @return
  214. * If the permission for the specified operation is not set then return FALSE. If the
  215. * permission is set then return NULL as this allows other modules to disable
  216. * access. The only exception is when the $op == 'create'. We will always
  217. * return TRUE if the permission is set.
  218. *
  219. * @ingroup tripal_stock
  220. */
  221. function chado_stock_node_access($node, $op, $account) {
  222. if ($op == 'create') {
  223. if (!user_access('create chado_stock content', $account)) {
  224. return FALSE;
  225. }
  226. return TRUE;
  227. }
  228. if ($op == 'update') {
  229. if (!user_access('edit chado_stock content', $account)) {
  230. return FALSE;
  231. }
  232. }
  233. if ($op == 'delete') {
  234. if (!user_access('delete chado_stock content', $account)) {
  235. return FALSE;
  236. }
  237. }
  238. if ($op == 'view') {
  239. if (!user_access('access chado_stock content', $account)) {
  240. return FALSE;
  241. }
  242. }
  243. return NULL;
  244. }
  245. /**
  246. * Implements hook_views_api()
  247. *
  248. * Purpose: Essentially this hook tells drupal that there is views support for
  249. * for this module which then includes tripal_stock.views.inc where all the
  250. * views integration code is
  251. *
  252. * @return
  253. * An array with fields important for views integration
  254. *
  255. * @ingroup tripal_stock
  256. */
  257. function tripal_stock_views_api() {
  258. return array(
  259. 'api' => 2.0,
  260. );
  261. }
  262. /**
  263. * Implements hook_theme(): Register themeing functions for this module
  264. *
  265. * @return
  266. * An array of themeing functions to register
  267. *
  268. * @ingroup tripal_stock
  269. */
  270. function tripal_stock_theme() {
  271. $theme_path = drupal_get_path('module', 'tripal_stock') . '/theme';
  272. $items = array(
  273. // property edit forms function templates
  274. 'tripal_stock_edit_ALL_properties_form' => array(
  275. 'arguments' => array('form'),
  276. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  277. ),
  278. 'tripal_stock_edit_ALL_db_references_form' => array(
  279. 'arguments' => array('form'),
  280. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  281. ),
  282. 'tripal_stock_edit_ALL_relationships_form' => array(
  283. 'arguments' => array('form'),
  284. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  285. ),
  286. // tripal_stock templates
  287. 'node__chado_stock' => array(
  288. 'template' => 'node--chado-generic',
  289. 'path' => drupal_get_path('module', 'tripal_core') . '/theme',
  290. 'render element' => 'node',
  291. 'base hook' => 'node',
  292. ),
  293. 'tripal_stock_base' => array(
  294. 'arguments' => array('node' => NULL),
  295. 'template' => 'tripal_stock_base',
  296. 'path' => "$theme_path/tripal_stock",
  297. ),
  298. 'tripal_stock_properties' => array(
  299. 'arguments' => array('node' => NULL),
  300. 'template' => 'tripal_stock_properties',
  301. 'path' => "$theme_path/tripal_stock",
  302. ),
  303. 'tripal_stock_publications' => array(
  304. 'arguments' => array('node' => NULL),
  305. 'template' => 'tripal_stock.publications',
  306. 'path' => "$theme_path/tripal_stock",
  307. ),
  308. 'tripal_stock_references' => array(
  309. 'arguments' => array('node' => NULL),
  310. 'template' => 'tripal_stock_references',
  311. 'path' => "$theme_path/tripal_stock",
  312. ),
  313. 'tripal_stock_relationships' => array(
  314. 'arguments' => array('node' => NULL),
  315. 'template' => 'tripal_stock_relationships',
  316. 'path' => "$theme_path/tripal_stock",
  317. ),
  318. 'tripal_stock_synonyms' => array(
  319. 'arguments' => array('node' => NULL),
  320. 'template' => 'tripal_stock_synonyms',
  321. 'path' => "$theme_path/tripal_stock",
  322. ),
  323. 'tripal_stock_collections' => array(
  324. 'arguments' => array('node' => NULL),
  325. 'template' => 'tripal_stock_collections',
  326. 'path' => "$theme_path/tripal_stock",
  327. ),
  328. 'tripal_stock_collections' => array(
  329. 'arguments' => array('node' => NULL),
  330. 'template' => 'tripal_stock_collections',
  331. 'path' => "$theme_path/tripal_stock",
  332. ),
  333. 'tripal_stock_phenotypes' => array(
  334. 'arguments' => array('node' => NULL),
  335. 'template' => 'tripal_stock_phenotypes',
  336. 'path' => "$theme_path/tripal_stock",
  337. ),
  338. 'tripal_stock_locations' => array(
  339. 'arguments' => array('node' => NULL),
  340. 'template' => 'tripal_stock_locations',
  341. 'path' => "$theme_path/tripal_stock",
  342. ),
  343. // tripal_organism templates
  344. 'tripal_organism_stocks' => array(
  345. 'arguments' => array('node' => NULL),
  346. 'template' => 'tripal_organism_stocks',
  347. 'path' => "$theme_path/tripal_organism",
  348. ),
  349. // help template
  350. 'tripal_stock_help' => array(
  351. 'template' => 'tripal_stock_help',
  352. 'arguments' => array(NULL),
  353. 'path' => $theme_path,
  354. ),
  355. );
  356. return $items;
  357. }
  358. /**
  359. * Implements hook_help()
  360. * Purpose: Adds a help page to the module list
  361. */
  362. function tripal_stock_help ($path, $arg) {
  363. if ($path == 'admin/help#tripal_stock') {
  364. return theme('tripal_stock_help', array());
  365. }
  366. }
  367. /**
  368. * @ingroup tripal_stock
  369. */
  370. function tripal_stock_block_info() {
  371. $blocks['base']['info'] = t('Tripal Stock Details');
  372. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  373. $blocks['properties']['info'] = t('Tripal Stock Properties');
  374. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  375. $blocks['references']['info'] = t('Tripal Stock References');
  376. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  377. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  378. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  379. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  380. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  381. $blocks['collections']['info'] = t('Tripal Stock Collections');
  382. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  383. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  384. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  385. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  386. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  387. $blocks['locations']['info'] = t('Tripal Stock Locations');
  388. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  389. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  390. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  391. return $blocks;
  392. }
  393. /**
  394. *
  395. * @ingroup tripal_stock
  396. */
  397. function tripal_stock_block_view($delta = '') {
  398. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  399. $nid = arg(1);
  400. $node = node_load($nid);
  401. $block = array();
  402. switch ($delta) {
  403. case 'base':
  404. $block['subject'] = t('Stock Details');
  405. $block['content'] = theme('tripal_stock_base', $node);
  406. break;
  407. case 'properties':
  408. $block['subject'] = t('Properties');
  409. $block['content'] = theme('tripal_stock_properties', $node);
  410. break;
  411. case 'references':
  412. $block['subject'] = t('References');
  413. $block['content'] = theme('tripal_stock_references', $node);
  414. break;
  415. case 'relationships':
  416. $block['subject'] = t('Relationships');
  417. $block['content'] = theme('tripal_stock_relationships', $node);
  418. break;
  419. case 'synonyms':
  420. $block['subject'] = t('Synonyms');
  421. $block['content'] = theme('tripal_stock_synonyms', $node);
  422. break;
  423. case 'collections':
  424. $block['subject'] = t('Stock Collections');
  425. $block['content'] = theme('tripal_stock_collections', $node);
  426. break;
  427. case 'phenotypes':
  428. $block['subject'] = t('Stock Phenotypes');
  429. $block['content'] = theme('tripal_stock_phenotypes', $node);
  430. break;
  431. case 'genotypes':
  432. $block['subject'] = t('Stock Genotypes');
  433. $block['content'] = theme('tripal_stock_genotypes', $node);
  434. break;
  435. case 'locations':
  436. $block['subject'] = t('Stock Locations');
  437. $block['content'] = theme('tripal_stock_locations', $node);
  438. break;
  439. case 'orgstocks':
  440. $block['subject'] = t('Organism Stocks');
  441. $block['content'] = theme('tripal_organism_stocks', $node);
  442. break;
  443. }
  444. return $block;
  445. }
  446. }
  447. /**
  448. *
  449. *
  450. * @ingroup tripal_stock
  451. */
  452. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  453. // we want to provide a new variable that contains the matched stocks.
  454. $stock = $variables['node']->stock;
  455. // normally we would use tripal_core_expand_chado_vars to expand our
  456. // organism object and add in the relationships, however whan a large
  457. // number of relationships are present this significantly slows the
  458. // query, therefore we will manually perform the query
  459. $sql = "
  460. SELECT
  461. S.name, S.uniquename, S.stock_id, CS.nid,
  462. CVT.name as rel_type, CVTs.name as obj_type,
  463. SR.value
  464. FROM {stock_relationship} SR
  465. INNER JOIN {stock} S on SR.object_id = S.stock_id
  466. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  467. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  468. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  469. WHERE SR.subject_id = :stock_id
  470. ";
  471. $as_subject = chado_query($sql, array(':stock_id' => $stock->stock_id));
  472. $sql = "
  473. SELECT
  474. S.name, S.uniquename, S.stock_id, CS.nid,
  475. CVT.name as rel_type, CVTs.name as sub_type,
  476. SR.value
  477. FROM {stock_relationship} SR
  478. INNER JOIN {stock} S on SR.subject_id = S.stock_id
  479. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  480. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  481. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  482. WHERE SR.object_id = :stock_id
  483. ";
  484. $as_object = chado_query($sql, array(':stock_id' => $stock->stock_id));
  485. // combine both object and subject relationshisp into a single array
  486. $relationships = array();
  487. $relationships['object'] = array();
  488. $relationships['subject'] = array();
  489. // iterate through the object relationships
  490. while ($relationship = $as_object->fetchObject()) {
  491. // get the relationship and child types
  492. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  493. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  494. if (!array_key_exists($rel_type, $relationships['object'])) {
  495. $relationships['object'][$rel_type] = array();
  496. }
  497. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  498. $relationships['object'][$rel_type][$sub_type] = array();
  499. }
  500. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  501. }
  502. // now add in the subject relationships
  503. while ($relationship = $as_subject->fetchObject()) {
  504. // get the relationship and child types
  505. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  506. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  507. if (!array_key_exists($rel_type, $relationships['subject'])) {
  508. $relationships['subject'][$rel_type] = array();
  509. }
  510. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  511. $relationships['subject'][$rel_type][$obj_type] = array();
  512. }
  513. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  514. }
  515. $stock->all_relationships = $relationships;
  516. }
  517. /*
  518. * Uses the value provided in the $id argument to find all stocks that match
  519. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  520. * stock it will redirect to that stock page, otherwise, a list of matching
  521. * stocks is shown.
  522. */
  523. function tripal_stock_match_stocks_page($id) {
  524. // if the URL alias configuration is set such that the URL
  525. // always begins with 'stock' then we want to use the ID as it is and
  526. // forward it on. Otherwise, try to find the matching stock.
  527. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  528. if (!$url_alias) {
  529. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  530. }
  531. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  532. if (preg_match('/^stock\//', $url_alias)) {
  533. drupal_goto($id);
  534. }
  535. $sql = "
  536. SELECT
  537. S.name, S.uniquename, S.stock_id,
  538. O.genus, O.species, O.organism_id,
  539. CVT.cvterm_id, CVT.name as type_name,
  540. CS.nid
  541. FROM {stock} S
  542. INNER JOIN {organism} O on S.organism_id = O.organism_id
  543. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  544. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  545. WHERE
  546. S.uniquename = :uname or S.name = :name
  547. ";
  548. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  549. $num_matches = 0;
  550. // iterate through the matches and build the table for showing matches
  551. $header = array('Uniquename', 'Name', 'Type', 'Species');
  552. $rows = array();
  553. $curr_match;
  554. while ($match = $results->fetchObject()) {
  555. $curr_match = $match;
  556. $rows[] = array(
  557. $match->uniquename,
  558. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  559. $match->type_name,
  560. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  561. );
  562. $num_matches++;
  563. }
  564. // if we have more than one match then generate the table, otherwise, redirect
  565. // to the matched stock
  566. if ($num_matches == 1) {
  567. drupal_goto("node/" . $curr_match->nid);
  568. }
  569. if ($num_matches == 0) {
  570. return "<p>No stocks matched the given name '$id'</p>";
  571. }
  572. $table_attrs = array(
  573. 'class' => 'tripal-table tripal-table-horz'
  574. );
  575. $output = "<p>The following stocks match the name '$id'.</p>";
  576. $output .= theme_table($header, $rows, $table_attrs, $caption);
  577. return $output;
  578. }
  579. /**
  580. * Implementation of hook_form_alter()
  581. *
  582. * @param $form
  583. * @param $form_state
  584. * @param $form_id
  585. */
  586. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  587. // turn of preview button for insert/updates
  588. if ($form_id == "chado_stock_node_form") {
  589. $form['actions']['preview']['#access'] = FALSE;
  590. }
  591. }