|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\properties\url; |
|
4
|
|
|
|
|
5
|
|
|
use app\models\Property; |
|
6
|
|
|
use app\models\PropertyStaticValues; |
|
7
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\caching\TagDependency; |
|
10
|
|
|
use yii\helpers\ArrayHelper; |
|
11
|
|
|
|
|
12
|
|
|
class PropertyPart extends UrlPart |
|
13
|
|
|
{ |
|
14
|
|
|
public $include_if_value = []; |
|
15
|
|
|
public $optional = true; |
|
16
|
|
|
public $property_id = null; |
|
17
|
|
|
|
|
18
|
|
|
private function checkIncludeIfValue() |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
if (is_object($this->model)) { |
|
21
|
|
|
$object_property_values = $this->model->getPropertyValuesByPropertyId($this->property_id); |
|
22
|
|
|
if (!is_object($object_property_values)) { |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
foreach ($object_property_values->values as $val) { |
|
26
|
|
|
if (in_array($val['value'], $this->include_if_value)) { |
|
27
|
|
|
return $val['slug']; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Helper method to properly populate $this->params |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $paramName |
|
38
|
|
|
* @param null $paramValue |
|
39
|
|
|
*/ |
|
40
|
|
|
private function addParam($paramName = "", $paramValue = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (empty($paramName)) { |
|
43
|
|
|
throw new \InvalidArgumentException("Parameter name must be present"); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (isset($this->parameters[$paramName])) { |
|
47
|
|
|
$this->parameters[$paramName][] = $paramValue; |
|
48
|
|
|
} else { |
|
49
|
|
|
$this->parameters[$paramName] = [$paramValue]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getNextPart($full_url, $next_part, &$previous_parts) |
|
57
|
|
|
{ |
|
58
|
|
|
$property = Property::findById($this->property_id); |
|
59
|
|
|
if (is_null($property)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
if ($property->has_static_values && $property->has_slugs_in_values) { |
|
63
|
|
|
$cacheTags = []; |
|
64
|
|
|
$static_values = PropertyStaticValues::getValuesForPropertyId($this->property_id); |
|
65
|
|
|
$slugs = explode('/', $next_part); |
|
66
|
|
|
$currentSlug = 0; |
|
67
|
|
|
$slugsCount = count($slugs); |
|
68
|
|
|
$appliedParts = []; |
|
69
|
|
|
foreach ($static_values as $value) { |
|
70
|
|
|
if ($slugs[$currentSlug] === $value['slug']) { |
|
71
|
|
|
$appliedParts[] = $value['slug']; |
|
72
|
|
|
if (isset($this->parameters['properties'][$this->property_id])) { |
|
73
|
|
|
$this->parameters['properties'][$this->property_id][] = $value['id']; |
|
74
|
|
|
} else { |
|
75
|
|
|
$this->parameters = [ |
|
76
|
|
|
'properties' => [ |
|
77
|
|
|
$this->property_id => [$value['id']], |
|
78
|
|
|
], |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
if (!empty($value['title_append'])) { |
|
82
|
|
|
if ($value["title_prepend"] == 1) { |
|
83
|
|
|
$this->addParam("title_prepend", $value["title_append"]); |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->addParam("title_append", $value["title_append"]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
$cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $value['id']); |
|
89
|
|
|
$currentSlug++; |
|
90
|
|
|
if ($currentSlug == $slugsCount) { |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
if ($currentSlug > 0) { |
|
96
|
|
|
$appliedPartsString = implode('/', $appliedParts); |
|
97
|
|
|
$part = new self( |
|
98
|
|
|
[ |
|
99
|
|
|
'gathered_part' => $appliedPartsString, |
|
100
|
|
|
'rest_part' => mb_substr($next_part, mb_strlen($appliedPartsString)), |
|
101
|
|
|
'parameters' => $this->parameters, |
|
102
|
|
|
'cacheTags' => $cacheTags, |
|
103
|
|
|
] |
|
104
|
|
|
); |
|
105
|
|
|
return $part; |
|
106
|
|
|
} |
|
107
|
|
|
return false; |
|
108
|
|
|
} else { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @inheritdoc |
|
115
|
|
|
*/ |
|
116
|
|
|
public function appendPart($route, $parameters = [], &$used_params = [], &$cacheTags = []) |
|
117
|
|
|
{ |
|
118
|
|
|
if (isset($parameters['properties'])) { |
|
119
|
|
|
$used_params[] = 'properties'; |
|
120
|
|
|
if (isset($parameters['properties'][$this->property_id]) && |
|
121
|
|
|
is_array($parameters['properties'][$this->property_id]) |
|
122
|
|
|
) { |
|
123
|
|
|
$property_id = $this->property_id; |
|
124
|
|
|
$psvs = Yii::$app->db->cache( |
|
125
|
|
|
function($db) use ($property_id, $parameters) { |
|
|
|
|
|
|
126
|
|
|
$vals = array_values($parameters['properties'][$property_id]); |
|
127
|
|
|
$vals = array_map(function($item){ |
|
128
|
|
|
return intval($item); |
|
129
|
|
|
}, $vals); |
|
130
|
|
|
return PropertyStaticValues::find() |
|
131
|
|
|
->where(['id' => $vals]) |
|
132
|
|
|
->orderBy('sort_order ASC, name ASC') |
|
133
|
|
|
->asArray(true) |
|
134
|
|
|
->all(); |
|
135
|
|
|
}, |
|
136
|
|
|
86400, new TagDependency(['tags'=>[ActiveRecordHelper::getCommonTag(PropertyStaticValues::className())]]) |
|
|
|
|
|
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
foreach ($psvs as $psv) { |
|
140
|
|
|
if (count($this->include_if_value) > 0) { |
|
141
|
|
|
if (!in_array($psv['value'], $this->include_if_value)) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
if (!empty($psvs)) { |
|
147
|
|
|
foreach ($psvs as $psv) { |
|
148
|
|
|
$cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $psv['id']); |
|
149
|
|
|
} |
|
150
|
|
|
return implode('/', ArrayHelper::getColumn($psvs, 'slug')); |
|
151
|
|
|
} else { |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
return $this->checkIncludeIfValue(); |
|
156
|
|
|
} |
|
157
|
|
|
} else { |
|
158
|
|
|
return $this->checkIncludeIfValue(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.