Skip to content

Commit

Permalink
Prevenção de erro de backend - mensagens amigáveis (#144)
Browse files Browse the repository at this point in the history
Criação de mensagem de erro amigável ao se obter status code diferente de 200.
  • Loading branch information
paratiuid authored and patymori committed Jan 17, 2020
1 parent 28aa411 commit 294e167
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 96 deletions.
190 changes: 104 additions & 86 deletions application/controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,9 @@ private function load_blog_rss_feed()

libxml_use_internal_errors(true);
$blog_xml_var = simplexml_load_string($blog_posts, 'SimpleXMLElement', LIBXML_NOCDATA);
if($blog_xml_var === false)
if($blog_xml_var === false)
$blog_xml_var = array();
$this->load->vars('blog_posts', $blog_xml_var);
$this->load->vars('blog_posts', $blog_xml_var);

}

Expand Down Expand Up @@ -1221,7 +1221,6 @@ public function search(){
switch ($this->language) {

case SCIELO_LANG:
//$url = WORDPRESS_API_PATH."/swp_api/search?nopaging=true&s=";
$paged_url = WORDPRESS_API_PATH."/swp_api/search?posts_per_page=10&s=";
$url = WORDPRESS_API_PATH."/swp_api/search?nopaging=true&s=";
break;
Expand All @@ -1238,108 +1237,127 @@ public function search(){
}

// receive input value
$q = $this->input->get('q');

// Clear input content to avoid xss
$q = htmlspecialchars(strip_tags($q));

// Rides Json paged url and embodies to accept blank
$paged_json_url = $paged_url.rawurlencode($q);

// Rides Json url and emboda to accept blank
$json_url = $url.rawurlencode($q);

// If page number is posted, assign it in the query. $ this-> uri-> segment (3) represents page number
if($this->uri->segment(3)){
$page_num = $this->uri->segment(3);
$paged_json_url = $paged_json_url."&page=".$page_num;
}

// Receive value sent by already formatted search field
$data["query"] = $q;

// Gets json from nonpaged search
$data["json"] = $this->doSearch($q, $json_url);

// gets json from paged search
$data["paged_json"] = $this->doSearch($q, $paged_json_url);


/*
## Making pagination
*/
$this->load->library('pagination');

$scielo_url = ($this->language == SCIELO_LANG) ? base_url($this->language . '/') : base_url();

// Displays total query results for pagination creation
$search_total_rows = count($data["json"]);

// Url to assemble pagination results
$data["url"] = base_url()."home/search/";


// Display number of pages in url and not number of records displayed
$config['use_page_numbers'] = TRUE;

// Keep field values sent via get when reloading page
$config['reuse_query_string'] = TRUE;


// commented because the route was not made for this url/pt
// $config['base_url'] = $scielo_url.'home/search/';
$config['base_url'] = base_url().'home/search';
$q = $this->input->get('q');

// Clear input content to avoid xss
$q = htmlspecialchars(strip_tags($q));

// Rides Json paged url and embodies to accept blank
$paged_json_url = $paged_url.rawurlencode($q);

// Rides Json url and emboda to accept blank
$json_url = $url.rawurlencode($q);

// If page number is posted, assign it in the query. $ this-> uri-> segment (3) represents page number
if($this->uri->segment(3)){
$page_num = $this->uri->segment(3);
$paged_json_url = $paged_json_url."&page=".$page_num;
}

$config['total_rows'] = $search_total_rows;
$config['per_page'] = 10;
// Error status
$data["error"] = 0;

$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
// Receive value sent by already formatted search field
$data["query"] = $q;

$config['first_link'] = lang('pagination_first_link');
$config['last_link'] = lang('pagination_last_link');

// Gets json from nonpaged search
$data["json"] = $this->doSearch($q, $json_url);

if(!isset($data["json"]["error"])) {
$data["paged_json"] = $this->doSearch($q, $paged_json_url);

$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
if(!isset($data["paged_json"]["error"])) {
/*
## Making pagination
*/
$this->load->library('pagination');

// Displays total query results for pagination creation
if($data["json"]){
$search_total_rows = count($data["json"]);
}else{
$search_total_rows = 0;
}
// Url to assemble pagination results
$data["url"] = base_url()."home/search/";


// Display number of pages in url and not number of records displayed
$config['use_page_numbers'] = TRUE;

// Keep field values sent via get when reloading page
$config['reuse_query_string'] = TRUE;


// commented because the route was not made for this url/pt
// $config['base_url'] = $scielo_url.'home/search/';
$config['base_url'] = base_url().'home/search';

// Pagination
$config['total_rows'] = $search_total_rows;
$config['per_page'] = 10;

$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';

$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['first_link'] = lang('pagination_first_link');
$config['last_link'] = lang('pagination_last_link');

$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';

$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';

$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';

$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';

$this->pagination->initialize($config);

} else {
$data["error"] = 1;
}
} else {
$data["error"] = 1;
}

// set page title
$pageMetadata = array('acf' => array('pageTitle' => ucfirst(lang('search_btn')) . ' | SciELO.org'));
$this->PageMetadata->initialize($pageMetadata);

$this->load->view("/pages/search_results", $data);

$this->pagination->initialize($config);
$this->load->view("/pages/search_results", $data);
}
}

