info@a-coding-project.de

Globales Menü + Bugfix in Settings (CMS)

Das Globale Menü haben wir im selbst erstelltem CMS vor einiger Zeit aus den Settings verschoben. Jedoch macht es mit den Rollen wieder Sinn, den Menü-Setting anzulegen. Denn der Admin im CMS soll auch das Admin-Menü bekommen.

Außerdem gibt es noch einen Bugfix in den Settings. Fangen wir aber erstmla mit dem Menü an. Hierzu muss folgender SQl-Query ausgeführt werden:

INSERT INTO `cms_settings` VALUES (3, 'global', 'global', 'mainmenu', '1', 1, 'Hauptmenü', 'menueselector');

Das ganze muss selbstverständlich auch wieder im Installer gemacht werden (installer/installer.php):

mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'global', 'global', 'title', '".$this->params[4]['name']."', 1, 'Titel des CMS','textbox');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'global', 'global', 'selectedskin', '1', 1, 'Aktueller Skin','skinselector');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'global', 'global', 'selectedmobileskin', '1', 1, 'Mobiler Skin','mobileskinselector');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'default', 'skins', 'skinbgcolor', 'dedede', 1, 'Hintergrundfarbe','colorpicker');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'default', 'skins', 'skinforecolor', '525252', 1, 'Schriftfarbe','colorpicker');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'default', 'skins', 'skinhighlight1', 'ff0000', 1, 'Highlight Farbe 1','colorpicker');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'default', 'skins', 'skinhighlight2', 'b3fa00', 1, 'Highlight Farbe 2','colorpicker');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3,'global', 'global', 'host', 'http://".$_SERVER['HTTP_HOST']."', 1, 'URL der Startseite','textbox');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (3, 'global', 'global', 'mainmenu', '4', 0, 'Hauptmenü', 'menueselector');");
mysql_query("INSERT INTO `".$this->params[3]['praefix']."settings` VALUES (2, 'global', 'global', 'mainmenu', '4', 1, 'Hauptmenü', 'menueselector');");

In der Klasse /system/sys.php können wir nun das neue Menü auslesen:

function displayGlobalMenu($globalstart,$globalend, $elementstart,$elementend,
                           $class){
  Menu::display(getSetting("global","global","mainmenu"),
                $globalstart,
                $globalend,
                $elementstart,
                $elementend,
                $class);
  }

Kommen wir nun zum Bugfix. In der /system/settings.php hatte das auslesen, ob der Settings noch schon vorhanden ist nicht funktioniert. Deswegen wurde immer das Insert durchgeführt. Der neue Code sieht so aus:

<?PHP
function getSetting($area,$areaType,$property){
  $area     = $GLOBALS['db']->EscapeString($area);
  $areaType = $GLOBALS['db']->EscapeString($areaType);
  $property = $GLOBALS['db']->EscapeString($property);
  return $GLOBALS['db']->ReadField("
    SELECT value FROM {'dbprefix'}settings WHERE role = '".$_SESSION['user']->role->ID."' AND area = '".$area."' AND areaType = '".$areaType."' AND property = '".$property."' UNION
    SELECT value FROM {'dbprefix'}settings WHERE role = '3' AND area = '".$area."' AND areaType = '".$areaType."' AND property = '".$property."'
   ");
}
function setSetting($area,$areaType,$property,$value,$role){
  $area     = $GLOBALS['db']->EscapeString($area);
  $areaType = $GLOBALS['db']->EscapeString($areaType);
  $property = $GLOBALS['db']->EscapeString($property);
  $value    = $GLOBALS['db']->EscapeString($value);
  $role     = $GLOBALS['db']->EscapeString($role);
  $allUserData = $GLOBALS['db']->ReadRow("SELECT * FROM  {'dbprefix'}settings WHERE area = '".$area."' AND areaType = '".$areaType."' AND property = '".$property."' AND role = '3'");
  if($GLOBALS['db']->EscapeString($allUserData->value) != $value or $role == 3){
    $exists = $GLOBALS['db']->ReadField("SELECT COUNT(*) FROM {'dbprefix'}settings WHERE area = '".$area."' AND areaType = '".$areaType."' AND property = '".$property."' AND role = '".$role."'") > 0;
    if($exists){
      $res = $GLOBALS['db']->Execute("UPDATE {'dbprefix'}settings SET value = '".$value."' WHERE area = '".$area."' AND areaType = '".$areaType."' AND property = '".$property."' AND role = '".$role."'");
    }
    else{
      $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}settings (value,area,areaType,property,role,description,type) VALUES ('".$value."', '".$area."', '".$areaType."', '".$property."', '".$role."','".$allUserData->description."','".$allUserData->type."')");
    }
  }
  else{
    $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}settings WHERE area = '".$area."' AND areaType = '".$areaType."' AND property = '".$property."' AND role = '".$role."'") ;
  }
  return $res;
}
?>

