<?php
// charger le fichier de settings général
$collection=new yamlReadwriter();

$collection_url=$_POST['source']; 
$collection->loadYaml(getTemplate("structure_collection.yaml")); // charger la structure complète

// charger le contenu de l'item
if($collection_url){
    $collection->loadContentFromYaml(CONTENTS."collections/".$collection_url);
}
$collection->setType("collection"); // important
$collection->setSnippetFolder(APP."form_parts/");

switch($_POST['action']){
    case "loadform":
        $menu_edit= array();
        include(APP."snippets/menu-viewer.php");

        $collection->addHidden("action","submitform");
        $collection->addHidden("destination","#collections-container");         

        $collection->addHidden("callback","yc_filldestination");         
        $collection->addHidden("endaction","yc_closeViewer");         

        if(!$collection_url){
            $collection->form_title="Créer une collection";
            $collection->form_submit="Créer";
        } else {
            $collection->form_title="Modifier un collection";
            $collection->form_submit="Modifier";
            $collection->form_delete=true;
        }

        echo "<div class='generic-viewercontent'>";
        $collection->writeForm();
        echo "</div>";        

        break;


    case "loadcollection":
        foreach($collection->getAsarray('items') as $num => $val){
            $retour[] = array(
                'id' => $val
            );
        }
        echo json_encode($retour, JSON_HEX_QUOT | JSON_HEX_TAG);
        break;

    case "submitform":
        $collection->setDestinationFolder(CONTENTS."collections/");
        $collection_url=$collection->processPost(true);
        $snippet=getSnippetFile("tool_collections.php");
        include($snippet);
        break;

    case "deleteelem":
        //$collection->setDestinationFolder(CONTENTS."collections/");

        $collection->deleteyaml();

        $snippet=getSnippetFile("tool_collections.php");
        include($snippet);

        break;        

}