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
00077 public function init()
00078 {
00079 parent::init();
00080
00081 $myConfig = $this->getConfig();
00082
00083 $this->_sSrcDir = $myConfig->getResourceDir( false );
00084 $this->_sGuiDir = $this->_sSrcDir.$this->_sGuiDir;
00085
00086
00087 $this->_loadGuiFiles();
00088
00089 $this->sTplName = "gui.tpl";
00090 $this->sTplErr = "gui_error.tpl";
00091 }
00092
00098 public function render()
00099 {
00100 parent::render();
00101
00102 if($this->_blLoaded) {
00103
00104 $sTheme = null;
00105 $aColors = $aStyles = array();
00106
00107 $sFilePath = $this->_sSrcDir.$this->_sUserGui;
00108
00109 if(is_readable($sFilePath)){
00110
00111 include($sFilePath);
00112
00113 if(is_array($aColors)){
00114 $this->_aViewData["user_colors"] = $aColors;
00115 }else{
00116 $this->_aViewData["user_colors"] = array();
00117 }
00118
00119 if(is_array($aStyles)){
00120 $this->_aViewData["user_styles"] = $aStyles;
00121 }else{
00122 $this->_aViewData["user_styles"] = array();
00123 }
00124 }
00125
00126 if(!$sTheme){
00127
00128 $sTheme = 'ce';
00129
00130 }
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["themes"] = $aThemes;
00140
00141 $this->_aViewData["theme"] = $sTheme;
00142
00143 $this->_aViewData["colors"] = $aColors;
00144
00145 $this->_aViewData["styles"] = $this->getStyleTree();
00146 $this->_aViewData["colorstyles"] = $this->getColors($sTheme,'const','index');
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
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 $sThemeId = $myConfig->getParameter('t');
00388
00389 $aThemeStyles = $this->getStyles();
00390
00391 $aColorStyles = $this->getColors($sThemeId,'const','index');
00392
00393 $aUserStyles = (array) $myConfig->getParameter('s');
00394
00395 return array_merge($aThemeStyles,$aColorStyles,$aUserStyles);
00396 }
00397
00398
00405 protected function _loadGuiFiles() {
00406
00407 $this->_blLoaded = false;
00408
00409 if(is_readable($this->_sGuiDir.$this->_sGuiXml)){
00410 $this->_oGuiDom = new DomDocument();
00411 $this->_oGuiDom->preserveWhiteSpace = false;
00412 $this->_oGuiDom->load( $this->_sGuiDir.$this->_sGuiXml );
00413 $this->_blLoaded = true;
00414 }
00415
00416 if(is_readable($this->_sGuiDir.$this->_sThemeXml) && $this->_blLoaded){
00417 $this->_oThemesDom = new DomDocument();
00418 $this->_oThemesDom->preserveWhiteSpace = false;
00419 $this->_oThemesDom->load( $this->_sGuiDir.$this->_sThemeXml );
00420 $this->_blLoaded = true;
00421 }else{
00422 $this->_blLoaded = false;
00423 }
00424 }
00425
00431 public function getThemes() {
00432 $oXPath = new DomXPath( $this->_oThemesDom );
00433 $oThemeList = $oXPath->query( "/themes/theme" );
00434 $aThemes = array();
00435 foreach ( $oThemeList as $oTheme ) {
00436 $aThemes[$oTheme->getAttribute('id')] = $oTheme->getAttribute('title');
00437 }
00438 return $aThemes;
00439 }
00440
00448 public function getColors($sThemeId,$sKey = 'index',$sValue = 'color') {
00449 $oXPath = new DomXPath( $this->_oThemesDom );
00450
00451 $oColorList = $oXPath->query( "/themes/theme[@id='{$sThemeId}']/color" );
00452 $aColors = array();
00453 foreach ( $oColorList as $oColor ) {
00454 if($oColor->hasAttribute($sKey)&&$oColor->hasAttribute($sValue)){
00455 $aColors[$oColor->getAttribute($sKey)] = $oColor->getAttribute($sValue);
00456 }
00457 }
00458
00459 return $aColors;
00460 }
00461
00467 public function getStyles() {
00468 $oXPath = new DomXPath( $this->_oGuiDom );
00469
00470 $oStyleList = $oXPath->query( "/gui/css//color" );
00471 $aStyles = array();
00472 foreach ( $oStyleList as $oStyle ) {
00473 $aStyles[$oStyle->getAttribute('const')] = $oStyle->getAttribute('color');
00474 }
00475
00476 return $aStyles;
00477 }
00478
00486 public function getRes( $sRes ) {
00487 $oXPath = new DomXPath( $this->_oGuiDom );
00488 $oList = $oXPath->query( "/gui/{$sRes}" );
00489
00490 return $oList;
00491 }
00492
00501 public function getResColors( $sRes, $sTpl ){
00502 $oXPath = new DomXPath( $this->_oGuiDom );
00503 $oList = $oXPath->query( "/gui/{$sRes}[@tpl='{$sTpl}']/color" );
00504
00505 $aList = array();
00506 foreach ( $oList as $oItem ) {
00507 $aList[$oItem->getAttribute('const')] = $oItem->getAttribute('color');
00508 }
00509
00510 return $aList;
00511 }
00512
00521 public function getImageColors($sImageID, $aStyles) {
00522 $oXPath = new DomXPath( $this->_oGuiDom );
00523 $oColorList = $oXPath->query( "/gui/gif[@const='{$sImageID}']/color" );
00524
00525 $aColors = array();
00526 foreach ( $oColorList as $oColor ) {
00527 $aColors[$oColor->getAttribute('index')] = $aStyles[$oColor->getAttribute('const')];
00528 }
00529
00530 return $aColors;
00531 }
00532
00541 public function getStyleTree() {
00542 $oXPath = new DomXPath( $this->_oGuiDom );
00543 $oStyleList = $oXPath->query( "/gui/css/*" );
00544
00545 return $oStyleList;
00546 }
00547
00556 function hex2rgb($sHexColor) {
00557
00558 if(strlen($sHexColor) == 4) {
00559 $r = 1;
00560 $g = 2;
00561 $b = 3;
00562 $c = 1;
00563 $n = 2;
00564 }elseif(strlen($sHexColor) == 7){
00565 $r = 1;
00566 $g = 3;
00567 $b = 5;
00568 $c = 2;
00569 $n = 1;
00570 }
00571
00572 $rh = str_repeat( substr($sHexColor,$r,$c), $n);
00573 $gh = str_repeat( substr($sHexColor,$g,$c), $n);
00574 $bh = str_repeat( substr($sHexColor,$b,$c), $n);
00575
00576 $rr = hexdec( $rh );
00577 $gg = hexdec( $gh );
00578 $bb = hexdec( $bh );
00579
00580 return array($rr,$gg,$bb);
00581 }
00582
00583 }