[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezmatrix/ -> ezmatrixtype.php (source)

   1  <?php
   2  //
   3  // Definition of eZMatrixType class
   4  //
   5  // Created on: <30-May-2003 14:18:35 sp>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezmatrixtype.php
  30  */
  31  
  32  /*!
  33    \class eZMatrixType ezmatrixtype.php
  34    \ingroup eZDatatype
  35    \brief The class eZMatrixType does
  36  
  37  */
  38  
  39  include_once ( 'kernel/classes/ezdatatype.php' );
  40  include_once ( 'kernel/classes/datatypes/ezmatrix/ezmatrix.php' );
  41  include_once ( 'kernel/classes/datatypes/ezmatrix/ezmatrixdefinition.php' );
  42  include_once ( 'lib/ezutils/classes/ezstringutils.php' );
  43  
  44  define( 'EZ_MATRIX_DEFAULT_NAME_VARIABLE', '_ezmatrix_default_name_' );
  45  
  46  define( 'EZ_MATRIX_NUMCOLUMNS_VARIABLE', '_ezmatrix_default_num_columns_' );
  47  define( 'EZ_MATRIX_NUMROWS_VARIABLE', '_ezmatrix_default_num_rows_' );
  48  define( 'EZ_MATRIX_CELL_VARIABLE', '_ezmatrix_cell_' );
  49  define( 'EZ_DATATYPESTRING_MATRIX', 'ezmatrix' );
  50  
  51  class eZMatrixType extends eZDataType
  52  {
  53      /*!
  54       Constructor
  55      */
  56      function eZMatrixType()
  57      {
  58          $this->eZDataType( EZ_DATATYPESTRING_MATRIX, ezi18n( 'kernel/classes/datatypes', 'Matrix', 'Datatype name' ),
  59                             array( 'serialize_supported' => true ) );
  60      }
  61  
  62      /*!
  63       Validates the input and returns true if the input was
  64       valid for this datatype.
  65      */
  66      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  67      {
  68          $data = false;
  69          if ( $http->hasPostVariable( $base . '_ezmatrix_cell_' . $contentObjectAttribute->attribute( 'id' ) ) )
  70              $data = $http->PostVariable( $base . '_ezmatrix_cell_' . $contentObjectAttribute->attribute( 'id' ) );
  71          $count = 0;
  72          for ( $i = 0; $i < count( $data ); ++$i )
  73               if ( trim( $data[$i] ) <> '' )
  74               {
  75                   ++$count;
  76                   break;
  77               }
  78          if ( $contentObjectAttribute->validateIsRequired() and ( $count == 0 or $data === false ) )
  79          {
  80              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  81                                                                   'Missing matrix input.' ) );
  82              return EZ_INPUT_VALIDATOR_STATE_INVALID;
  83          }
  84          return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
  85      }
  86  
  87      /*!
  88       Store content
  89      */
  90      function storeObjectAttribute( &$contentObjectAttribute )
  91      {
  92          $matrix =& $contentObjectAttribute->content();
  93          $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
  94          $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
  95          $contentObjectAttribute->setContent( $matrix );
  96      }
  97  
  98      function storeClassAttribute( &$contentClassAttribute, $version )
  99      {
 100          $matrixDefinition =& $contentClassAttribute->content();
 101          $contentClassAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
 102          $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
 103          $contentClassAttribute->setContent(  $matrixDefinition );
 104      }
 105  
 106      /*!
 107       Returns the content.
 108      */
 109      function &objectAttributeContent( &$contentObjectAttribute )
 110      {
 111          $matrix = new eZMatrix( '' );
 112  
 113          $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
 114  
 115          return $matrix;
 116      }
 117  
 118      function hasObjectAttributeContent( &$contentObjectAttribute )
 119      {
 120          $matrix =& $contentObjectAttribute->content();
 121          $columnsArray =& $matrix->attribute( 'columns' );
 122          $columns =& $columnsArray['sequential'];
 123          $count = 0;
 124          foreach ( $columns as $column )
 125          {
 126              $count += count( $column['rows'] );
 127          }
 128          return $count > 0;
 129      }
 130  
 131      /*!
 132       Returns the meta data used for storing search indeces.
 133      */
 134      function metaData( $contentObjectAttribute )
 135      {
 136          $matrix =& $contentObjectAttribute->content();
 137          $columnsArray =& $matrix->attribute( 'columns' );
 138          $columns =& $columnsArray['sequential'];
 139          $metaDataArray = array();
 140          foreach ( $columns as $column )
 141          {
 142              $rows = $column['rows'];
 143              foreach ( $rows as $row )
 144              {
 145                  $metaDataArray[] = array( 'id' => $column['identifier'],
 146                                            'text' => $row );
 147              }
 148          }
 149          return $metaDataArray;
 150      }
 151  
 152      /*!
 153       Fetches the http post var matrix cells input and stores it in the data instance.
 154      */
 155      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 156      {
 157          $cellsVarName = $base . EZ_MATRIX_CELL_VARIABLE . $contentObjectAttribute->attribute( 'id' );
 158          if ( $http->hasPostVariable( $cellsVarName ) )
 159          {
 160              $cells = array();
 161              foreach ( $http->postVariable( $cellsVarName ) as $cell )
 162              {
 163                  $cells[] = $cell;
 164              }
 165              $matrix =& $contentObjectAttribute->attribute( 'content' );
 166              $matrix->Cells =& $cells;
 167  
 168              $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
 169              $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
 170              $contentObjectAttribute->setContent( $matrix );
 171          }
 172          return true;
 173      }
 174  
 175      /*!
 176      */
 177      function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
 178      {
 179          switch ( $action )
 180          {
 181              case 'new_row' :
 182              {
 183                  $matrix =& $contentObjectAttribute->content( );
 184  
 185                  $postvarname = 'ContentObjectAttribute' . '_data_matrix_remove_' . $contentObjectAttribute->attribute( 'id' );
 186                  $addCountName = 'ContentObjectAttribute' . '_data_matrix_add_count_' . $contentObjectAttribute->attribute( 'id' );
 187  
 188                  $addCount = 1;
 189                  if ( $http->hasPostVariable( $addCountName ) )
 190                  {
 191                      $addCount = $http->postVariable( $addCountName );
 192                  }
 193  
 194                  if ( $http->hasPostVariable( $postvarname ) )
 195                  {
 196                      $selected = $http->postVariable( $postvarname );
 197                      $matrix->addRow( $selected[0], $addCount );
 198                  }
 199                  else
 200                  {
 201                      $matrix->addRow( false, $addCount );
 202                  }
 203  
 204                  $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
 205                  $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
 206                  $contentObjectAttribute->setContent( $matrix );
 207                  $contentObjectAttribute->store();
 208              }break;
 209              case 'remove_selected' :
 210              {
 211                  $matrix =& $contentObjectAttribute->content( );
 212                  $postvarname = 'ContentObjectAttribute' . '_data_matrix_remove_' . $contentObjectAttribute->attribute( 'id' );
 213                  $arrayRemove = $http->postVariable( $postvarname );
 214  
 215                  rsort( $arrayRemove );
 216                  foreach ( $arrayRemove as $rowNum)
 217                  {
 218                      $matrix->removeRow( $rowNum );
 219                  }
 220  
 221                  $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
 222                  $matrix->decodeXML( $contentObjectAttribute->attribute( 'data_text' ) );
 223                  $contentObjectAttribute->setContent( $matrix );
 224                  $contentObjectAttribute->store();
 225              }break;
 226              default :
 227              {
 228                  eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZMatrixType' );
 229              }break;
 230          }
 231      }
 232  
 233      /*!
 234       Returns the integer value.
 235      */
 236      function title( &$contentObjectAttribute, $name = 'name' )
 237      {
 238          $matrix =& $contentObjectAttribute->content( );
 239  
 240          $value = $matrix->attribute( $name );
 241  
 242          return $value;
 243      }
 244  
 245      /*!
 246       Sets the default value.
 247      */
 248      function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
 249      {
 250  
 251          if ( $currentVersion != false )
 252          {
 253              $matrix =& $originalContentObjectAttribute->content();
 254              $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
 255              // make sure that $matrix contains right columns
 256              $matrix->adjustColumnsToDefinition( $contentClassAttribute->attribute( 'content' ) );
 257  
 258              $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
 259              $contentObjectAttribute->setContent( $matrix );
 260          }
 261          else
 262          {
 263              $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
 264              $numRows = $contentClassAttribute->attribute( 'data_int1' );
 265              $matrix = new eZMatrix( '', $numRows, $contentClassAttribute->attribute( 'content' ) );
 266              // 'default name' is never used => just a stub
 267              // $matrix->setName( $contentClassAttribute->attribute( 'data_text1' ) );
 268              $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
 269              $contentObjectAttribute->setContent( $matrix );
 270          }
 271  
 272      }
 273  
 274      /*!
 275       \reimp
 276      */
 277      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 278      {
 279          // 'default name' is never used => just a stub
 280          // $defaultValueName = $base . EZ_MATRIX_DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
 281          $defaultValueName = '';
 282          $defaultNumColumnsName = $base . EZ_MATRIX_NUMCOLUMNS_VARIABLE . $classAttribute->attribute( 'id' );
 283          $defaultNumRowsName = $base . EZ_MATRIX_NUMROWS_VARIABLE . $classAttribute->attribute( 'id' );
 284          $dataFetched = false;
 285          // 'default name' is never used => just a stub
 286          /*
 287          if ( $http->hasPostVariable( $defaultValueName ) )
 288          {
 289              $defaultValueValue = $http->postVariable( $defaultValueName );
 290  
 291              if ( $defaultValueValue == '' )
 292              {
 293                  $defaultValueValue = '';
 294              }
 295              $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
 296              $dataFetched = true;
 297          }
 298          */
 299  
 300          if ( $http->hasPostVariable( $defaultNumRowsName ) )
 301          {
 302              $defaultNumRowsValue = $http->postVariable( $defaultNumRowsName );
 303  
 304              if ( $defaultNumRowsValue == '' )
 305              {
 306                  $defaultNumRowsValue = '1';
 307              }
 308              $classAttribute->setAttribute( 'data_int1', $defaultNumRowsValue );
 309              $dataFetched = true;
 310          }
 311  
 312          $columnNameVariable = $base . '_data_ezmatrix_column_name_' . $classAttribute->attribute( 'id' );
 313          $columnIDVariable = $base . '_data_ezmatrix_column_id_' . $classAttribute->attribute( 'id' );
 314  
 315  
 316          if ( $http->hasPostVariable( $columnNameVariable ) && $http->hasPostVariable( $columnIDVariable ) )
 317          {
 318              $columns = array();
 319              $i = 0;
 320              $columnNameList = $http->postVariable( $columnNameVariable );
 321              $columnIDList = $http->postVariable( $columnIDVariable );
 322  
 323              $matrixDefinition =& $classAttribute->attribute( 'content' );
 324              $columnNames =& $matrixDefinition->attribute( 'columns' );
 325              foreach ( $columnNames as $columnName )
 326              {
 327                  $columnID = '';
 328                  $name = '';
 329                  $index = $columnName['index'];
 330  
 331                  // after adding a new column $columnIDList and $columnNameList doesn't contain values for new column.
 332                  // if so just add column with empty 'name' and 'columnID'.
 333                  if ( isset( $columnIDList[$index] ) && isset( $columnNameList[$index] ) )
 334                  {
 335                      $columnID = $columnIDList[$index];
 336                      $name = $columnNameList[$index];
 337                      if ( strlen( $columnID ) == 0 )
 338                      {
 339                          $columnID = $name;
 340                          // Initialize transformation system
 341                          include_once ( 'lib/ezi18n/classes/ezchartransform.php' );
 342                          $trans =& eZCharTransform::instance();
 343                          $columnID = $trans->transformByGroup( $columnID, 'identifier' );
 344                      }
 345                  }
 346  
 347                  $columns[] = array( 'name' => $name,
 348                                      'identifier' => $columnID,
 349                                      'index' => $i );
 350  
 351                  $i++;
 352              }
 353  
 354              $matrixDefinition->ColumnNames =& $columns;
 355              $classAttribute->setContent( $matrixDefinition );
 356              $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
 357  
 358              $dataFetched = true;
 359          }
 360          if ( $dataFetched )
 361          {
 362              return true;
 363          }
 364          return false;
 365  
 366      }
 367  
 368  
 369      /*!
 370       Returns the content.
 371      */
 372      function &classAttributeContent( &$contentClassAttribute )
 373      {
 374          $matrixDefinition = new eZMatrixDefinition();
 375          $matrixDefinition->decodeClassAttribute( $contentClassAttribute->attribute( 'data_text5' ) );
 376          return $matrixDefinition;
 377      }
 378  
 379      /*!
 380      */
 381      function customClassAttributeHTTPAction( &$http, $action, &$contentClassAttribute )
 382      {
 383          $id = $contentClassAttribute->attribute( 'id' );
 384          switch ( $action )
 385          {
 386              case 'new_ezmatrix_column' :
 387              {
 388                  $matrixDefinition =& $contentClassAttribute->content( );
 389                  $matrixDefinition->addColumn( '' );
 390                  $contentClassAttribute->setContent( $matrixDefinition );
 391                  $contentClassAttribute->store();
 392              }break;
 393              case 'remove_selected' :
 394              {
 395                  $matrixDefinition =& $contentClassAttribute->content( );
 396  
 397                  $postvarname = 'ContentClass' . '_data_ezmatrix_column_remove_' . $contentClassAttribute->attribute( 'id' );
 398                  $array_remove = $http->postVariable( $postvarname );
 399                  foreach( $array_remove as $columnIndex )
 400                  {
 401                      $matrixDefinition->removeColumn( $columnIndex );
 402                  }
 403                  $contentClassAttribute->setContent( $matrixDefinition );
 404              }break;
 405              default :
 406              {
 407                  eZDebug::writeError( 'Unknown custom HTTP action: ' . $action, 'eZEnumType' );
 408              }break;
 409          }
 410      }
 411  
 412      /*!
 413       \reimp
 414      */
 415      function isIndexable()
 416      {
 417          return true;
 418      }
 419  
 420      /*!
 421       \return string representation of an contentobjectattribute data for simplified export
 422  
 423      */
 424      function toString( $contentObjectAttribute )
 425      {
 426          $matrix = $contentObjectAttribute->attribute( 'content' );
 427          $matrixArray = array();
 428          $rows = $matrix->attribute( 'rows' );
 429  
 430          foreach( $rows['sequential'] as $row )
 431          {
 432              $matrixArray[] = eZStringUtils::implodeStr( $row['columns'], '|' );
 433          }
 434  
 435          return eZStringUtils::implodeStr( $matrixArray, '&' );
 436  
 437      }
 438  
 439      function fromString( &$contentObjectAttribute, $string )
 440      {
 441          if ( $string != '' )
 442          {
 443              $matrix =& $contentObjectAttribute->attribute( 'content' );
 444              $matrixRowsList = eZStringUtils::explodeStr( $string, "&" );
 445              $cells = array();
 446              $matrix->Matrix['rows']['sequential'] = array();
 447              $matrix->NumRows = 0;
 448  
 449              foreach( $matrixRowsList as $key => $value )
 450              {
 451                  $newCells = eZStringUtils::explodeStr( $value, '|' );
 452                  $matrixArray[] = $newCells;
 453                  $cells = array_merge( $cells, $newCells );
 454  
 455                  $newRow['columns'] = $newCells;
 456                  $newRow['identifier'] =  'row_' . ( $numRows + 1 );
 457                  $newRow['name'] = 'Row_' . ( $numRows + 1 );
 458                  $matrix->NumRows++;
 459  
 460  
 461                  $matrix->Matrix['rows']['sequential'][] = $newRow;
 462              }
 463              $matrix->Cells = $cells;
 464          }
 465          return true;
 466      }
 467  
 468      /*!
 469       \reimp
 470      */
 471      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 472      {
 473          $content =& $classAttribute->content();
 474          if ( $content )
 475          {
 476              $defaultName = $classAttribute->attribute( 'data_text1' );
 477              $defaultRowCount = $classAttribute->attribute( 'data_int1' );
 478              $columns = $content->attribute( 'columns' );
 479              $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-name', $defaultName ) );
 480              $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-row-count', $defaultRowCount ) );
 481              $columnsNode = eZDOMDocument::createElementNode( 'columns' );
 482              $attributeParametersNode->appendChild( $columnsNode );
 483              foreach ( $columns as $column )
 484              {
 485                  $columnsNode->appendChild( eZDOMDocument::createElementNode( 'column',
 486                                                                               array( 'name' => $column['name'],
 487                                                                                      'identifier' => $column['identifier'],
 488                                                                                      'index' => $column['index'] ) ) );
 489              }
 490          }
 491      }
 492  
 493      /*!
 494       \reimp
 495      */
 496      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 497      {
 498          $defaultName = $attributeParametersNode->elementTextContentByName( 'default-name' );
 499          $defaultRowCount = $attributeParametersNode->elementTextContentByName( 'default-row-count' );
 500          $classAttribute->setAttribute( 'data_text1', $defaultName );
 501          $classAttribute->setAttribute( 'data_int1', $defaultRowCount );
 502  
 503          $matrixDefinition = new eZMatrixDefinition();
 504          $columnsNode =& $attributeParametersNode->elementByName( 'columns' );
 505          $columnsList = $columnsNode->children();
 506          foreach ( $columnsList  as $columnNode )
 507          {
 508              $columnName = $columnNode->attributeValue( 'name' );
 509              $columnIdentifier = $columnNode->attributeValue( 'identifier' );
 510              $matrixDefinition->addColumn( $columnName, $columnIdentifier );
 511          }
 512          $classAttribute->setAttribute( 'data_text5', $matrixDefinition->xmlString() );
 513      }
 514  
 515      /*!
 516       \reimp
 517      */
 518      function serializeContentObjectAttribute( &$package, &$objectAttribute )
 519      {
 520          $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
 521  
 522          $xml = new eZXML();
 523          $domDocument = $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
 524          $node->appendChild( $domDocument->root() );
 525  
 526          return $node;
 527      }
 528  
 529      /*!
 530       \reimp
 531      */
 532      function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
 533      {
 534          $rootNode = $attributeNode->firstChild();
 535          $xmlString = $rootNode->attributeValue( 'local_name' ) == 'data-text' ? '' : $rootNode->toString( 0 );
 536          $objectAttribute->setAttribute( 'data_text', $xmlString );
 537      }
 538  }
 539  
 540  eZDataType::register( EZ_DATATYPESTRING_MATRIX, 'ezmatrixtype' );
 541  
 542  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7