Test Failed
Push — develop ( ff58ad...b8f9b2 )
by steve
13:44 queued 12s
created

Slug::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2017 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 * @author Steve O'Brien <[email protected]> 17/04/2020
7
 * @package neon
8
 */
9
10
namespace neon\cms\form\fields;
11
12
use neon\cms\models\CmsUrl;
13
use \neon\core\form\fields\Field;
14
use neon\core\validators\RegularExpressionValidator;
15
use neon\core\validators\UniqueValidator;
16
use neon\daedalus\events\ObjectAddEvent;
17
use neon\daedalus\events\ObjectEditEvent;
18
use neon\daedalus\services\ddsManager\DdsObjectManager;
19
use yii\base\Event;
20
use yii\db\Exception;
21
22
class Slug extends Field
23
{
24
	public $pageId = '';
25
	public $params = '';
26
27
	/**
28
	 * @inheritdoc
29
	 */
30
	public function registerScripts($view)
31
	{
32
		$view->registerAssetBundle(\neon\cms\assets\CmsFormAsset::class);
33
	}
34
35
	/**
36
	 * @inheritdoc
37
	 */
38
	public function getFilterField()
39
	{
40
		return ['class' => 'text'];
41
	}
42
43
	/**
44
	 * @inheritdoc
45
	 */
46
	public function getProperties()
47
	{
48
		return array_merge(['pageId'], parent::getProperties());
49
	}
50
51
	/**
52
	 * @inheritdoc
53
	 */
54
	public function init()
55
	{
56
		$this->addValidator([
57
			'class' => UniqueValidator::class,
58
			'targetClass'=> CmsUrl::class,
59
			'targetAttribute'=>'url',
60
		],[],'unique');
61
62
		$this->addValidator([
63
			'class' => RegularExpressionValidator::class,
64
			'pattern' => '/^\/[a-z0-9-\/]+$/',
65
			'message' => 'must start with "/" and contain "a-z", "0-9", "-" and "/" characters only'
66
		]);
67
		
68
		// assuming operation using DDS where form $name maps to dds classType
69
		if (!$this->hasForm()) return;
70
		$classType = $this->getForm()->getName();
71
		Event::on(DdsObjectManager::class, "dds.afterAdd.$classType", [$this, 'addCmsUrl']);
72
		Event::on(DdsObjectManager::class, "dds.afterEdit.$classType", [$this, 'editCmsUrl']);
73
	}
74
75
	public function validate()
76
	{
77
		$this->setErrors([]);
78
		if ($this->pageId === '') {
79
			$this->addError('You must specify a page ID');
80
		}
81
		return parent::validate();
82
	}
83
84
	/**
85
	 * @inheritdoc
86
	 */
87
	public function setValueFromDb($value)
88
	{
89
		// assume we are in edit mode
90
		$validators = $this->getValidators();
91
		if (array_key_exists('unique', $validators)) {
92
			$validators['unique']->filter = ['!=', 'url', $value];
0 ignored issues
show
Bug Best Practice introduced by
The property filter does not exist on neon\core\validators\Validator. Since you implemented __set, consider adding a @property annotation.
Loading history...
93
		}
94
		return parent::setValueFromDb($value);
95
	}
96
97
	/**
98
	 * Handle after save edit
99
	 * @param ObjectEditEvent $event
100
	 */
101
	public function editCmsUrl(ObjectEditEvent $event)
102
	{
103
		if (!isset($event->object[$this->name])) return;
104
		$originalSlug = $event->originalValues[$this->name];
105
		$newSlug = $event->object[$this->name];
106
		// edit the slug in the cms_url table
107
		$url = CmsUrl::findOne(['url' => $originalSlug]);
108
		if (empty($newSlug)) return;
109
		if ($url === null) $url = new CmsUrl();
110
		$url->url = $newSlug;
111
		$url->nice_id = $this->pageId;
112
		$url->save();
113
	}
114
115
	/**
116
	 * Handle after save add
117
	 * @param ObjectAddEvent $event
118
	 */
119
	public function addCmsUrl(ObjectAddEvent $event)
120
	{
121
		if (!isset($event->object[$this->name])) return;
122
		$slug = $event->object[$this->name];
123
		if (empty($slug)) return;
124
		// add a new slug to cms url table
125
		$cmsUrl = new CmsUrl();
126
		$cmsUrl->url = $slug;
127
		$cmsUrl->nice_id = $this->pageId;
128
		$cmsUrl->save();
129
	}
130
}
131