Passed
Push — develop ( 84f32e...989890 )
by
unknown
15:42 queued 41s
created

CmsPage::afterSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2008 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 */
7
namespace neon\cms\models;
8
9
use neon\cms\models\CmsUrl;
10
use neon\core\db\ActiveRecord;
11
12
/**
13
 * CmsPage model
14
 *
15
 * The idea is that each url has a dedicated page.  A page then has content attached to it.
16
 * For example a page might show a an ecommerce product, a blog post and an author, each of these
17
 * items needs to be attached to the page.
18
 *
19
 * You could imagine a product manager for example would have to add a product and under the hood
20
 * configure a page record that shows the product.  This allows you to set custom page type things
21
 * and I think gives maximum flexibility, for example different templates for each page if you really
22
 * want.   However another approach could be to make a generic routes lookup table that defers everything
23
 * to the routing.
24
 *
25
 * @property string nice_id - a friendly looking id to reference this page in code and templates
26
 */
27
class CmsPage extends ActiveRecord
28
{
29
	const STATUS_PUBLISHED = 'PUBLISHED';
30
	const STATUS_DRAFT = 'DRAFT';
31
32
	public static $timestamps = true;
33
	public static $blame = true;
34
	public static $idIsUuid64 = true;
35
	public static $softDelete = true;
36
37
	/**
38
	 * @inheritdoc
39
	 */
40 28
	public static function tableName()
41
	{
42 28
		return '{{%cms_page}}';
43
	}
44
45
	/**
46
	 * Find page by its uuid (id) or nice_id
47
	 * @param string $pageId
48
	 * @return array of cms_page fields
49
	 */
50
	public static function findBy($pageId)
51
	{
52
		return neon()->cache->getOrSet("page.$pageId", function() use ($pageId) {
53
			return CmsPage::find()
54
				->where(['cms_page.nice_id' => $pageId])
55
				->orWhere(['id' => $pageId])
56
				->asArray()
57
				->one();
58
		});
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 */
64 28
	public function afterSave($insert, $changedAttributes)
65
	{
66 28
		parent::afterSave($insert, $changedAttributes);
67
		// clear caches
68 28
		neon()->cache->delete("page.".$this->nice_id);
69 28
		neon()->cache->delete("page.".$this->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on neon\cms\models\CmsPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
70 28
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75 28
	public function rules()
76
	{
77
		return [
78 28
			['nice_id', 'unique'],
79
			[[
80
				'name', 'title', 'status', 'description', 'keywords', 'template', 'status',
81
				'og_description', 'og_title', 'og_image', 'og_title_use_seo', 'og_description_use_seo'
82
			], 'safe']
83
		];
84
	}
85
86
	/**
87
	 * Get an array of all urls that link to this nice_id
88
	 *
89
	 * @return CmsUrl[]
90
	 */
91
	public function getUrls()
92
	{
93
		return CmsUrl::find()->where(['nice_id' => $this->nice_id])->all();
94
	}
95
96
	/**
97
	 * Update a pages url
98
	 * This also creates a 301 redirect from the existing url to the new url if £createRedirect is true
99
	 *
100
	 * @param string $newUrl - the new url for the page
101
	 * @param boolean $createRedirect - whether to create a 301 redirect or not
102
	 * @param boolean $throw - whether to throw an exception if the page has multiple urls
103
	 * @throws \yii\web\HttpException
104
	 * @return boolean - whether new url was saved
105
	 */
106
	public function redirect($newUrl, $createRedirect, $throw=true)
107
	{
108
		// find urls that link to this page
109
		$urls = CmsUrl::find()->where(['nice_id' => $this->nice_id])->limit(10)->all();
110
		if (count($urls) > 1) {
111
			if ($throw) abort(400, 'There must be only one url that links to this page - otherwise the wrong url may be updated');
112
			return false;
113
		}
114
		$url = $urls[0];
115
		return $url->updateUrl($newUrl, $createRedirect);
116
	}
117
118
}
119