Current Path : /data/web/virtuals/51568/virtual/www/subdom/pomucky.1/src/ |
Current File : /data/web/virtuals/51568/virtual/www/subdom/pomucky.1/src/Model.php |
<?php namespace app; abstract class Model { public const RULE_REQUIRED = 'required'; public const RULE_REQUIRED_CHECKBOX = 'requiredcheckbox'; public const RULE_ATLEASTONE_CHECKBOXGROUP = 'onecheckboxgroup'; public const RULE_REQUIRED_RADIOGROUP = 'requiredradio'; public const RULE_EMAIL = 'email'; public const RULE_MIN = 'min'; public const RULE_MAX = 'max'; public const RULE_MINNUM = 'minnum'; public const RULE_MAXNUM = 'maxnum'; public const RULE_MATCH = 'match'; public const RULE_UNIQUE = 'unique'; public const FIELD_INPUT = 'input'; public const FIELD_HIDDEN = 'hidden'; public const FIELD_TEXTAREA = 'textarea'; public const FIELD_CHECKBOX = 'checkbox'; public const FIELD_CHECKBOXGROUP = 'checkboxgroup'; public const FIELD_RADIOGROUP = 'radiogroup'; public const FIELD_SELECT = 'select'; public $errors = []; public $disabled = []; public $specialErrorMessages = []; abstract public function rules(): array; abstract public function types(): array; public function IDName() { return "id"; } public function rulesJSON() { $rules = $this->rules(); return "var rules = " . json_encode($rules); } public function typesJSON() { $types = $this->types(); return "var types = " . json_encode($types); } public function errorMessagesJSON() { $messages = $this->errorMessages(); return "var messagesPHP = " . json_encode($messages); } public function specialErrorMessagesJSON() { $json = []; foreach ($this->specialErrorMessages as $key => $errorMesage) { foreach ($errorMesage as $rule => $msg) { $json[$key."#".$rule] = $msg; } } return "var specialMessagesPHP = " . json_encode($json); } public function loadData($data) { foreach($data as $key => $value) { if (property_exists($this, $key)) { $this->{$key} = $value; } } } public function __toString() { $class_vars = get_class_vars(get_class($this)); $values = '<pre>'; foreach ($class_vars as $name => $value) { if (!is_object($value)) { $values .= "$name : {$this->$name}\n"; } } $values .= '</pre>'; return $values; } public function disableAllFields($data) { foreach($data as $key => $value) { if (property_exists($this, $key)) { $this->disabled[$key] = true; } } } public function setDisabledState($data, $state = true) { foreach($data as $key => $value) { if (property_exists($this, $key)) { $this->disabled[$key] = $state; } } } public function labels(): array { return []; } public function getLabel($attribute) { return $this->labels()[$attribute] ?? $attribute; } public function validate(array $exclude = []) { foreach ($this->rules() as $attribute => $rules) { if (in_array($attribute, $exclude)) { continue; } $value = $this->{$attribute}; foreach ($rules as $rule) { $ruleName = $rule; if (!\is_string($ruleName)) { $ruleName = $rule[0]; } if ($ruleName === self::RULE_REQUIRED && strlen($value) == 0) { $this->addErrorForRule($attribute, self::RULE_REQUIRED); } if ($ruleName === self::RULE_REQUIRED_CHECKBOX && empty($value)) { $this->addErrorForRule($attribute, self::RULE_REQUIRED_CHECKBOX, $rule); } if ($ruleName === self::RULE_ATLEASTONE_CHECKBOXGROUP && empty($value)) { $this->addErrorForRule($attribute, self::RULE_ATLEASTONE_CHECKBOXGROUP, $rule); } if ($ruleName === self::RULE_REQUIRED_RADIOGROUP && empty($value)) { $this->addErrorForRule($attribute, self::RULE_REQUIRED_RADIOGROUP); } if ($ruleName === self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) { $this->addErrorForRule($attribute, self::RULE_EMAIL); } if ($ruleName === self::RULE_MIN && strlen($value) < $rule['min']) { $this->addErrorForRule($attribute, self::RULE_MIN, $rule); } if ($ruleName === self::RULE_MINNUM && $value < $rule['minnum']) { $this->addErrorForRule($attribute, self::RULE_MINNUM, $rule); } if ($ruleName === self::RULE_MAX && strlen($value) > $rule['max']) { $this->addErrorForRule($attribute, self::RULE_MAX, $rule); } if ($ruleName === self::RULE_MAXNUM && $value > $rule['maxnum']) { $this->addErrorForRule($attribute, self::RULE_MAXNUM, $rule); } if ($ruleName === self::RULE_MATCH && $value !== $this->{$rule['match']}) { $rule['match'] = $this->getLabel($rule['match']); $this->addErrorForRule($attribute, self::RULE_MATCH, $rule); } if ($ruleName === self::RULE_UNIQUE) { $className = $rule['class']; $uniqueAttr = $rule['attribute'] ?? $attribute; $tableName = $className::tableName(); $IDName = $className::IDName(); $IDValue = $this->{$IDName}; $record = Application::$app->db->fetchOne("SELECT * FROM $tableName WHERE $uniqueAttr = :attr AND $IDName <> $IDValue", $value); if ($record) { $this->addErrorForRule($attribute, self::RULE_UNIQUE, ['field' => $this->getLabel($attribute)]); } } } } return empty($this->errors); } private function addErrorForRule(string $attribute, string $rule, $params = []) { $message = $this->errorMessages()[$rule] ?? ''; $message = $this->specialErrorMessages[$attribute][$rule] ?? $message; foreach ($params as $key => $value){ $message = \str_replace("{{$key}}", $value, $message); } $this->errors[$attribute][] = $message; } public function addError(string $attribute, string $message) { $this->errors[$attribute][] = $message; } public function errorMessages() { return [ self::RULE_REQUIRED => 'Toto pole je povinné', self::RULE_REQUIRED_CHECKBOX => 'Tuto položku musíš zvolit', self::RULE_ATLEASTONE_CHECKBOXGROUP => 'Musíš vybrat nejméně jednu položku', self::RULE_REQUIRED_RADIOGROUP => 'Jednu hodnotu musíš zvolit', self::RULE_EMAIL => 'Toto pole musí obsahovat validní email', self::RULE_MIN => 'Minimální počet znaků v tomto poli je {min}', self::RULE_MINNUM => 'Nejmenší povolená hodnota je {minnum}', self::RULE_MAX => 'Maximální počet znaků v tomto poli je {max}', self::RULE_MAXNUM => 'Největší povolená hodnota je {maxnum}', self::RULE_MATCH => 'Toto pole se musí shodovat s polem {match}', self::RULE_UNIQUE => 'Záznam s touto hodnotou v poli {field} již existuje', ]; } public function changeErrorMessage(string $attribute, $rule, string $message): void { $this->specialErrorMessages [$attribute][$rule] = $message; } public function hasError($attribute) { return $this->errors[$attribute] ?? false; } public function getFirstError($attribute): string { return $this->errors[$attribute][0] ?? ''; } public function sanitizeObject(\stdClass $data) { foreach($data as &$item) { $item = filter_var($item, FILTER_SANITIZE_SPECIAL_CHARS); $item = trim($item); } return $data; } }