eZ Publish  [trunk]
ezsimplifiedxmlinput.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZSimplifiedXMLInput class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 class eZSimplifiedXMLInput extends eZXMLInputHandler
00012 {
00013     function eZSimplifiedXMLInput( &$xmlData, $aliasedType, $contentObjectAttribute )
00014     {
00015         $this->eZXMLInputHandler( $xmlData, $aliasedType, $contentObjectAttribute );
00016 
00017         $this->IsInputValid = true;
00018         $this->ContentObjectAttribute = $contentObjectAttribute;
00019 
00020         $contentIni = eZINI::instance( 'content.ini' );
00021     }
00022 
00023     /*!
00024       Updates URL - object links.
00025     */
00026     static function updateUrlObjectLinks( $contentObjectAttribute, $urlIDArray )
00027     {
00028         $objectAttributeID = $contentObjectAttribute->attribute( 'id' );
00029         $objectAttributeVersion = $contentObjectAttribute->attribute('version');
00030 
00031         foreach( $urlIDArray as $urlID )
00032         {
00033             $linkObjectLink = eZURLObjectLink::fetch( $urlID, $objectAttributeID, $objectAttributeVersion );
00034             if ( $linkObjectLink == null )
00035             {
00036                 $linkObjectLink = eZURLObjectLink::create( $urlID, $objectAttributeID, $objectAttributeVersion );
00037                 $linkObjectLink->store();
00038             }
00039         }
00040     }
00041 
00042     /*!
00043      Validates the input and returns true if the input was valid for this datatype.
00044     */
00045     function validateInput( $http, $base, $contentObjectAttribute )
00046     {
00047         $contentObjectID = $contentObjectAttribute->attribute( 'contentobject_id' );
00048         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00049         $contentObjectAttributeVersion = $contentObjectAttribute->attribute('version');
00050         if ( $http->hasPostVariable( $base . '_data_text_' . $contentObjectAttributeID ) )
00051         {
00052             $data = $http->postVariable( $base . '_data_text_' . $contentObjectAttributeID );
00053 
00054             // Set original input to a global variable
00055             $originalInput = 'originalInput_' . $contentObjectAttributeID;
00056             $GLOBALS[$originalInput] = $data;
00057 
00058             // Set input valid true to a global variable
00059             $isInputValid = 'isInputValid_' . $contentObjectAttributeID;
00060             $GLOBALS[$isInputValid] = true;
00061 
00062             $text = $data;
00063 
00064             $text = preg_replace('/\r/', '', $text);
00065             $text = preg_replace('/\t/', ' ', $text);
00066 
00067             // first empty paragraph
00068             $text = preg_replace('/^\n/', '<p></p>', $text );
00069 
00070             eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext', $text, 'eZSimplifiedXMLInput::validateInput text' );
00071 
00072             $parser = new eZSimplifiedXMLInputParser( $contentObjectID, true, eZXMLInputParser::ERROR_ALL, true );
00073             $document = $parser->process( $text );
00074 
00075             if ( !is_object( $document ) )
00076             {
00077                 $GLOBALS[$isInputValid] = false;
00078                 $errorMessage = implode( ' ', $parser->getMessages() );
00079                 $contentObjectAttribute->setValidationError( $errorMessage );
00080                 return eZInputValidator::STATE_INVALID;
00081             }
00082 
00083             if ( $contentObjectAttribute->validateIsRequired() )
00084             {
00085                 $root = $document->documentElement;
00086                 if ( !$root->hasChildNodes() )
00087                 {
00088                     $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
00089                                                                          'Content required' ) );
00090                     return eZInputValidator::STATE_INVALID;
00091                 }
00092             }
00093             $contentObjectAttribute->setValidationLog( $parser->getMessages() );
00094 
00095             $xmlString = eZXMLTextType::domString( $document );
00096 
00097             $urlIDArray = $parser->getUrlIDArray();
00098 
00099             if ( count( $urlIDArray ) > 0 )
00100             {
00101                 $this->updateUrlObjectLinks( $contentObjectAttribute, $urlIDArray );
00102             }
00103 
00104             $contentObject = $contentObjectAttribute->attribute( 'object' );
00105             $contentObject->appendInputRelationList( $parser->getRelatedObjectIDArray(), eZContentObject::RELATION_EMBED );
00106             $contentObject->appendInputRelationList( $parser->getLinkedObjectIDArray(), eZContentObject::RELATION_LINK );
00107 
00108             $contentObjectAttribute->setAttribute( 'data_text', $xmlString );
00109             return eZInputValidator::STATE_ACCEPTED;
00110         }
00111         return eZInputValidator::STATE_ACCEPTED;
00112     }
00113 
00114     /*!
00115      Returns the input XML representation of the datatype.
00116     */
00117     function inputXML()
00118     {
00119         $contentObjectAttribute = $this->ContentObjectAttribute;
00120         $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
00121 
00122         $originalInput = 'originalInput_' . $contentObjectAttributeID;
00123         $isInputValid = 'isInputValid_' . $contentObjectAttributeID;
00124 
00125         if ( isset( $GLOBALS[$isInputValid] ) and $GLOBALS[$isInputValid] == false )
00126         {
00127             $output = $GLOBALS[$originalInput];
00128         }
00129         else
00130         {
00131             $dom = new DOMDocument( '1.0', 'utf-8' );
00132             $success = $dom->loadXML( $this->XMLData );
00133 
00134             $editOutput = new eZSimplifiedXMLEditOutput();
00135             $dom->formatOutput = true;
00136             if ( eZDebugSetting::isConditionTrue( 'kernel-datatype-ezxmltext', eZDebug::LEVEL_DEBUG ) )
00137                 eZDebug::writeDebug( $dom->saveXML(), eZDebugSetting::changeLabel( 'kernel-datatype-ezxmltext', __METHOD__ . ' xml string stored in database' ) );
00138             $output = $editOutput->performOutput( $dom );
00139         }
00140         return $output;
00141     }
00142 
00143     public $IsInputValid;
00144 }
00145 
00146 ?>