0251 / 590 837 15
info@a-coding-project.de
;

Templates Dateisystem (CMS)

Auch im Dateisystem des eigenen CMSwerden heute Templates eingeführt. Für diesen Beitrag benötigt ihr wieder die neueste Sprachdatei, die in das Verzeichnis /system/language gehört.

Als erstes legen wir mit der Dateiauflistung los /system/templates/files.html:

<h1>{LANG:FILES}</h1>
<form enctype="multipart/form-data" action="{VAR:UPLOADURL}" method="post">
<input type="hidden" name="dir" value="{VAR:DIR}" />
{LANG:FILE}: <input type="file" name="file"><br />
<input type="submit" value="{LANG:UPLOAD_FILE}">
</form>
<p>{VAR:MESSAGE}</p>
<form action="{VAR:NEWFOLDERURL}" method="POST">
  <p>
    {LANG:PATH}:
    {LOOP:PATH}
      <a href="{VAR:URL}">{VAR:LABEL}</a>
    {/LOOP:PATH}
    <input name="name" style="width:100px;" />
    <input name="newFolder" type="submit" value="{LANG:NEW}" />
  </p>
</form>
<ul style="list-style-type:none;">
  {LOOP:DIRS}
    <li>
      <img src="{ICON:FOLDER}" />
      <a href="{VAR:URL}">{VAR:LABEL}</a>
    </li>
  {/LOOP:DIRS}
</ul>
{VAR:DELETEFOLDERLINK}
{IF:FILES_EXIST}
  <table>
    <thead>
      <td>{LANG:PREVIEW}</td>
      <td>{LANG:FILENAME}</td>
      <td>{LANG:ACTIONS}</td>
    </thead>
    <tbody>
      {LOOP:FILES}
        <tr>
          <td>{VAR:IMAGE}</td>
          <td>{VAR:FILELINK}</td>
          <td>
            <a href="{VAR:DELETEURL}">
              <img src="{ICON:CROSS}" />
            </a>
          </td>
        </tr>
      {/LOOP:FILES}
    </tbody>
  </table>
{/IF:FILES_EXIST}
{IF:NOFILES}
  <p>{LANG:NO_FILES}</p>
{/IF:NOFILES}

Die Datei /admin/includes/files.phpdie das ganze befüllt sieht dann so aus:

<?PHP
  $template = new Template();
  $template->load("files","..");
  $template->assign_var("UPLOADURL","/admin/index.php?page=upload");
  $template->assign_var("DIR",htmlentities($_GET['dir']));
  $template->assign_var("NEWFOLDERURL","/admin/index.php?page=files&dir=".urlencode($_GET['dir']));
  
  $dir = $_GET['dir'];
  if($_POST['newFolder']){
    FileServer::createFolder("../content/uploads/".$dir,$_POST['name']);
  }
  if($_GET['unlink']){
    unlink("../content/uploads/".$_GET['dir']."/".$_GET['unlink']);
    $template->assign_var("MESSAGE",$language->getString("FILE")." ".htmlentities($_GET['unlink'])." ".$language->getString("WAS_DELETED"));
  }
  if($_GET['rmdir']){
    rmdir("../content/uploads/".$_GET['rmdir']);
    $template->assign_var("MESSAGE",$language->getString("DIR")." ".htmlentities($_GET['rmdir'])." ".$language->getString("WAS_DELETED"));
  }
  $verzeichnis = openDir("../content/uploads/".$dir);
  $pre = "";
  foreach(split("/",$dir) as $cDir){
    $index = $template->add_loop_item("PATH");
    $path .= "/".$cDir;
    if($path == "/"){
      $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=files");
      $template->assign_loop_var("PATH", $index, "LABEL","/");
      $path = "";
    }
    else{
      $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=files&dir=".$path);
      $template->assign_loop_var("PATH", $index, "LABEL",$cDir);
    }
  }
  $subFolders = FileServer::getFolders("../content/uploads/".$dir);
  if($subFolders){
    foreach($subFolders as $folder){
      $index = $template->add_loop_item("DIRS");
      $template->assign_loop_var("DIRS",$index,"URL","/admin/index.php?page=files&dir=".$dir."/".$folder);
      $template->assign_loop_var("DIRS",$index,"LABEL",$folder);
    }
  }
  if(trim($_GET['dir']) != "" & trim($_GET['dir']) != "/"){
    $template->assign_var("DELETEFOLDERLINK","<a href="/admin/index.php?page=files&rmdir=".$_GET['dir']."">Ordner l&ouml;schen</a>");
  }
  else{
    $template->assign_var("DELETEFOLDERLINK","");
  }
  $files = FileServer::getFiles("../content/uploads/".$dir);
  if($files){
    $template->show_if("FILES_EXIST",true);
    $template->show_if("NOFILES",false);
    foreach($files as $file){
      $index = $template->add_loop_item("FILES");
      $path_info = pathinfo("../content/uploads/".$dir."/".$file);
      if(strtolower($path_info['extension'] == 'jpg') or
         strtolower($path_info['extension'] == 'jpeg') or
         strtolower($path_info['extension'] == 'gif') or
         strtolower($path_info['extension'] == 'png') or
         strtolower($path_info['extension'] == 'bmp')){
         $template->assign_loop_var("FILES",$index,"IMAGE","<img src="/content/uploads/".$dir."/".$file.""style="max-width:100px;max-height:100px;" />");
      }
      else{
         $template->assign_loop_var("FILES",$index,"IMAGE","");
      }
      $template->assign_loop_var("FILES",$index,"FILELINK","<a href="../content/uploads".$dir."/".$file."">".$file."</a>");
      $template->assign_loop_var("FILES",$index,"DELETEURL","/admin/index.php?page=files&dir=".urlencode($_GET['dir'])."&unlink=".urlencode($file));
    }
  }
  else{
    $template->show_if("FILES_EXIST",false);
    $template->show_if("NOFILES",true);
  }
  $template->assign_var("MESSAGE","");
  $template->output();