/**
* Internal search query
*/
public function doSearch($q, $json_url){

$rtn = array();
$q = strtolower($q);
$json = file_get_contents($json_url);
$searchResult = json_decode($json,true);
$rtn = $searchResult;

return $rtn;

}
public function doSearch($q, $json_url){
$searchResult = "";
$q = strtolower($q);

$json = @file_get_contents($json_url);

if ($json === false) {
$searchResult = array(
"error" => true
);
} else {
$searchResult = json_decode($json,true);
}

return $searchResult;

}

}
1 change: 1 addition & 0 deletions application/language/english/scielo_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
$lang['search_btn'] = 'Search';
$lang['clean_btn'] = 'Clear';
$lang["content_error"] = 'Sorry, this content is currently unavailable';
$lang["search_content_error"] = 'An error occurred while searching. Please try again later.';
$lang['with_initial_letter'] = '&nbsp;with the initial letter&nbsp;';
$lang["internal_search_placeholder"] = "Criteria, guides, announcements etc";
$lang['displaying_results_for_term'] = 'Displaying results for term ';
Expand Down
1 change: 1 addition & 0 deletions application/language/portuguese-brazilian/scielo_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
$lang['search_btn'] = 'Buscar';
$lang['clean_btn'] = 'Limpar';
$lang["content_error"] = 'Desculpe, este conteúdo está temporariamente indisponível.';
$lang["search_content_error"] = 'Ocorreu um erro durante a busca. Por favor tente novamente mais tarde.';
$lang["internal_search_placeholder"] = "Critérios, guias, comunicados etc";
$lang['displaying_results_for_term'] = 'Exibindo resultados para o termo ';
$lang['no_items_found_for_term'] = 'Nenhum item encontrado para o termo ';
Expand Down
1 change: 1 addition & 0 deletions application/language/spanish/scielo_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
$lang['search_btn'] = 'Búsqueda';
$lang['clean_btn'] = 'Limpiar';
$lang["content_error"] = 'Lo sentimos, este contenido no está disponible en este momento';
$lang["search_content_error"] = 'Ocurrió un error al buscar. Por favor intenta nuevamente más tarde.';
$lang['with_initial_letter'] = '&nbsp;con la letra inicial&nbsp;';
$lang["internal_search_placeholder"] = "Criterios, guías, anuncios, etc.";
$lang['displaying_results_for_term'] = 'Mostrando resultados para el término ';
Expand Down
32 changes: 22 additions & 10 deletions application/views/pages/search_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@

<?php $this->load->view('partials/internal-search-box'); ?>


<?php if(!$error){ ?>

<?php if($paged_json){ ?>

<?php for($i=0;$i<count($paged_json);$i++) { ?>
Expand Down Expand Up @@ -137,20 +140,30 @@

</div>
</div>
<br><br>

<?php } ?>

</div>
<?php }else{

// Show error message
?>

<script type="text/javascript">
document.getElementById("q").select();
</script>

<div class="row">
<div class="col-xs-12 col-md-6 col-md-offset-3">
<div class="card card-no-content card-internal-search">
<?= lang('search_content_error'); ?>
</div>
</div>
</div>

<?php } ?>

<!--
<div>
<nav aria-label="Page navigation example">
<ul class="pagination">
<?//= $this->pagination->create_links();?>
</ul>
</nav>
</div>
-->

<div class="container">
<div class="row">
Expand All @@ -163,7 +176,6 @@
</div>
</div>
</div>


</section>

Expand Down
16 changes: 16 additions & 0 deletions static/sass/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,22 @@ section{
transform: translate(-50%,0);
}
}

&.card-internal-search{
min-height: 28rem;
padding-top: 19rem;
margin-bottom: 3rem;
line-height: 100%;

@media (max-width: $screen-xs-max) {
padding-top: 6rem;
line-height: 1.4rem;
min-height: 1px;
padding-bottom: 2rem;
padding-left: 1rem;
padding-right: 1rem;
}
}
}
}

Expand Down

0 comments on commit 294e167

Please sign in to comment.