args = $args; $this->class = $class; $this->cookie = $_COOKIE; $this->default = $default; $this->dir = THEME.$dir; $this->ext = $ext; $this->mode = $mode; $this->name = $dir; $this->post = $_POST; $this->separator = $separator; if ($auto) { $this->props = $this->getProps(); $this->selected = $this->getSelected(); if ($this->changed) { $this->writeSelected(); } } } private function getProps() { $props = []; if ($this->mode == 'select') { $dirHandle = opendir($this->dir); if ($dirHandle) { while (FALSE !== ($file = readdir($dirHandle))) { if (!is_dir($this->dir.'/'.$file) && preg_match("/[A-z0-9]+\.".$this->ext."\z/", $file)) { $props[] = str_replace('.'.$this->ext, '', $file); } } } } else if ($this->mode == 'increment') { $props = ['less', 'reset', 'more']; } return $props; } private function getSelected() { $cookie_val = isset($this->cookie['theme_'.$this->name]) ? $this->cookie['theme_'.$this->name] : ''; if ($this->mode == 'select') { if (isset($this->post['change_'.$this->name])) { foreach ($this->props as $prop) { if (isset($this->post[$prop.'_x'])) { $this->changed = TRUE; return $prop; } } } else if (!empty($cookie_val)) { if (in_array($cookie_val, $this->props)) { return $cookie_val; } } return $this->default; } else if ($this->mode == 'increment') { if (is_numeric($cookie_val) && !isset($this->post['reset_x'])) { $value = $cookie_val; } else { $value = $this->default; } if (isset($this->post['change_'.$this->name])) { $this->changed = TRUE; if (isset($this->post['less_x'])) { if (!isset($this->args['min']) || $value + $this->args['step'] >= $this->args['min']) { $value = $value - $this->args['step']; } } else if (isset($this->post['more_x'])) { if (!isset($this->args['max']) || $value + $this->args['step'] <= $this->args['max']) { $value = $value + $this->args['step']; } } } return $value; } return FALSE; } private function writeSelected() { if ($this->selected == $this->default) { setcookie('theme_'.$this->name, $this->selected, time() - 3600 * 24 * 14, '/'); } else { setcookie('theme_'.$this->name, $this->selected, time() + 3600 * 24 * 14, '/'); } } public function disable() { $this->enabled = FALSE; $this->selected = $this->default; } public function makeForm($class = '') { if ($this->enabled) { $this->buttons = $this->getButtons(); $form = '
'; $form .= '
'; $form .= ''; $form .= implode($this->separator, $this->buttons); $form .= '
'; $form .= '
'; return $form; } return FALSE; } private function getButtons() { foreach ($this->props as $prop) { if ($prop != $this->selected) { $this->buttons[] = ''; } } return $this->buttons; } public function makeHeadTag() { add_to_head(''); } }