tripal_stock.module 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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_references' => array(
  304. 'arguments' => array('node' => NULL),
  305. 'template' => 'tripal_stock_references',
  306. 'path' => "$theme_path/tripal_stock",
  307. ),
  308. 'tripal_stock_relationships' => array(
  309. 'arguments' => array('node' => NULL),
  310. 'template' => 'tripal_stock_relationships',
  311. 'path' => "$theme_path/tripal_stock",
  312. ),
  313. 'tripal_stock_synonyms' => array(
  314. 'arguments' => array('node' => NULL),
  315. 'template' => 'tripal_stock_synonyms',
  316. 'path' => "$theme_path/tripal_stock",
  317. ),
  318. 'tripal_stock_collections' => array(
  319. 'arguments' => array('node' => NULL),
  320. 'template' => 'tripal_stock_collections',
  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_phenotypes' => array(
  329. 'arguments' => array('node' => NULL),
  330. 'template' => 'tripal_stock_phenotypes',
  331. 'path' => "$theme_path/tripal_stock",
  332. ),
  333. 'tripal_stock_locations' => array(
  334. 'arguments' => array('node' => NULL),
  335. 'template' => 'tripal_stock_locations',
  336. 'path' => "$theme_path/tripal_stock",
  337. ),
  338. // tripal_organism templates
  339. 'tripal_organism_stocks' => array(
  340. 'arguments' => array('node' => NULL),
  341. 'template' => 'tripal_organism_stocks',
  342. 'path' => "$theme_path/tripal_organism",
  343. ),
  344. // help template
  345. 'tripal_stock_help' => array(
  346. 'template' => 'tripal_stock_help',
  347. 'arguments' => array(NULL),
  348. 'path' => $theme_path,
  349. ),
  350. );
  351. return $items;
  352. }
  353. /**
  354. * Implements hook_help()
  355. * Purpose: Adds a help page to the module list
  356. */
  357. function tripal_stock_help ($path, $arg) {
  358. if ($path == 'admin/help#tripal_stock') {
  359. return theme('tripal_stock_help', array());
  360. }
  361. }
  362. /**
  363. * @ingroup tripal_stock
  364. */
  365. function tripal_stock_block_info() {
  366. $blocks['base']['info'] = t('Tripal Stock Details');
  367. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  368. $blocks['properties']['info'] = t('Tripal Stock Properties');
  369. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  370. $blocks['references']['info'] = t('Tripal Stock References');
  371. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  372. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  373. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  374. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  375. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  376. $blocks['collections']['info'] = t('Tripal Stock Collections');
  377. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  378. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  379. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  380. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  381. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  382. $blocks['locations']['info'] = t('Tripal Stock Locations');
  383. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  384. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  385. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  386. return $blocks;
  387. }
  388. /**
  389. *
  390. * @ingroup tripal_stock
  391. */
  392. function tripal_stock_block_view($delta = '') {
  393. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  394. $nid = arg(1);
  395. $node = node_load($nid);
  396. $block = array();
  397. switch ($delta) {
  398. case 'base':
  399. $block['subject'] = t('Stock Details');
  400. $block['content'] = theme('tripal_stock_base', $node);
  401. break;
  402. case 'properties':
  403. $block['subject'] = t('Properties');
  404. $block['content'] = theme('tripal_stock_properties', $node);
  405. break;
  406. case 'references':
  407. $block['subject'] = t('References');
  408. $block['content'] = theme('tripal_stock_references', $node);
  409. break;
  410. case 'relationships':
  411. $block['subject'] = t('Relationships');
  412. $block['content'] = theme('tripal_stock_relationships', $node);
  413. break;
  414. case 'synonyms':
  415. $block['subject'] = t('Synonyms');
  416. $block['content'] = theme('tripal_stock_synonyms', $node);
  417. break;
  418. case 'collections':
  419. $block['subject'] = t('Stock Collections');
  420. $block['content'] = theme('tripal_stock_collections', $node);
  421. break;
  422. case 'phenotypes':
  423. $block['subject'] = t('Stock Phenotypes');
  424. $block['content'] = theme('tripal_stock_phenotypes', $node);
  425. break;
  426. case 'genotypes':
  427. $block['subject'] = t('Stock Genotypes');
  428. $block['content'] = theme('tripal_stock_genotypes', $node);
  429. break;
  430. case 'locations':
  431. $block['subject'] = t('Stock Locations');
  432. $block['content'] = theme('tripal_stock_locations', $node);
  433. break;
  434. case 'orgstocks':
  435. $block['subject'] = t('Organism Stocks');
  436. $block['content'] = theme('tripal_organism_stocks', $node);
  437. break;
  438. }
  439. return $block;
  440. }
  441. }
  442. /**
  443. *
  444. *
  445. * @ingroup tripal_stock
  446. */
  447. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  448. // we want to provide a new variable that contains the matched stocks.
  449. $stock = $variables['node']->stock;
  450. // normally we would use tripal_core_expand_chado_vars to expand our
  451. // organism object and add in the relationships, however whan a large
  452. // number of relationships are present this significantly slows the
  453. // query, therefore we will manually perform the query
  454. $sql = "
  455. SELECT
  456. S.name, S.uniquename, S.stock_id, CS.nid,
  457. CVT.name as rel_type, CVTs.name as obj_type,
  458. SR.value
  459. FROM {stock_relationship} SR
  460. INNER JOIN {stock} S on SR.object_id = S.stock_id
  461. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  462. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  463. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  464. WHERE SR.subject_id = :stock_id
  465. ";
  466. $as_subject = chado_query($sql, array(':stock_id' => $stock->stock_id));
  467. $sql = "
  468. SELECT
  469. S.name, S.uniquename, S.stock_id, CS.nid,
  470. CVT.name as rel_type, CVTs.name as sub_type,
  471. SR.value
  472. FROM {stock_relationship} SR
  473. INNER JOIN {stock} S on SR.subject_id = S.stock_id
  474. INNER JOIN {cvterm} CVT on SR.type_id = CVT.cvterm_id
  475. INNER JOIN {cvterm} CVTs on S.type_id = CVTs.cvterm_id
  476. LEFT JOIN public.chado_stock CS on S.stock_id = CS.stock_id
  477. WHERE SR.object_id = :stock_id
  478. ";
  479. $as_object = chado_query($sql, array(':stock_id' => $stock->stock_id));
  480. // combine both object and subject relationshisp into a single array
  481. $relationships = array();
  482. $relationships['object'] = array();
  483. $relationships['subject'] = array();
  484. // iterate through the object relationships
  485. while ($relationship = $as_object->fetchObject()) {
  486. // get the relationship and child types
  487. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  488. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  489. if (!array_key_exists($rel_type, $relationships['object'])) {
  490. $relationships['object'][$rel_type] = array();
  491. }
  492. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  493. $relationships['object'][$rel_type][$sub_type] = array();
  494. }
  495. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  496. }
  497. // now add in the subject relationships
  498. while ($relationship = $as_subject->fetchObject()) {
  499. // get the relationship and child types
  500. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  501. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  502. if (!array_key_exists($rel_type, $relationships['subject'])) {
  503. $relationships['subject'][$rel_type] = array();
  504. }
  505. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  506. $relationships['subject'][$rel_type][$obj_type] = array();
  507. }
  508. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  509. }
  510. $stock->all_relationships = $relationships;
  511. }
  512. /*
  513. * Uses the value provided in the $id argument to find all stocks that match
  514. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  515. * stock it will redirect to that stock page, otherwise, a list of matching
  516. * stocks is shown.
  517. */
  518. function tripal_stock_match_stocks_page($id) {
  519. // if the URL alias configuration is set such that the URL
  520. // always begins with 'stock' then we want to use the ID as it is and
  521. // forward it on. Otherwise, try to find the matching stock.
  522. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  523. if (!$url_alias) {
  524. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  525. }
  526. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  527. if (preg_match('/^stock\//', $url_alias)) {
  528. drupal_goto($id);
  529. }
  530. $sql = "
  531. SELECT
  532. S.name, S.uniquename, S.stock_id,
  533. O.genus, O.species, O.organism_id,
  534. CVT.cvterm_id, CVT.name as type_name,
  535. CS.nid
  536. FROM {stock} S
  537. INNER JOIN {organism} O on S.organism_id = O.organism_id
  538. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  539. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  540. WHERE
  541. S.uniquename = :uname or S.name = :name
  542. ";
  543. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  544. $num_matches = 0;
  545. // iterate through the matches and build the table for showing matches
  546. $header = array('Uniquename', 'Name', 'Type', 'Species');
  547. $rows = array();
  548. $curr_match;
  549. while ($match = $results->fetchObject()) {
  550. $curr_match = $match;
  551. $rows[] = array(
  552. $match->uniquename,
  553. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  554. $match->type_name,
  555. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  556. );
  557. $num_matches++;
  558. }
  559. // if we have more than one match then generate the table, otherwise, redirect
  560. // to the matched stock
  561. if ($num_matches == 1) {
  562. drupal_goto("node/" . $curr_match->nid);
  563. }
  564. if ($num_matches == 0) {
  565. return "<p>No stocks matched the given name '$id'</p>";
  566. }
  567. $table_attrs = array(
  568. 'class' => 'tripal-table tripal-table-horz'
  569. );
  570. $output = "<p>The following stocks match the name '$id'.</p>";
  571. $output .= theme_table($header, $rows, $table_attrs, $caption);
  572. return $output;
  573. }
  574. /**
  575. * Implementation of hook_form_alter()
  576. *
  577. * @param $form
  578. * @param $form_state
  579. * @param $form_id
  580. */
  581. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  582. // turn of preview button for insert/updates
  583. if ($form_id == "chado_stock_node_form") {
  584. $form['actions']['preview']['#access'] = FALSE;
  585. }
  586. }