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

Datenbankklasse Teil 11 (CMS)

Heute heißt es Abschiednehmen im CMS, denn der alte Datenbankzugriff wird ab gleich für immer abgelöst sein.

Ihr erinnert euch an den letzten Beitrag, in welchen wir den Admin-Bereich fertig gestellt haben. Heute ist der komplette Rest dran. Als erstes müssen wir in der /index.php ein Objekt der Datenbankklasse erstellen:

  include("system/sys.php");
  $db = new MySQL('system/dbsettings.php');
  $db->Connect();
  $currentpage = new Page();

Weiter geht’s mit der Datei /system/sys.php. Hier gibt es noch ein paar Funktionen, die den alten Zugriff verwendet hatten:

<?PHP
class sys{
  functionincludeContent(){
    global $currentpage;
    $currentpage->getContent();
  }

  function includeHeader(){
    global $currentpage;
    echo "<title>".$currentpage->title."</title>
          <meta http-equiv="Content-Type"content="text/html; 
          charset=iso-8859-1" />";
    $rows = $GLOBALS['db']->ReadRows("SELECT name, content
                        FROM {'dbprefix'}meta_global
                        UNION SELECT name, content
                        FROM {'dbprefix'}meta_local
                        WHERE page = '".$currentpage->id."'");
    if($rows){
      foreach($rows as $row){
        echo "<meta name="".$row->name.""content="".$row->content."" />";
      }
    }
    EventManager::raiseEvent("header_included",".",null);
  }

  function displayBreadcrump($separator,$class,$idpraefix){
    global $currentpage;
    $i = 1;
    $breadcrump = $currentpage->getBreadcrump();
    while($i <= count($breadcrump)){
      echo "<a href="".$breadcrump[$i-1][0].".html"class="".$class."" 
            id="".$idpraefix.$i."">".$breadcrump[$i-1][1]."</a>";
      if($i < count($breadcrump)){
        echo $separator;
      }
      $i++;
    }
  }
  
  functiondisplayMenu($id, $globalstart,$globalend, $elementstart,$elementend,
                       $class){
    Menu::display($id, $globalstart,$globalend, $elementstart,$elementend,$class);
  }

  functiondisplayGlobalMenu($globalstart,$globalend, $elementstart,$elementend,
                       $class){
    $row = $GLOBALS['db']->ReadRow("SELECT value FROM {'dbprefix'}settings WHERE property = 'globalmenuid'");
    if($row){
      Menu::display($row->value, $globalstart,$globalend, $elementstart,$elementend,$class);
    }
  }
 
  functiondisplayLocalMenu($globalstart,$globalend, $elementstart,$elementend,
                       $class){
    global $currentpage;
    if($currentpage->menu > -1){
      Menu::display($currentpage->menu, $globalstart,$globalend, $elementstart,$elementend,$class);
    }
  }

  function getColor($id){
    return "#".getSetting("skin".$id);
  }

  function localMenuExists(){
    global $currentpage;
    if($currentpage->menu){
      return true;
    }
    else{
      return false;
    }
  }
  
  function getTitle(){
    return getSetting("title");
  }
   
  function getFullSkinPath(){
    return getSetting("host")."/".SkinController::getCurrentSkinPath()."/";
  }

  function getMenues(){
    return $GLOBALS['db']->ReadRows("SELECT id, name, (
                                       SELECT COUNT( * ) 
                                       FROM {'dbprefix'}menu
                                       WHERE menuID = {'dbprefix'}menu_names.id
                                     )count
                                     FROM `{'dbprefix'}menu_names`");
  }
}
?>

Und noch ein paar Änderungen in der Klasse /system/classes/page.php:

<?PHP
class Page{
  var $id      = -1;
  var $alias   = '';
  var $title   = '';
  var $ownerid = -1;
  var $owner   = false;
  var $menu    = -1;
  var $meta    =null;
  
