Created
October 5, 2015 11:01
-
-
Save rahimov/1e7df4f44da121ba7b6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace frontend\models; | |
use common\models\CampaignItemCity; | |
use common\models\CampaignItemSpecialization; | |
use Yii; | |
use yii\base\Model; | |
use common\models\CampaignItem; | |
use common\models\City; | |
use common\models\Specialization; | |
use yii\base\InvalidCallException; | |
use yii\base\InvalidConfigException; | |
use yii\db\Query; | |
use yii\helpers\ArrayHelper; | |
use vova07\fileapi\behaviors\UploadBehavior; | |
class SellForm extends Model | |
{ | |
public $name; | |
public $remark; | |
public $image; | |
public $price; | |
public $discount; | |
public $price_special; | |
public $audience; | |
public $description; | |
public $dt_end; | |
public $lead_limit; | |
public $src_url; | |
public $specializations; | |
public $cities; | |
/* @var CampaignItem */ | |
private $campaignItemModel = null; | |
/** | |
* @throws InvalidConfigException | |
*/ | |
public function init() | |
{ | |
if ($this->campaignItem === null) { | |
throw new InvalidConfigException('Attribute CampaignItem should be configured'); | |
} | |
parent::init(); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function behaviors() | |
{ | |
return [ | |
'uploadBehavior' => [ | |
'class' => UploadBehavior::className(), | |
'attributes' => [ | |
'image' => [ | |
'path' => Yii::getAlias('@frontend').'/web/uploads/campaign', | |
'tempPath' => Yii::getAlias('@frontend').'/web/uploads/temp', | |
'url' => Yii::getAlias('@frontendUrl').'/uploads/campaign' | |
] | |
] | |
] | |
]; | |
} | |
/** | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['remark'], 'required'], | |
// [['remark', 'description'], 'required'], | |
[['name', 'remark', 'image'], 'string', 'max' => 255], | |
[['audience', 'lead_limit'], 'integer'], | |
[['description'], 'string'], | |
[['dt_end'], 'safe'], | |
[['price', 'discount', 'price_special'], 'string', 'max' => 50], | |
[['src_url'], 'string', 'max' => 1000], | |
[['remark', 'price', 'discount', 'price_special'], 'trim'], | |
[['cities'], 'in', 'range' => array_keys($this->listCities), 'allowArray' => true], | |
[['specializations'], 'in', 'range' => array_keys($this->listSpecializations), 'allowArray' => true], | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => Yii::t('common', 'ID'), | |
'audience_id' => Yii::t('common', 'Audience ID'), | |
'name' => 'Наименование', | |
'remark' => 'Наименование', | |
'description' => 'Опишите подробно ваше предложение', | |
'price' => 'Стоимость предложения', | |
'discount' => 'Ваша скидка', | |
'price_special' => 'Стоимость с учетом скидки', | |
'src_url' => 'Ссылка на ваш сайт с описанием предложения', | |
'dt_end' => 'Дата окончания действия предложения', | |
'lead_limit' => 'Максимальное количество покупателей', | |
]; | |
} | |
/** | |
* @return bool | |
* @throws \Exception | |
* @throws \yii\db\Exception | |
*/ | |
public function save() | |
{ | |
if ($this->validate()) { | |
$user = Yii::$app->user; | |
$userCompany = null; | |
if (!$user->isGuest){ | |
$userCompany = $user->identity->getCompanies()->one(); | |
} | |
$this->campaignItem->setAttributes( | |
[ | |
'name' => ($userCompany !== null) ? $userCompany->name : 'Новый', | |
'remark' => $this->remark, | |
'image' => $this->image, | |
'price' => $this->price, | |
'discount' => $this->discount, | |
'price_special' => $this->price_special, | |
'dt_start' => date('Y-m-d H:i:s'), | |
'dt_end' => date('Y-m-d H:i:s', strtotime($this->dt_end)), | |
'lead_limit' => $this->lead_limit, | |
'audience_id' => $this->audience ? 30 : 10, | |
'description' => $this->description, | |
'src_url' => $this->src_url, | |
'cities' => $this->cities, | |
'specializations' => $this->specializations, | |
'delivery_type'=> 'EMAIL', | |
'delivery_address' => $user->identity->email, | |
'scr_company_id' => ($userCompany !== null) ? $userCompany->id : null, | |
] | |
); | |
$transaction = \Yii::$app->db->beginTransaction(); | |
try { | |
$this->campaignItem->save(false); | |
$transaction->commit(); | |
$this->campaignItem->ord = -$this->campaignItem->id; | |
$this->campaignItem->save(false); | |
Yii::$app->sender->sendNewCampaignEmail($this->campaignItem,$user->identity); | |
Yii::$app->session->setFlash('alert', [ | |
'options' => ['class'=>'alert-success'], | |
'body' => Yii::t('frontend', 'Спасибо! Предложение отправлено на модерацию.') | |
]); | |
return true; | |
} catch (\Exception $e) { | |
$transaction->rollBack(); | |
throw new \Exception($e, $e->getCode()); | |
} | |
}; | |
return false; | |
} | |
/** | |
* Return Campaign Item Cities list as ['id'=>'name'] | |
* @return array | |
*/ | |
public function getListCities() | |
{ | |
return ArrayHelper::map(City::find()->all(), 'id', 'name'); | |
} | |
/** | |
* Return Campaign Item Specializations list as ['id'=>'name'] | |
* @return array | |
*/ | |
public function getListSpecializations() | |
{ | |
return ArrayHelper::map(Specialization::find()->all(), 'id', 'name'); | |
} | |
/** | |
* Getter to CampaignItem model | |
* @return CampaignItem | |
*/ | |
public function getCampaignItem() | |
{ | |
return $this->campaignItemModel; | |
} | |
/** | |
* Setter to CampaignItem model. | |
* Note: It may be used once at initialization. | |
* @param CampaignItem $value | |
*/ | |
public function setCampaignItem(CampaignItem $value) | |
{ | |
if ($this->campaignItemModel === null) { | |
$this->campaignItemModel = $value; | |
$this->setAttributes($this->campaignItemModel->attributes); | |
} else { | |
throw new InvalidCallException('Attribute Campaign Item was set earlier'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment