| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Cache;
- class Config extends Model
- {
- protected $table = 'tb_config';
- public $keyType = 'string';
- public $incrementing = false;
- public $timestamps = false;
- public string $metaKey = 'id';
- public string $metaValue = 'value';
- public string $metaDescription = 'description';
- public string $cacheName = 'config-meta';
- public int $cacheTime = CACHE_EXPIRE_TIME;
- public function __set($name, $value)
- {
- $this->{$name} = $value;
- }
- public function __get($name)
- {
- return ($this->{$name} ?? null);
- }
- /**
- * 모든 관리자 설정 값 조회
- */
- public function getAllMeta(): object|array
- {
- $cacheName = $this->cacheName;
- if (!$result = Cache::get($cacheName)) {
- $res = $this->get();
- if ($res) {
- $result = [];
- foreach ($res as $val) {
- $result[$val[$this->metaKey]] = $val[$this->metaValue];
- }
- Cache::forever($cacheName, (object)$result);
- }
- }
- return $result;
- }
- /**
- * 저장
- */
- public function save($saveData = [], array $attributes = []): void
- {
- if ($saveData && is_array($saveData)) {
- foreach ($saveData as $column => $value) {
- if (array_key_exists($column, $attributes)) {
- $description = $attributes[$column];
- $this->_metaUpdate($column, $value, $description);
- }
- }
- }
- Cache::forget($this->cacheName);
- $this->getAllMeta();
- }
- /**
- * 조회
- */
- public function item($column = '', $default = false): mixed
- {
- if (empty($column)) {
- return false;
- }
- $meta = (array)$this->getAllMeta();
- $return = $default;
- if (array_key_exists($column, $meta)) {
- $return = $meta[$column];
- }
- return $return;
- }
- /**
- * 등록/수정
- */
- private function _metaUpdate($column = '', $value = false, $description = ''): mixed
- {
- $column = trim($column);
- if (empty($column)) {
- return false;
- }
- $oldValue = $this->item($column);
- if (empty($value) && $value != 0) {
- $value = '';
- }
- if ($value === $oldValue) {
- return false;
- }
- if (false === $oldValue) {
- return $this->_addMeta($column, $value, $description); // 새로 추가
- }
- return $this->_updateMeta($column, $value, $description); // 수정
- }
- /**
- * 등록
- */
- private function _addMeta($column = '', $value = '', $description = ''): mixed
- {
- $column = trim($column);
- if (empty($column)) {
- return false;
- }
- $insertData = [
- $this->metaKey => $column,
- $this->metaValue => $value,
- $this->metaDescription => $description
- ];
- return $this->insert($insertData);
- }
- /**
- * 수정
- */
- private function _updateMeta($column = '', $value = '', $description = ''): mixed
- {
- $column = trim($column);
- if (empty($column)) {
- return false;
- }
- $updateData = [
- $this->metaValue => $value,
- $this->metaDescription => $description
- ];
- return $this->where($this->metaKey, $column)->update($updateData);
- }
- }
|