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 $this->_aViewData["colorstyles"] = $this->getColors($sTheme,'const','index');
00148
00149 return $this->sTplName;
00150
00151 } else {
00152 return $this->sTplErr;
00153 }
00154 }
00155
00164 public function fillColors($aStyles,$aColors) {
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 if (!is_writable($sFilePath)){
00207 $aErrors[] = 'Could not write to : '.$sFilePath;
00208 }
00209 }
00210
00211 $this->gif( $sTpl , $aGifStyles, $sFile, $this->_sSrcDir);
00212
00213 $aStyles[$sConst] = $sFile;
00214 }
00215
00216 $aCss = $this->getRes('css');
00217
00218 foreach ($aCss as $oCss) {
00219 $sStyle = "/* OXID look&feel generated CSS */\n\n";
00220 $sTpl = $oCss->getAttribute('tpl');
00221 $sFile = $oCss->getAttribute('file');
00222
00223
00224 $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00225
00226 $sFilePath = $this->_sSrcDir.$sFile;
00227
00228 if(file_exists($sFilePath) && !is_writable($sFilePath)){
00229 @chmod( $sFilePath, 0766);
00230 }
00231
00232 if (is_writable($sFilePath)){
00233 file_put_contents($sFilePath,$sStyle);
00234 } else {
00235 $aErrors[] = 'Could not write to : '.$sFilePath;
00236 }
00237 }
00238
00239 if($blAjax){
00240
00241 $sResponce = '';
00242
00243 if(count($aErrors)){
00244 $sResponce = implode("\n",$aErrors);
00245 }
00246
00247 die($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
00283 public function previewCss() {
00284
00285 $aStyles = $this->fillColors( $this->getUserStyles(), $this->getUserColors() );
00286
00287 $aGif = $this->getRes('gif');
00288 $sAdminUrl = $this->getViewConfig()->getViewConfigParam('selflink');
00289 foreach ($aGif as $oGif) {
00290 $sTpl = $oGif->getAttribute('tpl');
00291 $sConst = $oGif->getAttribute('const');
00292
00293 $aGifStyles = $this->fillColors( $this->getResColors('gif',$sTpl), $aStyles );
00294 $aGifUrl = $sAdminUrl.'?cl=gui&fnc=previewGif&gif='.$sTpl;
00295
00296 foreach ($aGifStyles as $i => $c) {
00297 $aGifUrl .= "&p[{$i}]=".urlencode($c);
00298 }
00299
00300 $aStyles[$sConst] = $aGifUrl;
00301 }
00302
00303
00304 $aStyles[$this->_sBgDir] = $this->getConfig()->getResourceUrl( null, false ).$this->_sBgDir;
00305
00306 $aCss = $this->getRes('css');
00307 $sStyle = "/* OXID GUI generated file css file */\n";
00308 foreach ($aCss as $oCss) {
00309 $sTpl = $oCss->getAttribute('tpl');
00310 $sStyle .= strtr( file_get_contents($this->_sGuiDir.$sTpl), $aStyles);
00311 }
00312
00313 header('Content-type: text/css');
00314 die( str_replace("\n",'',$sStyle) );
00315 }
00316
00322 public function previewGif() {
00323
00324 $myConfig = $this->getConfig();
00325
00326 $sTpl = basename($myConfig->getParameter('gif'));
00327 $aColors = $myConfig->getParameter('p');
00328
00329 $this->gif( $sTpl , $aColors );
00330 die();
00331 }
00332
00343 public function gif( $sTpl = null, $aColors = null, $sFile = null, $sDir = null ) {
00344
00345 $img = imagecreatefromgif($this->_sGuiDir.$sTpl);
00346
00347 foreach($aColors as $n => $c) {
00348 $x = $this->hex2rgb($c);
00349 imagecolorset($img, $n, $x[0], $x[1], $x[2]);
00350 }
00351
00352 if(is_null($sDir)) {
00353 header('Content-type: image/gif');
00354 imagegif($img);
00355 }else{
00356 imagegif( $img, $sDir.$sFile );
00357 }
00358
00359 imagedestroy($img);
00360 }
00361
00367 public function getUserColors(){
00368
00369 $myConfig = $this->getConfig();
00370
00371 $sThemeId = $myConfig->getParameter('t');
00372
00373 $aThemeColors = $this->getColors($sThemeId);
00374
00375 $aUserColors = (array) $myConfig->getParameter('c');
00376
00377 return array_merge($aUserColors,$aThemeColors);
00378 }
00379
00385 public function getUserStyles(){
00386 $myConfig = $this->getConfig();
00387
00388 $sThemeId = $myConfig->getParameter('t');
00389
00390 $aThemeStyles = $this->getStyles();
00391
00392 $aColorStyles = $this->getColors($sThemeId,'const','index');
00393
00394 $aUserStyles = (array) $myConfig->getParameter('s');
00395
00396 return array_merge($aThemeStyles,$aColorStyles,$aUserStyles);
00397 }
00398
00399
00406 protected function _loadGuiFiles() {
00407
00408 $this->_blLoaded = false;
00409
00410 if(is_readable($this->_sGuiDir.$this->_sGuiXml)){
00411 $this->_oGuiDom = new DomDocument();
00412 $this->_oGuiDom->preserveWhiteSpace = false;
00413 $this->_oGuiDom->load( $this->_sGuiDir.$this->_sGuiXml );
00414 $this->_blLoaded = true;
00415 }
00416
00417 if(is_readable($this->_sGuiDir.$this->_sThemeXml) && $this->_blLoaded){
00418 $this->_oThemesDom = new DomDocument();
00419 $this->_oThemesDom->preserveWhiteSpace = false;
00420 $this->_oThemesDom->load( $this->_sGuiDir.$this->_sThemeXml );
00421 $this->_blLoaded = true;
00422 }else{
00423 $this->_blLoaded = false;
00424 }
00425 }
00426
00432 public function getThemes() {
00433 $oXPath = new DomXPath( $this->_oThemesDom );
00434 $oThemeList = $oXPath->query( "/themes/theme" );
00435 $aThemes = array();
00436 foreach ( $oThemeList as $oTheme ) {
00437 $aThemes[$oTheme->getAttribute('id')] = $oTheme->getAttribute('title');
00438 }
00439 return $aThemes;
00440 }
00441
00449 public function getColors($sThemeId,$sKey = 'index',$sValue = 'color') {
00450 $oXPath = new DomXPath( $this->_oThemesDom );
00451
00452 $oColorList = $oXPath->query( "/themes/theme[@id='{$sThemeId}']/color" );
00453 $aColors = array();
00454 foreach ( $oColorList as $oColor ) {
00455 if($oColor->hasAttribute($sKey)&&$oColor->hasAttribute($sValue)){
00456 $aColors[$oColor->getAttribute($sKey)] = $oColor->getAttribute($sValue);
00457 }
00458 }
00459
00460 return $aColors;
00461 }
00462
00468 public function getStyles() {
00469 $oXPath = new DomXPath( $this->_oGuiDom );
00470
00471 $oStyleList = $oXPath->query( "/gui/css//color" );
00472 $aStyles = array();
00473 foreach ( $oStyleList as $oStyle ) {
00474 $aStyles[$oStyle->getAttribute('const')] = $oStyle->getAttribute('color');
00475 }
00476
00477 return $aStyles;
00478 }
00479
00487 public function getRes( $sRes ) {
00488 $oXPath = new DomXPath( $this->_oGuiDom );
00489 $oList = $oXPath->query( "/gui/{$sRes}" );
00490
00491 return $oList;
00492 }
00493
00502 public function getResColors( $sRes, $sTpl ){
00503 $oXPath = new DomXPath( $this->_oGuiDom );
00504 $oList = $oXPath->query( "/gui/{$sRes}[@tpl='{$sTpl}']/color" );
00505
00506 $aList = array();
00507 foreach ( $oList as $oItem ) {
00508 $aList[$oItem->getAttribute('const')] = $oItem->getAttribute('color');
00509 }
00510
00511 return $aList;
00512 }
00513
00522 public function getImageColors($sImageID, $aStyles) {
00523 $oXPath = new DomXPath( $this->_oGuiDom );
00524 $oColorList = $oXPath->query( "/gui/gif[@const='{$sImageID}']/color" );
00525
00526 $aColors = array();
00527 foreach ( $oColorList as $oColor ) {
00528 $aColors[$oColor->getAttribute('index')] = $aStyles[$oColor->getAttribute('const')];
00529 }
00530
00531 return $aColors;
00532 }
00533
00542 public function getStyleTree() {
00543 $oXPath = new DomXPath( $this->_oGuiDom );
00544 $oStyleList = $oXPath->query( "/gui/css/*" );
00545
00546 return $oStyleList;
00547 }
00548
00557 function hex2rgb($sHexColor) {
00558
00559 if(strlen($sHexColor) == 4) {
00560 $r = 1;
00561 $g = 2;
00562 $b = 3;
00563 $c = 1;
00564 $n = 2;
00565 }elseif(strlen($sHexColor) == 7){
00566 $r = 1;
00567 $g = 3;
00568 $b = 5;
00569 $c = 2;
00570 $n = 1;
00571 }
00572
00573 $rh = str_repeat( substr($sHexColor,$r,$c), $n);
00574 $gh = str_repeat( substr($sHexColor,$g,$c), $n);
00575 $bh = str_repeat( substr($sHexColor,$b,$c), $n);
00576
00577 $rr = hexdec( $rh );
00578 $gg = hexdec( $gh );
00579 $bb = hexdec( $bh );
00580
00581 return array($rr,$gg,$bb);
00582 }
00583
00584 }