Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezkeyword.php
Go to the documentation of this file.
1 <?php
18 class eZKeyword
19 {
20 function attributes()
21 {
22 return array( 'keywords',
23 'keyword_string',
24 'related_objects',
25 'related_nodes' );
26 }
27
28 function hasAttribute( $name )
29 {
30 return in_array( $name, $this->attributes() );
31 }
32
33 function attribute( $name )
34 {
35 switch ( $name )
36 {
37 case 'keywords' :
38 {
39 return $this->KeywordArray;
40 }break;
41
42 case 'keyword_string' :
43 {
44 return $this->keywordString();
45 }break;
46
47 case 'related_objects' :
48 case 'related_nodes' :
49 {
50 return $this->relatedObjects();
51 }break;
52 default:
53 {
54 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
55 return null;
56 }break;
57 }
58 }
59
63 function initializeKeyword( $keywordString )
64 {
65 if ( !is_array( $keywordString ) )
66 {
67 $keywordArray = explode( ',', $keywordString );
68 $keywordArray = array_unique ( $keywordArray );
69 }
70 else
71 {
72 $keywordArray = $keywordString;
73 }
74 foreach ( array_keys( $keywordArray ) as $key )
75 {
76 if ( trim( $keywordArray[$key] ) != '' )
77 {
78 $this->KeywordArray[$key] = trim( $keywordArray[$key] );
79 }
80 }
81 }
82
86 function store( $attribute )
87 {
88 $db = eZDB::instance();
89
90 $object = $attribute->attribute( 'object' );
91 $classID = $object->attribute( 'contentclass_id' );
92
93 // Get already existing keywords
94 if ( count( $this->KeywordArray ) > 0 )
95 {
96 $escapedKeywordArray = array();
97 foreach( $this->KeywordArray as $keyword )
98 {
99 $keyword = $db->escapeString( $keyword );
100 $escapedKeywordArray[] = $keyword;
101 }
102 $wordsString = implode( '\',\'', $escapedKeywordArray );
103 $existingWords = $db->arrayQuery( "SELECT * FROM ezkeyword WHERE keyword IN ( '$wordsString' ) AND class_id='$classID' " );
104 }
105 else
106 {
107 $existingWords = array();
108 }
109
110 $newWordArray = array();
111 $existingWordArray = array();
112 // Find out which words to store
113 foreach ( $this->KeywordArray as $keyword )
114 {
115 $wordExists = false;
116 $wordID = false;
117 foreach ( $existingWords as $existingKeyword )
118 {
119 if ( $keyword == $existingKeyword['keyword'] )
120 {
121 $wordExists = true;
122 $wordID = $existingKeyword['id'];
123 break;
124 }
125 }
126
127 if ( $wordExists == false )
128 {
129 $newWordArray[] = $keyword;
130 }
131 else
132 {
133 $existingWordArray[] = array( 'keyword' => $keyword, 'id' => $wordID );
134 }
135 }
136
137 // Store every new keyword
138 $addRelationWordArray = array();
139 foreach ( $newWordArray as $keyword )
140 {
141 $keyword = trim( $keyword );
142 $keyword = $db->escapeString( $keyword );
143 $db->query( "INSERT INTO ezkeyword ( keyword, class_id ) VALUES ( '$keyword', '$classID' )" );
144
145 $keywordID = $db->lastSerialID( 'ezkeyword', 'id' );
146 $addRelationWordArray[] = array( 'keyword' => $keywordID, 'id' => $keywordID );
147 }
148
149 $attributeID = $attribute->attribute( 'id' );
150 // Find the words which is new for this attribute
151 if ( $attributeID !== null )
152 {
153 $currentWordArray = $db->arrayQuery( "SELECT ezkeyword.id, ezkeyword.keyword FROM ezkeyword, ezkeyword_attribute_link
154 WHERE ezkeyword.id=ezkeyword_attribute_link.keyword_id
155 AND ezkeyword_attribute_link.objectattribute_id='$attributeID'" );
156 }
157 else
158 $currentWordArray = array();
159
160 foreach ( $existingWordArray as $existingWord )
161 {
162 $newWord = true;
163 foreach ( $currentWordArray as $currentWord )
164 {
165 if ( $existingWord['keyword'] == $currentWord['keyword'] )
166 {
167 $newWord = false;
168 }
169 }
170
171 if ( $newWord == true )
172 {
173 $addRelationWordArray[] = $existingWord;
174 }
175 }
176
177 // Find the current words no longer used
178 $removeWordRelationIDArray = array();
179 foreach ( $currentWordArray as $currentWord )
180 {
181 $stillUsed = false;
182 foreach ( $this->KeywordArray as $keyword )
183 {
184 if ( $keyword == $currentWord['keyword'] )
185 $stillUsed = true;
186 }
187 if ( !$stillUsed )
188 {
189 $removeWordRelationIDArray[] = $currentWord['id'];
190 }
191 }
192
193 if ( count( $removeWordRelationIDArray ) > 0 )
194 {
195 $removeIDString = implode( ', ', $removeWordRelationIDArray );
196 $db->query( "DELETE FROM ezkeyword_attribute_link WHERE keyword_id IN ( $removeIDString ) AND ezkeyword_attribute_link.objectattribute_id='$attributeID'" );
197 }
198
199 // Only store relation to new keywords
200 // Store relations to keyword for this content object
201 foreach ( $addRelationWordArray as $keywordArray )
202 {
203 $db->query( "INSERT INTO ezkeyword_attribute_link ( keyword_id, objectattribute_id ) VALUES ( '" . $keywordArray['id'] ."', '" . $attribute->attribute( 'id' ) . "' )" );
204 }
205
206 /* Clean up no longer used words:
207 * 1. Select words having no links.
208 * 2. Delete them.
209 * We cannot do this in one cross-table DELETE since older MySQL versions do not support this.
210 */
211 if ( $db->databaseName() == 'oracle' )
212 {
213 $query =
214 'SELECT ezkeyword.id FROM ezkeyword, ezkeyword_attribute_link ' .
215 'WHERE ezkeyword.id=ezkeyword_attribute_link.keyword_id(+) AND ' .
216 'ezkeyword_attribute_link.keyword_id IS NULL';
217 }
218 else
219 {
220 $query =
221 'SELECT ezkeyword.id FROM ezkeyword LEFT JOIN ezkeyword_attribute_link ' .
222 ' ON ezkeyword.id=ezkeyword_attribute_link.keyword_id' .
223 ' WHERE ezkeyword_attribute_link.keyword_id IS NULL';
224 }
225 $unusedWordsIDs = $db->arrayQuery( $query );
226 foreach ( $unusedWordsIDs as $wordID )
227 $db->query( 'DELETE FROM ezkeyword WHERE id=' . $wordID['id'] );
228 }
229
233 function fetch( &$attribute )
234 {
235 if ( $attribute->attribute( 'id' ) === null )
236 return;
237
238 $db = eZDB::instance();
239 $wordArray = $db->arrayQuery( "SELECT ezkeyword.keyword FROM ezkeyword_attribute_link, ezkeyword
240 WHERE ezkeyword_attribute_link.keyword_id=ezkeyword.id AND
241 ezkeyword_attribute_link.objectattribute_id='" . $attribute->attribute( 'id' ) ."' " );
242
243 $this->ObjectAttributeID = $attribute->attribute( 'id' );
244 foreach ( array_keys( $wordArray ) as $wordKey )
245 {
246 $this->KeywordArray[] = $wordArray[$wordKey]['keyword'];
247 }
248 $this->KeywordArray = array_unique ( $this->KeywordArray );
249 }
250
254 function setKeywordArray( $keywords )
255 {
256 $this->KeywordArray = $keywords;
257 }
258
262 function keywordArray( )
263 {
264 return $this->KeywordArray;
265 }
266
270 function keywordString()
271 {
272 return implode( ', ', $this->KeywordArray );
273 }
274
280 function relatedObjects()
281 {
282 $return = false;
283 if ( $this->ObjectAttributeID )
284 {
285 $return = array();
286
287 // Fetch words
288 $db = eZDB::instance();
289
290 $wordArray = $db->arrayQuery( "SELECT * FROM ezkeyword_attribute_link
291 WHERE objectattribute_id='" . $this->ObjectAttributeID ."' " );
292
293 $keywordIDArray = array();
294 // Fetch the objects which have one of these words
295 foreach ( $wordArray as $word )
296 {
297 $keywordIDArray[] = $word['keyword_id'];
298 }
299
300 $keywordCondition = $db->generateSQLINStatement( $keywordIDArray, 'keyword_id' );
301
302 if ( count( $keywordIDArray ) > 0 )
303 {
304 $objectArray = $db->arrayQuery( "SELECT DISTINCT ezcontentobject_attribute.contentobject_id FROM ezkeyword_attribute_link, ezcontentobject_attribute
305 WHERE $keywordCondition AND
306 ezcontentobject_attribute.id = ezkeyword_attribute_link.objectattribute_id
307 AND objectattribute_id <> '" . $this->ObjectAttributeID ."' " );
308
309 $objectIDArray = array();
310 foreach ( $objectArray as $object )
311 {
312 $objectIDArray[] = $object['contentobject_id'];
313 }
314
315 if ( count( $objectIDArray ) > 0 )
316 {
317 $aNodes = eZContentObjectTreeNode::findMainNodeArray( $objectIDArray );
318
319 foreach ( $aNodes as $key => $node )
320 {
321 $theObject = $node->object();
322 if ( $theObject->canRead() )
323 {
324 $return[] = $node;
325 }
326 }
327 }
328 }
329 }
330 return $return;
331 }
332
334 public $KeywordArray = array();
335
337 public $ObjectAttributeID = false;
338 }
339
340 ?>
eZContentObjectTreeNode\findMainNodeArray
static findMainNodeArray( $objectIDArray, $asObject=true)
Definition: ezcontentobjecttreenode.php:3018
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZKeyword
A content datatype which handles keyword index instances.
Definition: ezkeyword.php:19
eZKeyword\setKeywordArray
setKeywordArray( $keywords)
Definition: ezkeyword.php:254
eZKeyword\initializeKeyword
initializeKeyword( $keywordString)
Definition: ezkeyword.php:63
eZKeyword\keywordArray
keywordArray()
Definition: ezkeyword.php:262
eZKeyword\relatedObjects
relatedObjects()
Definition: ezkeyword.php:280
eZKeyword\attributes
attributes()
Definition: ezkeyword.php:20
eZKeyword\fetch
fetch(&$attribute)
Definition: ezkeyword.php:233
eZKeyword\$ObjectAttributeID
$ObjectAttributeID
Contains the ID attribute if fetched.
Definition: ezkeyword.php:337
eZKeyword\$KeywordArray
$KeywordArray
Contains the keywords.
Definition: ezkeyword.php:334
eZKeyword\keywordString
keywordString()
Definition: ezkeyword.php:270
eZKeyword\store
store( $attribute)
Definition: ezkeyword.php:86
eZKeyword\hasAttribute
hasAttribute( $name)
Definition: ezkeyword.php:28
eZKeyword\attribute
attribute( $name)
Definition: ezkeyword.php:33
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.