Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This is the model class for table "{{customer}}".
- *
- * The followings are the available columns in table '{{customer}}':
- *
- * @property integer $id
- * @property string $username
- * @property string $password
- * @property string $firstname
- * @property string $lastname
- * @property string $middlename
- * @property integer $active
- * @property string $date_create
- * @property string $last_login
- * @property float $balance
- * @property string $ip
- * @property string $token
- * @property string $role
- * @property string $email
- */
- class User extends CActiveRecord implements IUserLog {
- const ROLE_USER = 'user';
- const ROLE_CUSTOMER = 'customer';
- const ROLE_GUEST = 'guest';
- static public $roles = array(
- self::ROLE_USER => 'User',
- self::ROLE_CUSTOMER => 'Customer',
- // self::ROLE_GUEST => 'Guest'
- );
- static public $returnDataUser = array('username', 'firstname', 'lastname', 'date_create', 'balance', 'token');
- public $confirmPassword;
- public $token = null;
- public $verifyCode;
- static public $lableActive = array(
- '1' => 'Active',
- '0' => 'Not active'
- );
- /**
- * @return string the associated database table name
- */
- public function tableName() {
- return '{{user}}';
- }
- public function getLogId() {
- return $this->id;
- }
- public function getTypeUser() {
- return $this->role;
- }
- public function getRole() {
- return $this->role;
- }
- public function getFullName() {
- return $this->lastname . ' ' . $this->firstname . ' ' . $this->middlename;
- }
- public function getUsername() {
- return $this->username;
- }
- public function getBalance() {
- return $this->balance;
- }
- //todo add cache
- /**
- * @param $mark
- * @param CDbCriteria $criteria
- *
- * @return null
- * @throws CDbException
- * @throws CException
- */
- public function getFullAmountAndCountByMarkCustomer($mark, CDbCriteria $criteria) {
- if (HistoryBuy::getLabelStatus($mark) === null) {
- throw new CException('Undefined "mark"! Use value "mark" defined in class "' . __CLASS__ . '"');
- };
- $ids = implode(',', $this->getListIdShop());
- if ($ids == '') {
- $ids = 0;
- }
- $condition = ($criteria->condition !== '' ? $criteria->condition . ' AND ' : $criteria->condition) . 'mark=:marked';
- $params = CMap::mergeArray(array(':marked' => $mark), $criteria->params);
- $command = self::getDbConnection()->createCommand();
- $command->select('SUM(amount) as sum_amount, COUNT(amount) as sum_count_goods')
- ->from(HistoryBuy::model()->tableName())
- ->where($condition, $params);
- $result = $command->queryAll();
- return isset($result[0]) ? $result[0] : null;
- }
- //todo add cache
- /**
- * @param $mark
- * @param CDbCriteria $criteria
- *
- * @return null
- * @throws CDbException
- * @throws CException
- */
- public function getFullAmountAndCountByMarkUser($mark, CDbCriteria $criteria) {
- if (HistoryBuy::getLabelStatus($mark) === null) {
- throw new CException('Undefined "mark"! Use value "mark" defined in class "' . __CLASS__ . '"');
- };
- $condition = ($criteria->condition !== '' ? $criteria->condition . ' AND ' : $criteria->condition) . 'mark=:marked';
- $params = CMap::mergeArray(array(':marked' => $mark), $criteria->params);
- $command = self::getDbConnection()->createCommand();
- $command->select('SUM(amount) as sum_amount, COUNT(amount) as sum_count_goods')
- ->from(HistoryBuy::model()->tableName())
- ->where($condition, $params);
- $result = $command->queryAll();
- return isset($result[0]) ? $result[0] : null;
- }
- public function getDataForShop() {
- $user = new User();
- $arrayReturnColumn = array('id', 'username', 'last_login', 'date_create', 'balance', 'active');
- $select = implode(', ', $arrayReturnColumn);
- $sql = "SELECT {$select} FROM `{$user->tableName()}` WHERE id=:id ";
- $command = $user->getDbConnection()->createCommand($sql);
- $result = $command->queryRow(true, array(':id' => (int)Yii::app()->user->id));;
- if (empty($result)) {
- return array('error' => 'Not found user!');
- }
- $token = new TokensAuthenticate();
- $tokenNew = $token->addToken((int)Yii::app()->user->id)->token;
- $result['token'] = $tokenNew;
- return $result;
- }
- /**
- *
- * Получение списка id всех магазинов пользователя
- *
- * @return array
- * @throws CDbException
- */
- public function getListIdShop() {
- if ($this->role != self::ROLE_CUSTOMER) {
- return array();
- }
- $command = self::getDbConnection()->createCommand();
- $command->select('id,customer_id')
- ->from(Shops::model()->tableName())
- ->where('customer_id=:customer_id', array(':customer_id' => $this->id));
- $result = $command->queryAll();
- $ids = array();
- if (is_array($result) && count($result) > 0) {
- foreach ($result as $res) {
- $ids[] = $res['id'];
- }
- }
- return $ids;
- }
- /**
- * @return array validation rules for model attributes.
- */
- public function rules() {
- // NOTE: you should only define rules for those attributes that
- // will receive user inputs.
- return array(
- array('username, date_create, email, role', 'required'),
- array('username', 'unique'),
- array('active', 'numerical', 'integerOnly' => true),
- array('username', 'length', 'max' => 45),
- array('password, ip', 'length', 'max' => 100),
- array('firstname, lastname, middlename', 'length', 'max' => 65),
- array('last_login', 'safe'),
- array('email', 'email'),
- array('role', 'validRole'),
- array('confirmPassword, password', 'unsafe', 'on' => 'profile'),
- array('confirmPassword', 'checkConfirmPassword', 'on' => 'profile'),
- array('username', 'safe', 'on' => 'registration'),
- array('confirmPassword, password', 'required', 'on' => 'registration'),
- array('confirmPassword', 'checkConfirmPassword', 'on' => 'registration'),
- array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'registration'),
- array('balance', 'unsafe'),
- array('id, username, password, email, role, firstname, lastname, middlename, active, date_create, last_login, ip', 'safe', 'on' => 'search'),
- );
- }
- public function validRole($field, $params) {
- if (!isset(self::$roles[$this->role])) {
- $this->addError($field, "Invalid role.");
- }
- }
- public function checkConfirmPassword($field, $params) {
- if ($this->getScenario() == 'registration') {
- if ($this->password != $this->confirmPassword) {
- $this->addError($field, "Passwords do not match.");
- }
- } elseif ($this->getScenario() == 'profile') {
- if ($this->password != '' && ($this->password != $this->confirmPassword)) {
- $this->addError($field, "Passwords do not match.");
- }
- }
- }
- /**
- * @return array relational rules.
- */
- public function relations() {
- // NOTE: you may need to adjust the relation name and the related
- // class name for the relations automatically generated below.
- return array();
- }
- public function scopes() {
- return array(
- 'active' => array(
- 'condition' => 'active=1'
- )
- );
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public function attributeLabels() {
- return array(
- 'id' => 'ID',
- 'username' => 'Username',
- 'password' => 'Password',
- 'confirmPassword' => 'Confirm Password',
- 'firstname' => 'Firstname',
- 'lastname' => 'Lastname',
- 'middlename' => 'Middlename',
- 'active' => 'Active',
- 'date_create' => 'Date Create',
- 'last_login' => 'Last Login',
- 'balance' => 'Balance',
- 'ip' => 'Ip',
- 'role' => 'Role',
- 'email' => 'Email',
- );
- }
- public function beforeValidate() {
- if (!empty($this->username)) {
- $this->username = mb_strtolower($this->username);
- }
- if ($this->getScenario() == 'registration' && $this->isNewRecord) {
- $this->date_create = date('Y-m-d H:i:s');
- }
- return parent::beforeValidate();
- }
- public function beforeSave() {
- if ($this->getScenario() == 'registration' && $this->isNewRecord) {
- $this->username = trim($this->username);
- $this->password = md5(trim($this->password));
- }
- if ($this->getScenario() == 'profile'
- && $this->password != ''
- && $this->confirmPassword != ''
- && $this->confirmPassword == $this->password
- ) {
- $this->password = md5($this->password);
- }
- return parent::beforeSave();
- }
- /**
- * Retrieves a list of models based on the current search/filter conditions.
- *
- * Typical usecase:
- * - Initialize the model fields with values from filter form.
- * - Execute this method to get CActiveDataProvider instance which will filter
- * models according to data in model fields.
- * - Pass data provider to CGridView, CListView or any similar widget.
- *
- * @return CActiveDataProvider the data provider that can return the models
- * based on the search/filter conditions.
- */
- public function search() {
- // @todo Please modify the following code to remove attributes that should not be searched.
- $criteria = new CDbCriteria;
- $criteria->compare('id', $this->id);
- $criteria->compare('username', $this->username, true);
- $criteria->compare('password', $this->password, true);
- $criteria->compare('firstname', $this->firstname, true);
- $criteria->compare('lastname', $this->lastname, true);
- $criteria->compare('middlename', $this->middlename, true);
- $criteria->compare('active', $this->active);
- $criteria->compare('balance', $this->balance);
- $criteria->compare('date_create', $this->date_create, true);
- $criteria->compare('last_login', $this->last_login, true);
- $criteria->compare('ip', $this->ip, true);
- return new CActiveDataProvider($this, array(
- 'criteria' => $criteria,
- ));
- }
- /**
- * Returns the static model of the specified AR class.
- * Please note that you should have this exact method in all your CActiveRecord descendants!
- *
- * @param string $className active record class name.
- *
- * @return User the static model class
- */
- public static function model($className = __CLASS__) {
- return parent::model($className);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement