2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 /*********************************************************************************
41 * Description: Contains a variety of utility functions used to display UI
42 * components such as form headers and footers. Intended to be modified on a per
44 ********************************************************************************/
46 if(!defined('JSMIN_AS_LIB'))
47 define('JSMIN_AS_LIB', true);
49 require_once("include/SugarTheme/cssmin.php");
50 require_once("jssource/jsmin.php");
51 require_once('include/utils/sugar_file_utils.php');
54 * Class that provides tools for working with a theme.
71 protected $description;
74 * Defines which parent files to not include
78 protected $ignoreParentFiles = array();
81 * Defines which parent files to not include
85 public $directionality = 'ltr';
87 * Theme directory name
98 protected $parentTheme;
101 * Colors sets provided by the theme
103 * @deprecated only here for BC during upgrades
106 protected $colors = array();
109 * Font sets provided by the theme
111 * @deprecated only here for BC during upgrades
114 protected $fonts = array();
117 * Maximum sugar version this theme is for; defaults to 5.5.1 as all the themes without this
118 * parameter as assumed to work thru 5.5.1
122 protected $version = '5.5.1';
125 * Colors used in bar charts
129 protected $barChartColors = array(
130 "docBorder" => "0xffffff",
131 "docBg1" => "0xffffff",
132 "docBg2" => "0xffffff",
133 "xText" => "0x33485c",
134 "yText" => "0x33485c",
135 "title" => "0x333333",
136 "misc" => "0x999999",
137 "altBorder" => "0xffffff",
138 "altBg" => "0xffffff",
139 "altText" => "0x666666",
140 "graphBorder" => "0xcccccc",
141 "graphBg1" => "0xf6f6f6",
142 "graphBg2" => "0xf6f6f6",
143 "graphLines" => "0xcccccc",
144 "graphText" => "0x333333",
145 "graphTextShadow" => "0xf9f9f9",
146 "barBorder" => "0xeeeeee",
147 "barBorderHilite" => "0x333333",
148 "legendBorder" => "0xffffff",
149 "legendBg1" => "0xffffff",
150 "legendBg2" => "0xffffff",
151 "legendText" => "0x444444",
152 "legendColorKeyBorder" => "0x777777",
153 "scrollBar" => "0xcccccc",
154 "scrollBarBorder" => "0xeeeeee",
155 "scrollBarTrack" => "0xeeeeee",
156 "scrollBarTrackBorder" => "0xcccccc",
160 * Colors used in pie charts
164 protected $pieChartColors = array(
165 "docBorder" => "0xffffff",
166 "docBg1" => "0xffffff",
167 "docBg2" => "0xffffff",
168 "title" => "0x333333",
169 "subtitle" => "0x666666",
170 "misc" => "0x999999",
171 "altBorder" => "0xffffff",
172 "altBg" => "0xffffff",
173 "altText" => "0x666666",
174 "graphText" => "0x33485c",
175 "graphTextShadow" => "0xf9f9f9",
176 "pieBorder" => "0xffffff",
177 "pieBorderHilite" => "0x333333",
178 "legendBorder" => "0xffffff",
179 "legendBg1" => "0xffffff",
180 "legendBg2" => "0xffffff",
181 "legendText" => "0x444444",
182 "legendColorKeyBorder" => "0x777777",
183 "scrollBar" => "0xdfdfdf",
184 "scrollBarBorder" => "0xfafafa",
185 "scrollBarTrack" => "0xeeeeee",
186 "scrollBarTrackBorder" => "0xcccccc",
190 * Does this theme support group tabs
198 * Cache built of all css files locations
202 private $_cssCache = array();
205 * Cache built of all image files locations
209 private $_imageCache = array();
212 * Cache built of all javascript files locations
216 private $_jsCache = array();
219 * Cache built of all template files locations
223 private $_templateCache = array();
226 * Cache built of sprite meta data
230 private $_spriteCache = array();
233 * Size of the caches after the are initialized in the constructor
237 private $_initialCacheSize = array(
241 'templateCache' => 0,
246 * Controls whether or not to clear the cache on destroy; defaults to false
248 private $_clearCacheOnDestroy = false;
250 private $imageExtensions = array(
261 * Sets the theme properties from the defaults passed to it, and loads the file path cache from an external cache
263 * @param $defaults string defaults for the current theme
265 public function __construct(
269 // apply parent theme's properties first
270 if ( isset($defaults['parentTheme']) ) {
272 include("themes/{$defaults['parentTheme']}/themedef.php");
273 foreach ( $themedef as $key => $value ) {
274 if ( property_exists(__CLASS__,$key) ) {
275 // For all arrays ( except colors and fonts ) you can just specify the items
276 // to change instead of all of the values
277 if ( is_array($this->$key) && !in_array($key,array('colors','fonts')) )
278 $this->$key = array_merge($this->$key,$value);
280 $this->$key = $value;
284 foreach ( $defaults as $key => $value ) {
285 if ( property_exists(__CLASS__,$key) ) {
286 // For all arrays ( except colors and fonts ) you can just specify the items
287 // to change instead of all of the values
288 if ( is_array($this->$key) && !in_array($key,array('colors','fonts')) )
289 $this->$key = array_merge($this->$key,$value);
291 $this->$key = $value;
294 if ( !inDeveloperMode() ) {
295 if ( sugar_is_file($cachedfile = sugar_cached($this->getFilePath().'/pathCache.php'))) {
296 $caches = unserialize(file_get_contents($cachedfile));
297 if ( isset($caches['jsCache']) )
298 $this->_jsCache = $caches['jsCache'];
299 if ( isset($caches['cssCache']) )
300 $this->_cssCache = $caches['cssCache'];
301 if ( isset($caches['imageCache']) )
302 $this->_imageCache = $caches['imageCache'];
303 if ( isset($caches['templateCache']) )
304 $this->_templateCache = $caches['templateCache'];
306 $cachedfile = sugar_cached($this->getFilePath().'/spriteCache.php');
307 if(!empty($GLOBALS['sugar_config']['use_sprites']) && sugar_is_file($cachedfile)) {
308 $this->_spriteCache = unserialize(sugar_file_get_contents($cachedfile));
311 $this->_initialCacheSize = array(
312 'jsCache' => count($this->_jsCache),
313 'cssCache' => count($this->_cssCache),
314 'imageCache' => count($this->_imageCache),
315 'templateCache' => count($this->_templateCache),
316 'spriteCache' => count($this->_spriteCache),
321 * This is needed to prevent unserialize vulnerability
323 public function __wakeup()
325 // clean all properties
326 foreach(get_object_vars($this) as $k => $v) {
329 throw new Exception("Not a serializable object");
334 * Here we'll write out the internal file path caches to an external cache of some sort.
336 public function __destruct()
338 // Bug 28309 - Set the current directory to one which we expect it to be (i.e. the root directory of the install
339 set_include_path(realpath(dirname(__FILE__) . '/../..') . PATH_SEPARATOR . get_include_path());
340 chdir(dirname(__FILE__) . '/../..'); // destruct can be called late, and chdir could change
341 $cachedir = sugar_cached($this->getFilePath());
342 sugar_mkdir($cachedir, 0775, true);
343 // clear out the cache on destroy if we are asked to
344 if ( $this->_clearCacheOnDestroy ) {
346 if (is_file("$cachedir/pathCache.php"))
347 unlink("$cachedir/pathCache.php");
348 if (is_file("$cachedir/spriteCache.php"))
349 unlink("$cachedir/spriteCache.php");
352 elseif ( !inDeveloperMode() ) {
353 // only update the caches if they have been changed in this request
354 if ( count($this->_jsCache) != $this->_initialCacheSize['jsCache']
355 || count($this->_cssCache) != $this->_initialCacheSize['cssCache']
356 || count($this->_imageCache) != $this->_initialCacheSize['imageCache']
357 || count($this->_templateCache) != $this->_initialCacheSize['templateCache']
359 sugar_file_put_contents(
360 "$cachedir/pathCache.php",
363 'jsCache' => $this->_jsCache,
364 'cssCache' => $this->_cssCache,
365 'imageCache' => $this->_imageCache,
366 'templateCache' => $this->_templateCache,
372 if ( count($this->_spriteCache) != $this->_initialCacheSize['spriteCache']) {
373 sugar_file_put_contents(
374 "$cachedir/spriteCache.php",
375 serialize($this->_spriteCache)
382 * Specifies what is returned when the object is cast to a string, in this case it will be the
383 * theme directory name.
385 * @return string theme directory name
387 public function __toString()
389 return $this->dirName;
393 * Generic public accessor method for all the properties of the theme ( which are kept protected )
397 public function __get(
401 if ( isset($this->$key) )
405 public function __isset($key){
406 return isset($this->$key);
410 public function clearJSCache()
412 $this->_jsCache = array();
416 * Clears out the caches used for this themes
418 public function clearCache()
420 $this->_clearCacheOnDestroy = true;
424 * Return array of all valid fields that can be specified in the themedef.php file
428 public static function getThemeDefFields()
447 * Returns the file path of the current theme
451 public function getFilePath()
453 return 'themes/'.$this->dirName;
457 * Returns the image path of the current theme
461 public function getImagePath()
463 return $this->getFilePath().'/images';
467 * Returns the css path of the current theme
471 public function getCSSPath()
473 return $this->getFilePath().'/css';
477 * Returns the javascript path of the current theme
481 public function getJSPath()
483 return $this->getFilePath().'/js';
487 * Returns the tpl path of the current theme
491 public function getTemplatePath()
493 return $this->getFilePath().'/tpls';
497 * Returns the file path of the theme defaults
501 public final function getDefaultFilePath()
503 return 'themes/default';
507 * Returns the image path of the theme defaults
511 public final function getDefaultImagePath()
513 return $this->getDefaultFilePath().'/images';
517 * Returns the css path of the theme defaults
521 public final function getDefaultCSSPath()
523 return $this->getDefaultFilePath().'/css';
527 * Returns the template path of the theme defaults
531 public final function getDefaultTemplatePath()
533 return $this->getDefaultFilePath().'/tpls';
537 * Returns the javascript path of the theme defaults
541 public final function getDefaultJSPath()
543 return $this->getDefaultFilePath().'/js';
547 * Returns CSS for the current theme.
549 * @param $color string optional, specifies the css color file to use if the theme supports it; defaults to cookie value or theme default
550 * @param $font string optional, specifies the css font file to use if the theme supports it; defaults to cookie value or theme default
551 * @return string HTML code
553 public function getCSS(
558 // include style.css file
559 $html = '<link rel="stylesheet" type="text/css" href="'.$this->getCSSURL('yui.css').'" />';
560 $html .= '<link rel="stylesheet" type="text/css" href="'.$this->getCSSURL('deprecated.css').'" />';
561 $html .= '<link rel="stylesheet" type="text/css" href="'.$this->getCSSURL('style.css').'" />';
564 if(!empty($GLOBALS['sugar_config']['use_sprites']) && $GLOBALS['sugar_config']['use_sprites']) {
566 // system wide sprites
567 if(file_exists("cache/sprites/default/sprites.css"))
568 $html .= '<link rel="stylesheet" type="text/css" href="cache/sprites/default/sprites.css" />';
570 // theme specific sprites
571 if(file_exists("cache/sprites/{$this->dirName}/sprites.css"))
572 $html .= '<link rel="stylesheet" type="text/css" href="cache/sprites/'.$this->dirName.'/sprites.css" />';
575 if($this->parentTheme && $parent = SugarThemeRegistry::get($this->parentTheme)) {
576 if(file_exists("cache/sprites/{$parent->dirName}/sprites.css"))
577 $html .= '<link rel="stylesheet" type="text/css" href="cache/sprites/'.$parent->dirName.'/sprites.css" />';
580 // repeatable sprites
581 if(file_exists("cache/sprites/Repeatable/sprites.css"))
582 $html .= '<link rel="stylesheet" type="text/css" href="cache/sprites/Repeatable/sprites.css" />';
585 // for BC during upgrade
586 if ( !empty($this->colors) ) {
587 if ( isset($_SESSION['authenticated_user_theme_color']) && in_array($_SESSION['authenticated_user_theme_color'], $this->colors))
588 $color = $_SESSION['authenticated_user_theme_color'];
590 $color = $this->colors[0];
591 $html .= '<link rel="stylesheet" type="text/css" href="'.$this->getCSSURL('colors.'.$color.'.css').'" id="current_color_style" />';
594 if ( !empty($this->fonts) ) {
595 if ( isset($_SESSION['authenticated_user_theme_font']) && in_array($_SESSION['authenticated_user_theme_font'], $this->fonts))
596 $font = $_SESSION['authenticated_user_theme_font'];
598 $font = $this->fonts[0];
599 $html .= '<link rel="stylesheet" type="text/css" href="'.$this->getCSSURL('fonts.'.$font.'.css').'" id="current_font_style" />';
606 * Returns javascript for the current theme
608 * @return string HTML code
610 public function getJS()
612 $styleJS = $this->getJSURL('style.js');
614 <script type="text/javascript" src="$styleJS"></script>
619 * Returns the path for the tpl file in the current theme. If not found in the current theme, will revert
620 * to looking in the base theme.
622 * @param string $templateName tpl file name
623 * @return string path of tpl file to include
625 public function getTemplate(
629 if ( isset($this->_templateCache[$templateName]) )
630 return $this->_templateCache[$templateName];
633 if (sugar_is_file('custom/'.$this->getTemplatePath().'/'.$templateName))
634 $templatePath = 'custom/'.$this->getTemplatePath().'/'.$templateName;
635 elseif (sugar_is_file($this->getTemplatePath().'/'.$templateName))
636 $templatePath = $this->getTemplatePath().'/'.$templateName;
637 elseif (isset($this->parentTheme)
638 && SugarThemeRegistry::get($this->parentTheme) instanceOf SugarTheme
639 && ($filename = SugarThemeRegistry::get($this->parentTheme)->getTemplate($templateName)) != '')
640 $templatePath = $filename;
641 elseif (sugar_is_file('custom/'.$this->getDefaultTemplatePath().'/'.$templateName))
642 $templatePath = 'custom/'.$this->getDefaultTemplatePath().'/'.$templateName;
643 elseif (sugar_is_file($this->getDefaultTemplatePath().'/'.$templateName))
644 $templatePath = $this->getDefaultTemplatePath().'/'.$templateName;
646 $GLOBALS['log']->warn("Template $templateName not found");
650 $this->_imageCache[$templateName] = $templatePath;
652 return $templatePath;
656 * Returns an image tag for the given image.
658 * @param string $image image name
659 * @param string $other_attributes optional, other attributes to add to the image tag, not cached
660 * @param string $width optional, defaults to the actual image's width
661 * @param string $height optional, defaults to the actual image's height
662 * @param string $ext optional, image extension (TODO can we depricate this one ?)
663 * @param string $alt optional, only used when image contains something useful, i.e. "Sally's profile pic"
664 * @return string HTML image tag or sprite
666 public function getImage(
668 $other_attributes = '',
676 static $cached_results = array();
678 // trap depricated use of image extension
680 $imageNameExp = explode('.',$imageName);
681 if(count($imageNameExp) == 1)
682 $imageName .= '.gif';
687 // trap alt attributes in other_attributes
688 if(preg_match('/alt=["\']([^\'"]+)["\']/i', $other_attributes))
689 $GLOBALS['log']->debug("Sprites: alt attribute detected for $imageName");
691 // sprite handler, makes use of own caching mechanism
692 if(!empty($GLOBALS['sugar_config']['use_sprites']) && $GLOBALS['sugar_config']['use_sprites']) {
693 // get sprite metadata
694 if($sp = $this->getSpriteMeta($imageName)) {
695 // requested size should match
696 if( (!is_null($width) && $sp['width'] == $width) || (is_null($width)) &&
697 (!is_null($height) && $sp['height'] == $height) || (is_null($height)) )
699 if($sprite = $this->getSprite($sp['class'], $other_attributes, $alt))
706 if(empty($cached_results[$imageName])) {
707 $imageURL = $this->getImageURL($imageName,false);
708 if ( empty($imageURL) )
710 $cached_results[$imageName] = '<img src="'.getJSPath($imageURL).'" ';
713 $attr_width = (is_null($width)) ? "" : "width=\"$width\"";
714 $attr_height = (is_null($height)) ? "" : "height=\"$height\"";
715 return $cached_results[$imageName] . " $attr_width $attr_height $other_attributes alt=\"$alt\" />";
719 * Returns sprite meta data
721 * @param string $imageName Image filename including extension
722 * @return array Sprite meta data
724 public function getSpriteMeta($imageName) {
727 if(isset($this->_spriteCache[$imageName]))
728 return $this->_spriteCache[$imageName];
730 // sprite keys are base on imageURL
731 $imageURL = $this->getImageURL($imageName,false);
732 if(empty($imageURL)) {
733 $this->_spriteCache[$imageName] = false;
737 // load meta data, includes default images
738 require_once("include/SugarTheme/SugarSprites.php");
739 $meta = SugarSprites::getInstance();
741 // add current theme dir
742 $meta->loadSpriteMeta($this->dirName);
743 // add parent theme dir
744 if($this->parentTheme && $parent = SugarThemeRegistry::get($this->parentTheme)) {
745 $meta->loadSpriteMeta($parent->dirName);
749 if(isset($meta->sprites[$imageURL])) {
750 $this->_spriteCache[$imageName] = $meta->sprites[$imageURL];
751 // add imageURL to cache
752 //$this->_spriteCache[$imageName]['imageURL'] = $imageURL;
754 $this->_spriteCache[$imageName] = false;
755 $GLOBALS['log']->debug("Sprites: miss for $imageURL");
757 return $this->_spriteCache[$imageName];
761 * Returns sprite HTML span tag
763 * @param string class The md5 id used in the CSS sprites class
764 * @param string attr optional, list of additional html attributes
765 * @param string title optional, the title (equivalent to alt on img)
766 * @return string HTML span tag
768 public function getSprite($class, $attr, $title) {
770 // handle multiple class tags
771 $class_regex = '/class=["\']([^\'"]+)["\']/i';
772 preg_match($class_regex, $attr, $match);
773 if(isset($match[1])) {
774 $attr = preg_replace($class_regex, 'class="spr_'.$class.' ${1}"', $attr);
778 $attr .= ' class="spr_'.$class.'"';
782 $attr .= ' title="'.$title.'"';
784 // use </span> instead of /> to prevent weird UI results
785 $GLOBALS['log']->debug("Sprites: generated sprite -> $attr");
786 return "<span {$attr}></span>";
790 * Returns a link HTML tag with or without an embedded image
792 public function getLink(
795 $other_attributes = '',
797 $img_other_attributes = '',
801 $img_placement = 'imageonly'
806 $img = $this->getImage($img_name, $img_other_attributes, $img_width, $img_height, null, $img_alt);
808 $GLOBALS['log']->debug('Sprites: unknown image getLink');
811 switch($img_placement) {
812 case 'left': $inner_html = $img.$title; break;
813 case 'right': $inner_html = $title.$img; break;
814 default: $inner_html = $img; break;
817 $inner_html = $title;
820 return '<a href="'.$url.'" title="'.$title.'" '.$other_attributes.'>'.$inner_html.'</a>';
825 * Returns the URL for an image in the current theme. If not found in the current theme, will revert
826 * to looking in the base theme.
827 * @param string $imageName image file name
828 * @param bool $addJSPath call getJSPath() with the results to add some unique image tracking support
829 * @return string path to image
831 public function getImageURL(
835 if ( isset($this->_imageCache[$imageName]) ) {
837 return getJSPath($this->_imageCache[$imageName]);
839 return $this->_imageCache[$imageName];
842 if (($filename = $this->_getImageFileName('custom/'.$this->getImagePath().'/'.$imageName)) != '')
843 $imagePath = $filename;
844 elseif (($filename = $this->_getImageFileName($this->getImagePath().'/'.$imageName)) != '')
845 $imagePath = $filename;
846 elseif (isset($this->parentTheme)
847 && SugarThemeRegistry::get($this->parentTheme) instanceOf SugarTheme
848 && ($filename = SugarThemeRegistry::get($this->parentTheme)->getImageURL($imageName,false)) != '')
849 $imagePath = $filename;
850 elseif (($filename = $this->_getImageFileName('custom/'.$this->getDefaultImagePath().'/'.$imageName)) != '')
851 $imagePath = $filename;
852 elseif (($filename = $this->_getImageFileName($this->getDefaultImagePath().'/'.$imageName)) != '')
853 $imagePath = $filename;
854 elseif (($filename = $this->_getImageFileName('include/images/'.$imageName)) != '')
855 $imagePath = $filename;
857 $GLOBALS['log']->warn("Image $imageName not found");
861 $this->_imageCache[$imageName] = $imagePath;
864 return getJSPath($imagePath);
870 * Checks for an image using all of the accepted image extensions
872 * @param string $imageName image file name
873 * @return string path to image
875 protected function _getImageFileName(
879 // return now if the extension matches that of which we are looking for
880 if ( sugar_is_file($imageName) )
882 $pathParts = pathinfo($imageName);
883 foreach ( $this->imageExtensions as $extension )
884 if ( isset($pathParts['extension']) )
885 if ( ( $extension != $pathParts['extension'] )
886 && sugar_is_file($pathParts['dirname'].'/'.$pathParts['filename'].'.'.$extension) )
887 return $pathParts['dirname'].'/'.$pathParts['filename'].'.'.$extension;
893 * Returns the URL for the css file in the current theme. If not found in the current theme, will revert
894 * to looking in the base theme.
896 * @param string $cssFileName css file name
897 * @param bool $returnURL if true, returns URL with unique image mark, otherwise returns path to the file
898 * @return string path of css file to include
900 public function getCSSURL($cssFileName, $returnURL = true)
902 if ( isset($this->_cssCache[$cssFileName]) && sugar_is_file(sugar_cached($this->_cssCache[$cssFileName])) ) {
904 return getJSPath("cache/".$this->_cssCache[$cssFileName]);
906 return sugar_cached($this->_cssCache[$cssFileName]);
909 $cssFileContents = '';
910 $defaultFileName = $this->getDefaultCSSPath().'/'.$cssFileName;
911 $fullFileName = $this->getCSSPath().'/'.$cssFileName;
912 if (isset($this->parentTheme)
913 && SugarThemeRegistry::get($this->parentTheme) instanceOf SugarTheme
914 && ($filename = SugarThemeRegistry::get($this->parentTheme)->getCSSURL($cssFileName,false)) != '')
915 $cssFileContents .= file_get_contents($filename);
917 if (sugar_is_file($defaultFileName))
918 $cssFileContents .= file_get_contents($defaultFileName);
919 if (sugar_is_file('custom/'.$defaultFileName))
920 $cssFileContents .= file_get_contents('custom/'.$defaultFileName);
922 if (sugar_is_file($fullFileName)) {
923 $cssFileContents .= file_get_contents($fullFileName);
925 if (sugar_is_file('custom/'.$fullFileName)) {
926 $cssFileContents .= file_get_contents('custom/'.$fullFileName);
928 if (empty($cssFileContents)) {
929 $GLOBALS['log']->warn("CSS File $cssFileName not found");
933 // fix any image references that may be defined in css files
934 $cssFileContents = str_ireplace("entryPoint=getImage&",
935 "entryPoint=getImage&themeName={$this->dirName}&",
938 // create the cached file location
939 $cssFilePath = create_cache_directory($fullFileName);
941 // if this is the style.css file, prepend the base.css and calendar-win2k-cold-1.css
942 // files before the theme styles
943 if ( $cssFileName == 'style.css' && !isset($this->parentTheme) ) {
944 if ( inDeveloperMode() )
945 $cssFileContents = file_get_contents('include/javascript/yui/build/base/base.css') . $cssFileContents;
947 $cssFileContents = file_get_contents('include/javascript/yui/build/base/base-min.css') . $cssFileContents;
951 if ( !inDeveloperMode() && !sugar_is_file($cssFilePath) ) {
952 $cssFileContents = cssmin::minify($cssFileContents);
955 // now write the css to cache
956 sugar_file_put_contents($cssFilePath,$cssFileContents);
958 $this->_cssCache[$cssFileName] = $fullFileName;
961 return getJSPath("cache/".$fullFileName);
963 return sugar_cached($fullFileName);
967 * Returns the URL for an image in the current theme. If not found in the current theme, will revert
968 * to looking in the base theme.
970 * @param string $jsFileName js file name
971 * @param bool $returnURL if true, returns URL with unique image mark, otherwise returns path to the file
972 * @return string path to js file
974 public function getJSURL($jsFileName, $returnURL = true)
976 if ( isset($this->_jsCache[$jsFileName]) && sugar_is_file(sugar_cached($this->_jsCache[$jsFileName])) ) {
978 return getJSPath("cache/".$this->_jsCache[$jsFileName]);
980 return sugar_cached($this->_jsCache[$jsFileName]);
983 $jsFileContents = '';
984 $fullFileName = $this->getJSPath().'/'.$jsFileName;
985 $defaultFileName = $this->getDefaultJSPath().'/'.$jsFileName;
986 if (isset($this->parentTheme)
987 && SugarThemeRegistry::get($this->parentTheme) instanceOf SugarTheme
988 && ($filename = SugarThemeRegistry::get($this->parentTheme)->getJSURL($jsFileName,false)) != '') {
989 $jsFileContents .= file_get_contents($filename);
991 if (sugar_is_file($defaultFileName))
992 $jsFileContents .= file_get_contents($defaultFileName);
993 if (sugar_is_file('custom/'.$defaultFileName))
994 $jsFileContents .= file_get_contents('custom/'.$defaultFileName);
996 if (sugar_is_file($fullFileName))
997 $jsFileContents .= file_get_contents($fullFileName);
998 if (sugar_is_file('custom/'.$fullFileName))
999 $jsFileContents .= file_get_contents('custom/'.$fullFileName);
1000 if (empty($jsFileContents)) {
1001 $GLOBALS['log']->warn("Javascript File $jsFileName not found");
1005 // create the cached file location
1006 $jsFilePath = create_cache_directory($fullFileName);
1009 if ( !inDeveloperMode()&& !sugar_is_file(str_replace('.js','-min.js',$jsFilePath)) ) {
1010 $jsFileContents = JSMin::minify($jsFileContents);
1011 $jsFilePath = str_replace('.js','-min.js',$jsFilePath);
1012 $fullFileName = str_replace('.js','-min.js',$fullFileName);
1015 // now write the js to cache
1016 sugar_file_put_contents($jsFilePath,$jsFileContents);
1018 $this->_jsCache[$jsFileName] = $fullFileName;
1021 return getJSPath("cache/".$fullFileName);
1023 return sugar_cached($fullFileName);
1027 * Returns an array of all of the images available for the current theme
1031 public function getAllImages()
1033 // first, lets get all the paths of where to look
1034 $pathsToSearch = array($this->getImagePath());
1036 while (isset($theme->parentTheme) && SugarThemeRegistry::get($theme->parentTheme) instanceOf SugarTheme ) {
1037 $theme = SugarThemeRegistry::get($theme->parentTheme);
1038 $pathsToSearch[] = $theme->getImagePath();
1040 $pathsToSearch[] = $this->getDefaultImagePath();
1042 // now build the array
1043 $imageArray = array();
1044 foreach ( $pathsToSearch as $path )
1046 if (!sugar_is_dir($path)) $path = "custom/$path";
1047 if (sugar_is_dir($path) && is_readable($path) && $dir = opendir($path)) {
1048 while (($file = readdir($dir)) !== false) {
1056 if ( !isset($imageArray[$file]) )
1057 $imageArray[$file] = $this->getImageURL($file,false);
1071 * Registry for all the current classes in the system
1073 class SugarThemeRegistry
1076 * Array of all themes and thier object
1080 private static $_themes = array();
1083 * Name of the current theme; corresponds to an index key in SugarThemeRegistry::$_themes
1087 private static $_currentTheme;
1090 * Disable the constructor since this will be a singleton
1092 private function __construct() {}
1095 * Adds a new theme to the registry
1097 * @param $themedef array
1099 public static function add(
1103 // make sure the we know the sugar version
1104 if ( !isset($GLOBALS['sugar_version']) ) {
1105 include('sugar_version.php');
1106 $GLOBALS['sugar_version'] = $sugar_version;
1109 // Assume theme is designed for 5.5.x if not specified otherwise
1110 if ( !isset($themedef['version']) )
1111 $themedef['version']['regex_matches'] = array('5\.5\.*');
1113 // Check to see if theme is valid for this version of Sugar; return false if not
1114 $version_ok = false;
1115 if( isset($themedef['version']['exact_matches']) ){
1116 $matches_empty = false;
1117 foreach( $themedef['version']['exact_matches'] as $match ){
1118 if( $match == $GLOBALS['sugar_version'] ){
1123 if( !$version_ok && isset($themedef['version']['regex_matches']) ){
1124 $matches_empty = false;
1125 foreach( $themedef['version']['regex_matches'] as $match ){
1126 if( preg_match( "/$match/", $GLOBALS['sugar_version'] ) ){
1134 $theme = new SugarTheme($themedef);
1135 self::$_themes[$theme->dirName] = $theme;
1139 * Removes a new theme from the registry
1141 * @param $themeName string
1143 public static function remove(
1147 if ( self::exists($themeName) )
1148 unset(self::$_themes[$themeName]);
1152 * Returns a theme object in the registry specified by the given $themeName
1154 * @param $themeName string
1156 public static function get(
1160 if ( isset(self::$_themes[$themeName]) )
1161 return self::$_themes[$themeName];
1165 * Returns the current theme object
1167 * @return SugarTheme object
1169 public static function current()
1171 if ( !isset(self::$_currentTheme) )
1172 self::buildRegistry();
1174 return self::$_themes[self::$_currentTheme];
1178 * Returns the default theme object
1180 * @return SugarTheme object
1182 public static function getDefault()
1184 if ( !isset(self::$_currentTheme) )
1185 self::buildRegistry();
1187 if ( isset($GLOBALS['sugar_config']['default_theme']) && self::exists($GLOBALS['sugar_config']['default_theme']) ) {
1188 return self::get($GLOBALS['sugar_config']['default_theme']);
1191 return self::get(array_pop(array_keys(self::availableThemes())));
1195 * Returns true if a theme object specified by the given $themeName exists in the registry
1197 * @param $themeName string
1200 public static function exists(
1204 return (self::get($themeName) !== null);
1208 * Sets the given $themeName to be the current theme
1210 * @param $themeName string
1212 public static function set(
1216 if ( !self::exists($themeName) )
1219 self::$_currentTheme = $themeName;
1221 // set some of the expected globals
1222 $GLOBALS['barChartColors'] = self::current()->barChartColors;
1223 $GLOBALS['pieChartColors'] = self::current()->pieChartColors;
1228 * Builds the theme registry
1230 public static function buildRegistry()
1232 self::$_themes = array();
1233 $dirs = array("themes/","custom/themes/");
1235 // check for a default themedef file
1236 $themedefDefault = array();
1237 if ( sugar_is_file("custom/themes/default/themedef.php") ) {
1238 $themedef = array();
1239 require("custom/themes/default/themedef.php");
1240 $themedefDefault = $themedef;
1243 foreach ($dirs as $dirPath ) {
1244 if (sugar_is_dir('./'.$dirPath) && is_readable('./'.$dirPath) && $dir = opendir('./'.$dirPath)) {
1245 while (($file = readdir($dir)) !== false) {
1251 || $file == "default"
1252 || !sugar_is_dir("./$dirPath".$file)
1253 || !sugar_is_file("./{$dirPath}{$file}/themedef.php")
1256 $themedef = array();
1257 require("./{$dirPath}{$file}/themedef.php");
1258 $themedef = array_merge($themedef,$themedefDefault);
1259 $themedef['dirName'] = $file;
1260 // check for theme already existing in the registry
1261 // if so, then it will override the current one
1262 if ( self::exists($themedef['dirName']) ) {
1263 $existingTheme = self::get($themedef['dirName']);
1264 foreach ( SugarTheme::getThemeDefFields() as $field )
1265 if ( !isset($themedef[$field]) )
1266 $themedef[$field] = $existingTheme->$field;
1267 self::remove($themedef['dirName']);
1269 if ( isset($themedef['name']) ) {
1270 self::add($themedef);
1276 // default to setting the default theme as the current theme
1277 if ( !isset($GLOBALS['sugar_config']['default_theme']) || !self::set($GLOBALS['sugar_config']['default_theme']) ) {
1278 if ( count(self::availableThemes()) == 0 )
1280 sugar_die('No valid themes are found on this instance');
1282 self::set(self::getDefaultThemeKey());
1289 * getDefaultThemeKey
1291 * This function returns the default theme key. It takes into account string casing issues that may arise
1292 * from upgrades. It attempts to look for the Sugar theme and if not found, defaults to return the name of the last theme
1293 * in the array of available themes loaded.
1295 * @return $defaultThemeKey String value of the default theme key to use
1297 private static function getDefaultThemeKey()
1299 $availableThemes = self::availableThemes();
1300 foreach($availableThemes as $key=>$theme)
1302 if(strtolower($key) == 'sugar')
1308 return array_pop(array_keys($availableThemes));
1313 * Returns an array of available themes. Designed to be absorbed into get_select_options_with_id()
1317 public static function availableThemes()
1319 $themelist = array();
1320 $disabledThemes = array();
1321 if ( isset($GLOBALS['sugar_config']['disabled_themes']) )
1322 $disabledThemes = explode(',',$GLOBALS['sugar_config']['disabled_themes']);
1324 foreach ( self::$_themes as $themename => $themeobject ) {
1325 if ( in_array($themename,$disabledThemes) )
1327 $themelist[$themeobject->dirName] = $themeobject->name;
1329 asort($themelist, SORT_STRING);
1334 * Returns an array of un-available themes. Designed used with the theme selector in the admin panel
1338 public static function unAvailableThemes()
1340 $themelist = array();
1341 $disabledThemes = array();
1342 if ( isset($GLOBALS['sugar_config']['disabled_themes']) )
1343 $disabledThemes = explode(',',$GLOBALS['sugar_config']['disabled_themes']);
1345 foreach ( self::$_themes as $themename => $themeobject ) {
1346 if ( in_array($themename,$disabledThemes) )
1347 $themelist[$themeobject->dirName] = $themeobject->name;
1354 * Returns an array of all themes found in the current installation
1358 public static function allThemes()
1360 $themelist = array();
1362 foreach ( self::$_themes as $themename => $themeobject )
1363 $themelist[$themeobject->dirName] = $themeobject->name;
1369 * Clears out the cached path locations for all themes
1371 public static function clearAllCaches()
1373 foreach ( self::$_themes as $themeobject ) {
1374 $themeobject->clearCache();