<?php
require_once(get_template_directory().'/app.php');
include(__DIR__ .'/vendor/autoload.php');
//~ use WeDevs\ORM\WP\Post as Post;
//~ composer require jgrossi/corcel

function create_team_type() {
    $obj = new BaseCPT('team','Miembro del equipo');
    $obj->menu_icon = 'dashicons-id-alt';
    $obj->rewrite = array('slug' => 'equipo');
    /*dump($obj->build_cpt());*/
    register_post_type( $obj->slug, $obj->build_cpt() );
}
add_action( 'init', 'create_team_type' );

Routes::map('equipo/:slug', function($params){
    $query = '';
    Routes::load('team-member.php', $params, $query, 200);
});

Routes::map('actualidad', function($params){
    $query = '';
    Routes::load('actualidad.php', $params, $query, 200);
});
//~ Routes::map('actualidad/page/:page', function($params){
    //~ $query = '';
    //~ $params = [
        //~ 'pg' => $params['page']
    //~ ];
    //~ $query = 'posts_per_page=3&paged='.intval($params['pg']);
    //~ Routes::load('actualidad.php', $params, $query, 200);
//~ });

Routes::map('proyectos', function($params){
    $query = '';
    Routes::load('proyectos.php', $params, $query, 200);
});

// templatetag para rellenar con contenido dinamico la plantilla en desarollo
function frontpage_posts($numberposts=-1,$template_name='frontpage_post.twig') {
    $args = [
        'numberposts' => $numberposts,
        'post_type' => 'post',
    ];
    $ctx = ['posts' => Timber::get_posts( $args )];
    return Timber::fetch($template_name, $ctx);
}
add_filter( 'timber/twig', function( \Twig_Environment $twig ) {
    $twig->addFunction( new Timber\Twig_Function( 'frontpage_posts', 'frontpage_posts' ) );
    return $twig;
} );
// fin del templatetag para rellenar con contenido dinamico la plantilla en desarollo

// Galeria
function andaira_gallery($attr) {
	//~ $post = get_post();
	
	//~ static $instance = 0;
	//~ $instance++;
	
    if ( ! empty( $attr['ids'] ) ) {
        if ( empty( $attr['orderby'] ) ) {
            $attr['orderby'] = 'post__in';
        }
        $attr['include'] = $attr['ids'];
        
        $ids = explode(',', $attr['ids']);
    }
    
    $template_name='gallery.twig';
    
    $medias = array();
    
	$ids = explode(',', $attr['ids']);
	for ($x=0;$x<count($ids); $x++) {
		
		$media = new Timber\Image($ids[$x]);
		array_push($medias, $media);
 
	}
	$ctx = array('medias' => $medias);
    
    $tpl = Timber::fetch($template_name, $ctx);
    return $tpl;
}
// Fin galeria

class AndairaSite extends StarterSite{
    public $enable_ua = true;
    function __construct() {
        parent::__construct();
        $this->recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'; 
        $this->recaptcha_secret = '6Ldp7-cUAAAAALND-F6N211yRaam8jBF_W_jkzg_'; 
        $this->recaptcha_public = '6Ldp7-cUAAAAANJ68d2DomQJn-TbnUxfVPNHm95K';
        $this->CONTACT_FORM = dirname( __FILE__ ).'/templates/form_contact.html';
    }
    function add_to_context( $context ) {
        $context = parent::add_to_context($context);
        // Servicios
        $svs = array( 
            'child_of' => 27, 
            'parent ' => 27,
            'hierarchical' => 0,
            'sort_column' => 'menu_order', 
            'sort_order' => 'asc',
            'post_type'    => 'page',
            'post_status'  => 'publish',
        );
        $servicios = get_pages( $svs );
        $context['servicios'] = new Timber\PostQuery($servicios);
        //return $context;
        
        // Areas
        $areas = get_terms( [
			'taxonomy' => 'category',
			'parent' => 29,
			'hide_empty' => false,
			]
		);
        //dump($areas);
		$context['areas'] = $areas;
		
		//Logotipos
		$context['logotipo_cabecera'] = get_field('logotipo_cabecera', 'option');
		
		return $context;
    }
}

// Paginacion
$context['pagination'] = Timber::get_pagination();

// Galeria
add_shortcode('gallery', 'andaira_gallery');

$site = new AndairaSite();
//~ $site->main_menu_id = 2;
if (defined('WP_CLI') && WP_CLI) {require_once dirname(__FILE__).'/scripts/commands.php';}
?>
