tripal_stock.module 41 KB

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