Zu guter letzt muss auch das Select der /system/classes/settingsform.php geändert werden:

<?PHP

class SettingsForm{

public $area = „global“;

public $areaType = „global“;

public $role = null;

public $url = „“;

public function display(){

if($_POST[’save‘]){

foreach($_POST as $property=>$value){

if($property != „save“ && $property != „roles“){

setSetting($this->area,$this->areaType,$property,$value,$this->role);

}

}

}

$template = new Template();

$template->load(„../system/templates/form_settings.html“);

$roleselector = „<select name=roles onchange=document.location.href='“.$this->url.„&role=‘ + this.options[this.selectedIndex].value;>“;

$roles = $GLOBALS[‚db‘]->ReadRows(„SELECT * FROM {‚dbprefix‘}roles ORDER BY name“);

if($roles){

foreach($roles as $role){

if($this->role == $role->id){

$roleselector .= „<option value=.$role->id. selected=selected>“.$role->name.„</option>“;

}

else{

$roleselector .= „<option value=.$role->id.>“.$role->name.„</option>“;

}

}

}

$roleselector .= „</select>“;

$template->assign_var(„ROLES“,$roleselector);

$template->assign_var(„URL“,$this->url.„&role=“.$this->role);

$sql = „SELECT DISTINCT * FROM {‚dbprefix‘}settings WHERE area = ‚“.$this->area.„‚ AND areaType = ‚“.$this->areaType.„‚ AND activated = 1“;

if($this->role){

$sql .= “ AND role = ‚“.$this->role.„‚“;

if($this->role != 3){

$sql .=“ UNION SELECT DISTINCT * FROM {‚dbprefix‘}settings WHERE area = ‚“.$this->area.„‚ AND areaType = ‚“.$this->areaType.„‚ AND activated = 1 AND role = ‚3‘ AND property NOT IN (SELECT DISTINCT property FROM {‚dbprefix‘}settings WHERE area = ‚“.$this->area.„‚ AND areaType = ‚“.$this->areaType.„‚ AND activated = 1 AND role = ‚“.$this->role.„‚)“;

}

}

$rows = $GLOBALS[‚db‘]->ReadRows($sql);

if($rows){

foreach($rows as $row){

$index = $template->add_loop_item(„SETTINGS“);

$template->assign_loop_var(„SETTINGS“, $index, „PROPERTY“, $row->property);

$template->assign_loop_var(„SETTINGS“, $index, „DESCRIPTION“,$row->description);

$control = new $row->type;

$control->name = $row->property;

$control->value = $row->value;

$template->assign_loop_var(„SETTINGS“, $index, „CONTROL“,$control->getCode());

}

}

$template->output();

}

}

?>

Frage: Bei dem basecolor werden die Settings und die Benutzer nicht richtig aufgelistet (ich denke wg. Regex). Hat noch wer von euch das Problem?

Ü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