?>

Weiter geht’s mit der Ergebnisseite, in welcher auch ein Bild zur Mediathek hinzugefügt werden kann. Die Template Datei ist die folgende (/system/templates/upload.html):

<h1>{LANG:UPLOAD}</h1>
<p>{VAR:MESSAGE}</p>
{IF:SHOW_MEDIALIBARY}
  <h2>{LANG:ADD_IMAGE_TO_MEDIALIBARY}</h2>
  <form action="{VAR:URL}" method="POST">
    <input type="hidden" name="path" value="{VAR:FILE_PATH}" />
    <table>
      <tr>
        <td>{LANG:NAME}:</td>
        <td><input name="name" /></td>
      </tr>
      <tr>
        <td>{LANG:DESCRIPTION}:</td>
        <td><input name="description" /></td>
      </tr>
      <tr>
        <td></td>
        <td><input type="submit" value="{LANG:INSERT}" />
      </tr>
    </table>
  </form>
   <img src="{VAR:FILE_PATH}" style="max-width:300px" />
{/IF:SHOW_MEDIALIBARY}

Auch dafür gibt es selbstverständlich eine PHP-Datei. Hier ist es die /admin/includes/upload.php:

<?php
  $template = new Template();
  $template->load("upload","..");
  if(FileServer::upload("../content/uploads/".$_POST['dir'],$_FILES['file'])){
    $name = $_FILES['file']['name'];
    $template->assign_var("MESSAGE",str_replace("{FILENAME}",$name,$language->getString("FILE_UPLOADED")));
    $path_info = pathinfo("../content/uploads/".$_POST['dir']."/".$name);
    if(strtolower($path_info['extension'] == 'jpg') or
       strtolower($path_info['extension'] == 'jpeg') or
       strtolower($path_info['extension'] == 'gif') or
       strtolower($path_info['extension'] == 'png') or
       strtolower($path_info['extension'] == 'bmp')){
       $template->show_if("SHOW_MEDIALIBARY",true);
       $template->assign_var("URL","/admin/index.php?page=addImage");
       $template->assign_var("FILE_PATH","/content/uploads/".$base."/".$name);
    }
  }
  else {
    $template->assign_var("MESSAGE",$language->getString("FILE_NOT_UPLOADED"));
  }
  $template->output();
?>

Zu guter letzt kommt die /admin/includes/addImage.phpan die Reihe. Diese benutzt das bereits existierende Template messages:

<h1><?PHP echo htmlentities($language->getString("ADD_IMAGE")); ?></h1>
<?PHP
  $template = new Template();
  $template->load("message","..");
  if(ImageServer::insert($_POST['path'],$_POST['name'],$_POST['description'])){
    $template->assign_var("MESSAGE",htmlentities($language->getString("IMAGE_ADDED")));
  }
  else{
    $template->assign_var("MESSAGE",htmlentities($language->getString("IMAGE_NOT_ADDED")));
  }
  $template->output();
?>

Übrigens: Erstellt mal ein paar Entwürfe für den Admin-Bereich den wir Live am Mittwoch erstellen werden. Ihr könnt mir diese am besten vorher per Mail zukommen lassen (erst mal reines HTML)