info@a-coding-project.de

Menü Templates Teil 3 (CMS)

Diese If-Abfrage werden wir bei den anlegen einer neuen Seite bentzen. Hier wird je nach dem ob das Formular bereits abgesendet wurde oder nicht ein anderer Block eingeblendet. /system/templates/menu_new.html:

<h1>{LANG:NEW_MENU}</h1>
{IF:NOTSUCCEEDED}
  <form action="{VAR:URL}" method="POST">
    {LANG:NAME}: <input name="name"><br />

    <input type="submit" value="{LANG:CREATE}" />
  </form>
{/IF:NOTSUCCEEDED}
{IF:SUCCEEDED}
  <p>{LANG:MENU_CREATED}</p>
  <form action="{VAR:EDITURL}" method="POST">
    <input type="submit" value="{LANG:ADD_ENTRIES}" />
  </form>
{/IF:SUCCEEDED}

Dieses Template wird dann von der /admin/includes/menue-new.php gefüllt:

<?PHP
  $template = new Template();
  $template->load("menu_new","../");
  $template->show_if("NOTSUCCEEDED",!$_POST['name']);
  $template->assign_var("URL","/admin/index.php?page=menue-new");
  if($_POST['name']){
    $template->show_if("SUCCEEDED");
    $id = Menu::create($GLOBALS['db']->EscapeString($_POST['name']));
    if($id){
      $template->assign_var("EDITURL","/admin/index.php?page=menu-edit&menu=".$id);
    }
    else{
      $template->load("message","../");
      $template->assign_var("MESSAGE",$language->getString("MENU_NOT_CREATED"));
    }
  }
  $template->output();
?>

Das Bearbeiten des Menüs ist etwas schwieriger. Hierfür müssen auch noch die Klassen Menu und MenuEntry bearbeitet werden. Fangen wir an mit der /system/classes/menuentry.php. Diese bekommt die neue Funktion getEditableCode. Die Funktion displayEditAbleCode ruft diese einfach nur auf und gibt sie aus:

    public function getEditableCode(){
      $template = new Template();
      $template->load("menuentry_editable","../");
      $template->assign_var("ID", $this->id);
      $template->assign_var("TITLE",$this->title);
      if($this->type == 0){
        $template->assign_var("SELECTEDINTERNAL"," selected="1"");
        $selector = new PageSelector();
        $selector->name  = $this->id.'_href';
        $selector->value = $this->href;
        $template->assign_var("VALUECONTROL",$selector->getCode());
      }
      else{
        $template->assign_var("SELECTEDINTERNAL","");
        if($this->type == 1){
          $template->assign_var("SELECTEDIEXTERNAL"," selected="1"");
            
          $template->assign_var("VALUECONTROL","<input name="".$this->id."_href" value="".$this->href."" />");
        }
        else{
          $template->assign_var("SELECTEDIEXTERNAL","");
          if($this->type == 2){
            $template->assign_var("SELECTEDSUBMENU"," selected="1"");
            $selector = new MenueSelector();
            $selector->name  = $this->id.'_href';
            $selector->value = $this->href;
            $template->assign_var("VALUECONTROL",$selector->getCode());
          }
        }
      }
      return $template->getCode();
    }
    
    public function displayEditAble(){
      echo $this->getEdiableCode();
    }

Diese Funktion benutzt das neue Template /system/templates/menuentry_editable.html:

<input name="{VAR:ID}_title" value="{VAR:TITLE}" />
<select name="{VAR:ID}_type" onChange="typeSelected()">
  <option value="0"{VAR:SELECTEDINTERNAL}>{LANG:PAGE_INTERNAL}</option>
  <option value="1"{VAR:SELECTEDIEXTERNAL}>{LANG:PAGE_EXTERNAL}</option>
  <option value="2"{VAR:SELECTEDSUBMENU}>{LANG:MENU}</option>
