Exemple #1
0
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="actions-record-index">

    <h1 class="page-header"><?php 
echo Html::encode($this->title);
?>
</h1>
    <?php 
// echo $this->render('_search', ['model' => $searchModel]);
?>

    <p>
        <?php 
$dropdown = [];
foreach (\common\models\cf\ActionsRecord::getClassList() as $className => $text) {
    $shortClassName = array_reverse(explode('\\', $className))[0];
    $dropdown[] = ['label' => Html::tag('strong', $text), 'url' => ['create', 'className' => $shortClassName]];
}
echo \yii\bootstrap\ButtonDropdown::widget(['label' => Yii::t('app', 'Create Actions Record'), 'options' => ['class' => 'btn-success'], 'dropdown' => ['encodeLabels' => false, 'items' => $dropdown]]);
?>
    </p>

    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'rowOptions' => function ($model, $key, $index, $grid) {
    return ['class' => $model->Disabled || $model->Deleted || !$model->getDiscount()->isDue() ? 'danger' : 'success'];
}, 'columns' => [['class' => \common\components\DisableActionColumn::className(), 'stateAttribute' => 'Disabled'], ['header' => 'Активно', 'format' => 'raw', 'value' => function ($model) {
    /** @var \common\models\cf\ActionsRecord $model */
    if ($model->getDiscount()->isDue()) {
        //                        return ($model->disabled ? 'Активна, но отключена' : 'Работает');
        if ($model->Deleted) {
 /**
  * Creates a new ActionsRecord model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($className)
 {
     $model = new ActionsRecord();
     if (isset(ActionsRecord::getClassList()['common\\discounts\\' . $className])) {
         $model->class = 'common\\discounts\\' . $className;
     } else {
         throw new BadRequestHttpException('Неопределен класс акции!');
     }
     if (($errors = $this->performAjaxValidation($model)) !== null) {
         return $errors;
     }
     //        if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) {
     //            if ($model->load(Yii::$app->request->post())) {
     //                \Yii::$app->response->format = Response::FORMAT_JSON;
     //                echo Json::encode(ActiveForm::validate($model));
     //                \Yii::$app->end();
     //            }
     //        }
     /** @var DiscountAbstract $discount */
     /** @var DiscountAbstract[] $discountData */
     $discount = $model->getDiscount();
     $discountData = [];
     if ($discount->getIsMultiple()) {
         $discountData = [$discount];
         for ($i = 1; $i < $discount->count; $i++) {
             $discountData[] = $model->getDiscount();
         }
         if ($model->load(Yii::$app->request->post()) && Model::loadMultiple($discountData, Yii::$app->request->post()) && Model::validateMultiple($discountData)) {
             $settings = $discountData[0]->toArray();
             $settings['attributes'] = [];
             foreach ($discountData as $discountLoaded) {
                 $settings['attributes'][] = $discountLoaded->getAttributes(null, ['objects']);
             }
             if (in_array($className, ['BonusDiscount', 'PercentBonusPaymentDiscount'])) {
                 ArrayHelper::multisort($settings['attributes'], 'payment', SORT_DESC);
             }
             $model->Settings = Json::encode($settings);
             if ($model->save()) {
                 return $this->redirect(['index']);
             } else {
                 VarDumper::dump($model->errors, 5, true);
                 VarDumper::dump($settings, 5, true);
                 Yii::$app->end();
             }
         } elseif (($errors = $this->performAjaxValidationMultiple($discountData)) !== null) {
             //return $errors;
         }
     } else {
         if ($model->load(Yii::$app->request->post()) && $discount->load(Yii::$app->request->post())) {
             //                if($discount->objects !== false && !empty($discount->objects)) {
             //                    $discount->objects = array_map(function ($object) {
             //                        return $object['ID'];
             //                    }, $discount->objects);
             //                }
             $model->Settings = $discount->toJson();
             if ($discount->validate() && $model->validate()) {
                 $model->save();
                 return $this->redirect(['index']);
             } else {
                 Yii::$app->getSession()->setFlash('error', VarDumper::dumpAsString($model->errors));
             }
         }
     }
     return $this->render('create', ['model' => $model, 'discountData' => $discountData]);
 }