  function loadProperties($alias){
    if ($alias == "") {
    $alias = "home";
    }
    $row = $GLOBALS['db']->ReadRow("SELECT id,title,owner,menu FROM {'dbprefix'}pages 
                           WHERE alias = '".$alias."'");
    if($row){
      $this->id = $row->id;
      $this->title = $row->title;
      $this->ownerid = $row->owner;
      $this->menu = $row->menu;
      $this->alias = $alias;
      $this->meta  = new Meta();
      $this->meta->pageid = $this->id;
      $this->meta->load();
    }
  }

  function getContent(){
    EventManager::raiseEvent("content_top",".",$this);
    include(filterfilename("content/articles/".$this->alias));
    EventManager::raiseEvent("content_bottom",".",$this);
  }

  function getOwner(){
    if(!$this->owner){
      $alias = $GLOBALS['db']->ReadField("SELECT alias FROM {'dbprefix'}pages 
                                          WHERE id = '".$this->ownerid."'");
      if($alias){
        $this->owner = new Page();
        $this->owner->loadProperties($alias);
      }
    }
    return $this->owner;
  }

  function getBreadcrump(){
    if(!$this->owner) $this->getOwner();
    if($this->owner){
      $breadcrump = $this->owner->getBreadcrump();
    }
    $breadcrump[] = array($this->alias,$this->title);
    return $breadcrump;
  }

  function readContent($dirpraefix){
    $filename = $dirpraefix."content/articles/".$this->alias.".php";
    $handle = @fopen ($filename, "rb");
    if($handle && filesize($filename) > 0){
      $contents = fread ($handle, filesize ($filename));
      fclose ($handle);
    }
    else{
      $contents = "";
    }
    return $contents;
  }

  function save(){
    $res = $GLOBALS['db']->Execute("UPDATE {'dbprefix'}pages SET
                                    title = '".$this->title."',
                                    alias = '".$this->alias."',
                                    menu = '".$this->menu."' 
                                    WHERE id = '".$this->id."'");
    $this->meta->save();
    if($res){
      $args['title'] = $this->title;
      $args['alias'] = $this->alias;
      $args['menu']  = $this->menu;
      $args['id']    = $this->id;
      EventManager::raiseEvent("page_saved","../",$args);
    }
    return $res;
  }

  function writeContent($dirpraefix,$content){
    $filename = $dirpraefix."content/articles/".$this->alias.".php";
    $handle = fopen ($filename, "a");
    $res = fwrite ($handle, $content);
    fclose ($handle);
    if($res){
      $args['alias'] = $this->alias;
      $args['filename']  = $filename;
      $args['content'] = $content;
      EventManager::raiseEvent("pagecontent_writed","../",$args);
    }
    return $res;
  }

  function deleteContent($dirpraefix){
    $filename = $dirpraefix."content/articles/".$this->alias.".php";
    if(file_exists($filename)){
      unlink($filename);
      $args['alias'] = $this->alias;
      $args['filename']  = $filename;
      EventManager::raiseEvent("pagecontent_deleted","../",$args);
    }
  }

  function create($alias){
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}pages (alias) VALUES ('".$alias."')");
    if($res){
      $args['id']    = $GLOBALS['db']->InsertID();
      $args['alias'] = $alias;
      EventManager::raiseEvent("page_created","../",$args);
    }
    return $GLOBALS['db']->InsertID();
  }

  function delete(){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}pages WHERE alias = '".$this->alias."'");
    if($res){
      $args['alias'] = $this->alias;
      EventManager::raiseEvent("page_deleted","../",$args);
    }
    return $res;
  }
}
?>

/system/classes/menu.php:

<?PHP
class Menu{
  function display($id, $globalstart,$globalend, $elementstart,$elementend,$class){
    $rows = $GLOBALS['db']->ReadRows("SELECT title,href FROM {'dbprefix'}menu WHERE menuID = '".$id."' ORDER BY id");
    echo $globalstart;
    $i = 1;
    if($rows){
      foreach($rows as $row){
        echo $elementstart."<a href="".$row->href.""title="".$row->title."" 
             class="".$class." menue-".$id."-".$i."">".$row->title."</a>".$elementend;
        $i++;
      }
    }
    echo $globalend;
  }
  
  function getIdByName($name){
    $name = $GLOBALS['db']->EscapeString(strtolower(trim($name)));
    return $GLOBALS['db']->ReadField("SELECT id FROM {'dbprefix'}menu_names WHERE lower(trim(name)) = '".$name."' LIMIT 0,1");;
  }

  function displayEditable($id){
    $rows = $GLOBALS['db']->ReadRows("SELECT id, title,href FROM {'dbprefix'}menu WHERE menuID = '".$id."' ORDER BY id");
    if($rows){
      foreach($rows as $row){
        echo $elementstart."<input name="".$row->id."_title"value="".$row->title."" />
                            <input name="".$row->id."_href"value="".$row->href."" />
                            <input type="submit"name="".$row->id."_delete"value="X" /><br />";
      }
    }
  }

  function addEntry($menu,$title,$href){
    $maxID = $GLOBALS['db']->ReadField("SELECT MAX(id) FROM {'dbprefix'}menu 
                                        WHERE menuID = '".$menu."'");
    $id = $maxID + 1;
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}menu (id,menuID,title,href)
                                    VALUES('".$id."','".$menu."',
                                    '".$title."','".$href."')");
    if($res){
      $args['menu']  = $menu;
      $args['title'] = $title;
      $args['href']  = $href;
      EventManager::raiseEvent("menu_entry_added","../",$args);
    }
    return $res;
  }

