eZ Publish  [4.0]
ezsimplifiedxmlinput.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSimplifiedXMLInput class
00004 //
00005 // Created on: <28-Jan-2003 13:28:39 bf>
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 //include_once( 'kernel/classes/datatypes/ezxmltext/ezxmlinputhandler.php' );
00032 //include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00033 //include_once( 'lib/ezutils/classes/ezhttptool.php' );
00034 //include_once( 'lib/ezutils/classes/ezini.php' );
00035 
00036 class eZSimplifiedXMLInput extends eZXMLInputHandler
00037 {
00038     function eZSimplifiedXMLInput( &$xmlData, $aliasedType, $contentObjectAttribute )
00039     {
00040         $this->eZXMLInputHandler( $xmlData, $aliasedType, $contentObjectAttribute );
00041 
00042         $this->IsInputValid = true;
00043         $this->ContentObjectAttribute = $contentObjectAttribute;
00044 
00045         $contentIni = eZINI::instance( 'content.ini' );
00046     }
00047 
00048     /*!
00049       Updates URL - object links.
00050     */
00051     static function updateUrlObjectLinks( $contentObjectAttribute, $urlIDArray )
00052     {
00053         $objectAttributeID = $contentObjectAttribute->attribute( 'id' );
00054         $objectAttributeVersion = $contentObjectAttribute->attribute('version');
00055 
00056         foreach( $urlIDArray as $urlID )
00057         {
00058             $linkObjectLink = eZURLObjectLink::fetch( $urlID, $objectAttributeID, $objectAttributeVersion );
00059             if ( $linkObjectLink == null )
00060             {
00061                 $linkObjectLink = eZURLObjectLink::create( $urlID, $objectAttributeID, $objectAttributeVersion );
00062                 $linkObjectLink->store();
00063             }
00064         }
00065     }
00066 
00067     /*!
00068      \reimp
00069      Validates the input and returns true if the input was valid for this datatype.
00070     */
00071     function validateInput( $http, $base, $contentObjectAttribute )
00072     {
00073         $contentObjectID = $contentObjectAttribute->attribute( 'contentobject_id' );
00074         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00075         $contentObjectAttributeVersion = $contentObjectAttribute->attribute('version');
00076         if ( $http->hasPostVariable( $base . '_data_text_' . $contentObjectAttributeID ) )
00077         {
00078             $data = $http->postVariable( $base . '_data_text_' . $contentObjectAttributeID );
00079 
00080             // Set original input to a global variable
00081             $originalInput = 'originalInput_' . $contentObjectAttributeID;
00082             $GLOBALS[$originalInput] = $data;
00083 
00084             // Set input valid true to a global variable
00085             $isInputValid = 'isInputValid_' . $contentObjectAttributeID;
00086             $GLOBALS[$isInputValid] = true;
00087 
00088             //include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmlinputparser.php' );
00089 
00090             $text = $data;
00091 
00092             $text = preg_replace('/\r/', '', $text);
00093             $text = preg_replace('/\t/', ' ', $text);
00094 
00095             // first empty paragraph
00096             $text = preg_replace('/^\n/', '<p></p>', $text );
00097 
00098             eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', $text, 'eZSimplifiedXMLInput::validateInput text' );
00099 
00100             $parser = new eZSimplifiedXMLInputParser( $contentObjectID, true, eZXMLInputParser::ERROR_ALL, true );
00101             $document = $parser->process( $text );
00102 
00103             if ( !is_object( $document ) )
00104             {
00105                 $GLOBALS[$isInputValid] = false;
00106                 $errorMessage = implode( ' ', $parser->getMessages() );
00107                 $contentObjectAttribute->setValidationError( $errorMessage );
00108                 return eZInputValidator::STATE_INVALID;
00109             }
00110 
00111             if ( $contentObjectAttribute->validateIsRequired() )
00112             {
00113                 $root = $document->documentElement;
00114                 if ( !$root->hasChildNodes() )
00115                 {
00116                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00117                                                                          'Content required' ) );
00118                     return eZInputValidator::STATE_INVALID;
00119                 }
00120             }
00121             $contentObjectAttribute->setValidationLog( $parser->getMessages() );
00122 
00123             $xmlString = eZXMLTextType::domString( $document );
00124 
00125             $urlIDArray = $parser->getUrlIDArray();
00126 
00127             if ( count( $urlIDArray ) > 0 )
00128             {
00129                 $this->updateUrlObjectLinks( $contentObjectAttribute, $urlIDArray );
00130             }
00131 
00132             $contentObject = $contentObjectAttribute->attribute( 'object' );
00133             $contentObject->appendInputRelationList( $parser->getRelatedObjectIDArray(), eZContentObject::RELATION_EMBED );
00134             $contentObject->appendInputRelationList( $parser->getLinkedObjectIDArray(), eZContentObject::RELATION_LINK );
00135 
00136             $contentObjectAttribute->setAttribute( 'data_text', $xmlString );
00137             return eZInputValidator::STATE_ACCEPTED;
00138         }
00139         return eZInputValidator::STATE_ACCEPTED;
00140     }
00141 
00142     /*!
00143      \reimp
00144      Returns the input XML representation of the datatype.
00145     */
00146     function inputXML()
00147     {
00148         $contentObjectAttribute = $this->ContentObjectAttribute;
00149         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00150 
00151         $originalInput = 'originalInput_' . $contentObjectAttributeID;
00152         $isInputValid = 'isInputValid_' . $contentObjectAttributeID;
00153 
00154         if ( isset( $GLOBALS[$isInputValid] ) and $GLOBALS[$isInputValid] == false )
00155         {
00156             $output = $GLOBALS[$originalInput];
00157         }
00158         else
00159         {
00160             $dom = new DOMDocument( '1.0', 'utf-8' );
00161             $success = $dom->loadXML( $this->XMLData );
00162 
00163             //include_once( 'kernel/classes/datatypes/ezxmltext/handlers/input/ezsimplifiedxmleditoutput.php' );
00164 
00165             $editOutput = new eZSimplifiedXMLEditOutput();
00166             $dom->formatOutput = true;
00167             eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', $dom->saveXML(), 'eZSimplifiedXMLInput::inputXML xml string stored in database' );
00168             $output = $editOutput->performOutput( $dom );
00169         }
00170         return $output;
00171     }
00172 
00173     public $IsInputValid;
00174 }
00175 
00176 ?>