00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 include_once( 'kernel/classes/ezdatatype.php' );
00042 include_once( 'kernel/classes/datatypes/ezmatrix/ezmatrix.php' );
00043 include_once( 'kernel/classes/datatypes/ezmatrix/ezmatrixdefinition.php' );
00044 include_once( 'lib/ezutils/classes/ezstringutils.php' );
00045
00046 define( 'EZ_MATRIX_DEFAULT_NAME_VARIABLE', '_ezmatrix_default_name_' );
00047
00048 define( 'EZ_MATRIX_NUMCOLUMNS_VARIABLE', '_ezmatrix_default_num_columns_' );
00049 define( 'EZ_MATRIX_NUMROWS_VARIABLE', '_ezmatrix_default_num_rows_' );
00050 define( 'EZ_MATRIX_CELL_VARIABLE', '_ezmatrix_cell_' );
00051 define( 'EZ_DATATYPESTRING_MATRIX', 'ezmatrix' );
00052
00053 class eZMatrixType extends eZDataType
00054 {
00055
00056
00057
00058 function eZMatrixType()
00059 {
00060 $this->eZDataType( EZ_DATATYPESTRING_MATRIX, ezi18n( 'kernel/classes/datatypes', 'Matrix', 'Datatype name' ),
00061 array( 'serialize_supported' => true ) );
00062 }
00063
00064
00065
00066
00067
00068 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00069 {
00070 $data = false;
00071 if ( $http->hasPostVariable( $base . '_ezmatrix_cell_' . $contentObjectAttribute->attribute( 'id' ) ) )
00072 $data = $http->PostVariable( $base . '_ezmatrix_cell_' . $contentObjectAttribute->attribute( 'id' ) );
00073 $count = 0;
00074 for ( $i = 0; $i < count( $data ); ++$i )
00075 if ( trim( $data[$i] ) <> '' )
00076 {
00077 ++$count;
00078 break;
00079 }
00080 if ( $contentObjectAttribute->validateIsRequired() and ( $count == 0 or $data === false ) )
00081 {
00082 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00083 'Missing matrix input.' ) );
00084 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00085 }
00086 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00087 }
00088
00089
00090
00091
00092 function storeObjectAttribute( &$contentObjectAttribute )
00093 {
00094 $matrix =& $contentObjectAttribute->content();
00095 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00096 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
00097 $contentObjectAttribute->setContent( $matrix );
00098 }
00099
00100 function storeClassAttribute( &$contentClassAttribute, $version )
00101 {
00102 $matrixDefinition =& $contentClassAttribute->content();
00103 $contentClassAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
00104 $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
00105 $contentClassAttribute->setContent( $matrixDefinition );
00106 }
00107
00108
00109
00110
00111 function &objectAttributeContent( &$contentObjectAttribute )
00112 {
00113 $matrix = new eZMatrix( '' );
00114
00115 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
00116
00117 return $matrix;
00118 }
00119
00120 function hasObjectAttributeContent( &$contentObjectAttribute )
00121 {
00122 $matrix =& $contentObjectAttribute->content();
00123 $columnsArray =& $matrix->attribute( 'columns' );
00124 $columns =& $columnsArray['sequential'];
00125 $count = 0;
00126 foreach ( $columns as $column )
00127 {
00128 $count += count( $column['rows'] );
00129 }
00130 return $count > 0;
00131 }
00132
00133
00134
00135
00136 function metaData( $contentObjectAttribute )
00137 {
00138 $matrix =& $contentObjectAttribute->content();
00139 $columnsArray =& $matrix->attribute( 'columns' );
00140 $columns =& $columnsArray['sequential'];
00141 $metaDataArray = array();
00142 foreach ( $columns as $column )
00143 {
00144 $rows = $column['rows'];
00145 foreach ( $rows as $row )
00146 {
00147 $metaDataArray[] = array( 'id' => $column['identifier'],
00148 'text' => $row );
00149 }
00150 }
00151 return $metaDataArray;
00152 }
00153
00154
00155
00156
00157 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00158 {
00159 $cellsVarName = $base . EZ_MATRIX_CELL_VARIABLE . $contentObjectAttribute->attribute( 'id' );
00160 if ( $http->hasPostVariable( $cellsVarName ) )
00161 {
00162 $cells = array();
00163 foreach ( $http->postVariable( $cellsVarName ) as $cell )
00164 {
00165 $cells[] = $cell;
00166 }
00167 $matrix =& $contentObjectAttribute->attribute( 'content' );
00168 $matrix->Cells =& $cells;
00169
00170 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00171 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
00172 $contentObjectAttribute->setContent( $matrix );
00173 }
00174 return true;
00175 }
00176
00177
00178
00179 function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
00180 {
00181 switch ( $action )
00182 {
00183 case 'new_row' :
00184 {
00185 $matrix =& $contentObjectAttribute->content( );
00186
00187 $postvarname = 'ContentObjectAttribute' . '_data_matrix_remove_' . $contentObjectAttribute->attribute( 'id' );
00188 $addCountName = 'ContentObjectAttribute' . '_data_matrix_add_count_' . $contentObjectAttribute->attribute( 'id' );
00189
00190 $addCount = 1;
00191 if ( $http->hasPostVariable( $addCountName ) )
00192 {
00193 $addCount = $http->postVariable( $addCountName );
00194 }
00195
00196 if ( $http->hasPostVariable( $postvarname ) )
00197 {
00198 $selected = $http->postVariable( $postvarname );
00199 $matrix->addRow( $selected[0], $addCount );
00200 }
00201 else
00202 {
00203 $matrix->addRow( false, $addCount );
00204 }
00205
00206 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00207 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
00208 $contentObjectAttribute->setContent( $matrix );
00209 $contentObjectAttribute->store();
00210 }break;
00211 case 'remove_selected' :
00212 {
00213 $matrix =& $contentObjectAttribute->content( );
00214 $postvarname = 'ContentObjectAttribute' . '_data_matrix_remove_' . $contentObjectAttribute->attribute( 'id' );
00215 $arrayRemove = $http->postVariable( $postvarname );
00216
00217 rsort( $arrayRemove );
00218 foreach ( $arrayRemove as $rowNum)
00219 {
00220 $matrix->removeRow( $rowNum );
00221 }
00222
00223 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00224 $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
00225 $contentObjectAttribute->setContent( $matrix );
00226 $contentObjectAttribute->store();
00227 }break;
00228 default :
00229 {
00230 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZMatrixType' );
00231 }break;
00232 }
00233 }
00234
00235
00236
00237
00238 function title( &$contentObjectAttribute, $name = 'name' )
00239 {
00240 $matrix =& $contentObjectAttribute->content( );
00241
00242 $value = $matrix->attribute( $name );
00243
00244 return $value;
00245 }
00246
00247
00248
00249
00250 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00251 {
00252
00253 if ( $currentVersion != false )
00254 {
00255 $matrix =& $originalContentObjectAttribute->content();
00256 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00257
00258 $matrix->adjustColumnsToDefinition( $contentClassAttribute->attribute( 'content' ) );
00259
00260 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00261 $contentObjectAttribute->setContent( $matrix );
00262 }
00263 else
00264 {
00265 $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
00266 $numRows = $contentClassAttribute->attribute( 'data_int1' );
00267 $matrix = new eZMatrix( '', $numRows, $contentClassAttribute->attribute( 'content' ) );
00268
00269
00270 $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00271 $contentObjectAttribute->setContent( $matrix );
00272 }
00273
00274 }
00275
00276
00277
00278
00279 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00280 {
00281
00282
00283 $defaultValueName = '';
00284 $defaultNumColumnsName = $base . EZ_MATRIX_NUMCOLUMNS_VARIABLE . $classAttribute->attribute( 'id' );
00285 $defaultNumRowsName = $base . EZ_MATRIX_NUMROWS_VARIABLE . $classAttribute->attribute( 'id' );
00286 $dataFetched = false;
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 if ( $http->hasPostVariable( $defaultNumRowsName ) )
00303 {
00304 $defaultNumRowsValue = $http->postVariable( $defaultNumRowsName );
00305
00306 if ( $defaultNumRowsValue == '' )
00307 {
00308 $defaultNumRowsValue = '1';
00309 }
00310 $classAttribute->setAttribute( 'data_int1', $defaultNumRowsValue );
00311 $dataFetched = true;
00312 }
00313
00314 $columnNameVariable = $base . '_data_ezmatrix_column_name_' . $classAttribute->attribute( 'id' );
00315 $columnIDVariable = $base . '_data_ezmatrix_column_id_' . $classAttribute->attribute( 'id' );
00316
00317
00318 if ( $http->hasPostVariable( $columnNameVariable ) && $http->hasPostVariable( $columnIDVariable ) )
00319 {
00320 $columns = array();
00321 $i = 0;
00322 $columnNameList = $http->postVariable( $columnNameVariable );
00323 $columnIDList = $http->postVariable( $columnIDVariable );
00324
00325 $matrixDefinition =& $classAttribute->attribute( 'content' );
00326 $columnNames =& $matrixDefinition->attribute( 'columns' );
00327 foreach ( $columnNames as $columnName )
00328 {
00329 $columnID = '';
00330 $name = '';
00331 $index = $columnName['index'];
00332
00333
00334
00335 if ( isset( $columnIDList[$index] ) && isset( $columnNameList[$index] ) )
00336 {
00337 $columnID = $columnIDList[$index];
00338 $name = $columnNameList[$index];
00339 if ( strlen( $columnID ) == 0 )
00340 {
00341 $columnID = $name;
00342
00343 include_once( 'lib/ezi18n/classes/ezchartransform.php' );
00344 $trans =& eZCharTransform::instance();
00345 $columnID = $trans->transformByGroup( $columnID, 'identifier' );
00346 }
00347 }
00348
00349 $columns[] = array( 'name' => $name,
00350 'identifier' => $columnID,
00351 'index' => $i );
00352
00353 $i++;
00354 }
00355
00356 $matrixDefinition->ColumnNames =& $columns;
00357 $classAttribute->setContent( $matrixDefinition );
00358 $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
00359
00360 $dataFetched = true;
00361 }
00362 if ( $dataFetched )
00363 {
00364 return true;
00365 }
00366 return false;
00367
00368 }
00369
00370
00371
00372
00373
00374 function &classAttributeContent( &$contentClassAttribute )
00375 {
00376 $matrixDefinition = new eZMatrixDefinition();
00377 $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
00378 return $matrixDefinition;
00379 }
00380
00381
00382
00383 function customClassAttributeHTTPAction( &$http, $action, &$contentClassAttribute )
00384 {
00385 $id = $contentClassAttribute->attribute( 'id' );
00386 switch ( $action )
00387 {
00388 case 'new_ezmatrix_column' :
00389 {
00390 $matrixDefinition =& $contentClassAttribute->content( );
00391 $matrixDefinition->addColumn( '' );
00392 $contentClassAttribute->setContent( $matrixDefinition );
00393 $contentClassAttribute->store();
00394 }break;
00395 case 'remove_selected' :
00396 {
00397 $matrixDefinition =& $contentClassAttribute->content( );
00398
00399 $postvarname = 'ContentClass' . '_data_ezmatrix_column_remove_' . $contentClassAttribute->attribute( 'id' );
00400 $array_remove = $http->postVariable( $postvarname );
00401 foreach( $array_remove as $columnIndex )
00402 {
00403 $matrixDefinition->removeColumn( $columnIndex );
00404 }
00405 $contentClassAttribute->setContent( $matrixDefinition );
00406 }break;
00407 default :
00408 {
00409 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' );
00410 }break;
00411 }
00412 }
00413
00414
00415
00416
00417 function isIndexable()
00418 {
00419 return true;
00420 }
00421
00422
00423
00424
00425
00426 function toString( $contentObjectAttribute )
00427 {
00428 $matrix = $contentObjectAttribute->attribute( 'content' );
00429 $matrixArray = array();
00430 $rows = $matrix->attribute( 'rows' );
00431
00432 foreach( $rows['sequential'] as $row )
00433 {
00434 $matrixArray[] = eZStringUtils::implodeStr( $row['columns'], '|' );
00435 }
00436
00437 return eZStringUtils::implodeStr( $matrixArray, '&' );
00438
00439 }
00440
00441 function fromString( &$contentObjectAttribute, $string )
00442 {
00443 if ( $string != '' )
00444 {
00445 $matrix =& $contentObjectAttribute->attribute( 'content' );
00446 $matrixRowsList = eZStringUtils::explodeStr( $string, "&" );
00447 $cells = array();
00448 $matrix->Matrix['rows']['sequential'] = array();
00449 $matrix->NumRows = 0;
00450
00451 foreach( $matrixRowsList as $key => $value )
00452 {
00453 $newCells = eZStringUtils::explodeStr( $value, '|' );
00454 $matrixArray[] = $newCells;
00455 $cells = array_merge( $cells, $newCells );
00456
00457 $newRow['columns'] = $newCells;
00458 $newRow['identifier'] = 'row_' . ( $numRows + 1 );
00459 $newRow['name'] = 'Row_' . ( $numRows + 1 );
00460 $matrix->NumRows++;
00461
00462
00463 $matrix->Matrix['rows']['sequential'][] = $newRow;
00464 }
00465 $matrix->Cells = $cells;
00466 }
00467 return true;
00468 }
00469
00470
00471
00472
00473 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00474 {
00475 $content =& $classAttribute->content();
00476 if ( $content )
00477 {
00478 $defaultName = $classAttribute->attribute( 'data_text1' );
00479 $defaultRowCount = $classAttribute->attribute( 'data_int1' );
00480 $columns = $content->attribute( 'columns' );
00481 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-name', $defaultName ) );
00482 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-row-count', $defaultRowCount ) );
00483 $columnsNode = eZDOMDocument::createElementNode( 'columns' );
00484 $attributeParametersNode->appendChild( $columnsNode );
00485 foreach ( $columns as $column )
00486 {
00487 $columnsNode->appendChild( eZDOMDocument::createElementNode( 'column',
00488 array( 'name' => $column['name'],
00489 'identifier' => $column['identifier'],
00490 'index' => $column['index'] ) ) );
00491 }
00492 }
00493 }
00494
00495
00496
00497
00498 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00499 {
00500 $defaultName = $attributeParametersNode->elementTextContentByName( 'default-name' );
00501 $defaultRowCount = $attributeParametersNode->elementTextContentByName( 'default-row-count' );
00502 $classAttribute->setAttribute( 'data_text1', $defaultName );
00503 $classAttribute->setAttribute( 'data_int1', $defaultRowCount );
00504
00505 $matrixDefinition = new eZMatrixDefinition();
00506 $columnsNode =& $attributeParametersNode->elementByName( 'columns' );
00507 $columnsList = $columnsNode->children();
00508 foreach ( $columnsList as $columnNode )
00509 {
00510 $columnName = $columnNode->attributeValue( 'name' );
00511 $columnIdentifier = $columnNode->attributeValue( 'identifier' );
00512 $matrixDefinition->addColumn( $columnName, $columnIdentifier );
00513 }
00514 $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
00515 }
00516
00517
00518
00519
00520 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00521 {
00522 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00523
00524 $xml = new eZXML();
00525 $domDocument = $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
00526 $node->appendChild( $domDocument->root() );
00527
00528 return $node;
00529 }
00530
00531
00532
00533
00534 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00535 {
00536 $rootNode = $attributeNode->firstChild();
00537 $xmlString = $rootNode->attributeValue( 'local_name' ) == 'data-text' ? '' : $rootNode->toString( 0 );
00538 $objectAttribute->setAttribute( 'data_text', $xmlString );
00539 }
00540 }
00541
00542 eZDataType::register( EZ_DATATYPESTRING_MATRIX, 'ezmatrixtype' );
00543
00544 ?>