Example #1
0
 public function copyToUser($to_user_id, $status = Photobook::STATUS_NEW)
 {
     $newPhotobookForm = new PhotobookForm();
     $newPhotobookForm->user_id = $to_user_id;
     $newPhotobookForm->name = $this->user_id == $to_user_id ? Yii::t('app', 'Копия ') . $this->name : $this->name;
     $newPhotobookForm->status = $status;
     //Photobook::STATUS_NEW;
     $newPhotobookForm->data = $this->data;
     $newPhotobookForm->template = $this->template;
     $newPhotobookForm->photos = $this->photos;
     $newPhotobookForm->style_id = $this->style_id;
     $newPhotobookForm->cover_id = $this->cover_id;
     $newPhotobookForm->title_line_1 = $this->title_line_1;
     $newPhotobookForm->title_line_2 = $this->title_line_2;
     $newPhotobookForm->title_line_3 = $this->title_line_3;
     $newPhotobookForm->title_line_4 = $this->title_line_4;
     $newPhotobookForm->change_status_at = time();
     $newPhotobookForm->view_access_key = null;
     $newPhotobookForm->invoice_id = null;
     $newPhotobookForm->photos_zip_hash = null;
     if (!$newPhotobookForm->save()) {
         return ['error' => ['msg' => Yii::t('app', 'Не удалось записать в базу данных')]];
     }
     $new_pb_id = $newPhotobookForm->id;
     $new_user_id = $to_user_id;
     $src_path = UserUrl::photobook(false, $this->id, $this->user_id);
     $dst_path = UserUrl::photobook(false, $newPhotobookForm->id, $newPhotobookForm->user_id);
     try {
         Utils::recurse_copy($src_path, $dst_path);
         //fix window_text and tracing
         $src_window_text = UserUrl::photobookWindowText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($this->id, UserUrl::IMAGE_ORIGINAL, 'png');
         $dst_window_text = UserUrl::photobookWindowText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($newPhotobookForm->id, UserUrl::IMAGE_ORIGINAL, 'png');
         if (file_exists($src_window_text)) {
             copy($src_window_text, $dst_window_text);
             unlink($src_window_text);
         }
         $src_tracing = UserUrl::photobookTracingText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($this->id, UserUrl::IMAGE_ORIGINAL, 'png');
         $dst_tracing = UserUrl::photobookTracingText(false, $newPhotobookForm->id, $newPhotobookForm->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($newPhotobookForm->id, UserUrl::IMAGE_ORIGINAL, 'png');
         if (file_exists($src_tracing)) {
             copy($src_tracing, $dst_tracing);
             unlink($src_tracing);
         }
         return ['response' => ['status' => true, 'id' => $new_pb_id, 'user_id' => $new_user_id, 'redirect' => Url::toRoute(['photobooks/index', 'status' => Photobook::STATUS_NEW])]];
     } catch (\Exception $e) {
         return ['error' => ['msg' => $e->getMessage()]];
     }
 }
 public function actionLayouts()
 {
     $this->layout = 'layouts';
     $ref = Yii::$app->request->get('ref');
     $id = Yii::$app->request->get('id');
     //$selected_style_id=  Yii::$app->request->get('style_id', 0);
     $reset = Yii::$app->request->get('reset', 0);
     if (!empty($ref) && !empty($id)) {
         $user_id = AlphaId::id($ref, true);
         $pb_id = AlphaId::id($id, true);
         $model = new PhotobookForm();
         if (!$model->loadById($pb_id)) {
             Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Фотокнига не найдена'));
             $this->redirect(Url::toRoute(['photobooks/index']));
             return;
         }
         //$styles=Style::find()->where(['delete'=>0])->all();
         if (empty($model->data)) {
             $model->data = [];
         }
         if (empty($model->data['processed'])) {
             $model->data['processed'] = [];
         }
         if ($reset == 1) {
             $pages = $model->generatePages($model->style_id);
             if (empty($pages)) {
                 Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Необходимо загрузить как минимум по одному фото в группу'));
                 $this->redirect(Url::toRoute(['photobooks/upload-photos', 'id' => $id, 'ref' => $ref]));
                 return;
             }
             $model->data['pages'] = $pages;
         } else {
             $pages = $model->updatePages($model->style_id);
             if (empty($pages)) {
                 Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Необходимо загрузить как минимум по одному фото в группу'));
                 $this->redirect(Url::toRoute(['photobooks/upload-photos', 'id' => $id, 'ref' => $ref]));
             }
             $model->data['pages'] = $pages;
         }
         //$model->style_id=$selected_style_id;
         if (!$model->save()) {
             Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Не удалось сохранить'));
             $this->redirect(Url::toRoute(['photobooks/index']));
         }
         /* if($reset==1){
         
                         $this->redirect(Url::toRoute(['photobooks/layouts', 'ref'=>$ref, 'id'=>$id]));
                     }*/
         $photos = [];
         $groups = $model->photos;
         foreach ($groups as $group_name => $group) {
             foreach ($group['photos'] as $key => $photo_id) {
                 $mtime = UserUrl::photobookPhotos(false, $model->id, $model->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($photo_id, UserUrl::IMAGE_THUMB);
                 $photos[] = ['photo_id' => $photo_id, 'mtime' => $mtime];
             }
         }
         //$model->data['processed']
         return $this->redirect(Url::toRoute(['photobooks/edit', 'ref' => $ref, 'id' => $id]));
         // return $this->render('layouts', ['model'=>$model, 'ref'=>$ref, 'id'=>$id, 'pb_id'=>$pb_id, 'user_id'=>$user_id, 'pages'=>$pages, 'styles'=>$styles, 'photos'=>$photos, 'processed'=>$model->data['processed']]);
     } else {
         Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Страница не найдена'));
         $this->redirect(Url::toRoute(['photobooks/index']));
     }
 }
 public function actionChangeStyle()
 {
     \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $photobook_id = Yii::$app->request->get('photobook_id', 0);
     $style_id = Yii::$app->request->get('style_id', 0);
     $model = new PhotobookForm();
     if (empty($photobook_id)) {
         return ['error' => ['msg' => Yii::t('app', 'Не верный photobook_id')]];
     }
     if (!$model->loadById($photobook_id)) {
         return ['error' => ['msg' => Yii::t('app', 'Фотокнига не найдена')]];
     }
     $styleModel = new StyleForm();
     if (!$styleModel->loadById($style_id)) {
         return ['error' => ['msg' => Yii::t('app', 'Стиль не найден')]];
     }
     $model->style_id = $style_id;
     if (!$model->save()) {
         return ['error' => ['msg' => Yii::t('app', 'Ошибка записи в базу данных')]];
     }
     return ['response' => ['status' => true]];
 }