From cfa2e73b611e0ef837867aad6774036f21745687 Mon Sep 17 00:00:00 2001 From: Egorka Date: Sat, 7 Jul 2018 10:42:55 +0300 Subject: [PATCH] Global: - fix layout Meta Blog: - added Vimeo video - fix YouTube, Vimeo preview image - fix URL rule --- common/modules/blog/controllers/PostController.php | 71 ++++++++++++++++- .../blog/controllers/manage/PostController.php | 4 +- common/modules/blog/entities/BlogPost.php | 18 ++--- common/modules/blog/messages/ru/blog.php | 1 + .../blog/services/BlogPostManageService.php | 31 +++++--- common/modules/blog/urls/BlogMainUrlRule.php | 4 +- common/modules/blog/views/manage/post/_form.php | 10 ++- common/modules/blog/views/manage/post/view.php | 11 ++- core/helpers/VideoHelper.php | 86 +++++++++++++++++++++ core/widgets/VideoWidget.php | 35 +++++++++ frontend/web/themes/start/layouts/main.php | 18 +++-- .../themes/start/modules/blog/views/post/post.php | 18 ++++- static/cache/posts/368_287_10.jpg | Bin 170841 -> 0 bytes static/cache/posts/368_287_11.jpg | Bin 133231 -> 29906 bytes static/cache/posts/368_287_13.jpg | Bin 84795 -> 100707 bytes static/cache/posts/368_287_15.jpg | Bin 225453 -> 0 bytes static/cache/posts/368_287_16.jpg | Bin 98008 -> 0 bytes static/cache/posts/368_287_4.jpg | Bin 138982 -> 0 bytes static/cache/posts/368_287_5.jpg | Bin 169525 -> 0 bytes static/cache/posts/368_287_6.jpg | Bin 138612 -> 0 bytes static/cache/posts/368_287_7.jpg | Bin 105204 -> 0 bytes static/cache/posts/368_287_8.jpg | Bin 121730 -> 0 bytes static/cache/posts/368_287_9.jpg | Bin 168050 -> 0 bytes static/cache/posts/370_325_10.jpg | Bin 190503 -> 0 bytes static/cache/posts/370_325_11.jpg | Bin 145512 -> 29906 bytes static/cache/posts/370_325_13.jpg | Bin 91992 -> 113085 bytes static/cache/posts/370_325_15.jpg | Bin 260266 -> 0 bytes static/cache/posts/370_325_16.jpg | Bin 111808 -> 0 bytes static/cache/posts/370_325_4.jpg | Bin 151926 -> 0 bytes static/cache/posts/370_325_5.jpg | Bin 187333 -> 0 bytes static/cache/posts/370_325_6.jpg | Bin 157747 -> 0 bytes static/cache/posts/370_325_7.jpg | Bin 114411 -> 0 bytes static/cache/posts/370_325_8.jpg | Bin 136181 -> 0 bytes static/cache/posts/370_325_9.jpg | Bin 193442 -> 0 bytes static/cache/posts/683_407_10.jpg | Bin 359849 -> 0 bytes static/cache/posts/683_407_13.jpg | Bin 191697 -> 218024 bytes static/cache/posts/683_407_4.jpg | Bin 292458 -> 0 bytes static/cache/posts/683_407_5.jpg | Bin 398173 -> 0 bytes static/cache/posts/683_407_6.jpg | Bin 307835 -> 0 bytes static/cache/posts/683_407_7.jpg | Bin 236667 -> 0 bytes static/cache/posts/683_407_8.jpg | Bin 276403 -> 0 bytes static/cache/posts/683_407_9.jpg | Bin 395663 -> 0 bytes static/cache/posts/94_94_10.jpg | Bin 19149 -> 0 bytes static/cache/posts/94_94_11.jpg | Bin 16309 -> 12945 bytes static/cache/posts/94_94_13.jpg | Bin 12049 -> 12683 bytes static/cache/posts/94_94_15.jpg | Bin 20984 -> 0 bytes static/cache/posts/94_94_16.jpg | Bin 9936 -> 0 bytes static/cache/posts/94_94_4.jpg | Bin 19946 -> 0 bytes static/cache/posts/94_94_5.jpg | Bin 20793 -> 0 bytes static/cache/posts/94_94_6.jpg | Bin 19463 -> 0 bytes static/cache/posts/94_94_7.jpg | Bin 14357 -> 0 bytes static/cache/posts/94_94_8.jpg | Bin 15435 -> 0 bytes static/cache/posts/94_94_9.jpg | Bin 21244 -> 0 bytes static/cache/posts/admin_10.jpg | Bin 9221 -> 0 bytes static/cache/posts/admin_11.jpg | Bin 7991 -> 6856 bytes static/cache/posts/admin_13.jpg | Bin 6467 -> 6451 bytes static/cache/posts/admin_15.jpg | Bin 9340 -> 0 bytes static/cache/posts/admin_16.jpg | Bin 4881 -> 0 bytes static/cache/posts/admin_4.jpg | Bin 10477 -> 0 bytes static/cache/posts/admin_5.jpg | Bin 10440 -> 0 bytes static/cache/posts/admin_6.jpg | Bin 9851 -> 0 bytes static/cache/posts/admin_7.jpg | Bin 7665 -> 0 bytes static/cache/posts/admin_8.jpg | Bin 7919 -> 0 bytes static/cache/posts/admin_9.jpg | Bin 10188 -> 0 bytes static/cache/posts/home_slider_10.jpg | Bin 195957 -> 0 bytes static/cache/posts/home_slider_11.jpg | Bin 150448 -> 29906 bytes static/cache/posts/home_slider_13.jpg | Bin 93499 -> 116871 bytes static/cache/posts/home_slider_15.jpg | Bin 270580 -> 0 bytes static/cache/posts/home_slider_16.jpg | Bin 116719 -> 0 bytes static/cache/posts/home_slider_4.jpg | Bin 154080 -> 0 bytes static/cache/posts/home_slider_5.jpg | Bin 192280 -> 0 bytes static/cache/posts/home_slider_6.jpg | Bin 164303 -> 0 bytes static/cache/posts/home_slider_7.jpg | Bin 119588 -> 0 bytes static/cache/posts/home_slider_8.jpg | Bin 140850 -> 0 bytes static/cache/posts/home_slider_9.jpg | Bin 198855 -> 0 bytes static/cache/posts/list_10.jpg | Bin 70285 -> 0 bytes static/cache/posts/list_11.jpg | Bin 56205 -> 29906 bytes static/cache/posts/list_13.jpg | Bin 36935 -> 44338 bytes static/cache/posts/list_15.jpg | Bin 89065 -> 0 bytes static/cache/posts/list_16.jpg | Bin 38765 -> 0 bytes static/cache/posts/list_4.jpg | Bin 62419 -> 0 bytes static/cache/posts/list_5.jpg | Bin 71494 -> 0 bytes static/cache/posts/list_6.jpg | Bin 64349 -> 0 bytes static/cache/posts/list_7.jpg | Bin 45946 -> 0 bytes static/cache/posts/list_8.jpg | Bin 52284 -> 0 bytes static/cache/posts/list_9.jpg | Bin 73974 -> 0 bytes static/cache/posts/origin_10.jpg | Bin 349941 -> 0 bytes static/cache/posts/origin_11.jpg | Bin 569689 -> 29906 bytes static/cache/posts/origin_13.jpg | Bin 273420 -> 197068 bytes static/cache/posts/origin_15.jpg | Bin 1322989 -> 0 bytes static/cache/posts/origin_16.jpg | Bin 645124 -> 0 bytes static/cache/posts/origin_4.jpg | Bin 372509 -> 0 bytes static/cache/posts/origin_5.jpg | Bin 869774 -> 0 bytes static/cache/posts/origin_6.jpg | Bin 711235 -> 0 bytes static/cache/posts/origin_7.jpg | Bin 358675 -> 0 bytes static/cache/posts/origin_8.jpg | Bin 372287 -> 0 bytes static/cache/posts/origin_9.jpg | Bin 573526 -> 0 bytes static/cache/posts/thumb_10.jpg | Bin 43077 -> 0 bytes static/cache/posts/thumb_11.jpg | Bin 34992 -> 24281 bytes static/cache/posts/thumb_13.jpg | Bin 24228 -> 28377 bytes static/cache/posts/thumb_15.jpg | Bin 51933 -> 0 bytes static/cache/posts/thumb_16.jpg | Bin 23175 -> 0 bytes static/cache/posts/thumb_4.jpg | Bin 41672 -> 0 bytes static/cache/posts/thumb_5.jpg | Bin 45231 -> 0 bytes static/cache/posts/thumb_6.jpg | Bin 41030 -> 0 bytes static/cache/posts/thumb_7.jpg | Bin 29293 -> 0 bytes static/cache/posts/thumb_8.jpg | Bin 32591 -> 0 bytes static/cache/posts/thumb_9.jpg | Bin 46629 -> 0 bytes static/cache/posts/thumb_gallery_view_10.jpg | Bin 90551 -> 0 bytes static/cache/posts/thumb_gallery_view_11.jpg | Bin 77026 -> 29906 bytes static/cache/posts/thumb_gallery_view_13.jpg | Bin 51382 -> 54194 bytes static/cache/posts/thumb_gallery_view_15.jpg | Bin 114442 -> 0 bytes static/cache/posts/thumb_gallery_view_16.jpg | Bin 49638 -> 0 bytes static/cache/posts/thumb_gallery_view_4.jpg | Bin 83106 -> 0 bytes static/cache/posts/thumb_gallery_view_5.jpg | Bin 97868 -> 0 bytes static/cache/posts/thumb_gallery_view_6.jpg | Bin 76829 -> 0 bytes static/cache/posts/thumb_gallery_view_7.jpg | Bin 60358 -> 0 bytes static/cache/posts/thumb_gallery_view_8.jpg | Bin 66184 -> 0 bytes static/cache/posts/thumb_gallery_view_9.jpg | Bin 96361 -> 0 bytes static/origin/posts/10.jpg | Bin 103269 -> 96342 bytes static/origin/posts/11.jpg | Bin 144916 -> 7233 bytes static/origin/posts/13.jpg | Bin 55730 -> 93014 bytes static/origin/posts/15.jpg | Bin 2224799 -> 0 bytes static/origin/posts/16.jpg | Bin 1610138 -> 0 bytes static/origin/posts/5.jpg | Bin 387142 -> 946565 bytes static/origin/posts/7.jpg | Bin 83485 -> 0 bytes static/origin/posts/8.jpg | Bin 107149 -> 0 bytes static/origin/posts/9.jpg | Bin 141363 -> 0 bytes vagrant/nginx/log/static-error.log | 5 ++ 129 files changed, 274 insertions(+), 38 deletions(-) create mode 100644 core/helpers/VideoHelper.php create mode 100644 core/widgets/VideoWidget.php delete mode 100644 static/cache/posts/368_287_10.jpg delete mode 100644 static/cache/posts/368_287_15.jpg delete mode 100644 static/cache/posts/368_287_16.jpg delete mode 100644 static/cache/posts/368_287_4.jpg delete mode 100644 static/cache/posts/368_287_5.jpg delete mode 100644 static/cache/posts/368_287_6.jpg delete mode 100644 static/cache/posts/368_287_7.jpg delete mode 100644 static/cache/posts/368_287_8.jpg delete mode 100644 static/cache/posts/368_287_9.jpg delete mode 100644 static/cache/posts/370_325_10.jpg delete mode 100644 static/cache/posts/370_325_15.jpg delete mode 100644 static/cache/posts/370_325_16.jpg delete mode 100644 static/cache/posts/370_325_4.jpg delete mode 100644 static/cache/posts/370_325_5.jpg delete mode 100644 static/cache/posts/370_325_6.jpg delete mode 100644 static/cache/posts/370_325_7.jpg delete mode 100644 static/cache/posts/370_325_8.jpg delete mode 100644 static/cache/posts/370_325_9.jpg delete mode 100644 static/cache/posts/683_407_10.jpg delete mode 100644 static/cache/posts/683_407_4.jpg delete mode 100644 static/cache/posts/683_407_5.jpg delete mode 100644 static/cache/posts/683_407_6.jpg delete mode 100644 static/cache/posts/683_407_7.jpg delete mode 100644 static/cache/posts/683_407_8.jpg delete mode 100644 static/cache/posts/683_407_9.jpg delete mode 100644 static/cache/posts/94_94_10.jpg delete mode 100644 static/cache/posts/94_94_15.jpg delete mode 100644 static/cache/posts/94_94_16.jpg delete mode 100644 static/cache/posts/94_94_4.jpg delete mode 100644 static/cache/posts/94_94_5.jpg delete mode 100644 static/cache/posts/94_94_6.jpg delete mode 100644 static/cache/posts/94_94_7.jpg delete mode 100644 static/cache/posts/94_94_8.jpg delete mode 100644 static/cache/posts/94_94_9.jpg delete mode 100644 static/cache/posts/admin_10.jpg delete mode 100644 static/cache/posts/admin_15.jpg delete mode 100644 static/cache/posts/admin_16.jpg delete mode 100644 static/cache/posts/admin_4.jpg delete mode 100644 static/cache/posts/admin_5.jpg delete mode 100644 static/cache/posts/admin_6.jpg delete mode 100644 static/cache/posts/admin_7.jpg delete mode 100644 static/cache/posts/admin_8.jpg delete mode 100644 static/cache/posts/admin_9.jpg delete mode 100644 static/cache/posts/home_slider_10.jpg delete mode 100644 static/cache/posts/home_slider_15.jpg delete mode 100644 static/cache/posts/home_slider_16.jpg delete mode 100644 static/cache/posts/home_slider_4.jpg delete mode 100644 static/cache/posts/home_slider_5.jpg delete mode 100644 static/cache/posts/home_slider_6.jpg delete mode 100644 static/cache/posts/home_slider_7.jpg delete mode 100644 static/cache/posts/home_slider_8.jpg delete mode 100644 static/cache/posts/home_slider_9.jpg delete mode 100644 static/cache/posts/list_10.jpg delete mode 100644 static/cache/posts/list_15.jpg delete mode 100644 static/cache/posts/list_16.jpg delete mode 100644 static/cache/posts/list_4.jpg delete mode 100644 static/cache/posts/list_5.jpg delete mode 100644 static/cache/posts/list_6.jpg delete mode 100644 static/cache/posts/list_7.jpg delete mode 100644 static/cache/posts/list_8.jpg delete mode 100644 static/cache/posts/list_9.jpg delete mode 100644 static/cache/posts/origin_10.jpg delete mode 100644 static/cache/posts/origin_15.jpg delete mode 100644 static/cache/posts/origin_16.jpg delete mode 100644 static/cache/posts/origin_4.jpg delete mode 100644 static/cache/posts/origin_5.jpg delete mode 100644 static/cache/posts/origin_6.jpg delete mode 100644 static/cache/posts/origin_7.jpg delete mode 100644 static/cache/posts/origin_8.jpg delete mode 100644 static/cache/posts/origin_9.jpg delete mode 100644 static/cache/posts/thumb_10.jpg delete mode 100644 static/cache/posts/thumb_15.jpg delete mode 100644 static/cache/posts/thumb_16.jpg delete mode 100644 static/cache/posts/thumb_4.jpg delete mode 100644 static/cache/posts/thumb_5.jpg delete mode 100644 static/cache/posts/thumb_6.jpg delete mode 100644 static/cache/posts/thumb_7.jpg delete mode 100644 static/cache/posts/thumb_8.jpg delete mode 100644 static/cache/posts/thumb_9.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_10.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_15.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_16.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_4.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_5.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_6.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_7.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_8.jpg delete mode 100644 static/cache/posts/thumb_gallery_view_9.jpg delete mode 100644 static/origin/posts/15.jpg delete mode 100644 static/origin/posts/16.jpg delete mode 100644 static/origin/posts/7.jpg delete mode 100644 static/origin/posts/8.jpg delete mode 100644 static/origin/posts/9.jpg diff --git a/common/modules/blog/controllers/PostController.php b/common/modules/blog/controllers/PostController.php index 3f9f9b7..64bd745 100644 --- a/common/modules/blog/controllers/PostController.php +++ b/common/modules/blog/controllers/PostController.php @@ -2,14 +2,21 @@ namespace common\modules\blog\controllers; +use common\modules\blog\entities\BlogPost; use common\modules\blog\forms\BlogCommentForm; +use common\modules\blog\forms\BlogPostForm; +use common\modules\blog\helpers\BlogPostHelper; use common\modules\blog\repositories\read\BlogCategoryReadRepository; use common\modules\blog\repositories\read\BlogPostReadRepository; use common\modules\blog\repositories\read\BlogTagReadRepository; use common\modules\blog\services\BlogCommentService; +use core\entities\Meta; use frontend\components\FrontendController; use Yii; +use yii\base\Security; use yii\data\ActiveDataProvider; +use yii\helpers\FileHelper; +use yii\helpers\Json; use yii\web\NotFoundHttpException; use yii\filters\AccessControl; @@ -43,10 +50,10 @@ class PostController extends FrontendController { return [ 'access' => [ - 'class' => AccessControl::className(), + 'class' => AccessControl::class, 'rules' => [ [ - 'actions' => ['index', 'category', 'tag', 'post'], + 'actions' => ['index', 'category', 'tag', 'post', 'preview'], 'allow' => true, //'roles' => ['Blog'], ], @@ -159,4 +166,64 @@ class PostController extends FrontendController 'model' => $form, ]); } + + public function beforeAction($action) + { + // ...set `$this->enableCsrfValidation` here based on some conditions... + // call parent method that will check CSRF if such property is true. + if ($action->id === 'preview') { + # code... + $this->enableCsrfValidation = false; + } + return parent::beforeAction($action); + } + + public function actionPreview() + { + Yii::$app->controller->enableCsrfValidation = false; + $form = new BlogPostForm(); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + $post = new BlogPost(); + $post->id = -1; + $post->title = $form->title; + $post->description = $form->description; + $post->content = $form->content; + $post->category_id = $form->category_id; + $post->created_at = time(); + $post->updated_at = time(); + $post->published_at = time(); + + $meta = new Meta($form->meta->title, $form->meta->description, $form->meta->keywords); + $post->meta = $meta; + $post->meta_json = Json::encode([ + 'title' => $form->meta->title, + 'description' => $form->meta->description, + 'keywords' => $form->meta->keywords, + ]); + $post->video = $form->video; + $post->slug = $form->slug; + + if ($form->image) { + $post->setImage($form->image); + } + else if ($form->video) { + $src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg'; + $filename = (new Security())->generateRandomString(15) . '.jpg'; + copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $post->id . '.jpg')); + //copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $filename)); + $post->image = $filename; + } + + $path = $post->getUploadedFilePath('image'); + FileHelper::createDirectory(pathinfo($path, PATHINFO_DIRNAME), 0775, true); + $post->image->saveAs($path); + + $post->image = $post->getImageFileUrl('image'); + + return $this->render('post', [ + 'post' => $post, + ]); + } + return ''; + } } \ No newline at end of file diff --git a/common/modules/blog/controllers/manage/PostController.php b/common/modules/blog/controllers/manage/PostController.php index 9136068..9aeb403 100644 --- a/common/modules/blog/controllers/manage/PostController.php +++ b/common/modules/blog/controllers/manage/PostController.php @@ -28,7 +28,7 @@ class PostController extends Controller { return [ 'access' => [ - 'class' => AccessControl::className(), + 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, @@ -41,7 +41,7 @@ class PostController extends Controller ], ], 'verbs' => [ - 'class' => VerbFilter::className(), + 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], 'activate' => ['POST'], diff --git a/common/modules/blog/entities/BlogPost.php b/common/modules/blog/entities/BlogPost.php index ba28bd6..e957a7d 100644 --- a/common/modules/blog/entities/BlogPost.php +++ b/common/modules/blog/entities/BlogPost.php @@ -273,22 +273,22 @@ class BlogPost extends ActiveRecord public function getBlogComments(): ActiveQuery { - return $this->hasMany(BlogComment::className(), ['post_id' => 'id']); + return $this->hasMany(BlogComment::class, ['post_id' => 'id']); } public function getBlogTagAssignments(): ActiveQuery { - return $this->hasMany(BlogTagAssignment::className(), ['post_id' => 'id']); + return $this->hasMany(BlogTagAssignment::class, ['post_id' => 'id']); } public function getTags(): ActiveQuery { - return $this->hasMany(BlogTag::className(), ['id' => 'tag_id'])->viaTable('blog_tag_assignments', ['post_id' => 'id']); + return $this->hasMany(BlogTag::class, ['id' => 'tag_id'])->viaTable('blog_tag_assignments', ['post_id' => 'id']); } public function getCategory(): ActiveQuery { - return $this->hasOne(BlogCategory::className(), ['id' => 'category_id']); + return $this->hasOne(BlogCategory::class, ['id' => 'category_id']); } ###################################### @@ -296,15 +296,15 @@ class BlogPost extends ActiveRecord public function behaviors(): array { return [ - TimestampBehavior::className(), - MetaBehavior::className(), + TimestampBehavior::class, + MetaBehavior::class, [ - 'class' => SaveRelationsBehavior::className(), + 'class' => SaveRelationsBehavior::class, 'relations' => ['blogTagAssignments', 'blogComments'], ], [ // todo Image Sizes to settings or theme settings - 'class' => ImageUploadBehavior::className(), + 'class' => ImageUploadBehavior::class, 'attribute' => 'image', 'createThumbsOnRequest' => true, //'filePath' => $this::FILE_ORIGINAL_PATH . '/[[id]].[[extension]]', @@ -313,7 +313,7 @@ class BlogPost extends ActiveRecord 'thumbPath' => '@staticRoot/cache/posts/[[profile]]_[[id]].[[extension]]', 'thumbUrl' => '@static/cache/posts/[[profile]]_[[id]].[[extension]]', 'thumbs' => [ - 'blog_list' => ['width' => 750, 'height' => 300], + 'blog_list' => ['width' => 750, 'height' => 300, 'resizeUp' => true], 'blog_post' => ['width' => 900, 'height' => 300], 'admin' => ['width' => 60, 'height' => 60], diff --git a/common/modules/blog/messages/ru/blog.php b/common/modules/blog/messages/ru/blog.php index f40d671..00a7abc 100644 --- a/common/modules/blog/messages/ru/blog.php +++ b/common/modules/blog/messages/ru/blog.php @@ -54,6 +54,7 @@ return [ 'User' => 'Пользователь', 'Post' => 'Статья', 'YouTube URL' => 'Ссылка YouTube', + 'Video URL' => 'Ссылка на видео (YouTube, Vimeo)', 'Add Image' => 'Новое изображение', 'Video' => 'Видео', 'If a video link is specified, the image will be used as a preview image' => 'Если указана ссылка на видео, то изображение будет рассматриваться как картинка для предварительного просмотра', diff --git a/common/modules/blog/services/BlogPostManageService.php b/common/modules/blog/services/BlogPostManageService.php index a2ba4ff..e67edcd 100644 --- a/common/modules/blog/services/BlogPostManageService.php +++ b/common/modules/blog/services/BlogPostManageService.php @@ -10,6 +10,7 @@ use common\modules\blog\repositories\BlogCategoryRepository; use common\modules\blog\repositories\BlogRepository; use common\modules\blog\repositories\BlogTagRepository; use core\entities\Meta; +use core\helpers\VideoHelper; use core\services\TransactionManager; use yii\base\Security; use yii\helpers\Inflector; @@ -53,16 +54,6 @@ class BlogPostManageService ) ); - if ($form->image) { - $post->setImage($form->image); - } - else if ($form->video) { - $src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg'; - $filename = (new Security())->generateRandomString(15) . '.jpg'; - copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $post->id . '.jpg')); - $post->image = $filename; - } - $this->transaction->wrap(function () use ($post, $form) { if (is_array($form->tags->new_tags) && !empty($form->tags->new_tags)) { @@ -75,6 +66,23 @@ class BlogPostManageService } } $this->posts->save($post); + + if ($form->image) { + $post->setImage($form->image); + } + else if ($form->video) { + $src = VideoHelper::getThumb($form->video); + //$src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg'; + $filename = (new Security())->generateRandomString(15) . '.jpg'; + copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $post->id . '.jpg')); + $post->image = $filename; + } + + if ($form->status == BlogPost::STATUS_ACTIVE) { + $post->activate(); + } + + $this->posts->save($post); }); return $post; @@ -106,7 +114,8 @@ class BlogPostManageService } elseif ($form->video && (!$post->image || $form->reset_image)) { $post->cleanFiles(); - $src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg'; + $src = VideoHelper::getThumb($form->video); + //$src = 'https://i.ytimg.com/vi/' . BlogPostHelper::parseYoutubeUrl($post->video) . '/maxresdefault.jpg'; $filename = (new Security())->generateRandomString(15) . '.jpg'; copy($src, \Yii::getAlias(BlogPost::FILE_ORIGINAL_PATH . '/' . $post->id . '.jpg')); $post->image = $filename; diff --git a/common/modules/blog/urls/BlogMainUrlRule.php b/common/modules/blog/urls/BlogMainUrlRule.php index 67a7acf..c5157d2 100644 --- a/common/modules/blog/urls/BlogMainUrlRule.php +++ b/common/modules/blog/urls/BlogMainUrlRule.php @@ -4,7 +4,6 @@ namespace common\modules\blog\urls; use common\modules\blog\repositories\read\BlogPostReadRepository; use yii\base\BaseObject; -use yii\base\InvalidParamException; use yii\caching\Cache; use yii\caching\TagDependency; use yii\web\UrlNormalizerRedirectException; @@ -66,7 +65,8 @@ class BlogMainUrlRule extends BaseObject implements UrlRuleInterface }, null, new TagDependency(['tags' => ['blog']])); if (!$url) { - throw new InvalidParamException('Undefined id.'); + return '#'; + //throw new InvalidArgumentException('Undefined id.'); } $url = $this->prefix . '/' . $url; diff --git a/common/modules/blog/views/manage/post/_form.php b/common/modules/blog/views/manage/post/_form.php index d7b3a61..c5988f0 100644 --- a/common/modules/blog/views/manage/post/_form.php +++ b/common/modules/blog/views/manage/post/_form.php @@ -132,7 +132,7 @@ $this->registerJs($js2);
- field($model, 'video')->textInput()->label(Yii::t('blog', 'YouTube URL'))->hint(Yii::t('blog', 'If a video link is specified, the image will be used as a preview image')) ?> + field($model, 'video')->textInput()->label(Yii::t('blog', 'Video URL'))->hint(Yii::t('blog', 'If a video link is specified, the image will be used as a preview image')) ?>
@@ -158,7 +158,13 @@ $this->registerJs($js2);
- + 'btn btn-info', + 'value'=>'preview', + 'name'=>'submit_preview', + 'formaction' => Url::to(Yii::$app->params['frontendHostInfo'] . '/blog/post/preview'), + 'formtarget' => '_blank', + ]) ?>
field($model, 'status')->radioList(BlogPostHelper::statusList()) ?>
diff --git a/common/modules/blog/views/manage/post/view.php b/common/modules/blog/views/manage/post/view.php index c1fb1f3..38ecebf 100644 --- a/common/modules/blog/views/manage/post/view.php +++ b/common/modules/blog/views/manage/post/view.php @@ -76,7 +76,11 @@ $this->params['breadcrumbs'][] = $title;
- video) . '/maxresdefault.jpg', [ + + video), [ 'width' => 300, 'class' => 'thumbnail', ]) ?> @@ -88,7 +92,10 @@ $this->params['breadcrumbs'][] = $title;
- + $post->video, + 'style' => '', + ]) ?>
diff --git a/core/helpers/VideoHelper.php b/core/helpers/VideoHelper.php new file mode 100644 index 0000000..010b2b8 --- /dev/null +++ b/core/helpers/VideoHelper.php @@ -0,0 +1,86 @@ +thumbnail_large; + break; + } + return $src; + } + + public static function parseVimeoUrl($url) + { + $urls = parse_url($url); + // https://vimeo.com/274720181 + if($urls['host'] == 'vimeo.com'){ + $id = ltrim($urls['path'],'/'); + } + // https://player.vimeo.com/video/274720181 + else { + $parts = explode('/',$urls['path']); + $id = end($parts); + } + + return $id; + } + + public static function parseYoutubeUrl($url) + { + $urls = parse_url($url); + //url is http://youtu.be/xxxx + if($urls['host'] == 'youtu.be'){ + $id = ltrim($urls['path'],'/'); + } + //url is http://www.youtube.com/embed/xxxx + else if(strpos($urls['path'],'embed') == 1){ + $parts = explode('/',$urls['path']); + $id = end($parts); + } + //url is xxxx only + else if(strpos($url,'/')===false){ + $id = $url; + } + //http://www.youtube.com/watch?feature=player_embedded&v=m-t4pcO99gI + //url is http://www.youtube.com/watch?v=xxxx + else{ + parse_str($urls['query']); + /* @var $v */ + $id = $v; + if(!empty($feature)){ + $id = end(explode('v=',$urls['query'])); + } + } + return $id; + } + +} \ No newline at end of file diff --git a/core/widgets/VideoWidget.php b/core/widgets/VideoWidget.php new file mode 100644 index 0000000..504a723 --- /dev/null +++ b/core/widgets/VideoWidget.php @@ -0,0 +1,35 @@ +src)) { + case 'youtube': + $id = VideoHelper::parseYoutubeUrl($this->src); + $player = ''; + break; + case 'vimeo': + $id = VideoHelper::parseVimeoUrl($this->src); + $player = ''; + break; + } + return $player; + } +} \ No newline at end of file diff --git a/frontend/web/themes/start/layouts/main.php b/frontend/web/themes/start/layouts/main.php index 4ee8d47..f55d798 100644 --- a/frontend/web/themes/start/layouts/main.php +++ b/frontend/web/themes/start/layouts/main.php @@ -3,6 +3,8 @@ * @var $this \yii\web\View */ +use yii\helpers\Html; + \frontend\web\themes\start\layouts\code\assets\AppAsset::register($this); rmrevin\yii\fontawesome\cdn\AssetBundle::register($this); ?> @@ -12,13 +14,12 @@ rmrevin\yii\fontawesome\cdn\AssetBundle::register($this); - + - - - - Modern Business - Start Bootstrap Template + + <?= Html::encode($this->title) ?> + head() ?> @@ -27,12 +28,17 @@ rmrevin\yii\fontawesome\cdn\AssetBundle::register($this);