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

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

aIn der /system/classes/settingsform.php müssen werden nun bei jeder Rolle alle Einstellungen angezeigt:

<?PHP class SettingsForm{ public $area = &quot;global&quot;; public $areaType = &quot;global&quot;; public $role = null; public $url = &quot;&quot;; public function display(){ if($_POST['save']){ foreach($_POST as $property=>$value){ if($property != &quot;save&quot; && $property != &quot;roles&quot;){ setSetting($this->area,$this->areaType,$property,$value,$this->role); } } } $template = new Template(); $template->load(&quot;../system/templates/form_settings.html&quot;); $roleselector = &quot;<select name=&quot;roles&quot; onchange=&quot;document.location.href='&quot;.$this->url.&quot;&role=' + this.options[this.selectedIndex].value;&quot;>&quot;; $roles = $GLOBALS['db']->ReadRows(&quot;SELECT * FROM {'dbprefix'}roles ORDER BY name&quot;); if($roles){ foreach($roles as $role){ if($this->role == $role->id){ $roleselector .= &quot;<option value=&quot;&quot;.$role->id.&quot;&quot; selected=&quot;selected&quot;>&quot;.$role->name.&quot;</option>&quot;; } else{ $roleselector .= &quot;<option value=&quot;&quot;.$role->id.&quot;&quot;>&quot;.$role->name.&quot;</option>&quot;; } } } $roleselector .= &quot;</select>&quot;; $template->assign_var(&quot;ROLES&quot;,$roleselector); $template->assign_var(&quot;URL&quot;,$this->url.&quot;&role=&quot;.$this->role); $sql = &quot;SELECT DISTINCT * FROM {'dbprefix'}settings WHERE area = '&quot;.$this->area.&quot;' AND areaType = '&quot;.$this->areaType.&quot;' AND activated = 1&quot;; if($this->role){ $sql .= &quot; AND role = '&quot;.$this->role.&quot;'&quot;; if($this->role != 3){ $sql .=&quot; UNION SELECT DISTINCT * FROM {'dbprefix'}settings WHERE area = '&quot;.$this->area.&quot;' AND areaType = '&quot;.$this->areaType.&quot;' AND activated = 1 AND property NOT IN (SELECT DISTINCT property FROM {'dbprefix'}settings WHERE area = '&quot;.$this->area.&quot;' AND areaType = '&quot;.$this->areaType.&quot;' AND activated = 1 AND role = '&quot;.$this->role.&quot;')&quot;; } } $rows = $GLOBALS['db']->ReadRows($sql); if($rows){ foreach($rows as $row){ $index = $template->add_loop_item(&quot;SETTINGS&quot;); $template->assign_loop_var(&quot;SETTINGS&quot;, $index, &quot;PROPERTY&quot;, $row->property); $template->assign_loop_var(&quot;SETTINGS&quot;, $index, &quot;DESCRIPTION&quot;,$row->description); $control = new $row->type; $control->name = $row->property; $control->value = $row->value; $template->assign_loop_var(&quot;SETTINGS&quot;, $index, &quot;CONTROL&quot;,$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  = &quot;/admin/index.php?page=skin-settings&skin=&quot;.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 .= &quot;<script type="text/javascript"src="/system/jscolor/jscolor.js"></script>&quot;;
      }
    $res .=  &quot;<input class="color"name="".str_replace(""",&quot;&quot;&quot;,htmlentities($this->name)).""value="".str_replace(""",&quot;&quot;&quot;,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 😉