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 
00149             return $this->_sThisTemplate;
00150         } else {
00151             return $this->_sThisErrorTemplate;
00152         }
00153     }
00154 
00163     public function fillColors($aStyles,$aColors)
00164     {
00165         foreach ($aStyles as $sConst => $sIndex) {
00166             if ( array_key_exists($sIndex, $aColors) ) {
00167                 $aStyles[$sConst] = $aColors[$sIndex];
00168             }
00169         }
00170         return $aStyles;
00171     }
00172 
00180     public function save( $blAjax = true )
00181     {
00182         $sTheme = $this->getConfig()->getParameter('t');
00183         $aUserColors = $this->getUserColors();
00184         $aUserStyles = $this->getUserStyles();
00185 
00186         $this->saveUserSettings($sTheme, $aUserColors, $aUserStyles);
00187 
00188         $aStyles = $this->fillColors( $aUserStyles, $aUserColors );
00189 
00190         $aErrors = array();
00191 
00192         $aGif = $this->getRes('gif');
00193         foreach ($aGif as $oGif) {
00194             $sTpl    = $oGif->getAttribute('tpl');
00195             $sFile   = $oGif->getAttribute('file');
00196             $sConst  = $oGif->getAttribute('const');
00197 
00198             $aGifStyles = $this->fillColors( $this->getResColors('gif', $sTpl), $aStyles );
00199 
00200             $sFile = str_replace('.gif', '_'.$sTheme.'_.gif', $sFile);
00201 
00202             $sFilePath = $this->_sSrcDir.$sFile;
00203 
00204             if (file_exists($sFilePath) && !is_writable($sFilePath)) {
00205                 @chmod( $sFilePath, 0766);
00206             }
00207 
00208             if (is_writable($sFilePath) || (!file_exists($sFilePath) && is_dir(dirname($sFilePath)) && is_writable(dirname($sFilePath)))) {
00209                 $this->gif($sTpl, $aGifStyles, $sFile, $this->_sSrcDir);
00210             } else {
00211                 $aErrors[] = 'Could not write to : '.$sFilePath;
00212             }
00213 
00214             $aStyles[$sConst] = $sFile;
00215         }
00216 
00217         $aCss = $this->getRes('css');
00218 
00219         foreach ($aCss as $oCss) {
00220             $sStyle  = "/* OXID look&feel generated CSS */\n\n";
00221             $sTpl  = $oCss->getAttribute('tpl');
00222             $sFile = $oCss->getAttribute('file');
00223 
00224 
00225             $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00226 
00227             $sFilePath = $this->_sSrcDir.$sFile;
00228 
00229             if (file_exists($sFilePath) && !is_writable($sFilePath)) {
00230                 @chmod( $sFilePath, 0766);
00231             }
00232 
00233             if (is_writable($sFilePath) || (!file_exists($sFilePath) && is_dir(dirname($sFilePath)) && is_writable(dirname($sFilePath)))) {
00234                 file_put_contents($sFilePath, $sStyle);
00235             } else {
00236                 $aErrors[] = 'Could not write to : '.$sFilePath;
00237             }
00238         }
00239 
00240         if ($blAjax) {
00241             $sResponce = '';
00242 
00243             if (count($aErrors)) {
00244                 $sResponce = implode("\n", $aErrors);
00245             }
00246 
00247             oxUtils::getInstance()->showMessageAndExit( $sResponce );
00248         }
00249 
00250     }
00251 
00261     public function saveUserSettings( $sTheme, $aColors, $aStyles )
00262     {
00263         $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00264 
00265         if (file_exists($sFilePath) && !is_writable($sFilePath)) {
00266             @chmod( $sFilePath, 0766);
00267         }
00268 
00269         $sFile = "<?php \n";
00270         $sFile.= "/* OXID look&feel generated file */\n\n";
00271         $sFile.= '$sTheme  = "'.$sTheme.'";'."\n\n";
00272         $sFile.= '$aColors = '.var_export( (array) $aColors, true).';'."\n\n";
00273         $sFile.= '$aStyles = '.var_export( (array) $aStyles, true).';'."\n\n";
00274 
00275         file_put_contents($sFilePath, $sFile);
00276     }
00277 
00287     public function loadUserSettings(&$sTheme, &$aColors, &$aStyles)
00288     {
00289         $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00290         if (is_readable($sFilePath)) {
00291             include $sFilePath;
00292         }
00293     }
00294 
00300     public function previewCss()
00301     {
00302         $aStyles = $this->fillColors($this->getUserStyles(), $this->getUserColors());
00303 
00304         $aGif = $this->getRes('gif');
00305         $sAdminUrl = $this->getViewConfig()->getViewConfigParam('selflink');
00306         foreach ($aGif as $oGif) {
00307             $sTpl    = $oGif->getAttribute('tpl');
00308             $sConst  = $oGif->getAttribute('const');
00309 
00310             $aGifStyles = $this->fillColors( $this->getResColors('gif', $sTpl), $aStyles );
00311             $aGifUrl    = $sAdminUrl.'&cl=gui&fnc=previewGif&gif='.$sTpl;
00312 
00313             foreach ($aGifStyles as $i => $c) {
00314                 $aGifUrl .= "&p[{$i}]=".urlencode($c);
00315             }
00316 
00317             $aStyles[$sConst] = $aGifUrl;
00318         }
00319 
00320         //keep existing backgrounds
00321         $aStyles[$this->_sBgDir] = $this->getConfig()->getResourceUrl( null, false ).$this->_sBgDir;
00322 
00323         $aCss = $this->getRes('css');
00324         $sStyle  = "/* OXID GUI generated file css file */\n";
00325         foreach ($aCss as $oCss) {
00326             $sTpl = $oCss->getAttribute('tpl');
00327             $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00328         }
00329 
00330         oxUtils::getInstance()->setHeader('Content-type: text/css');
00331         oxUtils::getInstance()->showMessageAndExit( str_replace("\n", '', $sStyle) );
00332     }
00333 
00339     public function previewGif()
00340     {
00341         $myConfig = $this->getConfig();
00342 
00343         $sTpl     = basename($myConfig->getParameter('gif'));
00344         $aColors  = $myConfig->getParameter('p');
00345 
00346         $this->gif( $sTpl, $aColors );
00347         oxUtils::getInstance()->showMessageAndExit( '' );
00348     }
00349 
00360     public function gif( $sTpl = null, $aColors = null, $sFile = null, $sDir = null )
00361     {
00362         $img = imagecreatefromgif($this->_sGuiDir.$sTpl);
00363 
00364         foreach ($aColors as $n => $c) {
00365             $x = $this->hex2rgb($c);
00366             imagecolorset($img, $n, $x[0], $x[1], $x[2]);
00367         }
00368 
00369         if (is_null($sDir)) {
00370             oxUtils::getInstance()->setHeader('Content-type: image/gif');
00371             imagegif($img);
00372         } else {
00373             imagegif($img, $sDir.$sFile );
00374         }
00375 
00376         imagedestroy($img);
00377     }
00378 
00384     public function getUserColors()
00385     {
00386         $myConfig = $this->getConfig();
00387 
00388         $sThemeId = $myConfig->getParameter('t');
00389 
00390         $aThemeColors = $this->getColors($sThemeId);
00391 
00392         $aUserColors  = (array) $myConfig->getParameter('c');
00393 
00394         return array_merge($aUserColors, $aThemeColors);
00395     }
00396 
00402     public function getUserStyles()
00403     {
00404         $myConfig = $this->getConfig();
00405 
00406         $sThemeId = $myConfig->getParameter('t');
00407 
00408         $aThemeStyles = $this->getStyles();
00409 
00410         $aColorStyles = $this->getColors($sThemeId, 'const', 'index');
00411 
00412         $aUserStyles  = (array) $myConfig->getParameter('s');
00413 
00414         return array_merge($aThemeStyles, $aColorStyles, $aUserStyles);
00415     }
00416 
00417 
00424     protected function _loadGuiFiles()
00425     {
00426         $this->_blLoaded = false;
00427 
00428         if (is_readable($this->_sGuiDir.$this->_sGuiXml)) {
00429             $this->_oGuiDom = new DomDocument();
00430             $this->_oGuiDom->preserveWhiteSpace = false;
00431             $this->_oGuiDom->load( $this->_sGuiDir.$this->_sGuiXml );
00432             $this->_blLoaded = true;
00433         }
00434 
00435         if (is_readable($this->_sGuiDir.$this->_sThemeXml) &&  $this->_blLoaded) {
00436             $this->_oThemesDom = new DomDocument();
00437             $this->_oThemesDom->preserveWhiteSpace = false;
00438             $this->_oThemesDom->load( $this->_sGuiDir.$this->_sThemeXml );
00439             $this->_blLoaded = true;
00440         } else {
00441             $this->_blLoaded = false;
00442         }
00443     }
00444 
00450     public function getThemes()
00451     {
00452         $oXPath = new DomXPath( $this->_oThemesDom );
00453         $oThemeList = $oXPath->query( "/themes/theme" );
00454         $aThemes = array();
00455         foreach ( $oThemeList as $oTheme ) {
00456             $aThemes[$oTheme->getAttribute('id')] = $oTheme->getAttribute('title');
00457         }
00458         return $aThemes;
00459     }
00460 
00470     public function getColors($sThemeId, $sKey = 'index', $sValue = 'color')
00471     {
00472         $oXPath = new DomXPath( $this->_oThemesDom );
00473 
00474         $oColorList = $oXPath->query( "/themes/theme[@id='{$sThemeId}']/color" );
00475         $aColors = array();
00476         foreach ( $oColorList as $oColor ) {
00477             if ($oColor->hasAttribute($sKey)&&$oColor->hasAttribute($sValue)) {
00478                 $aColors[$oColor->getAttribute($sKey)] = $oColor->getAttribute($sValue);
00479             }
00480         }
00481 
00482         return $aColors;
00483     }
00484 
00490     public function getStyles()
00491     {
00492         $oXPath = new DomXPath( $this->_oGuiDom );
00493 
00494         $oStyleList = $oXPath->query( "/gui/css//color" );
00495         $aStyles = array();
00496         foreach ( $oStyleList as $oStyle ) {
00497             $aStyles[$oStyle->getAttribute('const')] = $oStyle->getAttribute('color');
00498         }
00499 
00500         return $aStyles;
00501     }
00502 
00510     public function getRes( $sRes )
00511     {
00512         $oXPath = new DomXPath( $this->_oGuiDom );
00513         $oList = $oXPath->query( "/gui/{$sRes}" );
00514 
00515         return $oList;
00516     }
00517 
00526     public function getResColors( $sRes, $sTpl )
00527     {
00528         $oXPath = new DomXPath( $this->_oGuiDom );
00529         $oList = $oXPath->query( "/gui/{$sRes}[@tpl='{$sTpl}']/color" );
00530 
00531         $aList = array();
00532         foreach ( $oList as $oItem ) {
00533             $aList[$oItem->getAttribute('const')] =  $oItem->getAttribute('color');
00534         }
00535 
00536         return $aList;
00537     }
00538 
00547     public function getImageColors($sImageID, $aStyles)
00548     {
00549         $oXPath = new DomXPath( $this->_oGuiDom );
00550         $oColorList = $oXPath->query( "/gui/gif[@const='{$sImageID}']/color" );
00551 
00552         $aColors = array();
00553         foreach ( $oColorList as $oColor ) {
00554             $aColors[$oColor->getAttribute('color')] =  $aStyles[$oColor->getAttribute('const')];
00555         }
00556 
00557         return  $aColors;
00558     }
00559 
00565     public function getStyleTree()
00566     {
00567         $oXPath = new DomXPath( $this->_oGuiDom );
00568         $oStyleList = $oXPath->query( "/gui/css/*" );
00569 
00570         return $oStyleList;
00571     }
00572 
00581     function hex2rgb($sHexColor)
00582     {
00583         if (strlen($sHexColor) == 4) {
00584             $r = 1;
00585             $g = 2;
00586             $b = 3;
00587             $c = 1;
00588             $n = 2;
00589         } elseif (strlen($sHexColor) == 7) {
00590             $r = 1;
00591             $g = 3;
00592             $b = 5;
00593             $c = 2;
00594             $n = 1;
00595         }
00596 
00597         $rh = str_repeat( substr($sHexColor, $r, $c), $n);
00598         $gh = str_repeat( substr($sHexColor, $g, $c), $n);
00599         $bh = str_repeat( substr($sHexColor, $b, $c), $n);
00600 
00601         $rr = hexdec( $rh );
00602         $gg = hexdec( $gh );
00603         $bb = hexdec( $bh );
00604 
00605         return array($rr, $gg, $bb);
00606     }
00607 
00608 }