eZ Publish  [4.0]
ezmatrixtype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZMatrixType class
00004 //
00005 // Created on: <30-May-2003 14:18:35 sp>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezmatrixtype.php
00032 */
00033 
00034 /*!
00035   \class eZMatrixType ezmatrixtype.php
00036   \ingroup eZDatatype
00037   \brief The class eZMatrixType does
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 class eZMatrixType extends eZDataType
00047 {
00048     const DEFAULT_NAME_VARIABLE = '_ezmatrix_default_name_';
00049 
00050     const NUM_COLUMNS_VARIABLE = '_ezmatrix_default_num_columns_';
00051     const NUM_ROWS_VARIABLE = '_ezmatrix_default_num_rows_';
00052     const CELL_VARIABLE = '_ezmatrix_cell_';
00053     const DATA_TYPE_STRING = 'ezmatrix';
00054 
00055     /*!
00056      Constructor
00057     */
00058     function eZMatrixType()
00059     {
00060         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', 'Matrix', 'Datatype name' ),
00061                            array( 'serialize_supported' => true ) );
00062     }
00063 
00064     /*!
00065      Validates the input and returns true if the input was
00066      valid for this datatype.
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 eZInputValidator::STATE_INVALID;
00085         }
00086         return eZInputValidator::STATE_ACCEPTED;
00087     }
00088 
00089     /*!
00090      Store content
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      Returns the content.
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      Returns the meta data used for storing search indeces.
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      Fetches the http post var matrix cells input and stores it in the data instance.
00156     */
00157     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00158     {
00159         $cellsVarName = $base . self::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, $parameters )
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      Returns the integer value.
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      Sets the default value.
00249     */
00250     function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
00251     {
00252 
00253         if ( $currentVersion != false )
00254         {
00255             $matrix = $originalContentObjectAttribute->content();
00256             $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
00257             // make sure that $matrix contains right columns
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             // 'default name' is never used => just a stub
00269             // $matrix->setName( $contentClassAttribute->attribute( 'data_text1' ) );
00270             $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
00271             $contentObjectAttribute->setContent( $matrix );
00272         }
00273 
00274     }
00275 
00276     /*!
00277      \reimp
00278     */
00279     function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
00280     {
00281         // 'default name' is never used => just a stub
00282         // $defaultValueName = $base . self::DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
00283         $defaultValueName = '';
00284         $defaultNumColumnsName = $base . self::NUM_COLUMNS_VARIABLE . $classAttribute->attribute( 'id' );
00285         $defaultNumRowsName = $base . self::NUM_ROWS_VARIABLE . $classAttribute->attribute( 'id' );
00286         $dataFetched = false;
00287         // 'default name' is never used => just a stub
00288         /*
00289         if ( $http->hasPostVariable( $defaultValueName ) )
00290         {
00291             $defaultValueValue = $http->postVariable( $defaultValueName );
00292 
00293             if ( $defaultValueValue == '' )
00294             {
00295                 $defaultValueValue = '';
00296             }
00297             $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
00298             $dataFetched = true;
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                 // after adding a new column $columnIDList and $columnNameList doesn't contain values for new column.
00334                 // if so just add column with empty 'name' and 'columnID'.
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                         // Initialize transformation system
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     function preStoreClassAttribute( $classAttribute, $version )
00371     {
00372         $matrixDefinition = $classAttribute->attribute( 'content' );
00373         $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
00374     }
00375 
00376     /*!
00377      Returns the content.
00378     */
00379     function classAttributeContent( $contentClassAttribute )
00380     {
00381         $matrixDefinition = new eZMatrixDefinition();
00382         $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
00383         return $matrixDefinition;
00384     }
00385 
00386     /*!
00387     */
00388     function customClassAttributeHTTPAction( $http, $action, $contentClassAttribute )
00389     {
00390         $id = $contentClassAttribute->attribute( 'id' );
00391         switch ( $action )
00392         {
00393             case 'new_ezmatrix_column' :
00394             {
00395                 $matrixDefinition = $contentClassAttribute->content( );
00396                 $matrixDefinition->addColumn( '' );
00397                 $contentClassAttribute->setContent( $matrixDefinition );
00398                 $contentClassAttribute->store();
00399             }break;
00400             case 'remove_selected' :
00401             {
00402                 $matrixDefinition = $contentClassAttribute->content( );
00403 
00404                 $postvarname = 'ContentClass' . '_data_ezmatrix_column_remove_' . $contentClassAttribute->attribute( 'id' );
00405                 $array_remove = $http->postVariable( $postvarname );
00406                 foreach( $array_remove as $columnIndex )
00407                 {
00408                     $matrixDefinition->removeColumn( $columnIndex );
00409                 }
00410                 $contentClassAttribute->setContent( $matrixDefinition );
00411             }break;
00412             default :
00413             {
00414                 eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' );
00415             }break;
00416         }
00417     }
00418 
00419     /*!
00420      \reimp
00421     */
00422     function isIndexable()
00423     {
00424         return true;
00425     }
00426 
00427     /*!
00428      \return string representation of an contentobjectattribute data for simplified export
00429 
00430     */
00431     function toString( $contentObjectAttribute )
00432     {
00433         $matrix = $contentObjectAttribute->attribute( 'content' );
00434         $matrixArray = array();
00435         $rows = $matrix->attribute( 'rows' );
00436 
00437         foreach( $rows['sequential'] as $row )
00438         {
00439             $matrixArray[] = eZStringUtils::implodeStr( $row['columns'], '|' );
00440         }
00441 
00442         return eZStringUtils::implodeStr( $matrixArray, '&' );
00443 
00444     }
00445 
00446     function fromString( $contentObjectAttribute, $string )
00447     {
00448         if ( $string != '' )
00449         {
00450             $matrix = $contentObjectAttribute->attribute( 'content' );
00451             $matrixRowsList = eZStringUtils::explodeStr( $string, "&" );
00452             $cells = array();
00453             $matrix->Matrix['rows']['sequential'] = array();
00454             $matrix->NumRows = 0;
00455 
00456             foreach( $matrixRowsList as $key => $value )
00457             {
00458                 $newCells = eZStringUtils::explodeStr( $value, '|' );
00459                 $matrixArray[] = $newCells;
00460                 $cells = array_merge( $cells, $newCells );
00461 
00462                 $newRow['columns'] = $newCells;
00463                 $newRow['identifier'] =  'row_' . ( $numRows + 1 );
00464                 $newRow['name'] = 'Row_' . ( $numRows + 1 );
00465                 $matrix->NumRows++;
00466 
00467 
00468                 $matrix->Matrix['rows']['sequential'][] = $newRow;
00469             }
00470             $matrix->Cells = $cells;
00471         }
00472         return true;
00473     }
00474 
00475     /*!
00476      \reimp
00477     */
00478     function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00479     {
00480         $content = $classAttribute->content();
00481         if ( $content )
00482         {
00483             $defaultName = $classAttribute->attribute( 'data_text1' );
00484             $defaultRowCount = $classAttribute->attribute( 'data_int1' );
00485             $columns = $content->attribute( 'columns' );
00486 
00487             $dom = $attributeParametersNode->ownerDocument;
00488             $defaultNameNode = $dom->createElement( 'default-name' );
00489             $defaultNameNode->appendChild( $dom->createTextNode( $defaultName ) );
00490             $attributeParametersNode->appendChild( $defaultNameNode );
00491             $defaultRowCountNode = $dom->createElement( 'default-row-count' );
00492             $defaultRowCountNode->appendChild( $dom->createTextNode( $defaultRowCount ) );
00493             $attributeParametersNode->appendChild( $defaultRowCountNode );
00494             $columnsNode = $dom->createElement( 'columns' );
00495             $attributeParametersNode->appendChild( $columnsNode );
00496             foreach ( $columns as $column )
00497             {
00498                 unset( $columnNode );
00499                 $columnNode = $dom->createElement( 'column' );
00500                 $columnNode->setAttribute( 'name', $column['name'] );
00501                 $columnNode->setAttribute( 'identifier', $column['identifier'] );
00502                 $columnNode->setAttribute( 'index', $column['index'] );
00503                 $columnsNode->appendChild( $columnNode );
00504             }
00505         }
00506     }
00507 
00508     /*!
00509      \reimp
00510     */
00511     function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
00512     {
00513         $defaultName = $attributeParametersNode->getElementsByTagName( 'default-name' )->item( 0 )->textContent;
00514         $defaultRowCount = $attributeParametersNode->getElementsByTagName( 'default-row-count' )->item( 0 )->textContent;
00515         $classAttribute->setAttribute( 'data_text1', $defaultName );
00516         $classAttribute->setAttribute( 'data_int1', $defaultRowCount );
00517 
00518         $matrixDefinition = new eZMatrixDefinition();
00519         $columnsNode = $attributeParametersNode->getElementsByTagName( 'columns' )->item( 0 );
00520         $columnsList = $columnsNode->getElementsByTagName( 'column' );
00521         foreach ( $columnsList  as $columnNode )
00522         {
00523             $columnName = $columnNode->getAttribute( 'name' );
00524             $columnIdentifier = $columnNode->getAttribute( 'identifier' );
00525             $matrixDefinition->addColumn( $columnName, $columnIdentifier );
00526         }
00527         $classAttribute->setContent( $matrixDefinition );
00528     }
00529 
00530     /*!
00531      \reimp
00532     */
00533     function serializeContentObjectAttribute( $package, $objectAttribute )
00534     {
00535         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00536 
00537         $dom = new DOMDocument( '1.0', 'utf-8' );
00538         $success = $dom->loadXML( $objectAttribute->attribute( 'data_text' ) );
00539 
00540         $importedRoot = $node->ownerDocument->importNode( $dom->documentElement, true );
00541         $node->appendChild( $importedRoot );
00542 
00543         return $node;
00544     }
00545 
00546     /*!
00547      \reimp
00548     */
00549     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00550     {
00551         $rootNode = $attributeNode->getElementsByTagName( 'ezmatrix' )->item( 0 );
00552         $xmlString = $rootNode ? $rootNode->ownerDocument->saveXML( $rootNode ) : '';
00553         $objectAttribute->setAttribute( 'data_text', $xmlString );
00554     }
00555 }
00556 
00557 eZDataType::register( eZMatrixType::DATA_TYPE_STRING, 'ezmatrixtype' );
00558 
00559 ?>