Customer.php 8.65 KB
<?php
    
    namespace common\models;
    
    use Yii;
    use yii\helpers\FileHelper;

    /**
     * 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 ==> официальный почтовый адрес
     */
    class Customer extends \yii\db\ActiveRecord
    {
        public $file;

        public $acceptance;

        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';
        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'customer';
        }
        
        /**
         * {@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'
                    ],
                    'string',
                    'max' => 255,
                ],
	            [
	            	[
	            		'phone_own',
			            'phone_office',
			            'phone_other',
			            'fax'
			        ],
		            'string',
		            'max' => 30,
	            ],

                [
                    [ 'gender' ],
                    'in',
                    'range' => [
                        self::MALE,
                        self::FEMALE,
                    ],
                ],
                [
                    [
                        'gender',
                        'organization',
                        'name',
                        #'secondname',
                        'birth',
                        'citizenship',
                        #'passport',
                        'email',
	                    'post_address',
	                    'passport_number',
	                    'passport_date',
	                    'passport_place',
	                    'passport_valid',
	                    'need_visa',
	                    'acceptance',
	                    'phone_office',
	                    'phone_other',
                    ],
                    'required',
                ],
	            [['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]
            
            ];
        }
        
        /**
         * {@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'),

            ];
        }
        
        
        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;
        }
    }