-
Notifications
You must be signed in to change notification settings - Fork 612
/
TagElementType.php
223 lines (197 loc) · 5.12 KB
/
TagElementType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace Craft;
/**
* The TagElementType class is responsible for implementing and definingtags as a native element type in Craft.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @copyright Copyright (c) 2014, Pixel & Tonic, Inc.
* @license http://craftcms.com/license Craft License Agreement
* @see http://craftcms.com
* @package craft.app.elementtypes
* @since 1.1
*/
class TagElementType extends BaseElementType
{
// Public Methods
// =========================================================================
/**
* @inheritDoc IComponentType::getName()
*
* @return string
*/
public function getName()
{
return Craft::t('Tags');
}
/**
* @inheritDoc IElementType::hasContent()
*
* @return bool
*/
public function hasContent()
{
return true;
}
/**
* @inheritDoc IElementType::hasTitles()
*
* @return bool
*/
public function hasTitles()
{
return true;
}
/**
* @inheritDoc IElementType::isLocalized()
*
* @return bool
*/
public function isLocalized()
{
return true;
}
/**
* @inheritDoc IElementType::getSources()
*
* @param string|null $context
*
* @return array|false
*/
public function getSources($context = null)
{
$sources = array();
foreach (craft()->tags->getAllTagGroups() as $tagGroup)
{
$key = 'taggroup:'.$tagGroup->id;
$sources[$key] = array(
'label' => HtmlHelper::encode(Craft::t($tagGroup->name)),
'criteria' => array('groupId' => $tagGroup->id)
);
}
return $sources;
}
/**
* @inheritDoc IElementType::defineSearchableAttributes()
*
* @return array
*/
public function defineSearchableAttributes()
{
// TODO: Remove this in 3.0
return array('name');
}
/**
* @inheritDoc IElementType::defineCriteriaAttributes()
*
* @return array
*/
public function defineCriteriaAttributes()
{
return array(
'group' => AttributeType::Mixed,
'groupId' => AttributeType::Mixed,
'order' => array(AttributeType::String, 'default' => 'content.title asc'),
// TODO: Deprecated
'name' => AttributeType::String,
'set' => AttributeType::Mixed,
'setId' => AttributeType::Mixed,
);
}
/**
* @inheritDoc IElementType::modifyElementsQuery()
*
* @param DbCommand $query
* @param ElementCriteriaModel $criteria
*
* @return mixed
*/
public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
{
$query
->addSelect('tags.groupId')
->join('tags tags', 'tags.id = elements.id');
// Still support the deprecated params
if ($criteria->name)
{
craft()->deprecator->log('TagElementType::modifyElementsQuery():name_param', 'The ‘name’ tag param has been deprecated. Use ‘title’ instead.');
$query->andWhere(DbHelper::parseParam('content.title', $criteria->name, $query->params));
}
if ($criteria->setId && !$criteria->groupId)
{
craft()->deprecator->log('TagElementType::modifyElementsQuery():setId_param', 'The ‘setId’ tag param has been deprecated. Use ‘groupId’ instead.');
$criteria->groupId = $criteria->setId;
$criteria->setId = null;
}
if ($criteria->set && !$criteria->group)
{
craft()->deprecator->log('TagElementType::modifyElementsQuery():set_param', 'The ‘set’ tag param has been deprecated. Use ‘group’ instead.');
$criteria->group = $criteria->set;
$criteria->set = null;
}
if ($criteria->groupId)
{
$query->andWhere(DbHelper::parseParam('tags.groupId', $criteria->groupId, $query->params));
}
if ($criteria->group)
{
$query->join('taggroups taggroups', 'taggroups.id = tags.groupId');
$query->andWhere(DbHelper::parseParam('taggroups.handle', $criteria->group, $query->params));
}
// Backwards compatibility with order=name (tags had names before 2.3)
if (is_string($criteria->order))
{
$criteria->order = preg_replace('/\bname\b/', 'title', $criteria->order, -1, $count);
if ($count) {
craft()->deprecator->log('tag_orderby_name', 'Ordering tags by ‘name’ has been deprecated. Order by ‘title’ instead.');
}
}
}
/**
* @inheritDoc IElementType::populateElementModel()
*
* @param array $row
*
* @return array
*/
public function populateElementModel($row)
{
return TagModel::populateModel($row);
}
/**
* @inheritDoc IElementType::getEditorHtml()
*
* @param BaseElementModel $element
*
* @return string
*/
public function getEditorHtml(BaseElementModel $element)
{
$html = craft()->templates->renderMacro('_includes/forms', 'textField', array(
array(
'label' => Craft::t('Title'),
'locale' => $element->locale,
'id' => 'title',
'name' => 'title',
'value' => $element->getContent()->title,
'errors' => $element->getErrors('title'),
'first' => true,
'autofocus' => true,
'required' => true
)
));
$html .= parent::getEditorHtml($element);
return $html;
}
/**
* @inheritDoc IElementType::saveElement()
*
* @param BaseElementModel $element
* @param array $params
*
* @return bool
*/
public function saveElement(BaseElementModel $element, $params)
{
return craft()->tags->saveTag($element);
}
}