Advertisement
mithereal

mithereals-yumuser

Aug 18th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.94 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This is the model class for a User in Yum
  5.  *
  6.  * The followings are the available columns in table '{{users}}':
  7.  * @property integer $id
  8.  * @property string $username
  9.  * @property string $password
  10.  * @property string $activationKey
  11.  * @property integer $createtime
  12.  * @property integer $lastvisit
  13.  * @property integer $superuser
  14.  * @property integer $status
  15.  *
  16.  * Relations
  17.  * @property YumProfile $profile
  18.  * @property array $roles array of YumRole
  19.  * @property array $users array of YumUser
  20.  *
  21.  * Scopes:
  22.  * @property YumUser $active
  23.  * @property YumUser $notactive
  24.  * @property YumUser $banned
  25.  * @property YumUser $superuser
  26.  *
  27.  */
  28. class YumUser extends YumActiveRecord {
  29.     const STATUS_NOTACTIVE = 0;
  30.     const STATUS_ACTIVE = 1;
  31.     const STATUS_BANNED = -1;
  32.     const STATUS_REMOVED = -2;
  33.     const STATUS_NOCONTRACT = -4;
  34.     const STATUS_REGISTER_FB = -5;
  35.  
  36.     public $username;
  37.     public $password;
  38.     public $activationKey;
  39.     public $password_changed = false;
  40.     //public $status;
  41.  
  42.     public function behaviors() {
  43.         return array(
  44.                 'CAdvancedArBehavior' => array(
  45.                     'class' => 'application.modules.user.components.CAdvancedArBehavior'));
  46.     }
  47.  
  48.     public static function model($className=__CLASS__) {
  49.         return parent::model($className);
  50.     }
  51.  
  52.     public function delete() {
  53.         if(Yum::module()->trulyDelete)
  54.             return parent::delete();
  55.         else {
  56.             $this->status = self::STATUS_REMOVED;
  57.             return $this->save(false, array('status'));
  58.         }
  59.     }
  60.  
  61.     public function afterDelete() {
  62.         if(Yum::hasModule('profiles') && $this->profile !== null)
  63.             $this->profile->delete();
  64.  
  65.         Yum::log(Yum::t('User {username} (id: {id}) has been deleted', array(
  66.                         '{username}' => $this->username,
  67.                         '{id}' => $this->id)));
  68.         return parent::afterDelete();
  69.     }
  70.  
  71.     public function isOnline() {
  72.         return $this->lastaction > time() - Yum::module()->offlineIndicationTime;
  73.     }
  74.  
  75.     // If Online status is enabled, we need to set the timestamp of the
  76.     // last action when a user does something
  77.     public function setLastAction() {
  78.         if(Yii::app()->user->isGuest) {
  79.             $this->lastaction = time();
  80.             return $this->save();
  81.         }
  82.     }
  83.  
  84.     public function getLogins() {
  85.         $sql = "select count(*) from activities where user_id = {$this->id} and action = 'login'";
  86.         $result = Yii::app()->db->createCommand($sql)->queryAll();
  87.         return $result[0]['count(*)'];
  88.     }
  89.  
  90.     public function logout() {
  91.         if(Yum::module()->enableOnlineStatus && !Yii::app()->user->isGuest) {
  92.             $this->lastaction = 0;
  93.             $this->save('lastaction');
  94.         }
  95.     }
  96.  
  97.     public function isActive() {
  98.         return $this->status == YumUser::STATUS_ACTIVE;
  99.     }
  100.  
  101.     // This function tries to generate a as human-readable password as possible
  102.     public static function generatePassword() {
  103.         $consonants = array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
  104.         $vocals = array("a","e","i","o","u");
  105.  
  106.         $password = '';
  107.  
  108.         srand((double) microtime() * 1000000);
  109.         for ($i = 1; $i <= 4; $i++) {
  110.             $password .= $consonants[rand(0, 19)];
  111.             $password .= $vocals[rand(0, 4)];
  112.         }
  113.         $password .= rand(0, 9);
  114.  
  115.         return $password;
  116.     }
  117.  
  118.     // Which memberships are bought by the user
  119.     public function getActiveMemberships() {
  120.         if(!Yum::hasModule('membership'))
  121.             return array();
  122.  
  123.         Yii::import('application.modules.membership.models.*');
  124.  
  125.         $roles = array();
  126.         if($this->memberships)
  127.             foreach($this->memberships as $membership) {
  128.                 if($membership->end_date > time())
  129.                     $roles[] = $membership->role;
  130.             }
  131.  
  132.         return $roles;
  133.     }
  134.  
  135.     public function search() {
  136.         $criteria = new CDbCriteria;
  137.  
  138.         if(Yum::hasModule('profile')) {
  139.             $criteria->with = array('profile');
  140.             if($this->profile)
  141.                 $criteria->compare('profile.email', $this->profile->email, true);
  142.  
  143.             if(isset($this->email))
  144.                 $criteria->addSearchCondition('profile.email',$this->email,true);
  145.         }
  146.  
  147.         $criteria->together = true;
  148.         $criteria->compare('t.id', $this->id, true);
  149.         $criteria->compare('t.username', $this->username, true);
  150.         $criteria->compare('t.status', $this->status);
  151.         $criteria->compare('t.superuser', $this->superuser);
  152.         $criteria->compare('t.createtime', $this->createtime, true);
  153.         $criteria->compare('t.lastvisit', $this->lastvisit, true);
  154.  
  155.         return new CActiveDataProvider(get_class($this), array(
  156.                     'criteria' => $criteria,
  157.                     'pagination' => array('pageSize' => 20),
  158.                     ));
  159.     }
  160.  
  161.     public function beforeValidate() {
  162.         if ($this->isNewRecord)
  163.             $this->createtime = time();
  164.  
  165.         return true;
  166.     }
  167.  
  168.     public function setPassword($password) {
  169.         if ($password != '') {
  170.             $this->password = YumUser::encrypt($password);
  171.             $this->lastpasswordchange = time();
  172.             $this->password_changed = true;
  173.             if(!$this->isNewRecord)
  174.                 return $this->save();
  175.             else
  176.                 return $this;
  177.         }
  178.     }
  179.  
  180.     public function afterSave() {
  181.         if(Yum::hasModule('profile') && Yum::module('profile')->enablePrivacysetting) {
  182.             // create a new privacy setting, if not already available
  183.             $setting = YumPrivacySetting::model()->findByPk($this->id);
  184.             if (!$setting) {
  185.                 $setting = new YumPrivacySetting();
  186.                 $setting->user_id = $this->id;
  187.                 $setting->save();
  188.             }
  189.  
  190.             if($this->isNewRecord) {
  191.                 Yum::log( Yum::t( 'A user has been created: user: {user}', array(
  192.                                 '{user}' => json_encode($this->attributes))));
  193.  
  194.  
  195.             }
  196.         }
  197.         return parent::afterSave();
  198.     }
  199.  
  200.     /**
  201.      * Returns resolved table name (incl. table prefix when it is set in db configuration)
  202.      * Following algorith of searching valid table name is implemented:
  203.      *  - try to find out table name stored in currently used module
  204.      *  - if not found try to get table name from UserModule configuration
  205.      *  - if not found user default {{users}} table name
  206.      * @return string
  207.      */
  208.     public function tableName() {
  209.         if (isset(Yum::module()->usersTable))
  210.             $this->_tableName = Yum::module()->usersTable;
  211.         elseif (isset(Yii::app()->modules['user']['usersTable']))
  212.             $this->_tableName = Yii::app()->modules['user']['usersTable'];
  213.         else
  214.             $this->_tableName = '{{users}}'; // fallback if nothing is set
  215.  
  216.         return Yum::resolveTableName($this->_tableName, $this->getDbConnection());
  217.     }
  218.  
  219.     public function rules() {
  220.         $passwordRequirements = Yum::module()->passwordRequirements;
  221.         $usernameRequirements = Yum::module()->usernameRequirements;
  222.  
  223.         $passwordrule = array_merge(array('password', 'YumPasswordValidator'), $passwordRequirements);
  224.  
  225.         $rules[] = $passwordrule;
  226.  
  227.         $rules[] = array('username', 'length',
  228.                 'max' => $usernameRequirements['maxLen'],
  229.                 'min' => $usernameRequirements['minLen'],
  230.                 'message' => Yum::t(
  231.                     'Username length needs to be between {minLen} and {maxlen} characters', array(
  232.                         '{minLen}' => $usernameRequirements['minLen'],
  233.                         '{maxLen}' => $usernameRequirements['maxLen'])));
  234.  
  235.         $rules[] = array('username',
  236.                 'unique',
  237.                 'message' => Yum::t("This user's name already exists."));
  238.         $rules[] = array(
  239.                 'username',
  240.                 'match',
  241.                 'pattern' => $usernameRequirements['match'],
  242.                 'message' => Yum::t($usernameRequirements['dontMatchMessage']));
  243.         //$rules[] = array('status', 'in', 'range' => array(0, 1, 2, 3, -1, -2, -4, -5));
  244.         $rules[] = array('superuser', 'in', 'range' => array(0, 1));
  245.         //$rules[] = array('username, createtime, lastvisit, lastpasswordchange, superuser, status', 'required');
  246.         $rules[] = array('username, createtime, lastvisit, lastpasswordchange, superuser', 'required');
  247.         $rules[] = array('notifyType, avatar', 'safe');
  248.         $rules[] = array('password', 'required', 'on' => array('insert'));
  249.     //  $rules[] = array('createtime, lastvisit, lastaction, superuser, status', 'numerical', 'integerOnly' => true);
  250.         $rules[] = array('createtime, lastvisit, lastaction, superuser', 'numerical', 'integerOnly' => true);
  251.  
  252.         if (Yum::hasModule('avatar')) {
  253.             // require an avatar image in the avatar upload screen
  254.             $rules[] = array('avatar', 'required', 'on' => 'avatarUpload');
  255.  
  256.             // if automatic scaling is deactivated, require the exact size 
  257.             $rules[] = array('avatar', 'EPhotoValidator',
  258.                     'allowEmpty' => true,
  259.                     'mimeType' => array('image/jpeg', 'image/png', 'image/gif'),
  260.                     'maxWidth' => Yum::module('avatar')->avatarMaxWidth,
  261.                     'maxHeight' => Yum::module('avatar')->avatarMaxWidth,
  262.                     'minWidth' => 50,
  263.                     'minHeight' => 50,
  264.                     'on' => 'avatarSizeCheck');
  265.         }
  266.         return $rules;
  267.     }
  268.  
  269.     public function hasRole($role_title) {
  270.         if(!Yum::hasModule('role'))
  271.             return false;
  272.  
  273.         foreach ($this->roles as $role)
  274.             if ($role->id == $role_title || $role->title == $role_title)
  275.                 return true;
  276.  
  277.         return false;
  278.     }
  279.  
  280.     public function getRoles() {
  281.         if(Yum::hasModule('role')) {
  282.             Yii::import('application.modules.role.models.*');
  283.             $roles = '';
  284.             foreach ($this->roles as $role)
  285.                 $roles .= ' ' . $role->title;
  286.  
  287.             return $roles;
  288.         }
  289.     }
  290.  
  291.     public function getPermissions() {
  292.         if(!Yum::hasModule('role'))
  293.             return array();
  294.  
  295.         Yii::import('application.modules.role.models.*');
  296.         $roles = $this->roles;
  297.         if(Yum::hasModule('membership'))
  298.             $roles = array_merge($roles, $this->getActiveMemberships());
  299.  
  300.         $permissions = array();
  301.         foreach($roles as $role) {
  302.             $sql = "select id, action.title from permission left join action on action.id = permission.action where type = 'role' and principal_id = {$role->id}";
  303.             foreach(Yii::app()->db->createCommand($sql)->query()->readAll() as $permission)
  304.                 $permissions[$permission['id']] = $permission['title'];
  305.         }
  306.  
  307.         return $permissions;
  308.     }
  309.  
  310.     public function can($action) {
  311.         foreach ($this->getPermissions() as $permission)
  312.             if ($permission == $action)
  313.                 return true;
  314.  
  315.         return false;
  316.     }
  317.  
  318.     public function relations() {
  319.         if(Yum::hasModule('profile'))
  320.             Yii::import('application.modules.profile.models.*');
  321.         return array(
  322.                 'permissions' => array(self::HAS_MANY, 'YumPermission', 'principal_id'),
  323.                 'managed_by' => array(self::HAS_MANY, 'YumPermission', 'subordinate_id'),
  324.                 'messages' => array(self::HAS_MANY, 'YumMessage', 'to_user_id', 'order' => 'messages.id DESC'),
  325.                 'sent_messages' => array(self::HAS_MANY, 'YumMessage', 'from_user_id'),
  326.                 'visits' => array(self::HAS_MANY, 'YumProfileVisit', 'visited_id'),
  327.                 'visited' => array(self::HAS_MANY, 'YumProfileVisit', 'visitor_id'),
  328.                 'profile' => array(self::HAS_MANY, 'YumProfile', 'user_id', 'order' => 'profile.profile_id DESC'),
  329.                 'friendships' => array(self::HAS_MANY, 'YumFriendship', 'inviter_id'),
  330.                 'friendships2' => array(self::HAS_MANY, 'YumFriendship', 'friend_id'),
  331.                 'friendship_requests' => array(self::HAS_MANY, 'YumFriendship', 'friend_id', 'condition' => 'status = 1'), // 1 = FRIENDSHIP_REQUEST
  332.                 'roles' => array(self::MANY_MANY, 'YumRole', 'user_has_role(user_id, role_id)'),
  333.                 'memberships' => array(self::HAS_MANY, 'YumMembership', 'user_id'),
  334.                 'privacy' => array(self::HAS_ONE, 'YumPrivacySetting', 'user_id'),
  335.                 );
  336.     }
  337.  
  338.     public function isFriendOf($invited_id) {
  339.         foreach ($this->getFriendships() as $friendship) {
  340.             if ($friendship->inviter_id == $this->id && $friendship->friend_id == $invited_id)
  341.                 return $friendship->status;
  342.         }
  343.  
  344.         return false;
  345.     }
  346.  
  347.     public function getFriendships() {
  348.         $condition = 'inviter_id = :uid or friend_id = :uid';
  349.         return YumFriendship::model()->findAll($condition, array(
  350.                     ':uid' => $this->id));
  351.     }
  352.  
  353.     // Friends can not be retrieve via the relations() method because a friend
  354.     // can either be in the invited_id or in the friend_id column.
  355.     // set $everything to true to also return pending and rejected friendships
  356.     public function getFriends($everything = false) {
  357.         if ($everything)
  358.             $condition = 'inviter_id = :uid';
  359.         else
  360.             $condition = 'inviter_id = :uid and status = 2';
  361.  
  362.         $friends = array();
  363.         Yii::import('application.modules.friendship.models.YumFriendship');
  364.         $friendships = YumFriendship::model()->findAll($condition, array(
  365.                     ':uid' => $this->id));
  366.         if ($friendships != NULL && !is_array($friendships))
  367.             $friendships = array($friendships);
  368.  
  369.         if ($friendships)
  370.             foreach ($friendships as $friendship)
  371.                 $friends[] = YumUser::model()->findByPk($friendship->friend_id);
  372.  
  373.         if ($everything)
  374.             $condition = 'friend_id = :uid';
  375.         else
  376.             $condition = 'friend_id = :uid and status = 2';
  377.  
  378.         $friendships = YumFriendship::model()->findAll($condition, array(
  379.                     ':uid' => $this->id));
  380.  
  381.         if ($friendships != NULL && !is_array($friendships))
  382.             $friendships = array($friendships);
  383.  
  384.  
  385.         if ($friendships)
  386.             foreach ($friendships as $friendship)
  387.                 $friends[] = YumUser::model()->findByPk($friendship->inviter_id);
  388.  
  389.         return $friends;
  390.     }
  391.  
  392.     // Registers a user
  393.     public function register($username=null, $password=null, $profile=null, $status=null) {
  394.         if ($username !== null && $password !== null) {
  395.             // Password equality is checked in Registration Form
  396.             $this->username = $username;
  397.             $this->password = $this->encrypt($password);
  398.         }
  399.         $this->activationKey = $this->generateActivationKey(false, $password);
  400.         $this->createtime = time();
  401.         $this->superuser = 0;
  402.  
  403.         // Users stay banned until they confirm their email address.
  404.  
  405.  
  406.         if($status !== null){
  407.             Yii::log("status is $status",'info');
  408.             $this->status = self::STATUS_NOCONTRACT;
  409.         }else{
  410.             Yii::log("status is null",'info');
  411.         $this->status = self::STATUS_NOTACTIVE;
  412.         }
  413.  
  414.        
  415.        
  416.  
  417.         if(!($profile instanceof YumProfile)) {
  418.             $email = $profile;
  419.             $this->profile = new YumProfile;
  420.             $this->profile->email = $email;
  421.             $profile = $this->profile;
  422.         }
  423.  
  424.         if($this->save()) {
  425.             $profile->user_id = $this->id;
  426.             $profile->save();
  427.             $this->profile = $profile;
  428.             Yum::log(Yum::t('User {username} registered. Generated activation Url is {activation_url} and has been sent to {email}', array(
  429.                             '{username}' => $this->username,
  430.                             '{email}' => $profile->email,
  431.                             '{activation_url}' => $this->getActivationUrl())));
  432.  
  433.             return $this;
  434.         }
  435.  
  436.         return false;
  437.     }
  438.  
  439.     public function getActivationUrl() {
  440.         $activationUrl = Yum::module('registration')->activationUrl;
  441.         if(is_array($activationUrl) && isset($this->profile)) {
  442.             $activationUrl = $activationUrl[0];
  443.             $params['key'] = $this->activationKey;
  444.             $params['email'] = $this->profile->email;
  445.  
  446.             return Yii::app()->controller->createAbsoluteUrl($activationUrl, $params);
  447.         }
  448.             return Yum::t('Activation Url can not be retrieved');
  449.     }
  450.  
  451.     public function isPasswordExpired() {
  452.         $distance = Yii::app()->getModule('user')->password_expiration_time * 60 * 60;
  453.         return $this->lastpasswordchange - $distance > time();
  454.     }
  455.  
  456.     /**
  457.      * Activation of an user account.
  458.      * If everything is set properly, and the emails exists in the database,
  459.      * and is associated with a correct user, and this user has the status
  460.      * NOTACTIVE and the given activationKey is identical to the one in the
  461.      * database then generate a new Activation key to avoid double activation,
  462.      * set the status to ACTIVATED and save the data
  463.      * Error Codes:
  464.      * -1 : User is not inactive, it can not be activated
  465.      * -2 : Wrong activation key
  466.      * -3 : Profile found, but no user - database inconsistency?
  467.      */
  468.     public static function activate($email, $key) {
  469.         Yii::import('application.modules.profile.models.*');
  470.  
  471.         if($profile = YumProfile::model()->find("email = :email", array(
  472.                         ':email' => $email))) {
  473.             if ($user = $profile->user) {  
  474.                 if ($user->status != self::STATUS_NOTACTIVE)
  475.                     return -1;
  476.                 if ($user->activationKey == $key) {
  477.                     $user->activationKey = $user->generateActivationKey(true);
  478.                     //$user->status = self::STATUS_ACTIVE;
  479.                     if($user->status == self::STATUS_NOTACTIVE){
  480.                         $user->status = self::STATUS_NOCONTRACT;
  481.                         }else if(isset($contract)){
  482.                         $user->status = self::STATUS_ACTIVATED;
  483.                         }
  484.                     if($user->save(false, array('activationKey', 'status'))) {
  485.                         Yum::log(Yum::t('User {username} has been activated', array(
  486.                                         '{username}' => $user->username)));
  487.                         if(Yum::hasModule('messages')
  488.                                 && Yum::module('registration')->enableActivationConfirmation) {
  489.                             Yii::import('application.modules.messages.models.YumMessage');
  490.                             YumMessage::write($user, 1,
  491.                                     Yum::t('Your activation succeeded'),
  492.                                     YumTextSettings::getText('text_email_activation', array(
  493.                                             '{username}' => $user->username,
  494.                                             '{link_login}' =>
  495.                                             Yii::app()->controller->createUrl('//user/user/login'))));
  496.                         }
  497.  
  498.                         return $user;
  499.                     }
  500.                 } else return -2;
  501.             } else return -3;
  502.         }
  503.         return false;
  504.     }
  505.  
  506.     /**
  507.      * @params boolean $activate Whether to generate activation key when user is registering first time (false)
  508.      * or when it is activating (true)
  509.      * @params string $password password entered by user
  510.      * @param array $params, optional, to allow passing values outside class in inherited classes
  511.      * By default it uses password and microtime combination to generated activation key
  512.      * When user is activating, activation key becomes micortime()
  513.      * @return string
  514.      */
  515.     public function generateActivationKey($activate=false) {
  516.         $this->activationKey = $activate
  517.             ? YumUser::encrypt(microtime())
  518.             : YumUser::encrypt(microtime() . $this->password);
  519.  
  520.         $this->save(false, array('activationKey'));
  521.         return $this->activationKey;
  522.     }
  523.  
  524.     public function attributeLabels() {
  525.         return array(
  526.                 'id' => Yum::t('#'),
  527.                 'username' => Yum::t("Username"),
  528.                 'password' => Yum::t("Password"),
  529.                 'verifyPassword' => Yum::t("Retype password"),
  530.                 'verifyCode' => Yum::t("Verification code"),
  531.                 'activationKey' => Yum::t("Activation key"),
  532.                 'createtime' => Yum::t("Registration date"),
  533.                 'lastvisit' => Yum::t("Last visit"),
  534.                 'lastaction' => Yum::t("Online status"),
  535.                 'superuser' => Yum::t("Superuser"),
  536.                 'status' => Yum::t("Status"),
  537.                 'avatar' => Yum::t("Avatar image"),
  538.                 );
  539.     }
  540.  
  541.     /**
  542.      * This function is used for password encryption.
  543.      * @return hash string.
  544.      */
  545.     public static function encrypt($string = "") {
  546.         $salt = Yum::module()->salt;
  547.         $hashFunc = Yum::module()->hashFunc;
  548.         $string = sprintf("%s%s%s", $salt, $string, $salt);
  549.  
  550.         if (!function_exists($hashFunc))
  551.             throw new CException('Function `' . $hashFunc . '` is not a valid callback for hashing algorithm.');
  552.  
  553.         return $hashFunc($string);
  554.     }
  555.  
  556.     public function limit($limit = 10)
  557.     {
  558.         $this->getDbCriteria()->mergeWith(array(
  559.                     'limit'=>$limit,
  560.                     ));
  561.         return $this;
  562.     }  
  563.  
  564.     public function scopes() {
  565.         return array(
  566.                 'active' => array('condition' => 'status=' . self::STATUS_ACTIVE,),
  567.                 'notactive' => array('condition' => 'status=' . self::STATUS_NOTACTIVE,),
  568.                 'banned' => array('condition' => 'status=' . self::STATUS_BANNED,),
  569.                 'nocontract' => array('condition' => 'status=' . self::STATUS_NOCONTRACT,),
  570.                 'fbnotactive' => array('condition' => 'status=' . self::STATUS_REGISTER_FB,),
  571.                 'superuser' => array('condition' => 'superuser = 1',),
  572.                 'public' => array(
  573.                     'join' => 'LEFT JOIN privacysetting on t.id = privacysetting.user_id',
  574.                     'condition' => 'appear_in_search = 1',),
  575.                 );
  576.     }
  577.  
  578.     public static function itemAlias($type, $code=NULL) {
  579.         $_items = array(
  580.                 'UserStatus' => array(
  581.                     '0' => Yum::t('Not active'),
  582.                     '1' => Yum::t('Active'),
  583.                     '-1' => Yum::t('Banned'),
  584.                     '-2' => Yum::t('Deleted'),
  585.                     '-4' => Yum::t('No Contract'),
  586.                     '-5' => Yum::t('Facebook not active'),
  587.                     ),
  588.                 'AdminStatus' => array(
  589.                     '0' => Yum::t('No'),
  590.                     '1' => Yum::t('Yes'),
  591.                     ),
  592.                 );
  593.         if (isset($code))
  594.             return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
  595.         else
  596.             return isset($_items[$type]) ? $_items[$type] : false;
  597.     }
  598.  
  599.     /**
  600.      * Get all users with a specified role.
  601.      * @param string $roleTitle title of role to be searched
  602.      * @return array users with specified role. Null if none are found.
  603.      */
  604.     public static function getUsersByRole($roleTitle) {
  605.         $role = YumRole::model()->findByAttributes(array('title' => $roleTitle));
  606.         return $role ? $role->users : null;
  607.     }
  608.  
  609.     /**
  610.      * Return admins.
  611.      * @return array syperusers names
  612.      */
  613.     public static function getAdmins() {
  614.         $admins = YumUser::model()->active()->superuser()->findAll();
  615.         $returnarray = array();
  616.         foreach ($admins as $admin)
  617.             array_push($returnarray, $admin->username);
  618.         return $returnarray;
  619.     }
  620.  
  621.     public function getAvatar($thumb = false) {
  622.         if (Yum::hasModule('avatar') && $this->profile) {
  623.             $return = '<div class="avatar">';
  624.  
  625.             $options = array();
  626.             if ($thumb)
  627.                 $options = array('style' => 'width: 50px; height:50px;');
  628.             else
  629.                 $options = array('style' => 'width: '.Yum::module('avatar')->avatarDisplayWidth.'px;');
  630.  
  631.             if (isset($this->avatar) && $this->avatar)
  632.                 $return .= CHtml::image(Yii::app()->baseUrl . '/'
  633.                         . $this->avatar, 'Avatar', $options);
  634.             else
  635.                 $return .= CHtml::image(Yii::app()->getAssetManager()->publish(
  636.                             Yii::getPathOfAlias('YumAssets.images') . ($thumb ? '/no_avatar_available_thumb.jpg' : '/no_avatar_available.jpg'),
  637.                             Yum::t('No image available'), array(
  638.                                 'title' => Yum::t('No image available'))));
  639.             $return .= '</div><!-- avatar -->';
  640.             return $return;
  641.         }
  642.     }
  643. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement