<?php
class PageFmSearch extends CDController{

  public function __construct() {
    $this->view = 'fm/page/search.twig';
  }

  public function get_context(){

    $this->data = CDTimber::get_context();

    $this->data['post']->hero_lead = 'Resultados para:';

    $this->data['post']->hero_title = $_GET['query'];

    if(isset($_GET['category'])){

      $current_category = new Timber\Term($_GET['category']);

      if($current_category->ID && $current_category->taxonomy == 'fm_magazine_cat'){
        $this->data['post']->hero_subtitle = 'En: '.$current_category->name;
      }

    }

    $current_page = get_query_var('paged');

    if(!$current_page){
      $current_page = 1;
    }

    $args = array(
      'post_type' => 'fm_magazine',
      's' => $_GET['query'],
      'posts_per_page' => 8,
      'post_parent__not_in' => array(0),
      'paged' => $current_page
    );

    $results = new Timber\PostQuery($args);

    $this->data['results'] = $results->get_posts();

  }

}
