gui.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class Gui extends oxAdminView
00008 {
00014     protected $_sSrcDir;
00015 
00021     protected $_sGuiDir   = "gui/";
00022 
00028     protected $_sBgDir    = "bg/";
00029 
00035     protected $_sGuiXml   = "gui.xml";
00036 
00042     protected $_sUserGui  = "usergui.php";
00043 
00049     protected $_oGuiDom ;
00050 
00056     protected $_sThemeXml = "theme.xml";
00057 
00063     protected $_oThemesDom ;
00064 
00070     protected $_blLoaded = false;
00071 
00076     protected $_sThisTemplate = 'gui.tpl';
00077 
00082     protected $_sThisErrorTemplate = 'gui_error.tpl';
00083 
00089     public function init()
00090     {
00091         parent::init();
00092 
00093         $myConfig   = $this->getConfig();
00094 
00095         $this->_sSrcDir = $myConfig->getResourceDir( false );
00096         $this->_sGuiDir = $this->_sSrcDir.$this->_sGuiDir;
00097 
00098 
00099         $this->_loadGuiFiles();
00100     }
00101 
00107     protected function _isDomLoaded()
00108     {
00109         return $this->_blLoaded;
00110     }
00111 
00117     public function render()
00118     {
00119         parent::render();
00120 
00121         if ( $this->_isDomLoaded() ) {
00122 
00123 
00124                 $sTheme = 'ce';
00125 
00126 
00127             $aUserColors = array();
00128             $aUserStyles = array();
00129 
00130             $this->loadUserSettings($sTheme, $aUserColors, $aUserStyles);
00131 
00132             $aThemes = $this->getThemes();
00133             $aColors = array();
00134 
00135             foreach ($aThemes as $id => $title) {
00136                 $aColors[$id] = $this->getColors($id);
00137             }
00138 
00139             $this->_aViewData["theme"]       = $sTheme;
00140             $this->_aViewData["themes"]      = $aThemes;
00141             $this->_aViewData["colors"]      = $aColors;
00142             $this->_aViewData["styles"]      = $this->getStyleTree();
00143             $this->_aViewData["colorstyles"] = $this->getColors($sTheme, 'const', 'index');
00144             $this->_aViewData["user_colors"] = $aUserColors;
00145             $this->_aViewData["user_styles"] = $aUserStyles;
00146 
00147             $this->_aViewData["sAddData"] = "stoken=".$this->getSession()->getSessionChallengeToken();
00148             $this->_aViewData["sShopHomeLink"] = $this->getConfig()->getShopHomeURL();
00149 
00150 
00151             return $this->_sThisTemplate;
00152         } else {
00153             return $this->_sThisErrorTemplate;
00154         }
00155     }
00156 
00165     public function fillColors($aStyles,$aColors)
00166     {
00167         foreach ($aStyles as $sConst => $sIndex) {
00168             if ( array_key_exists($sIndex, $aColors) ) {
00169                 $aStyles[$sConst] = $aColors[$sIndex];
00170             }
00171         }
00172         return $aStyles;
00173     }
00174 
00182     public function save( $blAjax = true )
00183     {
00184         $sTheme = $this->getConfig()->getParameter('t');
00185         $aUserColors = $this->getUserColors();
00186         $aUserStyles = $this->getUserStyles();
00187 
00188         $this->saveUserSettings($sTheme, $aUserColors, $aUserStyles);
00189 
00190         $aStyles = $this->fillColors( $aUserStyles, $aUserColors );
00191 
00192         $aErrors = array();
00193 
00194         $aGif = $this->getRes('gif');
00195         foreach ($aGif as $oGif) {
00196             $sTpl    = $oGif->getAttribute('tpl');
00197             $sFile   = $oGif->getAttribute('file');
00198             $sConst  = $oGif->getAttribute('const');
00199 
00200             $aGifStyles = $this->fillColors( $this->getResColors('gif', $sTpl), $aStyles );
00201 
00202             $sFile = str_replace('.gif', '_'.$sTheme.'_.gif', $sFile);
00203 
00204             $sFilePath = $this->_sSrcDir.$sFile;
00205 
00206             if (file_exists($sFilePath) && !is_writable($sFilePath)) {
00207                 @chmod( $sFilePath, 0766);
00208             }
00209 
00210             if (is_writable($sFilePath) || (!file_exists($sFilePath) && is_dir(dirname($sFilePath)) && is_writable(dirname($sFilePath)))) {
00211                 $this->gif($sTpl, $aGifStyles, $sFile, $this->_sSrcDir);
00212             } else {
00213                 $aErrors[] = 'Could not write to : '.$sFilePath;
00214             }
00215 
00216             $aStyles[$sConst] = $sFile;
00217         }
00218 
00219         $aCss = $this->getRes('css');
00220 
00221         foreach ($aCss as $oCss) {
00222             $sStyle  = "/* OXID look&feel generated CSS */\n\n";
00223             $sTpl  = $oCss->getAttribute('tpl');
00224             $sFile = $oCss->getAttribute('file');
00225 
00226 
00227             $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00228 
00229             $sFilePath = $this->_sSrcDir.$sFile;
00230 
00231             if (file_exists($sFilePath) && !is_writable($sFilePath)) {
00232                 @chmod( $sFilePath, 0766);
00233             }
00234 
00235             if (is_writable($sFilePath) || (!file_exists($sFilePath) && is_dir(dirname($sFilePath)) && is_writable(dirname($sFilePath)))) {
00236                 file_put_contents($sFilePath, $sStyle);
00237             } else {
00238                 $aErrors[] = 'Could not write to : '.$sFilePath;
00239             }
00240         }
00241 
00242         if ($blAjax) {
00243             $sResponce = '';
00244 
00245             if (count($aErrors)) {
00246                 $sResponce = implode("\n", $aErrors);
00247             }
00248 
00249             oxUtils::getInstance()->showMessageAndExit( $sResponce );
00250         }
00251 
00252     }
00253 
00263     public function saveUserSettings( $sTheme, $aColors, $aStyles )
00264     {
00265         $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00266 
00267         if (file_exists($sFilePath) && !is_writable($sFilePath)) {
00268             @chmod( $sFilePath, 0766);
00269         }
00270 
00271         $sFile = "<?php \n";
00272         $sFile.= "/* OXID look&feel generated file */\n\n";
00273         $sFile.= '$sTheme  = "'.$sTheme.'";'."\n\n";
00274         $sFile.= '$aColors = '.var_export( (array) $aColors, true).';'."\n\n";
00275         $sFile.= '$aStyles = '.var_export( (array) $aStyles, true).';'."\n\n";
00276 
00277         file_put_contents($sFilePath, $sFile);
00278     }
00279 
00289     public function loadUserSettings(&$sTheme, &$aColors, &$aStyles)
00290     {
00291         $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00292         if (is_readable($sFilePath)) {
00293             include $sFilePath;
00294         }
00295     }
00296 
00302     public function previewCss()
00303     {
00304         $aStyles = $this->fillColors($this->getUserStyles(), $this->getUserColors());
00305 
00306         $aGif = $this->getRes('gif');
00307         $sAdminUrl = $this->getViewConfig()->getViewConfigParam('selflink');
00308         foreach ($aGif as $oGif) {
00309             $sTpl    = $oGif->getAttribute('tpl');
00310             $sConst  = $oGif->getAttribute('const');
00311 
00312             $aGifStyles = $this->fillColors( $this->getResColors('gif', $sTpl), $aStyles );
00313             $aGifUrl    = $sAdminUrl.'&cl=gui&fnc=previewGif&gif='.$sTpl;
00314 
00315             foreach ($aGifStyles as $i => $c) {
00316                 $aGifUrl .= "&p[{$i}]=".urlencode($c);
00317             }
00318 
00319             $aStyles[$sConst] = $aGifUrl;
00320         }
00321 
00322         //keep existing backgrounds
00323         $aStyles[$this->_sBgDir] = $this->getConfig()->getResourceUrl( null, false ).$this->_sBgDir;
00324 
00325         $aCss = $this->getRes('css');
00326         $sStyle  = "/* OXID GUI generated file css file */\n";
00327         foreach ($aCss as $oCss) {
00328             $sTpl = $oCss->getAttribute('tpl');
00329             $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00330         }
00331 
00332         oxUtils::getInstance()->setHeader('Content-type: text/css');
00333         oxUtils::getInstance()->showMessageAndExit( str_replace("\n", '', $sStyle) );
00334     }
00335 
00341     public function previewGif()
00342     {
00343         $myConfig = $this->getConfig();
00344 
00345         $sTpl     = basename($myConfig->getParameter('gif'));
00346         $aColors  = $myConfig->getParameter('p');
00347 
00348         $this->gif( $sTpl, $aColors );
00349         oxUtils::getInstance()->showMessageAndExit( '' );
00350     }
00351 
00362     public function gif( $sTpl = null, $aColors = null, $sFile = null, $sDir = null )
00363     {
00364         $img = imagecreatefromgif($this->_sGuiDir.$sTpl);
00365 
00366         foreach ($aColors as $n => $c) {
00367             $x = $this->hex2rgb($c);
00368             imagecolorset($img, $n, $x[0], $x[1], $x[2]);
00369         }
00370 
00371         if (is_null($sDir)) {
00372             oxUtils::getInstance()->setHeader('Content-type: image/gif');
00373             imagegif($img);
00374         } else {
00375             imagegif($img, $sDir.$sFile );
00376         }
00377 
00378         imagedestroy($img);
00379     }
00380 
00386     public function getUserColors()
00387     {
00388         $myConfig = $this->getConfig();
00389 
00390         $sThemeId = $myConfig->getParameter('t');
00391 
00392         $aThemeColors = $this->getColors($sThemeId);
00393 
00394         $aUserColors  = (array) $myConfig->getParameter('c');
00395 
00396         return array_merge($aUserColors, $aThemeColors);
00397     }
00398 
00404     public function getUserStyles()
00405     {
00406         $myConfig = $this->getConfig();
00407 
00408         $sThemeId = $myConfig->getParameter('t');
00409 
00410         $aThemeStyles = $this->getStyles();
00411 
00412         $aColorStyles = $this->getColors($sThemeId, 'const', 'index');
00413 
00414         $aUserStyles  = (array) $myConfig->getParameter('s');
00415 
00416         return array_merge($aThemeStyles, $aColorStyles, $aUserStyles);
00417     }
00418 
00419 
00426     protected function _loadGuiFiles()
00427     {
00428         $this->_blLoaded = false;
00429 
00430         if (is_readable($this->_sGuiDir.$this->_sGuiXml)) {
00431             $this->_oGuiDom = new DomDocument();
00432             $this->_oGuiDom->preserveWhiteSpace = false;
00433             $this->_oGuiDom->load( $this->_sGuiDir.$this->_sGuiXml );
00434             $this->_blLoaded = true;
00435         }
00436 
00437         if (is_readable($this->_sGuiDir.$this->_sThemeXml) &&  $this->_blLoaded) {
00438             $this->_oThemesDom = new DomDocument();
00439             $this->_oThemesDom->preserveWhiteSpace = false;
00440             $this->_oThemesDom->load( $this->_sGuiDir.$this->_sThemeXml );
00441             $this->_blLoaded = true;
00442         } else {
00443             $this->_blLoaded = false;
00444         }
00445     }
00446 
00452     public function getThemes()
00453     {
00454         $oXPath = new DomXPath( $this->_oThemesDom );
00455         $oThemeList = $oXPath->query( "/themes/theme" );
00456         $aThemes = array();
00457         foreach ( $oThemeList as $oTheme ) {
00458             $aThemes[$oTheme->getAttribute('id')] = $oTheme->getAttribute('title');
00459         }
00460         return $aThemes;
00461     }
00462 
00472     public function getColors($sThemeId, $sKey = 'index', $sValue = 'color')
00473     {
00474         $oXPath = new DomXPath( $this->_oThemesDom );
00475 
00476         $oColorList = $oXPath->query( "/themes/theme[@id='{$sThemeId}']/color" );
00477         $aColors = array();
00478         foreach ( $oColorList as $oColor ) {
00479             if ($oColor->hasAttribute($sKey)&&$oColor->hasAttribute($sValue)) {
00480                 $aColors[$oColor->getAttribute($sKey)] = $oColor->getAttribute($sValue);
00481             }
00482         }
00483 
00484         return $aColors;
00485     }
00486 
00492     public function getStyles()
00493     {
00494         $oXPath = new DomXPath( $this->_oGuiDom );
00495 
00496         $oStyleList = $oXPath->query( "/gui/css//color" );
00497         $aStyles = array();
00498         foreach ( $oStyleList as $oStyle ) {
00499             $aStyles[$oStyle->getAttribute('const')] = $oStyle->getAttribute('color');
00500         }
00501 
00502         return $aStyles;
00503     }
00504 
00512     public function getRes( $sRes )
00513     {
00514         $oXPath = new DomXPath( $this->_oGuiDom );
00515         $oList = $oXPath->query( "/gui/{$sRes}" );
00516 
00517         return $oList;
00518     }
00519 
00528     public function getResColors( $sRes, $sTpl )
00529     {
00530         $oXPath = new DomXPath( $this->_oGuiDom );
00531         $oList = $oXPath->query( "/gui/{$sRes}[@tpl='{$sTpl}']/color" );
00532 
00533         $aList = array();
00534         foreach ( $oList as $oItem ) {
00535             $aList[$oItem->getAttribute('const')] =  $oItem->getAttribute('color');
00536         }
00537 
00538         return $aList;
00539     }
00540 
00549     public function getImageColors($sImageID, $aStyles)
00550     {
00551         $oXPath = new DomXPath( $this->_oGuiDom );
00552         $oColorList = $oXPath->query( "/gui/gif[@const='{$sImageID}']/color" );
00553 
00554         $aColors = array();
00555         foreach ( $oColorList as $oColor ) {
00556             $aColors[$oColor->getAttribute('color')] =  $aStyles[$oColor->getAttribute('const')];
00557         }
00558 
00559         return  $aColors;
00560     }
00561 
00567     public function getStyleTree()
00568     {
00569         $oXPath = new DomXPath( $this->_oGuiDom );
00570         $oStyleList = $oXPath->query( "/gui/css/*" );
00571 
00572         return $oStyleList;
00573     }
00574 
00583     function hex2rgb($sHexColor)
00584     {
00585         if (strlen($sHexColor) == 4) {
00586             $r = 1;
00587             $g = 2;
00588             $b = 3;
00589             $c = 1;
00590             $n = 2;
00591         } elseif (strlen($sHexColor) == 7) {
00592             $r = 1;
00593             $g = 3;
00594             $b = 5;
00595             $c = 2;
00596             $n = 1;
00597         }
00598 
00599         $rh = str_repeat( substr($sHexColor, $r, $c), $n);
00600         $gh = str_repeat( substr($sHexColor, $g, $c), $n);
00601         $bh = str_repeat( substr($sHexColor, $b, $c), $n);
00602 
00603         $rr = hexdec( $rh );
00604         $gg = hexdec( $gh );
00605         $bb = hexdec( $bh );
00606 
00607         return array($rr, $gg, $bb);
00608     }
00609 
00610 }