public function save() { Yii::getLogger()->log('start save photobook:' . $this->id, YII_DEBUG); $photobook = Photobook::findOne(['id' => $this->id]); if (empty($photobook)) { $photobook = new Photobook(); $photobook->user_id = $this->user_id; $photobook->name = $this->name; $photobook->status = $this->status; $photobook->data = PhotobookForm::photosEncode($this->data); $photobook->template = $this->template; $photobook->style_id = $this->style_id; $photobook->cover_id = $this->cover_id; $photobook->title_line_1 = $this->title_line_1; $photobook->title_line_2 = $this->title_line_2; $photobook->title_line_3 = $this->title_line_3; $photobook->title_line_4 = $this->title_line_4; $photobook->photos_zip_hash = $this->photos_zip_hash; $photobook->change_status_at = $this->change_status_at; $photobook->view_access_key = $this->view_access_key; $photobook->invoice_id = $this->invoice_id; $this->photos = $this->addTextGroupIfNotExists($this->photos); $photobook->photos = PhotobookForm::photosEncode($this->photos); Yii::getLogger()->log('save:', YII_DEBUG); if ($photobook->save()) { $this->id = $photobook->id; return $photobook; } else { Yii::getLogger()->log('save error', YII_DEBUG); } } else { $photobook->user_id = $this->user_id; $photobook->name = $this->name; $photobook->status = $this->status; $photobook->data = PhotobookForm::photosEncode($this->data); $photobook->template = $this->template; $this->photos = $this->addTextGroupIfNotExists($this->photos); $photobook->photos = PhotobookForm::photosEncode($this->photos); $photobook->style_id = $this->style_id; $photobook->cover_id = $this->cover_id; $photobook->title_line_1 = $this->title_line_1; $photobook->title_line_2 = $this->title_line_2; $photobook->title_line_3 = $this->title_line_3; $photobook->title_line_4 = $this->title_line_4; $photobook->photos_zip_hash = $this->photos_zip_hash; $photobook->change_status_at = $this->change_status_at; $photobook->view_access_key = $this->view_access_key; $photobook->invoice_id = $this->invoice_id; Yii::getLogger()->log('update:', YII_DEBUG); //$photobook->id=$this->id; if ($photobook->update()) { return $photobook; } else { Yii::getLogger()->log('update error:' . print_r($photobook, true), YII_DEBUG); } } return null; }
public function actionViewBook() { $this->layout = 'editor'; $ref = Yii::$app->request->get('ref'); $id = Yii::$app->request->get('id'); $selected_style_id = Yii::$app->request->get('style_id', 0); if (!empty($ref) && !empty($id)) { $user_id = AlphaId::id($ref, true); $pb_id = AlphaId::id($id, true); if ($user_id != Yii::$app->user->identity->getId()) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Страница не найдена')); $this->redirect(Url::toRoute(['photobooks/index'])); return; } $model = new PhotobookForm(); if (!$model->loadById($pb_id)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Фотокнига не найдена')); $this->redirect(Url::toRoute(['photobooks/index'])); return; } if (empty($model->data['pages'])) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'В фотокниги нет ни одного разворота')); $this->redirect(Url::toRoute(['photobooks/index', 'status' => $model->status])); return; } $style_id = $model->style_id; $style = new StyleForm(); if (!$style->loadById($style_id)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Стиль для данной фотокниги не найден.')); $this->redirect(Url::toRoute(['photobooks/index'])); return; } $selected_cover = new CoverForm(); if (!$selected_cover->loadById($model->cover_id)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Обложка для данной фотокниги не найден.')); $this->redirect(Url::toRoute(['photobooks/index'])); return; } $covers = Cover::find()->where(['status' => 1])->all(); $new_covers = []; foreach ($covers as $key => $cover) { $new_covers[$cover->material_type][] = $cover; } $pages = $model->data['pages']; $base_price = (count($pages) - 2) * $style->price_spread; $cover_price_sign = $selected_cover->price_sign; $selected_cover->price; $total_price = $base_price; if ($cover_price_sign == "=") { $total_price = $selected_cover->price; } else { if ($cover_price_sign == "+") { $total_price += $selected_cover->price; } else { if ($cover_price_sign == "-") { $total_price -= $selected_cover->price; } } } $user = User::findOne(['id' => $user_id]); $total_price = CurrencyConvertor::conv($total_price, $user->default_currency); $curse = CurrencyConvertor::getCurse($user->default_currency); return $this->render('viewBook', ['model' => $model, 'ref' => $ref, 'id' => $id, 'pb_id' => $pb_id, 'user_id' => $user_id, 'pages' => $pages, 'style' => $style, 'selected_cover' => $selected_cover, 'covers' => $new_covers, 'total_price' => $total_price, 'default_currency' => $user->default_currency, 'cover_price_sign' => $cover_price_sign, 'cover_price' => $selected_cover->price, 'price_spread' => $style->price_spread, 'curse' => $curse]); } else { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Страница не найдена')); $this->redirect(Url::toRoute(['photobooks/index'])); } }
public function actionDeleteCartRow() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if (Yii::$app->user->isGuest) { return ['error' => ['msg' => Yii::t('app', 'Доступ закрыт')]]; } $cart_id = Yii::$app->request->get('id', 0); if ($cart_id <= 0) { return ['error' => ['msg' => Yii::t('app', 'Не верный id')]]; } $carForm = new CartForm(); if (!$carForm->loadById($cart_id)) { return ['error' => ['msg' => Yii::t('app', 'Запись не найдена')]]; } if (Yii::$app->user->identity->role != User::ROLE_ADMIN) { if ($carForm->user_id != Yii::$app->user->identity->getId()) { return ['error' => ['msg' => Yii::t('app', 'Ошибка прав доступа')]]; } } if ($carForm->product_type == Cart::PRODUCT_PHOTOBOOK && !empty($carForm->product_info) && !empty($carForm->product_info['Photobook'])) { $pb_id = $carForm->product_info['Photobook']['id']; $photobook = new PhotobookForm(); if ($photobook->loadById($pb_id)) { $photobook->setStatus(Photobook::STATUS_NEW); } } if (!Cart::deleteAll(['id' => $cart_id])) { return ['error' => ['msg' => Yii::t('app', 'Не удалось удалить строку')]]; } return ['response' => ['status' => true]]; }