theme.php (10195B)
1 <?php 2 class ControllerDesignTheme extends Controller { 3 public function index() { 4 $this->load->language('design/theme'); 5 6 $this->document->setTitle($this->language->get('heading_title')); 7 8 $data['breadcrumbs'] = array(); 9 10 $data['breadcrumbs'][] = array( 11 'text' => $this->language->get('text_home'), 12 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 13 ); 14 15 $data['breadcrumbs'][] = array( 16 'text' => $this->language->get('heading_title'), 17 'href' => $this->url->link('design/theme', 'user_token=' . $this->session->data['user_token'], true) 18 ); 19 20 $data['user_token'] = $this->session->data['user_token']; 21 22 $data['stores'] = array(); 23 24 $this->load->model('setting/store'); 25 26 $results = $this->model_setting_store->getStores(); 27 28 foreach ($results as $result) { 29 $data['stores'][] = array( 30 'store_id' => $result['store_id'], 31 'name' => $result['name'] 32 ); 33 } 34 35 $data['header'] = $this->load->controller('common/header'); 36 $data['column_left'] = $this->load->controller('common/column_left'); 37 $data['footer'] = $this->load->controller('common/footer'); 38 39 $this->response->setOutput($this->load->view('design/theme', $data)); 40 } 41 42 public function history() { 43 $this->load->language('design/theme'); 44 45 if (isset($this->request->get['page'])) { 46 $page = $this->request->get['page']; 47 } else { 48 $page = 1; 49 } 50 51 $data['histories'] = array(); 52 53 $this->load->model('design/theme'); 54 $this->load->model('setting/store'); 55 56 $history_total = $this->model_design_theme->getTotalThemes(); 57 58 $results = $this->model_design_theme->getThemes(($page - 1) * 10, 10); 59 60 foreach ($results as $result) { 61 $store_info = $this->model_setting_store->getStore($result['store_id']); 62 63 if ($store_info) { 64 $store = $store_info['name']; 65 } else { 66 $store = ''; 67 } 68 69 $data['histories'][] = array( 70 'store_id' => $result['store_id'], 71 'store' => ($result['store_id'] ? $store : $this->language->get('text_default')), 72 'route' => $result['route'], 73 'theme' => $result['theme'], 74 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])), 75 'edit' => $this->url->link('design/theme/template', 'user_token=' . $this->session->data['user_token'], true), 76 'delete' => $this->url->link('design/theme/delete', 'user_token=' . $this->session->data['user_token'] . '&theme_id=' . $result['theme_id'], true) 77 ); 78 } 79 80 $pagination = new Pagination(); 81 $pagination->total = $history_total; 82 $pagination->page = $page; 83 $pagination->limit = 10; 84 $pagination->url = $this->url->link('design/theme/history', 'user_token=' . $this->session->data['user_token'] . '&page={page}', true); 85 86 $data['pagination'] = $pagination->render(); 87 88 $data['results'] = sprintf($this->language->get('text_pagination'), ($history_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($history_total - 10)) ? $history_total : ((($page - 1) * 10) + 10), $history_total, ceil($history_total / 10)); 89 90 $this->response->setOutput($this->load->view('design/theme_history', $data)); 91 } 92 93 public function path() { 94 $this->load->language('design/theme'); 95 96 $json = array(); 97 98 if (isset($this->request->get['store_id'])) { 99 $store_id = $this->request->get['store_id']; 100 } else { 101 $store_id = 0; 102 } 103 104 $this->load->model('setting/setting'); 105 106 $theme = $this->model_setting_setting->getSettingValue('config_theme', $store_id); 107 108 // This is only here for compatibility with old themes. 109 if ($theme == 'theme_default') { 110 $theme = $this->model_setting_setting->getSettingValue('theme_default_directory', $store_id); 111 } 112 113 if (isset($this->request->get['path'])) { 114 $path = $this->request->get['path']; 115 } else { 116 $path = ''; 117 } 118 119 if (substr(str_replace('\\', '/', realpath(DIR_CATALOG . 'view/theme/default/template/' . $path)), 0, strlen(DIR_CATALOG . 'view')) == DIR_CATALOG . 'view') { 120 $path_data = array(); 121 122 // We grab the files from the default theme directory first as the custom themes drops back to the default theme if selected theme files can not be found. 123 $files = glob(rtrim(DIR_CATALOG . 'view/theme/{default,' . $theme . '}/template/' . $path, '/') . '/*', GLOB_BRACE); 124 125 if ($files) { 126 foreach($files as $file) { 127 if (!in_array(basename($file), $path_data)) { 128 if (is_dir($file)) { 129 $json['directory'][] = array( 130 'name' => basename($file), 131 'path' => trim($path . '/' . basename($file), '/') 132 ); 133 } 134 135 if (is_file($file)) { 136 $json['file'][] = array( 137 'name' => basename($file), 138 'path' => trim($path . '/' . basename($file), '/') 139 ); 140 } 141 142 $path_data[] = basename($file); 143 } 144 } 145 } 146 } 147 148 if (!empty($this->request->get['path'])) { 149 $json['back'] = array( 150 'name' => $this->language->get('button_back'), 151 'path' => urlencode(substr($path, 0, strrpos($path, '/'))), 152 ); 153 } 154 155 $this->response->addHeader('Content-Type: application/json'); 156 $this->response->setOutput(json_encode($json)); 157 } 158 159 public function template() { 160 $this->load->language('design/theme'); 161 162 $json = array(); 163 164 if (isset($this->request->get['store_id'])) { 165 $store_id = $this->request->get['store_id']; 166 } else { 167 $store_id = 0; 168 } 169 170 $this->load->model('setting/setting'); 171 172 $theme = $this->model_setting_setting->getSettingValue('config_theme', $store_id); 173 174 // This is only here for compatibility with old themes. 175 if ($theme == 'theme_default') { 176 $theme = $this->model_setting_setting->getSettingValue('theme_default_directory', $store_id); 177 } 178 179 if (isset($this->request->get['path'])) { 180 $path = $this->request->get['path']; 181 } else { 182 $path = ''; 183 } 184 185 $this->load->model('design/theme'); 186 187 $theme_info = $this->model_design_theme->getTheme($store_id, $theme, $path); 188 189 if ($theme_info) { 190 $json['code'] = html_entity_decode($theme_info['code']); 191 } elseif (is_file(DIR_CATALOG . 'view/theme/' . $theme . '/template/' . $path) && (substr(str_replace('\\', '/', realpath(DIR_CATALOG . 'view/theme/' . $theme . '/template/' . $path)), 0, strlen(DIR_CATALOG . 'view')) == DIR_CATALOG . 'view')) { 192 $json['code'] = file_get_contents(DIR_CATALOG . 'view/theme/' . $theme . '/template/' . $path); 193 } elseif (is_file(DIR_CATALOG . 'view/theme/default/template/' . $path) && (substr(str_replace('\\', '/', realpath(DIR_CATALOG . 'view/theme/default/template/' . $path)), 0, strlen(DIR_CATALOG . 'view')) == DIR_CATALOG . 'view')) { 194 $json['code'] = file_get_contents(DIR_CATALOG . 'view/theme/default/template/' . $path); 195 } 196 197 $this->response->addHeader('Content-Type: application/json'); 198 $this->response->setOutput(json_encode($json)); 199 } 200 201 public function save() { 202 $this->load->language('design/theme'); 203 204 $json = array(); 205 206 if (isset($this->request->get['store_id'])) { 207 $store_id = $this->request->get['store_id']; 208 } else { 209 $store_id = 0; 210 } 211 212 $this->load->model('setting/setting'); 213 214 $theme = $this->model_setting_setting->getSettingValue('config_theme', $store_id); 215 216 // This is only here for compatibility with old themes. 217 if ($theme == 'theme_default') { 218 $theme = $this->model_setting_setting->getSettingValue('theme_default_directory', $store_id); 219 } 220 221 if (isset($this->request->get['path'])) { 222 $path = $this->request->get['path']; 223 } else { 224 $path = ''; 225 } 226 227 // Check user has permission 228 if (!$this->user->hasPermission('modify', 'design/theme')) { 229 $json['error'] = $this->language->get('error_permission'); 230 } 231 232 if (substr($path, -5) != '.twig') { 233 $json['error'] = $this->language->get('error_twig'); 234 } 235 236 if (!$json) { 237 $this->load->model('design/theme'); 238 239 $pos = strpos($path, '.'); 240 241 $this->model_design_theme->editTheme($store_id, $theme, ($pos !== false) ? substr($path, 0, $pos) : $path, $this->request->post['code']); 242 243 $json['success'] = $this->language->get('text_success'); 244 } 245 246 $this->response->addHeader('Content-Type: application/json'); 247 $this->response->setOutput(json_encode($json)); 248 } 249 250 public function reset() { 251 $this->load->language('design/theme'); 252 253 $json = array(); 254 255 if (isset($this->request->get['store_id'])) { 256 $store_id = $this->request->get['store_id']; 257 } else { 258 $store_id = 0; 259 } 260 261 $this->load->model('setting/setting'); 262 263 $theme = $this->model_setting_setting->getSettingValue('config_theme', $store_id); 264 265 // This is only here for compatibility with old themes. 266 if ($theme == 'theme_default') { 267 $theme = $this->model_setting_setting->getSettingValue('theme_default_directory', $store_id); 268 } 269 270 if (isset($this->request->get['path'])) { 271 $path = $this->request->get['path']; 272 } else { 273 $path = ''; 274 } 275 276 if (is_file(DIR_CATALOG . 'view/theme/' . $theme . '/template/' . $path) && (substr(str_replace('\\', '/', realpath(DIR_CATALOG . 'view/theme/' . $theme . '/template/' . $path)), 0, strlen(DIR_CATALOG . 'view')) == DIR_CATALOG . 'view')) { 277 $json['code'] = file_get_contents(DIR_CATALOG . 'view/theme/' . $theme . '/template/' . $path); 278 } 279 280 $this->response->addHeader('Content-Type: application/json'); 281 $this->response->setOutput(json_encode($json)); 282 } 283 284 public function delete() { 285 $this->load->language('design/theme'); 286 287 $json = array(); 288 289 if (isset($this->request->get['theme_id'])) { 290 $theme_id = $this->request->get['theme_id']; 291 } else { 292 $theme_id = 0; 293 } 294 295 // Check user has permission 296 if (!$this->user->hasPermission('modify', 'design/theme')) { 297 $json['error'] = $this->language->get('error_permission'); 298 } 299 300 if (!$json) { 301 $this->load->model('design/theme'); 302 303 $this->model_design_theme->deleteTheme($theme_id); 304 305 $json['success'] = $this->language->get('text_success'); 306 } 307 308 $this->response->addHeader('Content-Type: application/json'); 309 $this->response->setOutput(json_encode($json)); 310 } 311 }