Guest User

Untitled

a guest
Jul 4th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.38 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This is the model class for table "members".
  5.  *
  6.  * The followings are the available columns in table 'members':
  7.  * @property integer $ID
  8.  * @property string $username
  9.  * @property integer $permissionLevel
  10.  * @property string $password
  11.  * @property string $email
  12.  * @property string $date
  13.  */
  14. class Members extends CActiveRecord
  15. {
  16.    /**
  17.     * Defining variables not present in the database
  18.     */
  19.    public $password_repeat;
  20.    //public $verifyCode;
  21.  
  22.     /**
  23.      * Returns the static model of the specified AR class.
  24.      * @param string $className active record class name.
  25.      * @return Members the static model class
  26.      */
  27.     public static function model($className=__CLASS__)
  28.     {
  29.         return parent::model($className);
  30.     }
  31.  
  32.     /**
  33.      * @return string the associated database table name
  34.      */
  35.     public function tableName()
  36.     {
  37.         return 'members';
  38.     }
  39.  
  40.     /**
  41.      * @return array validation rules for model attributes.
  42.      */
  43.     public function rules()
  44.     {
  45.         // NOTE: you should only define rules for those attributes that
  46.         // will receive user inputs.
  47.         return array(
  48.             array('username', 'required'),
  49.          array('password', 'required'),
  50.        //  array('verifyCode', 'required'),
  51.          //array('password_repeat', 'required'),
  52.          // email has to be a valid email address
  53.             array('email', 'email'),
  54.             // verifyCode needs to be entered correctly
  55.             //array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
  56.             // The following rule is used to compare the passwords
  57.          array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'register'),        
  58.          // The following rule is used by search().
  59.             // Please remove those attributes that should not be searched.
  60.             //array('ID, username, permissionLevel, email, date', 'safe', 'on'=>'search'),
  61.          array('password', 'safe'),
  62.          array('password_repeat', 'safe'),
  63.         // array('verifyCode', 'safe'),
  64.         );
  65.     }
  66.  
  67.     /**
  68.      * @return array relational rules.
  69.      */
  70.     public function relations()
  71.     {
  72.         // NOTE: you may need to adjust the relation name and the related
  73.         // class name for the relations automatically generated below.
  74.         return array(
  75.         );
  76.     }
  77.  
  78.     /**
  79.      * @return array customized attribute labels (name=>label)
  80.      */
  81.     public function attributeLabels()
  82.     {
  83.         return array(
  84.             'ID' => 'ID',
  85.             'username' => 'Username',
  86.             'permissionLevel' => 'Permission Level',
  87.             'password' => 'Password',
  88.             'email' => 'Email',
  89.             'date' => 'Date',
  90.         // 'verifyCode' => 'Verification Code',
  91.         );
  92.     }
  93.  
  94.     /**
  95.      * Retrieves a list of models based on the current search/filter conditions.
  96.      * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  97.      */
  98.     public function search()
  99.     {
  100.         // Warning: Please modify the following code to remove attributes that
  101.         // should not be searched.
  102.  
  103.         $criteria=new CDbCriteria;
  104.  
  105.         $criteria->compare('ID',$this->ID);
  106.         $criteria->compare('username',$this->username,true);
  107.         $criteria->compare('permissionLevel',$this->permissionLevel);
  108.         $criteria->compare('password',$this->password,true);
  109.         $criteria->compare('email',$this->email,true);
  110.         $criteria->compare('date',$this->date,true);
  111.  
  112.         return new CActiveDataProvider($this, array(
  113.             'criteria'=>$criteria,
  114.         ));
  115.     }
  116.  
  117.    /**
  118.     * Checks if the username already exists in the database
  119.     * $return true if unique, false if not.
  120.     */
  121.    public function isUserUnique()
  122.    {
  123.       if ($this::model()->findByAttributes(array('username'=>$this->username)))
  124.          return false;
  125.       else
  126.          return true;
  127.    }
  128.    
  129.    /**
  130.     * Creates an ccount based on user input
  131.     * @return false upon failure, return true if successful.
  132.     */
  133.     public function createAccount()
  134.     {
  135.       if ($this->isUserUnique())
  136.       {
  137.          $member = new Members('register');
  138.          $member->username=$this->username;
  139.          $member->permissionLevel=2;
  140.          $member->password=crypt($this->password, "comaan11");
  141.          $member->email=$this->email;
  142.          $member->date=Date("Y-m-d H:i:s");
  143.          
  144.          $member->save();
  145.          print_r($member->getErrors());
  146.       }
  147.       else
  148.       {
  149.          $this->addError('username','A user with that name already exists.');
  150.          return false;
  151.       }
  152.          
  153.          
  154.       //for debugging
  155.       //print_r($member->getErrors());
  156.     }
  157. }
Add Comment
Please, Sign In to add comment