gui.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class Gui extends oxAdminView
00009 {
00015     protected $_sSrcDir;
00016 
00022     protected $_sGuiDir   = "gui/";
00023 
00029     protected $_sBgDir    = "bg/";
00030 
00036     protected $_sGuiXml   = "gui.xml";
00037 
00043     protected $_sUserGui  = "usergui.php";
00044 
00050     protected $_oGuiDom ;
00051 
00057     protected $_sThemeXml = "theme.xml";
00058 
00064     protected $_oThemesDom ;
00065 
00071     protected $_blLoaded = false;
00072 
00078     public function init()
00079     {
00080         parent::init();
00081 
00082         $myConfig   = $this->getConfig();
00083 
00084         $this->_sSrcDir = $myConfig->getResourceDir( false );
00085         $this->_sGuiDir = $this->_sSrcDir.$this->_sGuiDir;
00086 
00087 
00088         $this->_loadGuiFiles();
00089 
00090         $this->sTplName = "gui.tpl";
00091         $this->sTplErr  = "gui_error.tpl";
00092     }
00093 
00099     public function render()
00100     {
00101         parent::render();
00102 
00103         if($this->_blLoaded) {
00104 
00105             $sTheme = null;
00106             $aColors = $aStyles = array();
00107 
00108             $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00109 
00110             if(is_readable($sFilePath)){
00111 
00112                 include($sFilePath);
00113 
00114                 if(is_array($aColors)){
00115                     $this->_aViewData["user_colors"] = $aColors;
00116                 }else{
00117                     $this->_aViewData["user_colors"] = array();
00118                 }
00119 
00120                 if(is_array($aStyles)){
00121                     $this->_aViewData["user_styles"] = $aStyles;
00122                 }else{
00123                     $this->_aViewData["user_styles"] = array();
00124                 }
00125             }
00126 
00127             if(!$sTheme){
00128 
00129                     $sTheme = 'ce';
00130 
00131             }
00132 
00133             $aThemes = $this->getThemes();
00134             $aColors = array();
00135 
00136             foreach ($aThemes as $id => $title){
00137                 $aColors[$id] = $this->getColors($id);
00138             }
00139 
00140             $this->_aViewData["themes"] = $aThemes;
00141 
00142             $this->_aViewData["theme"]  = $sTheme;
00143 
00144             $this->_aViewData["colors"] = $aColors;
00145 
00146             $this->_aViewData["styles"] = $this->getStyleTree();
00147 
00148             return $this->sTplName;
00149 
00150         } else {
00151             return $this->sTplErr;
00152         }
00153     }
00154 
00163     public function fillColors($aStyles,$aColors) {
00164         foreach ($aStyles as $sConst => $sIndex) {
00165             if( array_key_exists($sIndex,$aColors) ) {
00166                 $aStyles[$sConst] = $aColors[$sIndex];
00167             }
00168         }
00169         return $aStyles;
00170     }
00171 
00179     public function save( $blAjax = true ) {
00180 
00181         $sTheme = $this->getConfig()->getParameter('t');
00182         $aUserColors = $this->getUserColors();
00183         $aUserStyles = $this->getUserStyles();
00184 
00185         $this->saveUserSettings($sTheme,$aUserColors,$aUserStyles);
00186 
00187         $aStyles = $this->fillColors( $aUserStyles, $aUserColors );
00188 
00189         $aErrors = array();
00190 
00191         $aGif = $this->getRes('gif');
00192         foreach ($aGif as $oGif) {
00193             $sTpl    = $oGif->getAttribute('tpl');
00194             $sFile   = $oGif->getAttribute('file');
00195             $sConst  = $oGif->getAttribute('const');
00196 
00197             $aGifStyles = $this->fillColors( $this->getResColors('gif',$sTpl), $aStyles );
00198 
00199             $sFile = str_replace('.gif','_'.$sTheme.'_.gif',$sFile);
00200 
00201             $sFilePath = $this->_sSrcDir.$sFile;
00202 
00203             if(file_exists($sFilePath) && !is_writable($sFilePath)){
00204                 @chmod( $sFilePath, 0766);
00205                 if (!is_writable($sFilePath)){
00206                     $aErrors[] = 'Could not write to : '.$sFilePath;
00207                 }
00208             }
00209 
00210             $this->gif( $sTpl , $aGifStyles,  $sFile, $this->_sSrcDir);
00211 
00212             $aStyles[$sConst] = $sFile;
00213         }
00214 
00215         $aCss = $this->getRes('css');
00216 
00217         foreach ($aCss as $oCss) {
00218             $sStyle  = "/* OXID look&feel generated CSS */\n\n";
00219             $sTpl  = $oCss->getAttribute('tpl');
00220             $sFile = $oCss->getAttribute('file');
00221 
00222 
00223             $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00224 
00225             $sFilePath = $this->_sSrcDir.$sFile;
00226 
00227             if(file_exists($sFilePath) && !is_writable($sFilePath)){
00228                 @chmod( $sFilePath, 0766);
00229             }
00230 
00231             if (is_writable($sFilePath)){
00232                 file_put_contents($sFilePath,$sStyle);
00233             } else {
00234                 $aErrors[] = 'Could not write to : '.$sFilePath;
00235             }
00236         }
00237 
00238         if($blAjax){
00239 
00240             $sResponce = '';
00241 
00242             if(count($aErrors)){
00243                 $sResponce = implode("\n",$aErrors);
00244             }
00245 
00246             die($sResponce);
00247         }
00248 
00249     }
00250 
00260     public function saveUserSettings( $sTheme, $aColors, $aStyles ){
00261 
00262            $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00263 
00264         if(file_exists($sFilePath) && !is_writable($sFilePath)){
00265                @chmod( $sFilePath, 0766);
00266         }
00267 
00268            $sFile = "<?php \n";
00269            $sFile.= "/* OXID look&feel generated file */\n\n";
00270            $sFile.= '$sTheme  = "'.$sTheme.'";'."\n\n";
00271            $sFile.= '$aColors = '.var_export( (array) $aColors,true).';'."\n\n";
00272            $sFile.= '$aStyles = '.var_export( (array) $aStyles,true).';'."\n\n";
00273 
00274         file_put_contents($sFilePath,$sFile);
00275     }
00276 
00282     public function previewCss() {
00283 
00284         $aStyles = $this->fillColors( $this->getUserStyles(), $this->getUserColors() );
00285 
00286         $aGif = $this->getRes('gif');
00287         $sAdminUrl = $this->getViewConfig()->getViewConfigParam('selflink');
00288         foreach ($aGif as $oGif) {
00289             $sTpl    = $oGif->getAttribute('tpl');
00290             $sConst  = $oGif->getAttribute('const');
00291 
00292             $aGifStyles = $this->fillColors( $this->getResColors('gif',$sTpl), $aStyles );
00293             $aGifUrl    = $sAdminUrl.'?cl=gui&fnc=previewGif&gif='.$sTpl;
00294 
00295             foreach ($aGifStyles as $i => $c) {
00296                 $aGifUrl .= "&p[{$i}]=".urlencode($c);
00297             }
00298 
00299             $aStyles[$sConst] = $aGifUrl;
00300         }
00301 
00302         //keep existing backgrounds
00303         $aStyles[$this->_sBgDir] = $this->getConfig()->getResourceUrl( null, false ).$this->_sBgDir;
00304 
00305         $aCss = $this->getRes('css');
00306         $sStyle  = "/* OXID GUI generated file css file */\n";
00307         foreach ($aCss as $oCss) {
00308             $sTpl = $oCss->getAttribute('tpl');
00309             $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00310         }
00311 
00312         header('Content-type: text/css');
00313         die( str_replace("\n",'',$sStyle) );
00314     }
00315 
00321     public function previewGif() {
00322 
00323         $myConfig = $this->getConfig();
00324 
00325         $sTpl     = basename($myConfig->getParameter('gif'));
00326         $aColors  = $myConfig->getParameter('p');
00327 
00328         $this->gif( $sTpl , $aColors );
00329         die();
00330     }
00331 
00342     public function gif( $sTpl = null, $aColors = null, $sFile = null, $sDir = null ) {
00343 
00344         $img = imagecreatefromgif($this->_sGuiDir.$sTpl);
00345 
00346         foreach($aColors as $n => $c) {
00347             $x = $this->hex2rgb($c);
00348             imagecolorset($img, $n, $x[0], $x[1], $x[2]);
00349         }
00350 
00351         if(is_null($sDir)) {
00352             header('Content-type: image/gif');
00353             imagegif($img);
00354         }else{
00355             imagegif( $img, $sDir.$sFile );
00356         }
00357 
00358         imagedestroy($img);
00359     }
00360 
00366     public function getUserColors(){
00367 
00368         $myConfig = $this->getConfig();
00369 
00370         $sThemeId = $myConfig->getParameter('t');
00371 
00372         $aThemeColors = $this->getColors($sThemeId);
00373 
00374         $aUserColors  = (array) $myConfig->getParameter('c');
00375 
00376         return array_merge($aUserColors,$aThemeColors);
00377     }
00378 
00384     public function getUserStyles(){
00385         $myConfig = $this->getConfig();
00386 
00387         $aThemeStyles = $this->getStyles();
00388 
00389         $aUserStyles  = (array) $myConfig->getParameter('s');
00390 
00391         return array_merge($aThemeStyles,$aUserStyles);
00392     }
00393 
00394 
00401     protected function _loadGuiFiles() {
00402 
00403         $this->_blLoaded = false;
00404 
00405         if(is_readable($this->_sGuiDir.$this->_sGuiXml)){
00406             $this->_oGuiDom = new DomDocument();
00407             $this->_oGuiDom->preserveWhiteSpace = false;
00408             $this->_oGuiDom->load( $this->_sGuiDir.$this->_sGuiXml );
00409             $this->_blLoaded = true;
00410         }
00411 
00412          if(is_readable($this->_sGuiDir.$this->_sThemeXml) &&  $this->_blLoaded){
00413             $this->_oThemesDom = new DomDocument();
00414             $this->_oThemesDom->preserveWhiteSpace = false;
00415             $this->_oThemesDom->load( $this->_sGuiDir.$this->_sThemeXml );
00416             $this->_blLoaded = true;
00417         }else{
00418             $this->_blLoaded = false;
00419         }
00420     }
00421 
00427     public function getThemes() {
00428         $oXPath = new DomXPath( $this->_oThemesDom );
00429         $oThemeList = $oXPath->query( "/themes/theme" );
00430         $aThemes = array();
00431         foreach ( $oThemeList as $oTheme ) {
00432             $aThemes[$oTheme->getAttribute('id')] = $oTheme->getAttribute('title');
00433         }
00434         return $aThemes;
00435     }
00436 
00444     public function getColors($sThemeId) {
00445         $oXPath = new DomXPath( $this->_oThemesDom );
00446 
00447         $oColorList = $oXPath->query( "/themes/theme[@id='{$sThemeId}']/color" );
00448         $aColors = array();
00449         foreach ( $oColorList as $oColor ) {
00450             $aColors[$oColor->getAttribute('index')] = $oColor->getAttribute('color');
00451         }
00452 
00453         return $aColors;
00454     }
00455 
00461     public function getStyles() {
00462         $oXPath = new DomXPath( $this->_oGuiDom );
00463 
00464         $oStyleList = $oXPath->query( "/gui/css//color" );
00465         $aStyles = array();
00466         foreach ( $oStyleList as $oStyle ) {
00467             $aStyles[$oStyle->getAttribute('const')] = $oStyle->getAttribute('color');
00468         }
00469 
00470         return $aStyles;
00471     }
00472 
00480     public function getRes( $sRes ) {
00481         $oXPath = new DomXPath( $this->_oGuiDom );
00482         $oList = $oXPath->query( "/gui/{$sRes}" );
00483 
00484         return $oList;
00485     }
00486 
00495     public function getResColors( $sRes, $sTpl ){
00496         $oXPath = new DomXPath( $this->_oGuiDom );
00497         $oList = $oXPath->query( "/gui/{$sRes}[@tpl='{$sTpl}']/color" );
00498 
00499         $aList = array();
00500         foreach ( $oList as $oItem ) {
00501             $aList[$oItem->getAttribute('const')] =  $oItem->getAttribute('color');
00502         }
00503 
00504         return $aList;
00505     }
00506 
00515     public function getImageColors($sImageID, $aStyles) {
00516         $oXPath = new DomXPath( $this->_oGuiDom );
00517         $oColorList = $oXPath->query( "/gui/gif[@const='{$sImageID}']/color" );
00518 
00519         $aColors = array();
00520         foreach ( $oColorList as $oColor ) {
00521             $aColors[$oColor->getAttribute('index')] =  $aStyles[$oColor->getAttribute('const')];
00522         }
00523 
00524         return  $aColors;
00525     }
00526 
00535     public function getStyleTree() {
00536         $oXPath = new DomXPath( $this->_oGuiDom );
00537         $oStyleList = $oXPath->query( "/gui/css/*" );
00538 
00539         return $oStyleList;
00540     }
00541 
00550     function hex2rgb($sHexColor) {
00551 
00552         if(strlen($sHexColor) == 4) {
00553             $r = 1;
00554             $g = 2;
00555             $b = 3;
00556             $c = 1;
00557             $n = 2;
00558         }elseif(strlen($sHexColor) == 7){
00559             $r = 1;
00560             $g = 3;
00561             $b = 5;
00562             $c = 2;
00563             $n = 1;
00564         }
00565 
00566         $rh = str_repeat( substr($sHexColor,$r,$c), $n);
00567         $gh = str_repeat( substr($sHexColor,$g,$c), $n);
00568         $bh = str_repeat( substr($sHexColor,$b,$c), $n);
00569 
00570         $rr = hexdec( $rh );
00571         $gg = hexdec( $gh );
00572         $bb = hexdec( $bh );
00573 
00574         return array($rr,$gg,$bb);
00575     }
00576 
00577 }

Generated on Thu Dec 4 12:04:55 2008 for OXID eShop CE by  doxygen 1.5.5