eZ Publish  [trunk]
eziniaddonpackagehandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZINIAddonPackageHandler 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 /*!
00012   \class eZINIAddonPackageHandler ezcontentclasspackagehandler.php
00013   \brief Handles content classes in the package system
00014 
00015 */
00016 
00017 class eZINIAddonPackageHandler extends eZPackageHandler
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZINIAddonPackageHandler()
00023     {
00024         $this->eZPackageHandler( 'eziniaddon',
00025                                  array( 'extract-install-content' => true ) );
00026     }
00027 
00028     /*!
00029      Creates a new override setting for the specified override.
00030 
00031      \param installParameters - optional value
00032             array( 'site_access_map' => array( <package site access> => <install site access> ) )
00033     */
00034     function install( $package, $installType, $parameters,
00035                       $name, $os, $filename, $subdirectory,
00036                       $content, $installParameters,
00037                       &$installData )
00038     {
00039         $db = eZDB::instance();
00040 
00041         $siteAccess = $content->getAttribute( 'site-access' );
00042         if ( isset( $installParameters['site_access_map'] ) &&
00043              isset( $installParameters['site_access_map'][$siteAccess] ) )
00044         {
00045             $siteAccess = $installParameters['site_access_map'][$siteAccess];
00046         }
00047 
00048         $filename = $content->getAttribute( 'filename' );
00049 
00050         $ini = eZINI::instance( $filename, 'settings', null, null, true );
00051         $ini->prependOverrideDir( "siteaccess/$siteAccess", false, 'siteaccess' );
00052         $ini->loadCache();
00053 
00054         $blocks =& $content->elementByName( 'blocks' );
00055         $blockArray =& $blocks->getElementsByTagName( 'block' );
00056 
00057         foreach ( $blockArray as $block )
00058         {
00059             $blockname = $block->getAttribute( 'name' );
00060 
00061             $blockVariableArray = $block->getElementsByTagName( 'block-variable' );
00062             foreach( $blockVariableArray as $blockVariable )
00063             {
00064                 $variableName = $blockVariable->getAttribute( 'name' );
00065                 $variableValues = $blockVariable->getElementsByTagName( 'value' );
00066 
00067                 if ( count( $variableValues ) == 1 )
00068                 {
00069                     $value = eZINIAddonPackageHandler::currentID( $variableValues[0], $db );
00070                     $ini->setVariable( $blockname, $variableName, $value );
00071                 }
00072                 else
00073                 {
00074                     $valueArray = array();
00075                     foreach( $variableValues as $variableNode )
00076                     {
00077                         $valueName = $variableNode->getAttribute( 'name' );
00078                         $value = eZINIAddonPackageHandler::currentID( $variableNode, $db );
00079                         $valueArray[$valueName] = $value;
00080                     }
00081                     $ini->setVariable( $blockname, $variableName, $valueArray );
00082                 }
00083             }
00084         }
00085 
00086         $ini->save();
00087 
00088         return true;
00089     }
00090 
00091     /*!
00092      \static get current id of value node
00093 
00094      \param value DOMNode
00095      \param db connection
00096     */
00097     static function currentID( $valueNode, $db )
00098     {
00099         $remoteIDType = $valueNode->getAttribute( 'remote-id' );
00100         $value = $valueNode->textContent;
00101 
00102         if ( $remoteIDType !== false )
00103         {
00104             switch( $remoteIDType )
00105             {
00106                 case 'class':
00107                 {
00108                     $result = $db->arrayQuery( 'SELECT id FROM ezcontentclass WHERE remote_id=\'' . $db->escapeString( $value ) . '\'' );
00109                 } break;
00110 
00111                 case 'node':
00112                 {
00113                     $result = $db->arrayQuery( 'SELECT node_id FROM ezcontentobject_tree WHERE remote_id=\'' . $db->escapeString( $value ) . '\'' );
00114                 } break;
00115 
00116                 case 'object':
00117                 {
00118                     $result = $db->arrayQuery( 'SELECT id FROM ezcontentobject WHERE remote_id=\'' . $db->escapeString( $value ) . '\'' );
00119                 } break;
00120 
00121                 default:
00122                 {
00123                     eZDebug::writeError( 'Unknown remote id type ' . $remoteIDType, __METHOD__ );
00124                 } break;
00125             }
00126 
00127             if ( count( $result ) != 1 )
00128             {
00129                 eZDebug::writeError( 'Invalid result fetching id from ' . $remoteIDType . ', remote_id: ' . $value, __METHOD__ );
00130             }
00131             else
00132             {
00133                 $value = $result[0][0];
00134             }
00135         }
00136 
00137         return $value;
00138     }
00139 
00140     /*!
00141      \static
00142      Adds the content of the ini override to the package
00143 
00144      \param package
00145      \param ini filename, ex: site.ini
00146      \param iniOverrideArray structure  array( <site_access> => array( <ini_block_name> => array( <ini_block_values> ) ) )
00147      \param remoteIDArrat structure: array( <class|node|object> => array( <id> => <remote_id> ) )
00148     */
00149     function addOverrideAddon( $package, $filename, &$iniOverrideArray, $remoteIDArray )
00150     {
00151         foreach( array_keys( $iniOverrideArray ) as $siteAccess )
00152         {
00153             $iniNode = eZINIAddonPackageHandler::iniDOMTree( $filename, $siteAccess, $iniOverrideArray[$siteAccess], $remoteIDArray );
00154             if ( !$iniNode )
00155             {
00156                 continue;
00157             }
00158 
00159             $package->appendInstall( 'eziniaddon', false, false, true,
00160                                      $siteAccess . '-' . $filename, 'eziniaddon',
00161                                      array( 'content' => $iniNode ) );
00162             $package->appendInstall( 'eziniaddon', false, false, false,
00163                                      $siteAccess . '-' . $filename, 'eziniaddon',
00164                                      array( 'content' => false ) );
00165         }
00166     }
00167 
00168     /*!
00169      \static
00170 
00171      Create DOMNode from inioverride
00172 
00173      \param ini filename
00174      \param siteaccess
00175      \param ini values, struct: array( <ini_block_name> => array( <ini_block_values> ) )
00176      \param remoteID array
00177 
00178      \return DOMNode, false if fails
00179     */
00180     function iniDOMTree( $filename, $siteAccess, &$blockArray, $remoteIDArray )
00181     {
00182         if ( !$filename || !$siteAccess || !$blockArray )
00183         {
00184             return false;
00185         }
00186 
00187         $iniNode = eZDOMDocument::createElementNode( 'ini-addon', array( 'site-access' => $siteAccess,
00188                                                                           'filename' => $filename ) );
00189 
00190         $blocksNode = eZDOMDocument::createElementNode( 'blocks' );
00191         $iniNode->appendChild( $blocksNode );
00192         foreach( array_keys( $blockArray ) as $blockName )
00193         {
00194             $block =& $blockArray[$blockName];
00195             unset( $blockNode );
00196             $blockNode = eZDOMDocument::createElementNode( 'block', array( 'name' => $blockName ) );
00197             $blocksNode->appendChild( $blockNode );
00198 
00199             foreach( array_keys( $block ) as $blockVariable )
00200             {
00201                 $variableValue =& $block[$blockVariable];
00202                 unset( $variableNode );
00203                 $variableNode = eZDomDocument::createElementNode( 'block-variable', array( 'name' => $blockVariable ) );
00204                 $blockNode->appendChild( $variableNode );
00205 
00206                 if ( is_array( $variableValue ) )
00207                 {
00208                     foreach( array_keys( $variableValue) as $valueName )
00209                     {
00210                         $value = $variableValue[$valueName];
00211                         unset( $valueNode );
00212                         $valueNode = eZDomDocument::createElementNode( 'value', array( 'name' => $valueName ) );
00213                         $variableNode->appendChild( $valueNode );
00214                         $remoteID = false;
00215                         if ( is_int( $value ) )
00216                         {
00217                             if ( strpos( $valueName, 'class' ) !== false )
00218                             {
00219                                 $value = $remoteIDArray['class'][(string)$value];
00220                                 $remoteID = 'class';
00221                             }
00222                             else if( strpos( $valueName, 'node' ) !== false )
00223                             {
00224                                 $value = $remoteIDArray['node'][(string)$value];
00225                                 $remoteID = 'node';
00226                             }
00227                             else if ( strpos( $valueName, 'object' ) !== false )
00228                             {
00229                                 $value = $remoteIDArray['class'][(string)$value];
00230                                 $remoteID = 'object';
00231                             }
00232                             else
00233                             {
00234                                 eZDebug::writeNotice( 'Could not interpret ' . $valueName . ': ' . $value, __METHOD__ );
00235                             }
00236                         }
00237                         if ( $remoteID )
00238                         {
00239                             $valueNode->appendAttribute( eZDomDocument::createAttributeNode( 'remote-id', $remoteID ) );
00240                         }
00241                         $valueNode->appendChild( eZDomDocument::createTextNode( $value ) );
00242                     }
00243                 }
00244                 else
00245                 {
00246                     $remoteID = false;
00247                     if ( is_int( $variableValue ) )
00248                     {
00249                         if ( strpos( $blockVariable, 'class' ) !== false )
00250                         {
00251                             $variableValue = $remoteIDArray['class'][(string)$value];
00252                             $remoteID = 'class';
00253                         }
00254                         else if( strpos( $blockVariable, 'node' ) !== false )
00255                         {
00256                             $variableValue = $remoteIDArray['node'][(string)$value];
00257                             $remoteID = 'node';
00258                         }
00259                         else if ( strpos( $blockVariable, 'object' ) !== false )
00260                         {
00261                             $variableValue = $remoteIDArray['class'][(string)$value];
00262                             $remoteID = 'object';
00263                         }
00264                         else
00265                         {
00266                             eZDebug::writeNotice( 'Could not interpret ' . $blockVariable . ': ' . $variableValue, __METHOD__ );
00267                         }
00268                     }
00269                     unset( $valueNode );
00270                     $valueNode = eZDomDocument::createElementNode( 'value' );
00271                     $variableNode->appendChild( $valueNode );
00272                     if ( $remoteID )
00273                     {
00274                         $valueNode->appendAttribute( eZDomDocument::createAttributeNode( 'remote-id', $remoteID ) );
00275                     }
00276                     $valueNode->appendChild( eZDomDocument::createTextNode( $variableValue ) );
00277                 }
00278             }
00279         }
00280         return $iniNode;
00281     }
00282 }
00283 ?>