</select>
{VAR:VALUECONTROL}
<input type="submit" name="{VAR:ID}_delete" value="X" /><br />

In der neuen Funktion getEtidableCode der /system/classes/menu.php wird nun der Code aller Menüeinträge zusammengefasst und weitergegeben:

  function getEditableCode($id){
    $res = "";
    $entries = self::getEntries($id);
    if($entries){
      foreach($entries as $entry){ 
        $res .= $entry->getEditableCode();
      }
    }
    return $res;
  }

  function displayEditable($id){
    echo $this->getEdiableCode($id);
  }

Jetzt kommen wir zur richtigen Menü-Bearbeiten Seite. Zuerst das Template /system/templates/menu_edit.html:

<h1>{LANG:EDIT_MENU}</h1>
<p>{VAR:MESSAGE}</p>
<form action="{VAR:URL}" method="POST">
  <h2>{LANG:EDITMENU}</h2>
  {VAR:EDITABLEMENU}
  <input name="save" type="submit" value="{LANG:SAVE}"/>
  <h2>{LANG:ENTRY_ADD}</h2>
  <table>
    <tr>
      <td style="width:100px;">{LANG:TEXT}:</td>
      <td><input name="newtitle" style="width:200px;" /></td>
    </tr>
    <tr>
      <td>{LANG:TYPE}:</td>
      <td>
        <script language="javascript">
          function typeSelected(){
            document.getElementsByName('newPage')[0].style.visibility ='hidden';
            document.getElementsByName('newurl')[0].style.visibility ='hidden';
            document.getElementsByName('newMenu')[0].style.visibility  ='hidden';
            if(document.getElementById('type').selectedIndex == 0){
              document.getElementsByName('newPage')[0].style.visibility ='visible';
              document.getElementById('newLabel').innerHTML = '{LANG:PAGE}:';
            }
            else if(document.getElementById('type').selectedIndex == 1){
              document.getElementsByName('newurl')[0].style.visibility  ='visible';
              document.getElementById('newLabel').innerHTML = 'Url:';
            }
            else{
              document.getElementsByName('newMenu')[0].style.visibility ='visible';
              document.getElementById('newLabel').innerHTML = '{LANG:MENU}:';
            }
          }
        </script>
        <select id="type" name="type" onChange="typeSelected()" style="width:200px;">
          <option value="0">{LANG:PAGE_INTERNAL}</option>
          <option value="1">{LANG:PAGE_EXTERNAL}</option>
          <option value="2">{LANG:MENU}</option>
        </select>
      </td>
      </tr>
      <tr>
        <td><span id="newLabel">{LANG:PAGE}</a>:</td>
        <td>
          <input name="newurl" value="http://" style="width:200px;visibility:hidden" />
          {VAR:MENUSELECTOR}
          {VAR:PAGESELECTOR}
        </td>
      </tr>
  </table>
  <input name="add" type="submit" value="Hinzufügen"/>
</form>

Der dazugehörige Code der /admin/includes/menu-edit.php sieht so aus:

