HEX
Server: Apache
System: Linux srv-plesk28.ps.kz 5.14.0-284.18.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 29 17:06:27 EDT 2023 x86_64
User: greencl1 (10085)
PHP: 8.1.33
Disabled: apache_setenv,dl,eval,exec,openlog,passthru,pcntl_exec,pcntl_fork,popen,posix_getpwuid,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,socket_create,socket_create_listen,socket_create_pair,syslog,system,socket_listen,stream_socket_server
Upload Files
File: /var/www/vhosts/greenclinic.kz/test.greenclinic.kz/modules/backend/traits/PreferenceMaker.php
<?php namespace Backend\Traits;

use Str;
use Backend\Models\UserPreference;

/**
 * Preference Maker Trait
 *
 * Adds methods for modifying user preferences in a controller class, or a class
 * that contains a `$controller` property referencing a controller.
 */
trait PreferenceMaker
{
    /**
     * Cache for retrieved user preferences.
     *
     * @var array
     */
    protected static $preferenceCache = [];

    /**
     * Saves a widget related key/value pair in to the users preferences
     * @param string $key Unique key for the data store.
     * @param mixed $value The value to store.
     * @return void
     */
    public function putUserPreference(string $key, $value): void
    {
        $preferences = $this->getUserPreferences();
        $preferences[$key] = $value;

        $this->getPreferenceStorage()->set($this->getPreferenceKey(), $preferences);

        // Re-cache user preferences
        self::$preferenceCache[$this->getPreferenceKey()] = $preferences;
    }

    /**
     * Retrieves a widget related key/value pair from the user preferences
     *
     * @param string $key Unique key for the data store.
     * @param mixed $default A default value to use when value is not found.
     * @return mixed
     */
    public function getUserPreference(string $key = null, $default = null)
    {
        $preferences = $this->getUserPreferences();

        return (isset($preferences[$key])) ? $preferences[$key] : $default;
    }

    /**
     * Retrieves and caches all user preferences for this particular controller/widget.
     *
     * @return array
     */
    public function getUserPreferences(): array
    {
        if (isset(self::$preferenceCache[$this->getPreferenceKey()])) {
            return self::$preferenceCache[$this->getPreferenceKey()];
        }

        $preferences = $this->getPreferenceStorage()->get($this->getPreferenceKey(), []);

        // Cache user preferences
        self::$preferenceCache[$this->getPreferenceKey()] = $preferences;

        return $preferences;
    }

    /**
     * Clears a single preference key from the user preferences for this controller/widget.
     *
     * @param string $key Unique key for the data store.
     * @return void
     */
    public function clearUserPreference(string $key): void
    {
        $preferences = $this->getUserPreferences();

        if (!isset($preferences[$key])) {
            return;
        }

        unset($preferences[$key]);

        if (count($preferences)) {
            $this->getPreferenceStorage()->set($this->getPreferenceKey(), $preferences);

            // Re-cache user preferences
            self::$preferenceCache[$this->getPreferenceKey()] = $preferences;
        } else {
            // Remove record from user preferences
            $this->clearUserPreferences();
        }
    }

    /**
     * Clears all user preferences for this controller/widget.
     *
     * @return void
     */
    public function clearUserPreferences(): void
    {
        $this->getPreferenceStorage()->reset($this->getPreferenceKey());

        self::$preferenceCache[$this->getPreferenceKey()] = [];
    }

    /**
     * Returns a unique identifier for this widget and controller action for preference storage.
     *
     * @return string
     */
    protected function getPreferenceKey(): string
    {
        $controller = (property_exists($this, 'controller') && $this->controller)
            ? $this->controller
            : $this;

        $uniqueId = (method_exists($this, 'getId')) ? $this->getId() : $controller->getId();

        // Removes Class name and "Controllers" directory
        $rootNamespace = Str::getClassId(Str::getClassNamespace(Str::getClassNamespace($controller)));

        // The controller action is intentionally omitted, preferences should be shared for all actions
        return $rootNamespace . '::' . strtolower(class_basename($controller)) . '.' . strtolower($uniqueId);
    }

    /**
     * Specifies the model used for storing the user preferences.
     *
     * @return October\Rain\Database\Model
     */
    protected function getPreferenceStorage()
    {
        return UserPreference::forUser();
    }
}