Rollenspezifische Einstellungen Speichern Teil 3 (CMS)
Heute kommen wir in unserem CMS zum Abschluss der Rollenspezifischen Einstellungen. Dafür müssen als erstes die Einstellungen richtig gespeichert werden. Doppelte Einstellungen werden gelöscht, so dass sie wieder einzeln sind.
/system/settings.php:
<?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($exist){ $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; } ?>
aIn der /system/classes/settingsform.php müssen werden nun bei jeder Rolle alle Einstellungen angezeigt:
<?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 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(); } } ?>
Dann müssen wir noch die /admin/skin-settings.php mit der neuen Technik ausstatten.
<h1>Skin-Einstellungen <?PHP echo $_GET['skin']; ?></h1> <?PHP $settings = new SettingsForm(); $settings->role = 3; $settings->areaType = 'skins'; $settings->area = $_GET['skin']; if($_GET['role']) $settings->role = $_GET['role']; $settings->url = "/admin/index.php?page=skin-settings&skin=".urlencode($_GET['skin']); $settings->display(); ?>
Und zu guter letzt muss der /system/classes/colorpicker.phpnoch an die neue Control-Klasse angepasst werden:
<?PHP classcolorpicker extends Control{ public function getCode(){ global $colorPickerIncludes; $res = ""; if(!$colorPickerIncludes){ $colorPickerIncludes = true; $res .= "<script type="text/javascript"src="/system/jscolor/jscolor.js"></script>"; } $res .= "<input class="color"name="".str_replace(""",""",htmlentities($this->name)).""value="".str_replace(""",""",htmlentities($this->value))."" />"; return $res; } } ?>
Wenn ihr schon eine Login-Seite erstellt habt, könnt ihr euch dort mit eurem Admin-Account einloggen und im Admin-Bereich eine Einstellung für den Admin ändern und das Ergebnis bewundern 😉
Du arbeitest in einer Agentur oder als Freelancer?
Dann wirf doch mal einen Blick auf unsere Software FeatValue.
Über uns
Wir entwickeln Webanwendungen mit viel Leidenschaft. Unser Wissen geben wir dabei gerne weiter. Mehr über a coding project