Passed
Push — 2.7.11 ( ...beab38 )
by steve
18:35 queued 14s
created

PageGrid::scopeDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: steve
5
 * Date: 12/12/2015
6
 * Time: 11:28
7
 */
8
9
namespace neon\cms\grid;
10
11
use neon\cms\components\Page;
12
use neon\cms\models\CmsPage;
13
use neon\cms\models\CmsUrl;
14
use neon\core\grid\query\IQuery;
15
use \neon\core\helpers\Url;
16
use \neon\core\helpers\Html;
17
use neon\user\models\User;
18
use yii\web\HttpException;
19
20
class PageGrid extends \neon\core\grid\Grid
21
{
22
	/**
23
	 * @inheritdoc
24
	 */
25
	public function configure()
26
	{
27
		$this->query = (new \yii\db\Query())
28
			->from('cms_page')
29
			->select('cms_page.*, cms_url.url')
30
			->innerJoin('cms_url', 'cms_page.nice_id = cms_url.nice_id')
31
			->where("cms_page.template NOT LIKE 'partials/%'")
32
			->groupBy('`cms_page`.`nice_id`')
33
			->orderBy('url, title');
34
35
		$this->title = 'Pages';
36
37
		$this->setPageSize(100);
38
39
		$this->addColumn('id')->setAsIndex()->setVisible(false);
40
41
		$this->addColumn('screenshot')->setWidth('150px');
42
		// columns
43
		$this->addTextColumn('nice_id')
44
			->setTitle('Id')
45
			->setDbField('cms_page.nice_id')
46
			->setWidth('100px')
47
			->setVisible(true);
48
49
		//$this->addTextColumn('name')->setTitle('Reference Name');
50
		$this->addTextColumn('url')->setDbField('cms_url.url')->setTitle('Url slug')->setWidth('200px');
51
		$this->addTextColumn('title')->setWidth('450px');
52
		$this->addTextColumn('template')->setWidth('100px');
53
		
54
		$this->addColumn('og_image')->setTitle('Social')->setWidth('100px')->setMember(new \neon\core\form\fields\Image('og_image'));
55
56
		$this->addButtonColumn('edit')
57
			->addButton('edit', ['/cms/editor/index' , 'id' => '{{nice_id}}'], '', '', '', ['data-toggle' => 'tooltip', 'title'=>'Edit']);
58
59
		$this->addScope('published', 'Published');
60
		$this->addScope('draft', 'Draft');
61
		$this->addScope('deleted', 'Deleted');
62
	}
63
64
	/**
65
	 * @inheritdoc
66
	 */
67
	public function setup()
68
	{
69
		if ($this->isScopeActive('draft')) {
70
			$this->getColumn('nice_id')->addRowAction('actionPublish', 'Publish');
71
		}
72
	}
73
74
	public function renderNice_id($model)
75
	{
76
		return "<div style='text-align:center;width'><div>{$model['name']}</div><code>{$model['nice_id']}</code></div>";
77
	}
78
79
	public function renderTitle($model)
80
	{
81
		return '<div style="font-weight:600;">' . $model['title'] . '</div><div stlye="margin-top:4px;">' . $model['description'] . '</div>';
82
	}
83
84
	public function renderUrl($model, $key, $index, $column)
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

84
	public function renderUrl($model, $key, $index, /** @scrutinizer ignore-unused */ $column)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed. ( Ignorable by Annotation )

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

84
	public function renderUrl($model, $key, /** @scrutinizer ignore-unused */ $index, $column)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

84
	public function renderUrl($model, /** @scrutinizer ignore-unused */ $key, $index, $column)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
	{
86
		return CmsUrl::getUrlByNiceId($model['nice_id']);
87
	}
88
89
	public function renderScreenshot($model)
90
	{
91
		$url = neon()->urlManager->createAbsoluteUrl(CmsUrl::getUrlByNiceId($model['nice_id']));
92
		return "<img width='150' src='https://image.thum.io/get/auth/54089-qckimgt/width/300/$url' />";
93
	}
94
95
	/**
96
	 * Quick publish action
97
	 * @param string $id - page id (gets passed row column data marked as index)
98
	 * @throws HttpException - if no page found with id
99
	 */
100
	public function actionPublish($id)
101
	{
102
		$page = $this->_getPage($id);
103
		$page->status = CmsPage::STATUS_PUBLISHED;
104
		$page->save();
105
	}
106
107
	/**
108
	 * Quick make draft action
109
	 * @param string $id - page id (gets passed row column data marked as index)
110
	 * @throws HttpException
111
	 */
112
	public function actionDraft($id)
113
	{
114
		$page = $this->_getPage($id);
115
		$page->status = CmsPage::STATUS_DRAFT;
116
		$page->save();
117
	}
118
119
	/**
120
	 * Get user active record by id
121
	 * @throws HttpException 500 error if user with $id does not exist
122
	 * @param string $uuid
123
	 * @return User
124
	 */
125
	private function _getPage($uuid)
126
	{
127
		$page = CmsPage::find()->where(['id' => $uuid])->one();
128
		if ($page === null) {
129
			throw new HttpException(404, "No page exists with id '$uuid'");
130
		}
131
		return $page;
132
	}
133
134
	/**
135
	 * @param IQuery $query
136
	 */
137
	public function scopeDeleted(IQuery $query)
138
	{
139
		$query->where('deleted', '=', 1);
140
	}
141
142
	/**
143
	 * @param IQuery $query
144
	 */
145
	public function scopePublished(IQuery $query)
146
	{
147
		$query->where('status', '=', 'PUBLISHED');
148
	}
149
150
	/**
151
	 * @param IQuery $query
152
	 */
153
	public function scopeDraft(IQuery $query)
154
	{
155
		$query->where('status', '=', 'DRAFT');
156
	}
157
158
}
159