<?php
// charger le fichier de settings général
$item=new yamlReadwriter();
$item_url=$_POST['source']; 
$item->loadYaml(getTemplate("structure_item.yaml")); // charger la structure complète

// charger le contenu de l'item
$item->loadContentFromYaml(CONTENTS."items/".$item_url);
$item->setType("item"); // important
$item->setSnippetFolder(APP."form_parts/");

switch($_POST['action']){
    case "gethtml":
        $snippet=$_POST['snippet'];
        $menu_edit= array("arrow","edit","viewmode");
        if($snippet=="fullwindow"){ $menu_edit[]="iteminfo"; }
        include(APP."snippets/menu-viewer.php");        
        $item->contentDeleteField('date_creation');
        $snippet= getSnippetFile("display_item_".$snippet.".php");
        include($snippet);
        break; 

    case "loadform":
        $menu_edit= array();
        include(APP."snippets/menu-viewer.php");
        
        $item->addHidden("action","submitform");
        $item->addHidden("callback","yc_updategriditems"); 
        $item->addHidden("endaction","yc_closeViewer"); 
        
        if(!$item_url){
            $item->form_title="Créer un item";
            $item->form_submit="Créer";
        } else {
            $item->form_title="Modifier un item";
            $item->form_submit="Modifier";
            $item->form_delete=true;
        }

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

        break;

    case "submitform":
        $item->setDestinationFolder(CONTENTS."items/");
        $item_url=$item->processPost(true);

        ob_start(); // Start the output buffer
        $snippet=getSnippetFile("display_item_mosaique.php");
        include($snippet);
        $html=ob_get_contents();
        ob_end_clean();

        supprimer_cache();
        $retour= array( 0 => array(
            'id' => $item->filename,
            'html' => $html
        )
                      );
        echo json_encode($retour, JSON_HEX_QUOT | JSON_HEX_TAG);
        break;

    case "deleteelem":
        $item->setDestinationFolder(CONTENTS."items/");
        $item->deleteyaml();
        $retour= array( 0 => array(
            'id' => $item->filename,
        )
                      );
        echo json_encode($retour, JSON_HEX_QUOT | JSON_HEX_TAG);

        supprimer_cache();

        break;
}