  function editEntry($menu,$id,$title,$href){
    $res =  $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET 
                                     href = '".$href."', 
                                     title = '".$title."' 
                                     WHERE id = '".$id."' 
                                     AND menuID = '".$menu."'");
    if($res){
      $args['menu']  = $menu;
      $args['title'] = $title;
      $args['href']  = $href;
      $args['id']    = $href;
      EventManager::raiseEvent("menu_entry_edit","../",$args);
    }
    return $res;
  }

  function deleteEntry($menu,$id){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}menu WHERE menuID = '".$menu."' AND id = '".$id."'");
    if($res){
      $res = $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET id = id - 1 WHERE id > '".$id."'");
      if($res){
        $args['menu']  = $menu;
        $args['id'] = $id;
        EventManager::raiseEvent("menu_entry_deleted","../",$args);
      }
    }
    return $res;
  }

  function create($name){
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}menu_names (name) VALUES ('".$name."')");
    if($res){
      $args['name']  = $name;
      EventManager::raiseEvent("menu_created","../",$args);
    }
    return $GLOBALS['db']->InsertID();
  }

  function delete($id){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}menu_names WHERE id = '".$id."'");
    if($res){
      $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}menu WHERE menuID = '".$id."'");
      if($res){
        $args['id']  = $id;
        EventManager::raiseEvent("menu_deleted","../",$args);
      }
    }
    return $res;
  }
}
?>

Und zu guter letzt: /system/classes/eventmanager.php:

<?PHP
  class EventManager {
  
    function addHandler($file, $event){
       $file  = $GLOBALS['db']->EscapeString($file);
       $event = $GLOBALS['db']->EscapeString($event);
       return $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}events (event, file) VALUES ('".$event."','".$file."')");
    }
    
    function raiseEvent($name,$base,$args){
       $handler = self::getHandler($name);
       if($handler){
         foreach($handler as $file){
           include($base."/".$file);
         }
       }
    }
    
    function getHandler($name){
       $name = $GLOBALS['db']->EscapeString($name);
       $mySqlRes = $GLOBALS['db']->ReadRows("SELECT file FROM {'dbprefix'}events WHERE event = '".$name."'");
       if($mySqlRes){
         foreach($mySqlRes as $row){
           $res[] = $row->file;
         }
       }
       return $res;
    }
  
  }
?>

Ab jetzt wird es wieder spannender: Wir lösen die letzten Punkte auf Codeplex und dann geht die Alpha-Version raus!

Kommentare

Daniels Blog. Themen aus meiner Welt. schrieb am 29.03.2010:

[...] mussten alle  Scripte, auf die neu Art auf die Datenbank zuzugreifen, umgebaut werden. Mit diesem Artikel ist die Arbeit an diesem Thema abgeschlossen. Das soll natürlich nicht heißen das die Arbeit an [...]

Ferid schrieb am 01.04.2010:

Da du jetzt eine Datenbank Klasse Benutzt wollte ich dich auf die eingebaute Klasse PDO von PHP aufmerksam machen. Damit kannst du abstrakte Datenbank Verbindungen programmieren die du dann für viele DBMS benutzen kannst. So kannst du viele Fliegen mit eine Klappe schlagen. http://www.php.net/manual/de/book.pdo.php

jULiM schrieb am 23.06.2011:

Hab ne Frage: Wo genau tritt die Funktion displayMenu in Erscheinung? Ich seh hier nur displayGlobalMenu und displayLocalMenu die was bewirken... Hab ich da was falsch gemacht?

Elvis67 schrieb am 01.01.2012:

Habt ihr da nicht noch eine Datei vergessn? db.php! Datenbank Name Aktionen Eintr&auml;ge &lt;?PHP foreach(MySQL::getTables($_GET[&#039;dbpage&#039;] * 20,20) as $table){ echo &quot; ".$table." <a title="Anzeigen" rel="nofollow"> </a> <a title="Bearbeiten" rel="nofollow"> </a> <a title="L&ouml;schen" rel="nofollow"> </a> ".MySQL::countTableEntries($table)." "; } ?&gt; &lt;?PHP $pagecount = ceil(MySQL::$countTables / 20); for($cPage = 0;$cPage &lt; $pagecount;$cPage++){ echo &quot;<a>".($cPage + 1)."</a> "; } ?&gt; steht so auch noch bei google code!

Elvis67 schrieb am 01.01.2012:

Na da hat der Parser wohl den Quellcode zerstückelt. Ich meine jedenfalls diese Datei hier: http://code.google.com/p/contentlion/source/browse/trunk/admin/includes/db.php ;-)