/
ezkeywordtype.php
311 lines (267 loc) · 9.98 KB
/
ezkeywordtype.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
//
// Definition of eZKeywordType class
//
// Created on: <29-Apr-2003 14:59:12 bf>
//
// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
// SOFTWARE NAME: eZ Publish
// SOFTWARE RELEASE: 4.1.x
// COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
// SOFTWARE LICENSE: GNU General Public License v2.0
// NOTICE: >
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2.0 of the GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of version 2.0 of the GNU General
// Public License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//
// ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
//
/*!
\class eZKeywordType ezkeywordtype.php
\ingroup eZDatatype
\brief A content datatype which handles keyword indexes
*/
class eZKeywordType extends eZDataType
{
const DATA_TYPE_STRING = 'ezkeyword';
/*!
Initializes with a keyword id and a description.
*/
function eZKeywordType()
{
$this->eZDataType( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', 'Keywords', 'Datatype name' ),
array( 'serialize_supported' => true ) );
}
/*!
Sets the default value.
*/
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
{
if ( $currentVersion != false )
{
$originalContentObjectAttributeID = $originalContentObjectAttribute->attribute( 'id' );
$contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
// if translating or copying an object
if ( $originalContentObjectAttributeID != $contentObjectAttributeID )
{
// copy keywords links as well
$keyword = $originalContentObjectAttribute->content();
if ( is_object( $keyword ) )
{
$keyword->store( $contentObjectAttribute );
}
}
}
}
/*!
Validates the input and returns true if the input was
valid for this datatype.
*/
function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
$classAttribute = $contentObjectAttribute->contentClassAttribute();
if ( $http->hasPostVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
{
$data = $http->postVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
if ( $data == "" )
{
if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
{
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
'Input required.' ) );
return eZInputValidator::STATE_INVALID;
}
}
}
else if ( !$classAttribute->attribute( 'is_information_collector' ) and $contentObjectAttribute->validateIsRequired() )
{
$contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes', 'Input required.' ) );
return eZInputValidator::STATE_INVALID;
}
return eZInputValidator::STATE_ACCEPTED;
}
/*!
Fetches the http post var keyword input and stores it in the data instance.
*/
function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
{
if ( $http->hasPostVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) ) )
{
$data = $http->postVariable( $base . '_ezkeyword_data_text_' . $contentObjectAttribute->attribute( 'id' ) );
$keyword = new eZKeyword();
$keyword->initializeKeyword( $data );
$contentObjectAttribute->setContent( $keyword );
return true;
}
return false;
}
/*!
Does nothing since it uses the data_text field in the content object attribute.
See fetchObjectAttributeHTTPInput for the actual storing.
*/
function storeObjectAttribute( $attribute )
{
// create keyword index
$keyword = $attribute->content();
if ( is_object( $keyword ) )
{
$keyword->store( $attribute );
}
}
function storeClassAttribute( $attribute, $version )
{
}
function storeDefinedClassAttribute( $attribute )
{
}
function validateClassAttributeHTTPInput( $http, $base, $attribute )
{
return eZInputValidator::STATE_ACCEPTED;
}
function fixupClassAttributeHTTPInput( $http, $base, $attribute )
{
}
function fetchClassAttributeHTTPInput( $http, $base, $attribute )
{
return true;
}
/*!
Returns the content.
*/
function objectAttributeContent( $attribute )
{
$keyword = new eZKeyword();
$keyword->fetch( $attribute );
return $keyword;
}
/*!
Returns the meta data used for storing search indeces.
*/
function metaData( $attribute )
{
$keyword = new eZKeyword();
$keyword->fetch( $attribute );
$return = $keyword->keywordString();
return $return;
}
/*!
Delete stored object attribute
*/
function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
{
if ( $version != null ) // Do not delete if discarding draft
{
return;
}
$contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
$db = eZDB::instance();
/* First we retrieve all the keyword ID related to this object attribute */
$res = $db->arrayQuery( "SELECT keyword_id
FROM ezkeyword_attribute_link
WHERE objectattribute_id='$contentObjectAttributeID'" );
if ( !count ( $res ) )
{
/* If there are no keywords at all, we abort the function as there
* is nothing more to do */
return;
}
$keywordIDs = array();
foreach ( $res as $record )
$keywordIDs[] = $record['keyword_id'];
$keywordIDString = implode( ', ', $keywordIDs );
/* Then we see which ones only have a count of 1 */
$res = $db->arrayQuery( "SELECT keyword_id
FROM ezkeyword, ezkeyword_attribute_link
WHERE ezkeyword.id = ezkeyword_attribute_link.keyword_id
AND ezkeyword.id IN ($keywordIDString)
GROUP BY keyword_id
HAVING COUNT(*) = 1" );
$unusedKeywordIDs = array();
foreach ( $res as $record )
$unusedKeywordIDs[] = $record['keyword_id'];
$unusedKeywordIDString = implode( ', ', $unusedKeywordIDs );
/* Then we delete those unused keywords */
if ( $unusedKeywordIDString )
$db->query( "DELETE FROM ezkeyword WHERE id IN ($unusedKeywordIDString)" );
/* And as last we remove the link between the keyword and the object
* attribute to be removed */
$db->query( "DELETE FROM ezkeyword_attribute_link
WHERE objectattribute_id='$contentObjectAttributeID'" );
}
/*!
Returns the content of the keyword for use as a title
*/
function title( $attribute, $name = null )
{
$keyword = new eZKeyword();
$keyword->fetch( $attribute );
$return = $keyword->keywordString();
return $return;
}
function hasObjectAttributeContent( $contentObjectAttribute )
{
$keyword = new eZKeyword();
$keyword->fetch( $contentObjectAttribute );
$array = $keyword->keywordArray();
return count( $array ) > 0;
}
function isIndexable()
{
return true;
}
/*!
\return string representation of an contentobjectattribute data for simplified export
*/
function toString( $contentObjectAttribute )
{
$keyword = new eZKeyword();
$keyword->fetch( $contentObjectAttribute );
return $keyword->keywordString();
}
function fromString( $contentObjectAttribute, $string )
{
if ( $string != '' )
{
$keyword = new eZKeyword();
$keyword->initializeKeyword( $string );
$contentObjectAttribute ->setContent( $keyword );
}
return true;
}
function serializeContentObjectAttribute( $package, $objectAttribute )
{
$node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
$keyword = new eZKeyword();
$keyword->fetch( $objectAttribute );
$keyWordString = $keyword->keywordString();
$dom = $node->ownerDocument;
$keywordStringNode = $dom->createElement( 'keyword-string' );
$keywordStringNode->appendChild( $dom->createTextNode( $keyWordString ) );
$node->appendChild( $keywordStringNode );
return $node;
}
function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
{
$keyWordString = $attributeNode->getElementsByTagName( 'keyword-string' )->item( 0 )->textContent;
$keyword = new eZKeyword();
$keyword->initializeKeyword( $keyWordString );
$objectAttribute->setContent( $keyword );
}
function supportsBatchInitializeObjectAttribute()
{
return true;
}
}
eZDataType::register( eZKeywordType::DATA_TYPE_STRING, 'eZKeywordType' );
?>