Customer.php 11.7 KB
<?php
    
    namespace common\models;
    
    use Yii;
    use yii\helpers\FileHelper;
    use yii\behaviors\TimestampBehavior;
    
    /**
     * This is the model class for table "customer".
     *
     * @property int      $id
     * @property string   $name
     * @property string   $secondname
     * @property string   $dignity
     * @property int      $gender
     * @property string   $birth
     * @property string   $citizenship
     * @property string   $passport
     * @property string   $email
     * @property string   $organization
     * @property int      $status
     * @property int      $language_id
     * @property string   $image
     * @property bool     $conference
     * @property bool     $geee
     * @property bool     $gere
     * New properties
     * @property  string  $phone_own
     * @property  string  $phone_office
     * @property  string  $phone_other
     * @property  string  $fax
     * @property  integer $passport_number
     * @property  integer $passport_date
     * @property  string  $passport_place
     * @property  integer $passport_valid
     * @property  boolean $need_visa
     * @property  string  $special_meal
     * @property boolean  $working_language_ru
     * @property boolean  $working_language_en
     * @property boolean  $working_language_fr
     * @property boolean  $meal_halal
     * @property boolean  $meal_kashrut
     * @property boolean  $meal_vegan
     * @property integer  $working_lang
     * @property string   $post_address ==> официальный почтовый адрес
     * @property integer  $created_at
     *
     * -- from 19.09
     * @property string $nationality
     * @property string $country_from
     * @property integer $user_work_status
     *
     *
     *
     *
     */
    class Customer extends \yii\db\ActiveRecord
    {
        public $file;
        
        public $acceptance;
        
        const ADMIN_SCENARIO = 'admin';

	    #----------------------------------------------------------------------
	    #                   Текущий статус пользователя
	    #----------------------------------------------------------------------
        const STATUS_NEW = 2;
        const STATUS_ACTIVE = 1;
        const STATUS_NO = 0;

	    #----------------------------------------------------------------------
	    #                  Специальная еда
	    #----------------------------------------------------------------------
        const MEAL_1 = 'Халяль';
        const MEAL_2 = 'Кашрут';
        const MEAL_3 = 'Вегитарианець';
        const MEAL_NONE = 'Не выбрано';


	    #----------------------------------------------------------------------
	    #                            Пол
	    #----------------------------------------------------------------------
        const MALE = '1';
        const FEMALE = '2';



        #----------------------------------------------------------------------
	    #                   Деловые статусы посетителя форума
        #----------------------------------------------------------------------

	    const WORK_STATUS_0 = 'International organization';
	    const WORK_STATUS_1 = 'Official delegation';
	    const WORK_STATUS_2 = 'Non-government organization';
	    const WORK_STATUS_3 = 'Expert';
	    const WORK_STATUS_4 = 'Press';


        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'customer';
        }
        
        public function behaviors()
        {
            return [
                [
                    'class'              => TimestampBehavior::className(),
                    'updatedAtAttribute' => false,
                ],
            ];
        }
        
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                    [
                        'gender',
                        'status',
                        'language_id',
                    ],
                    'default',
                    'value' => null,
                ],
                [
                    [
                        'gender',
                        'status',
                        'language_id',
                        'acceptance',
                        'working_lang',
                    ],
                    'integer',
                ],
                [
                    [ 'organization' ],
                    'string',
                ],
                [
                    [
                        'conference',
                        'geee',
                        'gere',
                        'need_visa',
                        'working_language_ru',
                        'working_language_en',
                        'working_language_fr',
                        'meal_halal',
                        'meal_kashrut',
                        'meal_vegan',
                    
                    ],
                    'boolean',
                ],
                
                [
                    [
                        'name',
                        'secondname',
                        'dignity',
                        'birth',
                        'citizenship',
                        'passport',
                        'email',
                        'image',
                        'passport_place',
                        'special_meal',
                        'passport_number',
                        'passport_date',
                        'passport_valid',
                        'post_address',
	                    'nationality',
	                    'country_from',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [
                        'phone_own',
                        'phone_office',
                        'phone_other',
                        'fax',
                    ],
                    'string',
                    'max' => 30,
                ],
                
                [
                    [ 'gender' ],
                    'in',
                    'range' => [
                        self::MALE,
                        self::FEMALE,
                    ],
                ],
	            [
		            [ 'user_work_status' ],
		            'in',
		            'range' => [
			            '0',
			            '1',
			            '2',
			            '3',
			            '4',
		            ],
	            ],
                [
                    [
                        'gender',
                        'organization',
                        'name',
                        #'secondname',
                        'birth',
                        'citizenship',
                        #'passport',
                        'email',
                        'post_address',
                        'passport_number',
                        'passport_date',
                        'passport_place',
                        'passport_valid',
                        'need_visa',
                        'phone_office',
                        'phone_other',
                        'need_visa',
	                    'nationality',
	                    'country_from',
	                    'user_work_status',
                    ],
                    'required',
                ],
                [
                    [
                        'acceptance',
                    ],
                    'required',
                    'except' => self::ADMIN_SCENARIO,
                ],
                [
                    [
                        'phone_office',
                        'phone_other',
                    ],
                    'match',
                    'pattern' => '#^\+?[\d\s]+$#',
                ],
                [
                    [ 'acceptance' ],
                    'compare',
                    'compareValue' => 1,
                ],
                [
                    'email',
                    'email',
                ],
                [
                    'file',
                    'file',
                    'skipOnEmpty'              => false,
                    'checkExtensionByMimeType' => false,
                    'extensions'               => [
                        'png',
                        'jpg',
                        'gif',
                    ],
                    'maxSize'                  => 1024 * 1024 * 1024 * 2,
                    'except'                   => self::ADMIN_SCENARIO,
                ],
            
            ];
        }
        
        /**
         * {@inheritdoc}
         */
        public function attributeLabels()
        {
            return [
                'id'              => Yii::t('app', 'ID'),
                'name'            => Yii::t('app', 'Name'),
                'secondname'      => Yii::t('app', 'Secondname'),
                'dignity'         => Yii::t('app', 'Dignity'),
                'gender'          => Yii::t('app', 'Gender'),
                'birth'           => Yii::t('app', 'Birth'),
                'citizenship'     => Yii::t('app', 'Citizenship'),
                'passport'        => Yii::t('app', 'Passport'),
                'email'           => Yii::t('app', 'Email'),
                'organization'    => Yii::t('app', 'Organization'),
                'status'          => Yii::t('app', 'Status'),
                'language_id'     => Yii::t('app', 'Language ID'),
                'image'           => Yii::t('app', 'Image'),
                'conference'      => Yii::t('app', 'Conference'),
                'geee'            => Yii::t('app', 'Geee'),
                'gere'            => Yii::t('app', 'Gere'),
                'need_visa'       => Yii::t('app', 'Need visa'),
                'passport_number' => Yii::t('app', 'Passport number'),
                'passport_date'   => Yii::t('app', 'Passport date'),
                'passport_place'  => Yii::t('app', 'Passport place'),
                'passport_valid'  => Yii::t('app', 'Passport valid'),
                'phone_own'       => Yii::t('app', 'Phone own'),
                'phone_office'    => Yii::t('app', 'Phone office'),
                'phone_other'     => Yii::t('app', 'Phone other'),
                'fax'             => Yii::t('app', 'fax'),
                'acceptance'      => Yii::t('app', 'acceptance'),
                'working_lang'    => Yii::t('app', 'Working language'),
                'special_meal'    => Yii::t('app', 'Special meal'),
                'post_address'    => Yii::t('app', 'Post address'),

                'nationality'     => Yii::t('app', 'Nationality'),
                'country_from'    => Yii::t('app', 'Country from'),
                'user_work_status'    => Yii::t('app', 'Work status'),

            ];
        }
        
        public function upload()
        {
            /**
             * @var \yii\web\UploadedFile $file ;
             */
            if (!empty($this->file) and $this->validate()) {
                if (!file_exists(\Yii::getAlias('@storage/customers/'))) {
                    FileHelper::createDirectory(\Yii::getAlias('@storage/customers/'));
                }
                $filename = $this->file->baseName . '_' . Yii::$app->security->generateRandomString(
                        5
                    ) . '.' . $this->file->extension;
                if ($this->file->saveAs(\Yii::getAlias('@storage/customers/') . $filename)) {
                    $this->image = '/storage/customers/' . $filename;
                    $this->file->name = $filename;
                    return true;
                }
                return false;
                
            }
            return true;
        }
    }