PanelModule.php 2.45 KB
Newer Older
Oscar S. Siordia's avatar
Oscar S. Siordia committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<?php

class PanelModule extends CWebModule
{
	/**
	 * @var string
	 * @desc hash method (md5,sha1 or algo hash function http://www.php.net/manual/en/function.hash.php)
	 */
	public $hash='md5';

	/**
	 * @var array
	 * @desc Profile model relation from other models
	 */
	public $profileRelations = array();

	public $fieldsMessage = '';

	/**
	 * @var int
	 * @desc Remember Me Time (seconds), defalt = 2592000 (30 days)
	 */
	public $rememberMeTime = 2592000; // 30 days

	public $returnUrl = array("dashboard/index");

	public function init()
	{
		// this method is called when the module is being created
		// you may place code here to customize the module or the application

		// import the module-level models and components
		$this->setImport(array(
			'panel.models.*',
			'panel.components.*',
		));
	}

	public function beforeControllerAction($controller, $action)
	{
		if(parent::beforeControllerAction($controller, $action))
		{
			// this method is called before any module controller action is performed
			// you may place customized code here
			return true;
		}
		else
			return false;
	}

	/**
	 * @return hash string.
	 */
	public static function encrypting($string="", $salt="") {
		$hash = Yii::app()->getModule('panel')->hash;
		if ($hash=="md5")
			return md5($string);
		if ($hash=="sha1")
			return sha1($string);
        if ($hash=='blowfish' && $salt=='')
            return crypt($string, Randomness::blowfishSalt());
        if ($hash=='blowfish' && !empty($salt))
            return crypt($string, $salt);
		else
			return hash($hash,$string);
	}

	/**
	 * Return safe user data.
	 * @param user id not required
	 * @return user object or false
	 */
	static private $_users=array();

	public static function user($id=0,$clearCache=false) {
        if (!$id&&!Yii::app()->user->isGuest)
            $id = Yii::app()->user->id;
		if ($id) {
            if (!isset(self::$_users[$id])||$clearCache)
                self::$_users[$id] = User::model()->with(array('profile'))->findbyPk($id);
			return self::$_users[$id];
        } else return false;
	}

	/**
	 * @var array
	 * @desc Behaviors for models
	 */
	public $componentBehaviors=array();

	public function getBehaviorsFor($componentName){
	    if (isset($this->componentBehaviors[$componentName])) {
	        return $this->componentBehaviors[$componentName];
	    } else {
	        return array();
	    }
	}
       
}