-
Notifications
You must be signed in to change notification settings - Fork 46
/
PreviewController.php
77 lines (65 loc) · 2.7 KB
/
PreviewController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace luya\cms\frontend\controllers;
use luya\cms\frontend\base\Controller;
use luya\cms\menu\InjectItem;
use luya\cms\models\NavItem;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
/**
* CMS Preview Rendering
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.0
*/
class PreviewController extends Controller
{
/**
* Renders the preview action.
*
* @param integer $itemId The nav item to render.
* @param integer $version The version to display.
* @param integer $date The date from the preview frame, is false when not using the preview frame from the cms.
* @throws ForbiddenHttpException
* @throws NotFoundHttpException
*/
public function actionIndex($itemId, $version = false, $date = false): \yii\web\Response|string
{
if (Yii::$app->adminuser->isGuest) {
throw new ForbiddenHttpException('Unable to see the preview page, session expired or not logged in.');
}
$navItem = NavItem::findOne($itemId);
if (!$navItem) {
throw new NotFoundHttpException("The requested nav item with id {$itemId} does not exist.");
}
$langShortCode = $navItem->lang->short_code;
Yii::$app->composition['langShortCode'] = $langShortCode;
$item = Yii::$app->menu->find()->where(['id' => $itemId])->with('hidden')->lang($langShortCode)->one();
if ($item && !$date && $navItem->nav_item_type_id == $version) {
return $this->redirect($item->link);
}
// this item is still offline so we have to inject and fake it with the inject api
if (!$item) {
// create new item to inject
$inject = new InjectItem([
'id' => $itemId,
'navId' => $navItem->nav->id,
'childOf' => Yii::$app->menu->home->id,
'title' => $navItem->title,
'alias' => $navItem->alias,
'isHidden' => true,
]);
// inject item into menu component
Yii::$app->menu->injectItem($inject);
// find the inject menu item
$item = Yii::$app->menu->find()->where(['id' => $inject->id])->with('hidden')->lang($langShortCode)->one();
// something really went wrong while finding injected item
if (!$item) {
throw new NotFoundHttpException("Unable to find the preview for this ID, maybe the page is still Offline?");
}
}
// set the current item, as it would be resolved wrong from the url manager / request path
Yii::$app->menu->current = $item;
return $this->renderContent($this->renderItem($itemId, null, $version));
}
}