Dotclear

source: plugins/themeEditor/class.themeEditor.php @ 2663:8dd9be9b33c1

Revision 2663:8dd9be9b33c1, 8.9 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add reset button in theme editor (available only on template file and if there is a model, from parent or from template set), addresses #1703

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcThemeEditor
15{
16     protected $core;
17
18     protected $user_theme;
19     protected $parent_theme;
20     protected $tplset_theme;
21
22     public $tpl = array();
23     public $css = array();
24     public $js  = array();
25     public $po  = array();
26
27     public function __construct($core)
28     {
29          $this->core =& $core;
30          $this->default_theme = path::real($this->core->blog->themes_path.'/default');
31          $this->user_theme = path::real($this->core->blog->themes_path.'/'.$this->core->blog->settings->system->theme);
32          $this->tplset_theme = DC_ROOT.'/inc/public/default-templates/'.DC_DEFAULT_TPLSET;
33          if (null !== $this->core->themes) {
34               $parent_theme = $this->core->themes->moduleInfo($this->core->blog->settings->system->theme,'parent');
35               if ($parent_theme) {
36                    $this->parent_theme = path::real($this->core->blog->themes_path.'/'.$parent_theme);
37               }
38               $tplset = $this->core->themes->moduleInfo($this->core->blog->settings->system->theme,'tplset');
39               if ($tplset) {
40                    $this->tplset_theme = DC_ROOT.'/inc/public/default-templates/'.$tplset;
41               }
42          }
43          $this->findTemplates();
44          $this->findStyles();
45          $this->findScripts();
46          $this->findLocales();
47     }
48
49     public function filesList($type,$item='%1$s',$split=true)
50     {
51          $files = $this->getFilesFromType($type);
52
53          if (empty($files)) {
54               return '<p>'.__('No file').'</p>';
55          }
56
57          $list = '';
58          if ($split) {
59               $list_theme = '';   // Files from current theme
60               $list_parent = '';  // Files from parent of current theme
61               $list_tpl = '';          // Files from template set used by current theme
62               foreach ($files as $k => $v)
63               {
64                    if (strpos($v,$this->user_theme) === 0) {
65                         $li = sprintf('<li class="default-file">%s</li>',$item);
66                         $list_theme .= sprintf($li,$k,html::escapeHTML($k));
67                    } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) {
68                         $li = sprintf('<li class="parent-file">%s</li>',$item);
69                         $list_parent .= sprintf($li,$k,html::escapeHTML($k));
70                    } else {
71                         $li = sprintf('<li>%s</li>',$item);
72                         $list_tpl .= sprintf($li,$k,html::escapeHTML($k));
73                    }
74               }
75               $list .= ($list_theme != '' ? sprintf('<li class="group-file">'.__('From theme:').'<ul>%s</ul></li>',$list_theme) : '');
76               $list .= ($list_parent != '' ? sprintf('<li class="group-file">'.__('From parent:').'<ul>%s</ul></li>',$list_parent) : '');
77               $list .= ($list_tpl != '' ? sprintf('<li class="group-file">'.__('From template set:').'<ul>%s</ul></li>',$list_tpl) : '');
78          } else {
79               foreach ($files as $k => $v)
80               {
81                    if (strpos($v,$this->user_theme) === 0) {
82                         $li = sprintf('<li class="default-file">%s</li>',$item);
83                    } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) {
84                         $li = sprintf('<li class="parent-file">%s</li>',$item);
85                    } else {
86                         $li = sprintf('<li>%s</li>',$item);
87                    }
88                    $list .= sprintf($li,$k,html::escapeHTML($k));
89               }
90          }
91
92          return sprintf('<ul>%s</ul>',$list);
93     }
94
95     public function getFileContent($type,$f)
96     {
97          $files = $this->getFilesFromType($type);
98
99          if (!isset($files[$f])) {
100               throw new Exception(__('File does not exist.'));
101          }
102
103          $F = $files[$f];
104          if (!is_readable($F)) {
105               throw new Exception(sprintf(__('File %s is not readable'),$f));
106          }
107
108          return array(
109               'c' => file_get_contents($F),
110               'w' => $this->getDestinationFile($type,$f) !== false,
111               'type' => $type,
112               'f' => $f
113          );
114     }
115
116     public function writeFile($type,$f,$content)
117     {
118          $files = $this->getFilesFromType($type);
119
120          if (!isset($files[$f])) {
121               throw new Exception(__('File does not exist.'));
122          }
123
124          try
125          {
126               $dest = $this->getDestinationFile($type,$f);
127
128               if ($dest == false) {
129                    throw new Exception();
130               }
131
132               if ($type == 'tpl' && !is_dir(dirname($dest))) {
133                    files::makeDir(dirname($dest));
134               }
135
136               if ($type == 'po' && !is_dir(dirname($dest))) {
137                    files::makeDir(dirname($dest));
138               }
139
140               $fp = @fopen($dest,'wb');
141               if (!$fp) {
142                    throw new Exception('tocatch');
143               }
144
145               $content = preg_replace('/(\r?\n)/m',"\n",$content);
146               $content = preg_replace('/\r/m',"\n",$content);
147
148               fwrite($fp,$content);
149               fclose($fp);
150
151               # Updating inner files list
152               $this->updateFileInList($type,$f,$dest);
153          }
154          catch (Exception $e)
155          {
156               throw new Exception(sprintf(__('Unable to write file %s. Please check your theme files and folders permissions.'),$f));
157          }
158     }
159
160     public function deletableFile($type,$f)
161     {
162          if ($type != 'tpl') {
163               // Only tpl files may be deleted
164               return false;
165          }
166
167          $files = $this->getFilesFromType($type);
168          if (isset($files[$f])) {
169               $dest = $this->getDestinationFile($type,$f);
170               if ($dest) {
171                    if (file_exists($dest) && is_writable($dest)) {
172                         // Is there a model (parent theme or template set) ?
173                         if (isset($this->tpl_model[$f])) {
174                              return true;
175                         }
176                    }
177               }
178          }
179          return false;
180     }
181
182     public function deleteFile($type,$f)
183     {
184          if ($type != 'tpl') {
185               // Only tpl files may be deleted
186               return;
187          }
188
189          $files = $this->getFilesFromType($type);
190          if (!isset($files[$f])) {
191               throw new Exception(__('File does not exist.'));
192          }
193
194          try
195          {
196               $dest = $this->getDestinationFile($type,$f);
197               if ($dest) {
198                    // File exists and may be deleted
199                    unlink($dest);
200
201                    // Updating template files list
202                    $this->findTemplates();
203               }
204          }
205          catch (Exception $e)
206          {
207               throw new Exception(sprintf(__('Unable to delete file %s. Please check your theme files and folders permissions.'),$f));
208          }
209     }
210
211     protected function getDestinationFile($type,$f)
212     {
213          if ($type == 'tpl') {
214               $dest = $this->user_theme.'/tpl/'.$f;
215          } elseif ($type == 'po') {
216               $dest = $this->user_theme.'/locales/'.$f;
217          } else {
218               $dest = $this->user_theme.'/'.$f;
219          }
220
221          if (file_exists($dest) && is_writable($dest)) {
222               return $dest;
223          }
224
225          if ($type == 'tpl' && !is_dir(dirname($dest))) {
226               if (is_writable($this->user_theme)) {
227                    return $dest;
228               }
229          }
230
231          if ($type == 'po' && !is_dir(dirname($dest))) {
232               if (is_writable($this->user_theme)) {
233                    return $dest;
234               }
235          }
236
237          if (is_writable(dirname($dest))) {
238               return $dest;
239          }
240
241          return false;
242     }
243
244     protected function getFilesFromType($type)
245     {
246          switch ($type)
247          {
248               case 'tpl':
249                    return $this->tpl;
250               case 'css':
251                    return $this->css;
252               case 'js':
253                    return $this->js;
254               case 'po':
255                    return $this->po;
256               default:
257                    return array();
258          }
259     }
260
261     protected function updateFileInList($type,$f,$file)
262     {
263          switch ($type)
264          {
265               case 'tpl':
266                    $list =& $this->tpl;
267                    break;
268               case 'css':
269                    $list =& $this->css;
270                    break;
271               case 'js':
272                    $list =& $this->js;
273                    break;
274               case 'po':
275                    $list =& $this->po;
276                    break;
277               default:
278                    return;
279          }
280
281          $list[$f] = $file;
282     }
283
284     protected function findTemplates()
285     {
286          $this->tpl = array_merge(
287               $this->getFilesInDir($this->tplset_theme),
288               $this->getFilesInDir($this->parent_theme.'/tpl')
289               );
290          $this->tpl_model = $this->tpl;
291
292          $this->tpl = array_merge($this->tpl,$this->getFilesInDir($this->user_theme.'/tpl'));
293
294          # Then we look in 'default-templates' plugins directory
295          $plugins = $this->core->plugins->getModules();
296          foreach ($plugins as $p) {
297               $this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl);
298               $this->tpl_model = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl_model);
299          }
300
301          uksort($this->tpl,array($this,'sortFilesHelper'));
302     }
303
304     protected function findStyles()
305     {
306          $this->css = $this->getFilesInDir($this->user_theme,'css');
307          $this->css= array_merge($this->css,$this->getFilesInDir($this->user_theme.'/style','css','style/'));
308     }
309
310     protected function findScripts()
311     {
312          $this->js = $this->getFilesInDir($this->user_theme,'js');
313          $this->js = array_merge($this->js,$this->getFilesInDir($this->user_theme.'/js','js','js/'));
314     }
315
316     protected function findLocales()
317     {
318          $langs = l10n::getISOcodes(1,1);
319          foreach ($langs as $k => $v) {
320               if ($this->parent_theme) {
321                    $this->po = array_merge($this->po,$this->getFilesInDir($this->parent_theme.'/locales/'.$v,'po',$v.'/'));
322               }
323               $this->po = array_merge($this->po,$this->getFilesInDir($this->user_theme.'/locales/'.$v,'po',$v.'/'));
324          }
325     }
326
327     protected function getFilesInDir($dir,$ext=null,$prefix='',$model=null)
328     {
329          $dir = path::real($dir);
330          if (!$dir || !is_dir($dir) || !is_readable($dir)) {
331               return array();
332          }
333
334          $d = dir($dir);
335          $res = array();
336          while (($f = $d->read()) !== false)
337          {
338               if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f) && (!$ext || preg_match('/\.'.preg_quote($ext).'$/i',$f))) {
339                    if (!$model || preg_match('/^'.preg_quote($model).'$/i', $f)) {
340                         $res[$prefix.$f] = $dir.'/'.$f;
341                    }
342               }
343          }
344
345          return $res;
346     }
347
348     protected function sortFilesHelper($a,$b)
349     {
350          if ($a == $b) {
351               return 0;
352          }
353
354          $ext_a = files::getExtension($a);
355          $ext_b = files::getExtension($b);
356
357          return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b);
358     }
359}
Note: See TracBrowser for help on using the repository browser.

Sites map