Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmatrixtype.php
Go to the documentation of this file.
1 <?php
18 class eZMatrixType extends eZDataType
19 {
20 const DEFAULT_NAME_VARIABLE = '_ezmatrix_default_name_';
21
22 const NUM_COLUMNS_VARIABLE = '_ezmatrix_default_num_columns_';
23 const NUM_ROWS_VARIABLE = '_ezmatrix_default_num_rows_';
24 const CELL_VARIABLE = '_ezmatrix_cell_';
25 const DATA_TYPE_STRING = 'ezmatrix';
26
27 public function __construct()
28 {
29 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', 'Matrix', 'Datatype name' ),
30 array( 'serialize_supported' => true ) );
31 }
32
37 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
38 {
39 $data = false;
40 if ( $http->hasPostVariable( $base . '_ezmatrix_cell_' . $contentObjectAttribute->attribute( 'id' ) ) )
41 $data = $http->PostVariable( $base . '_ezmatrix_cell_' . $contentObjectAttribute->attribute( 'id' ) );
42 $count = 0;
43 if ( is_array( $data ) )
44 {
45 for ( $i = 0; $i < count( $data ); ++$i )
46 if ( trim( $data[$i] ) <> '' )
47 {
48 ++$count;
49 break;
50 }
51 }
52 if ( $contentObjectAttribute->validateIsRequired() and ( $count == 0 or $data === false ) )
53 {
54 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
55 'Missing matrix input.' ) );
56 return eZInputValidator::STATE_INVALID;
57 }
58 return eZInputValidator::STATE_ACCEPTED;
59 }
60
64 function storeObjectAttribute( $contentObjectAttribute )
65 {
66 $matrix = $contentObjectAttribute->content();
67 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
68 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
69 $contentObjectAttribute->setContent( $matrix );
70 }
71
72 function storeClassAttribute( $contentClassAttribute, $version )
73 {
74 $matrixDefinition = $contentClassAttribute->content();
75 $contentClassAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
76 $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
77 $contentClassAttribute->setContent( $matrixDefinition );
78 }
79
83 function objectAttributeContent( $contentObjectAttribute )
84 {
85 $matrix = new eZMatrix( '' );
86
87 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
88
89 return $matrix;
90 }
91
92 function hasObjectAttributeContent( $contentObjectAttribute )
93 {
94 $matrix = $contentObjectAttribute->content();
95 $columnsArray = $matrix->attribute( 'columns' );
96 $columns = $columnsArray['sequential'];
97 $count = 0;
98 foreach ( $columns as $column )
99 {
100 $count += count( $column['rows'] );
101 }
102 return $count > 0;
103 }
104
108 function metaData( $contentObjectAttribute )
109 {
110 $matrix = $contentObjectAttribute->content();
111 $columnsArray = $matrix->attribute( 'columns' );
112 $columns = $columnsArray['sequential'];
113 $metaDataArray = array();
114 foreach ( $columns as $column )
115 {
116 $rows = $column['rows'];
117 foreach ( $rows as $row )
118 {
119 $metaDataArray[] = array( 'id' => $column['identifier'],
120 'text' => $row );
121 }
122 }
123 return $metaDataArray;
124 }
125
129 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
130 {
131 $cellsVarName = $base . self::CELL_VARIABLE . $contentObjectAttribute->attribute( 'id' );
132 if ( $http->hasPostVariable( $cellsVarName ) )
133 {
134 $cells = array();
135 foreach ( $http->postVariable( $cellsVarName ) as $cell )
136 {
137 $cells[] = $cell;
138 }
139 $matrix = $contentObjectAttribute->attribute( 'content' );
140 $matrix->Cells = $cells;
141
142 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
143 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
144 $contentObjectAttribute->setContent( $matrix );
145 }
146 return true;
147 }
148
149 function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
150 {
151 switch ( $action )
152 {
153 case 'new_row' :
154 {
155 $matrix = $contentObjectAttribute->content( );
156
157 $postvarname = 'ContentObjectAttribute' . '_data_matrix_remove_' . $contentObjectAttribute->attribute( 'id' );
158 $addCountName = 'ContentObjectAttribute' . '_data_matrix_add_count_' . $contentObjectAttribute->attribute( 'id' );
159
160 $addCount = 1;
161 if ( $http->hasPostVariable( $addCountName ) )
162 {
163 $addCount = $http->postVariable( $addCountName );
164 }
165
166 if ( $http->hasPostVariable( $postvarname ) )
167 {
168 $selected = $http->postVariable( $postvarname );
169 $matrix->addRow( $selected[0], $addCount );
170 }
171 else
172 {
173 $matrix->addRow( false, $addCount );
174 }
175
176 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
177 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
178 $contentObjectAttribute->setContent( $matrix );
179 $contentObjectAttribute->store();
180 }break;
181 case 'remove_selected' :
182 {
183 $matrix = $contentObjectAttribute->content( );
184 $postvarname = 'ContentObjectAttribute' . '_data_matrix_remove_' . $contentObjectAttribute->attribute( 'id' );
185 $arrayRemove = $http->postVariable( $postvarname );
186
187 rsort( $arrayRemove );
188 foreach ( $arrayRemove as $rowNum)
189 {
190 $matrix->removeRow( $rowNum );
191 }
192
193 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
194 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
195 $contentObjectAttribute->setContent( $matrix );
196 $contentObjectAttribute->store();
197 }break;
198 default :
199 {
200 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZMatrixType' );
201 }break;
202 }
203 }
204
208 function title( $contentObjectAttribute, $name = 'name' )
209 {
210 $matrix = $contentObjectAttribute->content( );
211
212 $value = $matrix->attribute( $name );
213
214 return $value;
215 }
216
220 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
221 {
222
223 if ( $currentVersion != false )
224 {
225 $matrix = $originalContentObjectAttribute->content();
226 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
227 // make sure that $matrix contains right columns
228 $matrix->adjustColumnsToDefinition( $contentClassAttribute->attribute( 'content' ) );
229
230 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
231 $contentObjectAttribute->setContent( $matrix );
232 }
233 else
234 {
235 $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
236 $numRows = $contentClassAttribute->attribute( 'data_int1' );
237 $matrix = new eZMatrix( '', $numRows, $contentClassAttribute->attribute( 'content' ) );
238 // 'default name' is never used => just a stub
239 // $matrix->setName( $contentClassAttribute->attribute( 'data_text1' ) );
240 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
241 $contentObjectAttribute->setContent( $matrix );
242 }
243
244 }
245
246 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
247 {
248 // 'default name' is never used => just a stub
249 // $defaultValueName = $base . self::DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
250 $defaultValueName = '';
251 $defaultNumColumnsName = $base . self::NUM_COLUMNS_VARIABLE . $classAttribute->attribute( 'id' );
252 $defaultNumRowsName = $base . self::NUM_ROWS_VARIABLE . $classAttribute->attribute( 'id' );
253 $dataFetched = false;
254 // 'default name' is never used => just a stub
255 /*
256 if ( $http->hasPostVariable( $defaultValueName ) )
257 {
258 $defaultValueValue = $http->postVariable( $defaultValueName );
259
260 if ( $defaultValueValue == '' )
261 {
262 $defaultValueValue = '';
263 }
264 $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
265 $dataFetched = true;
266 }
267 */
268
269 if ( $http->hasPostVariable( $defaultNumRowsName ) )
270 {
271 $defaultNumRowsValue = $http->postVariable( $defaultNumRowsName );
272
273 if ( $defaultNumRowsValue == '' )
274 {
275 $defaultNumRowsValue = '1';
276 }
277 $classAttribute->setAttribute( 'data_int1', $defaultNumRowsValue );
278 $dataFetched = true;
279 }
280
281 $columnNameVariable = $base . '_data_ezmatrix_column_name_' . $classAttribute->attribute( 'id' );
282 $columnIDVariable = $base . '_data_ezmatrix_column_id_' . $classAttribute->attribute( 'id' );
283
284
285 if ( $http->hasPostVariable( $columnNameVariable ) && $http->hasPostVariable( $columnIDVariable ) )
286 {
287 $columns = array();
288 $i = 0;
289 $columnNameList = $http->postVariable( $columnNameVariable );
290 $columnIDList = $http->postVariable( $columnIDVariable );
291
292 $matrixDefinition = $classAttribute->attribute( 'content' );
293 $columnNames = $matrixDefinition->attribute( 'columns' );
294 foreach ( $columnNames as $columnName )
295 {
296 $columnID = '';
297 $name = '';
298 $index = $columnName['index'];
299
300 // after adding a new column $columnIDList and $columnNameList doesn't contain values for new column.
301 // if so just add column with empty 'name' and 'columnID'.
302 if ( isset( $columnIDList[$index] ) && isset( $columnNameList[$index] ) )
303 {
304 $columnID = $columnIDList[$index];
305 $name = $columnNameList[$index];
306 if ( strlen( $columnID ) == 0 )
307 {
308 $columnID = $name;
309 // Initialize transformation system
310 $trans = eZCharTransform::instance();
311 $columnID = $trans->transformByGroup( $columnID, 'identifier' );
312 }
313 }
314
315 $columns[] = array( 'name' => $name,
316 'identifier' => $columnID,
317 'index' => $i );
318
319 $i++;
320 }
321
322 $matrixDefinition->ColumnNames = $columns;
323 $classAttribute->setContent( $matrixDefinition );
324 $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
325
326 $dataFetched = true;
327 }
328 if ( $dataFetched )
329 {
330 return true;
331 }
332 return false;
333
334 }
335
336 function preStoreClassAttribute( $classAttribute, $version )
337 {
338 $matrixDefinition = $classAttribute->attribute( 'content' );
339 $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
340 }
341
345 function classAttributeContent( $contentClassAttribute )
346 {
347 $matrixDefinition = new eZMatrixDefinition();
348 $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
349 return $matrixDefinition;
350 }
351
352 function customClassAttributeHTTPAction( $http, $action, $contentClassAttribute )
353 {
354 $id = $contentClassAttribute->attribute( 'id' );
355 switch ( $action )
356 {
357 case 'new_ezmatrix_column' :
358 {
359 $matrixDefinition = $contentClassAttribute->content( );
360 $matrixDefinition->addColumn( '' );
361 $contentClassAttribute->setContent( $matrixDefinition );
362 $contentClassAttribute->store();
363 }break;
364 case 'remove_selected' :
365 {
366 $matrixDefinition = $contentClassAttribute->content( );
367
368 $postvarname = 'ContentClass' . '_data_ezmatrix_column_remove_' . $contentClassAttribute->attribute( 'id' );
369 $array_remove = $http->postVariable( $postvarname );
370 foreach( $array_remove as $columnIndex )
371 {
372 $matrixDefinition->removeColumn( $columnIndex );
373 }
374 $contentClassAttribute->setContent( $matrixDefinition );
375 }break;
376 default :
377 {
378 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' );
379 }break;
380 }
381 }
382
383 function isIndexable()
384 {
385 return true;
386 }
387
392 function toString( $contentObjectAttribute )
393 {
394 $matrix = $contentObjectAttribute->attribute( 'content' );
395 $matrixArray = array();
396 $rows = $matrix->attribute( 'rows' );
397
398 foreach( $rows['sequential'] as $row )
399 {
400 $matrixArray[] = eZStringUtils::implodeStr( $row['columns'], '|' );
401 }
402
403 return eZStringUtils::implodeStr( $matrixArray, '&' );
404
405 }
406
407 function fromString( $contentObjectAttribute, $string )
408 {
409 $matrix = $contentObjectAttribute->attribute( 'content' );
410 $matrix->Cells = array();
411 $matrix->Matrix['rows']['sequential'] = array();
412 $matrix->NumRows = 0;
413
414 if ( $string != '' )
415 {
416 $matrixRowsList = eZStringUtils::explodeStr( $string, "&" );
417
418 foreach( $matrixRowsList as $key => $value )
419 {
420 $newCells = eZStringUtils::explodeStr( $value, '|' );
421 $matrixArray[] = $newCells;
422 $matrix->Cells = array_merge( $matrix->Cells, $newCells );
423
424 $newRow['columns'] = $newCells;
425 $newRow['identifier'] = 'row_' . ( $matrix->NumRows + 1 );
426 $newRow['name'] = 'Row_' . ( $matrix->NumRows + 1 );
427 $matrix->NumRows++;
428
429 $matrix->Matrix['rows']['sequential'][] = $newRow;
430 }
431 }
432
433 return true;
434 }
435
436 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
437 {
438 $content = $classAttribute->content();
439 if ( $content )
440 {
441 $defaultName = $classAttribute->attribute( 'data_text1' );
442 $defaultRowCount = $classAttribute->attribute( 'data_int1' );
443 $columns = $content->attribute( 'columns' );
444
445 $dom = $attributeParametersNode->ownerDocument;
446 $defaultNameNode = $dom->createElement( 'default-name' );
447 $defaultNameNode->appendChild( $dom->createTextNode( $defaultName ) );
448 $attributeParametersNode->appendChild( $defaultNameNode );
449 $defaultRowCountNode = $dom->createElement( 'default-row-count' );
450 $defaultRowCountNode->appendChild( $dom->createTextNode( $defaultRowCount ) );
451 $attributeParametersNode->appendChild( $defaultRowCountNode );
452 $columnsNode = $dom->createElement( 'columns' );
453 $attributeParametersNode->appendChild( $columnsNode );
454 foreach ( $columns as $column )
455 {
456 unset( $columnNode );
457 $columnNode = $dom->createElement( 'column' );
458 $columnNode->setAttribute( 'name', $column['name'] );
459 $columnNode->setAttribute( 'identifier', $column['identifier'] );
460 $columnNode->setAttribute( 'index', $column['index'] );
461 $columnsNode->appendChild( $columnNode );
462 }
463 }
464 }
465
466 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
467 {
468 $defaultName = $attributeParametersNode->getElementsByTagName( 'default-name' )->item( 0 )->textContent;
469 $defaultRowCount = $attributeParametersNode->getElementsByTagName( 'default-row-count' )->item( 0 )->textContent;
470 $classAttribute->setAttribute( 'data_text1', $defaultName );
471 $classAttribute->setAttribute( 'data_int1', $defaultRowCount );
472
473 $matrixDefinition = new eZMatrixDefinition();
474 $columnsNode = $attributeParametersNode->getElementsByTagName( 'columns' )->item( 0 );
475 $columnsList = $columnsNode->getElementsByTagName( 'column' );
476 foreach ( $columnsList as $columnNode )
477 {
478 $columnName = $columnNode->getAttribute( 'name' );
479 $columnIdentifier = $columnNode->getAttribute( 'identifier' );
480 $matrixDefinition->addColumn( $columnName, $columnIdentifier );
481 }
482 $classAttribute->setContent( $matrixDefinition );
483 }
484
485 function serializeContentObjectAttribute( $package, $objectAttribute )
486 {
487 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
488
489 $dom = new DOMDocument( '1.0', 'utf-8' );
490 $success = $dom->loadXML( $objectAttribute->attribute( 'data_text' ) );
491
492 $importedRoot = $node->ownerDocument->importNode( $dom->documentElement, true );
493 $node->appendChild( $importedRoot );
494
495 return $node;
496 }
497
498 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
499 {
500 $rootNode = $attributeNode->getElementsByTagName( 'ezmatrix' )->item( 0 );
501 $xmlString = $rootNode ? $rootNode->ownerDocument->saveXML( $rootNode ) : '';
502 $objectAttribute->setAttribute( 'data_text', $xmlString );
503 }
504
505 function supportsBatchInitializeObjectAttribute()
506 {
507 return true;
508 }
509
510 function batchInitializeObjectAttributeData( $classAttribute )
511 {
512 $numRows = $classAttribute->attribute( 'data_int1' );
513 $matrix = new eZMatrix( '', $numRows, $classAttribute->attribute( 'content' ) );
514 $db = eZDB::instance();
515 return array( 'data_text' => "'" . $db->escapeString( $matrix->xmlString() ) . "'" );
516 }
517 }
518
519 eZDataType::register( eZMatrixType::DATA_TYPE_STRING, 'ezmatrixtype' );
520
521 ?>
eZCharTransform\instance
static instance()
Definition: ezchartransform.php:434
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZDataType\createContentObjectAttributeDOMNode
createContentObjectAttributeDOMNode( $objectAttribute)
Definition: ezdatatype.php:1371
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
eZMatrixDefinition
The class eZMatrixDefinition does.
Definition: ezmatrixdefinition.php:19
eZMatrixType
The class eZMatrixType does.
Definition: ezmatrixtype.php:19
eZMatrixType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezmatrixtype.php:220
eZMatrixType\unserializeContentObjectAttribute
unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode)
Definition: ezmatrixtype.php:498
eZMatrixType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezmatrixtype.php:505
eZMatrixType\customObjectAttributeHTTPAction
customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters)
Definition: ezmatrixtype.php:149
eZMatrixType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezmatrixtype.php:92
eZMatrixType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezmatrixtype.php:466
eZMatrixType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezmatrixtype.php:246
eZMatrixType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezmatrixtype.php:83
eZMatrixType\isIndexable
isIndexable()
Definition: ezmatrixtype.php:383
eZMatrixType\storeObjectAttribute
storeObjectAttribute( $contentObjectAttribute)
Definition: ezmatrixtype.php:64
eZMatrixType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezmatrixtype.php:129
eZMatrixType\DEFAULT_NAME_VARIABLE
const DEFAULT_NAME_VARIABLE
Definition: ezmatrixtype.php:20
eZMatrixType\serializeContentObjectAttribute
serializeContentObjectAttribute( $package, $objectAttribute)
Definition: ezmatrixtype.php:485
eZMatrixType\batchInitializeObjectAttributeData
batchInitializeObjectAttributeData( $classAttribute)
Definition: ezmatrixtype.php:510
eZMatrixType\CELL_VARIABLE
const CELL_VARIABLE
Definition: ezmatrixtype.php:24
eZMatrixType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezmatrixtype.php:25
eZMatrixType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezmatrixtype.php:407
eZMatrixType\metaData
metaData( $contentObjectAttribute)
Definition: ezmatrixtype.php:108
eZMatrixType\preStoreClassAttribute
preStoreClassAttribute( $classAttribute, $version)
Definition: ezmatrixtype.php:336
eZMatrixType\toString
toString( $contentObjectAttribute)
Definition: ezmatrixtype.php:392
eZMatrixType\storeClassAttribute
storeClassAttribute( $contentClassAttribute, $version)
Definition: ezmatrixtype.php:72
eZMatrixType\classAttributeContent
classAttributeContent( $contentClassAttribute)
Definition: ezmatrixtype.php:345
eZMatrixType\title
title( $contentObjectAttribute, $name='name')
Definition: ezmatrixtype.php:208
eZMatrixType\NUM_COLUMNS_VARIABLE
const NUM_COLUMNS_VARIABLE
Definition: ezmatrixtype.php:22
eZMatrixType\customClassAttributeHTTPAction
customClassAttributeHTTPAction( $http, $action, $contentClassAttribute)
Definition: ezmatrixtype.php:352
eZMatrixType\__construct
__construct()
Definition: ezmatrixtype.php:27
eZMatrixType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezmatrixtype.php:37
eZMatrixType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezmatrixtype.php:436
eZMatrixType\NUM_ROWS_VARIABLE
const NUM_ROWS_VARIABLE
Definition: ezmatrixtype.php:23
eZMatrix
The class eZMatrix does.
Definition: ezmatrix.php:19
eZStringUtils\implodeStr
static implodeStr( $values, $delimiter='|')
Definition: ezstringutils.php:44
eZStringUtils\explodeStr
static explodeStr( $str, $delimiter='|')
Definition: ezstringutils.php:19
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.