00001 <?php
00002
00003
00004
00005 class WidgetJCSS implements IWidget {
00006 const META = 256;
00007 const JS = 512;
00008 const CSS = 1024;
00009 const ALL = 1792;
00010
00011
00012
00013
00014
00015
00016 public $page_data;
00017
00018
00019
00020
00021
00022
00023
00024
00025 public static function output($page_data, $policy = self::ALL) {
00026 $w = new WidgetJCSS($page_data);
00027 return $w->render($policy);
00028 }
00029
00030 public function __construct($page_data) {
00031 $this->page_data = $page_data;
00032 }
00033
00034
00035
00036
00037
00038
00039
00040 public function render($policy = self::ALL) {
00041 $this->collect($this->page_data, $policy);
00042 Load::models('jcsscompressedfiles');
00043 if (Common::flag_is_set($policy, self::CSS)) {
00044 $this->preprocess_css($this->page_data);
00045 }
00046 if (Common::flag_is_set($policy, self::JS)) {
00047 $this->preprocess_js($this->page_data);
00048 }
00049 return $this->page_data->head->render($policy);
00050 }
00051
00052
00053
00054
00055
00056
00057 protected function preprocess_css($page_data) {
00058 $page_data->head->css_files = $this->replace_by_compressed(JCSSManager::TYPE_CSS, $page_data->head->css_files);
00059 foreach($page_data->head->conditional_css_files as $browser => $files) {
00060 switch ($browser) {
00061 case HeadData::IE50:
00062 $type = JCSSManager::TYPE_CSS_IE50;
00063 break;
00064 case HeadData::IE55:
00065 $type = JCSSManager::TYPE_CSS_IE55;
00066 break;
00067 case HeadData::IE6:
00068 $type = JCSSManager::TYPE_CSS_IE6;
00069 break;
00070 case HeadData::IE7:
00071 $type = JCSSManager::TYPE_CSS_IE7;
00072 break;
00073 default:
00074 continue;
00075 break;
00076 }
00077 $page_data->head->conditional_css_files[$browser] = $this->replace_by_compressed($type, $files);
00078 }
00079 }
00080
00081
00082
00083
00084
00085
00086 protected function preprocess_js($page_data) {
00087 $page_data->head->js_files = $this->replace_by_compressed(JCSSManager::TYPE_JS, $page_data->head->js_files);
00088 }
00089
00090
00091
00092
00093 protected function replace_by_compressed($type, $arr_files) {
00094 $arr_files = array_unique($arr_files);
00095 if (count($arr_files) == 0 || !Config::get_value(ConfigJCSSManager::USE_COMPRESSED)) {
00096 return $arr_files;
00097 }
00098
00099 $compressed_files = JCSSCompressedFiles::find($type);
00100 $ret = $arr_files;
00101 foreach($compressed_files as $compressed_file) {
00102 $ret = $compressed_file->substitute($ret);
00103 }
00104
00105 return $ret;
00106 }
00107
00108
00109
00110
00111
00112
00113 protected function collect(PageData $page_data, $policy) {
00114 if (Common::flag_is_set($policy, self::JS)) {
00115 $this->collect_js($page_data);
00116 }
00117 if (Common::flag_is_set($policy, self::CSS)) {
00118 $this->collect_css($page_data);
00119 }
00120 }
00121
00122
00123
00124
00125 protected function collect_js(PageData $page_data) {
00126 $js_files = $this->invoke_collect_event(JCSSManager::TYPE_JS);
00127 foreach(array_reverse($js_files) as $f) {
00128 $page_data->head->add_js_file($f, true);
00129 }
00130 }
00131
00132
00133
00134
00135 protected function collect_css(PageData $page_data) {
00136 foreach(JCSSManager::get_css_types() as $type) {
00137 $css_files = $this->invoke_collect_event($type);
00138 $css_files = array_reverse($css_files);
00139 switch($type) {
00140 case JCSSManager::TYPE_CSS:
00141 foreach($css_files as $f) {
00142 $page_data->head->add_css_file($f, true);
00143 }
00144 break;
00145 default:
00146 foreach($css_files as $f) {
00147 $page_data->head->add_conditional_css_file(
00148 $this->translate_conditional_css_type($type),
00149 $f,
00150 true
00151 );
00152 }
00153 break;
00154 }
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 protected function translate_conditional_css_type($type) {
00165 return substr($type, 4);
00166 }
00167
00168
00169
00170
00171
00172
00173 protected function invoke_collect_event($type) {
00174 $files = array();
00175 EventSource::Instance()->invoke_event('jcssmanager_collect', $type, $files);
00176 return $files;
00177 }
00178 }