Order.php 2.66 KB
<?php
namespace app\models;

use Yii;

class Order extends \yii\db\ActiveRecord
{
    
    public static function tableName()
    {
        return 'orders';
    }    
    
    public function rules()
    {
        return [
            [['name', 'phone'], 'required','whenClient' => true],
            //['email', 'email'],
            [['body','patronymic','surname'], 'safe'],
            
            [
                [
                    'total',
                ],
                'number',
                'min' => 100,
            ],
            
            [['email'],'email'],
        ];
    }
    
    public function attributeLabels()
    {
        return [
            'name' => 'Имя',
            'phone'=>'Телефон',
            'body'=>'Сообщение',
            'adress'=>'Адрес',
            'patronymic'=>'Отчество',
            'surname'=>'Фамилия',
            'email'=>'E-mail',
            'total' => 'Общая сумма',
        ];
    }
    
	public function beforeSave($insert) {
            $this->date_time = new \yii\db\Expression('NOW()');
            return parent::beforeSave($insert);
	}
        
        public function beforeDelete() {
            return parent::beforeDelete();
        }    
    
    public function contact($email,$body)
    {
        if ($this->validate()) {
                        $body .= 'Вся сумма: '.$this->total;
                        $body .= "\n\r";            
                        $body .= 'Имя: '.$this->name;
                        $body .= "\n\r";
                        $body .= 'Фамилия: '.$this->surname;
                        $body .= "\n\r";
                        $body .= 'Отчество: '.$this->patronymic;
                        $body .= "\n\r";
                        $body .= 'E-mail: '.$this->email;
                        $body .= "\n\r";                        
                        $body .= 'Телефон: '.$this->phone;
                        $body .= "\n\r";
                        $body .= 'Адрес: '.$this->adress;
                        $body .= "\n\r";
                        $body .= 'Сообщение: '.$this->body;
                        $body .= "\n\r"; 
                        
            Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom(['send@artweb.ua' => 'send'])
                ->setSubject('Заказ на сайте Бубочка')
                ->setTextBody($body)
                ->send();
            return true;
        } else {
            return false;
        }
    }    
    
}