<?php
namespace Grav\Plugin;

use Grav\Common\Data;
use Grav\Common\Plugin;
use Grav\Common\Grav;
use Grav\Common\Uri;
use Grav\Common\Taxonomy;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;

class twigDisplay extends \Twig_Extension
{
     public function getName()
    {
        return 'ExampleTwigExtension';
    }
    public function getFunctions(): array
    {
        return [
            new \Twig_SimpleFunction('getJson', [$this, 'getJson'])
        ];
    }
    public function getJson()
    {
        $grav = Grav::instance();
        $debugger = $grav['debugger'];
       

        
        $page = $grav['page'];
        $pages = $page->evaluate(['@page.children' => '/coalitions']);

        // initialiser l'objet
        $data = array();
        $i = 0;
        $a = 0;
        foreach ($pages as $key => $coa) {
            $header = $coa->header();
            $url = $coa->url();
            /*dump(st'01/01/'+$header->info['date']))*/

            if(isset($header->member)){
                
                $coa_object = array(
                    'lane'  => $i,
                    'title' => $coa->title(),
                    'coa_name' => $header->info['orga'],
                    'start' => strtotime('01/01/'.$header->info['date'])*1000,
                    'end' => strtotime(date("Y"))*1000, 
                    'activist' => array()
                );
            if (isset($header->member)) {
                foreach ($header->member as $member) {
                    if (strtotime($member["free"]) != "") {
                        $f = strtotime($member["free"]);
                    }else{
                        $f = strtotime(date("Y"));
                    }
                    $member_array = array(
                        'id' => $member["membre"],
                        'start' => strtotime($member['prison'])*1000,
                        'end' => $f*1000,
                        'url' =>  $url,
                        'lane' => $i,
                        'pos' => $a
                    );
                    array_push($coa_object['activist'], $member_array);
                    $a ++;
                }

            }
            array_push($data, $coa_object);
            
            $i ++;
                
            
            
            /*$debugger->addMessage($coa->header());*/
           
            }
        }

       


        return $data;
    }
   
}