<?PHP
  $template = new Template();
  $template->load("menu_edit","../");
  if($_POST['add']){
    if(trim($_POST['newtitle']) != ""){
      if(trim($_POST['newurl']) != ""){
        $entry        = new MenuEntry();
        $entry->menu  = $_GET['menu'];
        $entry->title = $_POST['newtitle'];
        $entry->type  = $_POST['type'];
        if($entry->type == 0){
          $entry->href  = $_POST['newPage'];
        }
        else if($entry->type == 1){
          $entry->href  = $_POST['newurl'];
        }
        else{
          $entry->href  = $_POST['newMenu'];
        }
        $res = $entry->save();
        if($res){
          $template->assign_var("MESSAGE",$GLOBALS['language']->getString("ENTRY_ADDED"));
        }
        else{
          $template->assign_var("MESSAGE",$GLOBALS['language']->getString("ENTRY_NOT_ADDED"));
        }
      }
      else{
        $template->assign_var("MESSAGE",$GLOBALS['language']->getString("ENTER_LINK_TARGET"));
      }
    }
    else{
        $template->assign_var("MESSAGE",$GLOBALS['language']->getString("ENTER_LINK_TITLE"));
    }
  }
  else if($_POST['save']){
    foreach($_POST as $param=>$value){
      if(strlen($param) >= 6){
        if(substr($param,-5,5) == "_href"){
          $entries[substr($param,0,-5)][href] = $value;
        }
        else if(strlen($param) >= 7 && substr($param,-6,6) == "_title"){
          $entries[substr($param,0,-6)][title] = $value;
        }
        else if(substr($param,-5,5) == "_type"){
          $entries[substr($param,0,-5)][type] = $value;
        }
      }
    }
    foreach($entries as $id=>$params){
      $entry        = new MenuEntry();
      $entry->id    = $id;
      $entry->menu  = $_GET['menu'];
      $entry->title = $params['title'];
      $entry->href  = $params['href'];
      $entry->type  = $params['type'];
      $entry->save();
    }
  }
  else{
    foreach($_POST as $param=>$value){
      if(strlen($param) >= 8){
        if(substr($param,-7,7) == "_delete"){
          Menu::deleteEntry($GLOBALS['db']->EscapeString($_GET['menu']),
                            $GLOBALS['db']->EscapeString(substr($param,0,-7)));
        }
      }
    }
  }
  
  $template->assign_var("URL","index.php?page=menu-edit&menu=".urlencode($_GET['menu']));
  $template->assign_var("EDITABLEMENU",Menu::getEditableCode($GLOBALS['db']->EscapeString($_GET['menu'])));

  $selector = new MenueSelector();
  $selector->name  = 'newMenu';
  $selector->value = -1;
  $selector->style = 'width:200px;position:relative;left:-210px;visibility:hidden';
  $template->assign_var("MENUSELECTOR", $selector->getCode());

  $selector = new PageSelector();
  $selector->name  = 'newPage';
  $selector->value = -1;
  $selector->style = 'width:200px;position:relative;left:-415px;';
  $template->assign_var("PAGESELECTOR", $selector->getCode());
  
  $template->assign_var("MESSAGE","");
  $template->output();
?>

Das Löschen eines Menüs ist wieder etwas leichter. Hierfür gibt es das neue Template /system/templates/menu_delete.html:

<p>{LANG:DELETE_MENU_ASK}</p>
<a href="{VAR:CANCELURL}">{LANG:CANCEL}</a>
<a href="{VAR:DELETEURL}">{LANG:DELETE_MENU}</a>

Die Steuerung erfolgt in der /admin/includes/menu-delete.php:

<h1><?PHP echo $language->getString("DELETE_MENU"); ?></h1>
<?PHP
  $template = new Template();
  if(!$_GET['delete']){
    $template->load("menu_delete","../");
    $template->assign_var("CANCELURL","/admin/?page=menues");
    $template->assign_var("DELETEURL","/admin/?page=menu-delete&menu=".$_GET['menu']."&delete=true");
  }
  else{
    $template->load("message","../");
    if(Menu::delete($GLOBALS['db']->EscapeString($_GET['menu']))){
      $template->assign_var("MESSAGE",$language->getString("MENU_DELETED"));

    }
    else{
      $template->assign_var("MESSAGE",$language->getString("MENU_NOT_DELETED"));
    }
  }
  $template->output();
?>

Für die Templates braucht ihr die neue Sprachdatei. Jetzt sind wir mit den Templates der Menüs soweit durch. Als nächstes kommt das Dateisystem an die Reihe.

Über uns

Stefan Wienströer

Wir entwickeln Webanwendungen mit viel Leidenschaft. Unser Wissen geben wir dabei gerne weiter. Mehr über a coding project

Auch interessant