model->table = 'backend_user_preferences'; } /** * Create an instance of the settings model, intended as a static method */ public function instance() { if (isset(self::$instances[$this->recordCode])) return self::$instances[$this->recordCode]; $item = UserPreferences::forUser(); $item = $item->scopeFindRecord($this->model, $this->recordCode, $item->userContext)->first(); if (!$item) { $this->model->initSettingsData(); if (method_exists($this->model, 'forceSave')) $this->model->forceSave(); else $this->model->save(); $this->model->reload(); $item = $this->model; } return self::$instances[$this->recordCode] = $item; } /** * Checks if the model has been set up previously, intended as a static method */ public function isConfigured() { return UserPreferences::forUser()->findRecord($this->recordCode, $item->userContext)->count() > 0; } /** * Before the model is saved, ensure the record code is set * and the jsonable field values */ public function beforeModelSave() { $preferences = UserPreferences::forUser(); list($namespace, $group, $item) = $preferences->parseKey($this->recordCode); $this->model->item = $item; $this->model->group = $group; $this->model->namespace = $namespace; $this->model->user_id = $preferences->userContext->id; if ($this->fieldValues) $this->model->value = $this->fieldValues; } /** * Checks if a key is legitimate or should be added to * the field value collection */ protected function isKeyAllowed($key) { /* * Let the core columns through */ if ($key == 'namespace' || $key == 'group') return true; return parent::isKeyAllowed($key); } }