Passed
Push — develop ( 68c7a0...29faa4 )
by Neill
22:59 queued 13s
created

EditorController::actionPageAddEditor()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 16
ccs 0
cts 14
cp 0
crap 20
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 05/09/2016 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 */
7
8
namespace neon\cms\controllers;
9
10
use neon\cms\form\Page;
11
use neon\cms\models\CmsPage;
12
use neon\cms\models\CmsUrl;
13
use neon\core\grid\GridAssets;
14
use neon\core\helpers\Arr;
15
use neon\core\web\AdminController;
16
use yii\web\HttpException;
17
18
19
class EditorController extends AdminController
20
{
21
	public $layout = '/admin-workbench';
22
23
	/**
24
	 * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
25
	 * @return string
26
	 */
27
	public function actionIndex($id=null)
28
	{
29
		(new \neon\core\form\fields\Ide('ide'))->registerScripts(neon()->view);
30
		$pageResults = CmsPage::find()->asArray()->orderBy('name')->all();
31
		$urls = collect(CmsUrl::find()->asArray()->all())->indexBy('nice_id')->all();
32
		$pages = collect($pageResults)->map(function($page) use($urls) {
33
			$page['url'] = Arr::get($urls, [$page['nice_id'], 'url'], '');
34
			return $page;
35
		})->indexBy('id');
36
		neon()->cms->page->setEditMode(true);
37
		return $this->render('index', [
38
			'pages' => $pages,
39
			'id' => $id
40
		]);
41
	}
42
43
	/**
44
	 * An endpoint that cancels the edit mode - this can be called via ajax
45
	 * then the next page call edit mode will be cancelled - can manually refresh if exec in js context
46
	 */
47
	public function actionExitEditMode()
48
	{
49
		neon()->cms->page->setEditMode(false);
50
	}
51
52
	/**
53
	 * Add a new page
54
	 * @return array|string
55
	 * @throws \yii\base\UnknownPropertyException
56
	 */
57
	public function actionPageAdd()
58
	{
59
		$pageForm = new Page();
60
		if (neon()->request->isAjax) {
61
			return $pageForm->ajaxValidation();
62
		}
63
		if ($pageForm->processRequest()) {
64
			if (!$pageForm->save($errors)) {
65
				// something went wrong
66
				throw new \Exception( 'Something went wrong ' . print_r($errors, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($errors, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
				throw new \Exception( 'Something went wrong ' . /** @scrutinizer ignore-type */ print_r($errors, true));
Loading history...
67
			}
68
			$data = $pageForm->getData();
69
			return $this->redirect(['/cms/editor/index', 'id' => $data['nice_id']]);
70
		}
71
72
		return $this->render('add.tpl', ['page' => $pageForm]);
73
	}
74
75
	/**
76
	 * So users can create pages uses a standard themes/page/editor.tpl
77
	 * in the future this could ship with eon - but the editor would have 
78
	 * to also provide layout and global components. (nav / footer / etc)
79
	 * Or potentially you could select the layout file - and the editor could
80
	 * replace the $content smarty variable used for layouts in themes...
81
	 */
82
	public function actionPageAddEditor()
83
	{
84
		$pageForm = new Page();
85
		if (neon()->request->isAjax) {
86
			return $pageForm->ajaxValidation();
87
		}
88
		if ($pageForm->processRequest()) {
89
			if (!$pageForm->save($errors)) {
90
				// something went wrong
91
				throw new \Exception( 'Something went wrong ' . print_r($errors, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($errors, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
				throw new \Exception( 'Something went wrong ' . /** @scrutinizer ignore-type */ print_r($errors, true));
Loading history...
92
			}
93
			$data = $pageForm->getData();
94
			return $this->redirect(['/cms/editor/index', 'id' => $data['nice_id']]);
95
		}
96
97
		return $this->render('add.tpl', ['page' => $pageForm]);
98
	}
99
100
	/**
101
	 * Handle saving page updates
102
	 */
103
	public function actionPageUpdate()
104
	{
105
		$data = neon()->request->post();
106
		abort_if(! isset($data['CmsPage']['id']), 400, 'A "CmsPage[id]" parameter must be supplied');
107
		$cmsPage = CmsPage::findOne($data['CmsPage']['id']);
108
		$cmsPage->load(neon()->request->post());
109
		$cmsPage->save();
110
111
		if (isset($data['CmsPage']['url'])) {
112
			$cmsPage->redirect($data['CmsPage']['url'], neon()->request->post('redirect'), false);
113
		}
114
115
		$page = $cmsPage->toArray();
116
		$url = $cmsPage->getUrls();
117
		$page['url'] = $url[0]['url'];
118
		return $page;
119
	}
120
121
}
122