Skip to content

Commit

Permalink
Implement LocalizedStringsGetCommand
Browse files Browse the repository at this point in the history
Fixes #26890

Signed-off-by: Victor Boctor <victor@mantishub.net>
  • Loading branch information
vboctor committed Apr 26, 2020
1 parent 29e0d6d commit e87fcc4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
25 changes: 8 additions & 17 deletions api/rest/restcore/lang_rest.php
Expand Up @@ -46,23 +46,14 @@
* @return \Slim\Http\Response The augmented response.
*/
function rest_lang_get( \Slim\Http\Request $p_request, \Slim\Http\Response $p_response, array $p_args ) {
$t_strings = $p_request->getParam( 'string' );
if( !is_array( $t_strings ) ) {
$t_strings = array( $t_strings );
}

$t_current_language = lang_get_current();
$t_localized_strings = array();
foreach( $t_strings as $t_string ) {
if( !lang_exists( $t_string, $t_current_language) ) {
continue;
}

$t_localized_strings[] = array( 'name' => $t_string, 'localized' => lang_get( $t_string ) );
}

$t_result = array( 'strings' => $t_localized_strings );
$t_result['language'] = $t_current_language;
$t_data = array(
'query' => array(
'string' => $p_request->getParam( 'string' )
)
);

$t_command = new LocalizedStringsGetCommand( $t_data );
$t_result = $t_command->execute();

return $p_response->withStatus( HTTP_STATUS_SUCCESS )->withJson( $t_result );
}
64 changes: 64 additions & 0 deletions core/commands/LocalizedStringsGetCommand.php
@@ -0,0 +1,64 @@
<?php
# MantisBT - A PHP based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.

/**
* A command that gets a set of localized strings.
*/
class LocalizedStringsGetCommand extends Command {
/**
* Constructor
*
* @param array $p_data The command data.
*/
function __construct( array $p_data ) {
parent::__construct( $p_data );
}

/**
* Validate the data.
*/
function validate() {
}

/**
* Process the command.
*
* @returns array Command response
*/
protected function process() {
$t_strings = $this->query( 'string' );
if( !is_array( $t_strings ) ) {
$t_strings = array( $t_strings );
}

$t_current_language = lang_get_current();
$t_localized_strings = array();

foreach( $t_strings as $t_string ) {
if( !lang_exists( $t_string, $t_current_language ) ) {
continue;
}

$t_localized_strings[] = array( 'name' => $t_string, 'localized' => lang_get( $t_string ) );
}

return array(
'strings' => $t_localized_strings,
'language' => $t_current_language,
);
}
}

0 comments on commit e87fcc4

Please sign in to comment.