view admin_xml.php @ 32:4cea8b128fdd

Possibilité de choisir des templates différents en fonction du type de sortie. Le fichier templates.conf permet d'associer à chaque type de sortie (correspondant à une section du fichier INI) des templates pour le squelette, les posts et les commentaires. Il est possible de définir virtuellement une infinité de type de sorties différents, pour le moment seul de quoi obtenir un flux Atom a été ajouté. Pour changer le type de sortie, il faut passer en GET au script appellant un paramètre outputType. Si le paramètre reçu ne correspondant à aucun des types de sorties définis dans templates.conf, alors on prend du xhtml par défaut.
author Franck Deroche <webmaster@defr.org>
date Mon, 05 Nov 2007 09:06:46 +0100
parents 629389204276
children
line wrap: on
line source
<?php
    require("classes.php");

    $Req = new Requete();
    $Data = new DataAccess;

    header("Content-Type: application/xml; charset=utf-8");
    $doc = new DOMDocument('1.0', 'utf-8');
    $root = $doc->createElementNS('http://defr.org/2005/blog-post', 'post');
    $doc->appendChild($root);
    
    $mode = $Req->get('mode', 'GET', 'get');
    $type = $Req->get('type', 'GET', 'post');
    $id = $Req->get('id');
    
    $root->appendChild($doc->createElement('id', $id));
    $root->appendChild(DOMUtils::createCDATAWrapper($doc, 'debug', var_export($Req, true)));
    switch($mode . '-' . $type)
    {
	case 'edit-post':
	    $nvTitre = $Req->get('title');
	    $nvContenu = $Req->get('content');
	    $nvMood = $Req->get('mood');
	    $Data->Query
	    ("
		UPDATE Mess 
		SET Titre='{$nvTitre}', Message='{$nvContenu}', Emot='{$nvMood}'
		WHERE num_mess={$id}
	    ");
	    // Récuperation des nouveaux tags à appliquer
	    $arTags = TextUtils::splitTags($Req->get('tags'));
	    // Récuperation des anciens tags
	    $Data->Query
	    ("
		SELECT T.idTag, T.Tag
		FROM Lien_Tags_Posts L
		LEFT JOIN Tags T ON L.idTag = T.idTag
		WHERE L.idMess = {$id}
	    ");
	    
	    $oldTags = array();
	    while(0 !== ($row = $Data->GetRow()))
	    {
		$oldTags[] = $row['Tag'];
		$mapOld[$row['Tag']] = $row['idTag'];
	    }
	    // Calcul des différences
	    $tagsSupprimes = array_diff($oldTags, $arTags);
	    $tagsAjoutes = array_diff($arTags, $oldTags);
	    // Suppression des tags non affectés
	    if(is_array($tagsSupprimes))
	    {
		foreach($tagsSupprimes as $tagSuppr)
		{
		    $supprIDs .= $mapOld[$tagSuppr] . ',';
		}
		$Data->Query
		("
		    DELETE FROM Lien_Tags_Posts 
		    WHERE idMess={$id} AND idTag IN ({$supprIDs}0)
		");
	    }
	    // Ajout des tags ajoutés
	    if(is_array($tagsAjoutes))
	    {
		foreach($tagsAjoutes as $tagAj)
		{
		    $n = $Data->Query("SELECT idTag FROM Tags WHERE Tag='{$tagAj}'");
		    if($n ==  0)
		    {
			// Si le tag n'existe pas, on le crée
			$Data->Query("INSERT INTO Tags SET Tag='{$tagAj}'");
			$Data->Query("SELECT idTag FROM Tags WHERE Tag='{$tagAj}'");
		    }
		    $row = $Data->GetRow();
		    $Data->Query
		    ("
			INSERT INTO Lien_Tags_Posts 
			SET idMess={$id}, idTag={$row['idTag']}
		    ");
		}
	    }
	case 'get-post':
	    $Data->Query
	    ("
		SELECT Titre, Message, Emot
		FROM Mess
		WHERE num_mess={$id}
	    ");
	    $row = $Data->GetRow();
	    $titre_CDATA = $doc->createCDATASection($row['Titre']);
	    $root->appendChild(DOMUtils::createCDATAWrapper($doc, 'titre', $row['Titre']));
	    $root->appendChild(DOMUtils::createCDATAWrapper($doc, 'contenu', $row['Message']));
	    $root->appendChild($doc->createElement('mood', $row['Emot']));
	    // Obtention des tags
	    $Data->Query
	    ("
		SELECT T.idTag, T.Tag
		FROM Lien_Tags_Posts L
		LEFT JOIN Tags T ON L.idTag = T.idTag
		WHERE L.idMess = {$id}
	    ");
	    while(0 !== ($row = $Data->GetRow()))
		$root->appendChild($doc->createElement('tag', $row['Tag']));
	    break;
	case 'edit-brouillon':
	    $nvTitre = $Req->get('title');
	    $nvContenu = $Req->get('content');
	    $Data->Query
	    ("
		UPDATE Brouillons
		SET Titre='{$nvTitre}', Contenu='{$nvContenu}'
		WHERE id_brouillon={$id}
	    ");
	case 'get-brouillon':
	    $Data->Query
	    ("
		SELECT Titre, Contenu
		FROM Brouillons
		WHERE id_brouillon={$id}
	    ");
	    $row = $Data->GetRow();
	    $root->appendChild(DOMUtils::createCDATAWrapper($doc, 'titre', $row['Titre']));
	    $root->appendChild(DOMUtils::createCDATAWrapper($doc, 'contenu', $row['Contenu']));
	    break;
    }
    echo $doc->saveXML();
?>