Просмотр файла vkclone-0.0.1/protected/models/Settings.php

Размер файла: 3.71Kb
<?php
/**
 * Base class of settings category model.
 * It pretends CModel to make it possible use this object with CActiveForm widget.
 */
class Settings extends CFormModel {
	static private $_instance;

	/**
	 * site settings
	 */
	public $siteClosed = false;
	public $siteClosedMessage = null;

	public $language = 'en';
	public $timezone = 'Europe/Moscow';

	/**
	 * users settings
	 */
	public $registrationClosed = false;
	public $registrationClosedMessage = null;
	public $authenticationClosed = false;
	public $onlineMinutesLimit = 10;

	/**
	 * DB table that stores all settings
	 */
	const SETTINGS_TABLE = '{{settings}}';
	/**
	 * Static array to store all settings retrieved from DB
	 */
	static private $config;

	/**
	 * validation rules
	 */
	public function rules() {
		return array(
			// site
			array('registrationClosed, authenticationClosed', 'boolean'),
			array('onlineMinutesLimit', 'numerical', 'integerOnly' => true, 'min' => 1),
			array('registrationClosedMessage', 'safe'),

			// users
			array('siteClosed', 'boolean'),
			array('siteClosedMessage', 'safe'),
			array('language', 'in', 'range' => array_keys($this->possibleLanguages)),
			array('timezone', 'in', 'range' => array_keys($this->possibleTimezones)),
		);
	}

	/**
	 * field labels
	 */
	public function attributeLabels() {
		return array(
			//  users
			'registrationClosed' => Yii::t('Admin', 'Registration closed'),
			'registrationClosedMessage' => Yii::t('Admin', 'Reason of closed registration'),
			'authenticationClosed' => Yii::t('Admin', 'Authentication closed'),

			// site
			'siteClosed' => Yii::t('Admin', 'Closing state'),
			'siteClosedMessage' => Yii::t('Admin', 'Reason of site closing'),
			'language' => Yii::t('Admin', 'Site default language'),
			'timezone' => Yii::t('Admin', 'Site default timezone'),
		);
	}

	/**
	 * Retrieves settings from database
	 */
	public function afterConstruct() {
		parent::afterConstruct();
		self::$config = $this->attributes;

		$settings = Yii::app()->db->createCommand()->select('name, value')->from(self::SETTINGS_TABLE)->queryAll();
		foreach ($settings as $settingsItem)
			if (array_key_exists($settingsItem['name'], self::$config))
				self::$config[$settingsItem['name']]
					= $this->$settingsItem['name']
						= unserialize($settingsItem['value']);
	}

	/**
	 * Saves setting
	 */
	public function set($name, $value) {
		if (array_key_exists($name, self::$config)) {
			self::$config[$name] = $value;
			if (Yii::app()->db->createCommand()
				->select('count(*)')
				->from(self::SETTINGS_TABLE)
				->where('name = :name', array(':name' => $name))->queryScalar() == 1)
				Yii::app()->db->createCommand()->update(
					self::SETTINGS_TABLE,
					array('value' => serialize($value)),
					'name = :name',
					array(':name' => $name));
			else
				Yii::app()->db->createCommand()->insert(
					self::SETTINGS_TABLE,
					array('name' => $name, 'value' => serialize($value)));
			$this->$name = $value;
		} else
			throw new Exception('Unknow settings item ['.$name.'] in category "'.$this->category().'"!');
	}

	/**
	 * Returns setting
	 */
	public function get($name) {
		if (array_key_exists($name, self::$config))
			return self::$config[$name];
		else
			throw new Exception('Unknow settings item ['.$name.']!');
	}

	/**
	 * Saves all settings items that has been changed
	 */
	public function save() {
		foreach (self::$config as $attribute => $value) {
			if ($this->$attribute !== $value)
				$this->set($attribute, $this->$attribute);
		}
	}

	/**
	 * Returns settings category
	 */
	public function category() {
		return $this->getScenario();
	}

	/**
	 * Returns settngs instance
	 */
	static public function instance() {
		return !empty(self::$_instance)
			? self::$_instance
			: (self::$_instance = new self);
	}
}