Dotclear

source: plugins/themeEditor/class.themeEditor.php @ 2601:065a2aab5b84

Revision 2601:065a2aab5b84, 6.6 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Cope with tplsets in theme editor plugin, no more use the tpls of default theme.

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/'.'mustek';
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')
50     {
51          $files = $this->getFilesFromType($type);
52
53          if (empty($files)) {
54               return '<p>'.__('No file').'</p>';
55          }
56
57          $list = '';
58          foreach ($files as $k => $v)
59          {
60               if (strpos($v,$this->user_theme) === 0) {
61                    $li = sprintf('<li class="default-file">%s</li>',$item);
62               } elseif ($this->parent_theme && strpos($v,$this->parent_theme) === 0) {
63                    $li = sprintf('<li class="parent-file">%s</li>',$item);
64               } else {
65                    $li = sprintf('<li>%s</li>',$item);
66               }
67               $list .= sprintf($li,$k,html::escapeHTML($k));
68          }
69
70          return sprintf('<ul>%s</ul>',$list);
71     }
72
73     public function getFileContent($type,$f)
74     {
75          $files = $this->getFilesFromType($type);
76
77          if (!isset($files[$f])) {
78               throw new Exception(__('File does not exist.'));
79          }
80
81          $F = $files[$f];
82          if (!is_readable($F)) {
83               throw new Exception(sprintf(__('File %s is not readable'),$f));
84          }
85
86          return array(
87               'c' => file_get_contents($F),
88               'w' => $this->getDestinationFile($type,$f) !== false,
89               'type' => $type,
90               'f' => $f
91          );
92     }
93
94     public function writeFile($type,$f,$content)
95     {
96          $files = $this->getFilesFromType($type);
97
98          if (!isset($files[$f])) {
99               throw new Exception(__('File does not exist.'));
100          }
101
102          try
103          {
104               $dest = $this->getDestinationFile($type,$f);
105
106               if ($dest == false) {
107                    throw new Exception();
108               }
109
110               if ($type == 'tpl' && !is_dir(dirname($dest))) {
111                    files::makeDir(dirname($dest));
112               }
113
114               if ($type == 'po' && !is_dir(dirname($dest))) {
115                    files::makeDir(dirname($dest));
116               }
117
118               $fp = @fopen($dest,'wb');
119               if (!$fp) {
120                    throw new Exception('tocatch');
121               }
122
123               $content = preg_replace('/(\r?\n)/m',"\n",$content);
124               $content = preg_replace('/\r/m',"\n",$content);
125
126               fwrite($fp,$content);
127               fclose($fp);
128
129               # Updating inner files list
130               $this->updateFileInList($type,$f,$dest);
131          }
132          catch (Exception $e)
133          {
134               throw new Exception(sprintf(__('Unable to write file %s. Please check your theme files and folders permissions.'),$f));
135          }
136     }
137
138     protected function getDestinationFile($type,$f)
139     {
140          if ($type == 'tpl') {
141               $dest = $this->user_theme.'/tpl/'.$f;
142          } elseif ($type == 'po') {
143               $dest = $this->user_theme.'/locales/'.$f;
144          } else {
145               $dest = $this->user_theme.'/'.$f;
146          }
147
148          if (file_exists($dest) && is_writable($dest)) {
149               return $dest;
150          }
151
152          if ($type == 'tpl' && !is_dir(dirname($dest))) {
153               if (is_writable($this->user_theme)) {
154                    return $dest;
155               }
156          }
157
158          if ($type == 'po' && !is_dir(dirname($dest))) {
159               if (is_writable($this->user_theme)) {
160                    return $dest;
161               }
162          }
163
164          if (is_writable(dirname($dest))) {
165               return $dest;
166          }
167
168          return false;
169     }
170
171     protected function getFilesFromType($type)
172     {
173          switch ($type)
174          {
175               case 'tpl':
176                    return $this->tpl;
177               case 'css':
178                    return $this->css;
179               case 'js':
180                    return $this->js;
181               case 'po':
182                    return $this->po;
183               default:
184                    return array();
185          }
186     }
187
188     protected function updateFileInList($type,$f,$file)
189     {
190          switch ($type)
191          {
192               case 'tpl':
193                    $list =& $this->tpl;
194                    break;
195               case 'css':
196                    $list =& $this->css;
197                    break;
198               case 'js':
199                    $list =& $this->js;
200                    break;
201               case 'po':
202                    $list =& $this->po;
203                    break;
204               default:
205                    return;
206          }
207
208          $list[$f] = $file;
209     }
210
211     protected function findTemplates()
212     {
213          $this->tpl = array_merge(
214               $this->getFilesInDir($this->tplset_theme),
215               $this->getFilesInDir($this->parent_theme.'/tpl'),
216               $this->getFilesInDir($this->user_theme.'/tpl')
217               );
218
219          # Then we look in 'default-templates' plugins directory
220          $plugins = $this->core->plugins->getModules();
221          foreach ($plugins as $p) {
222               $this->tpl = array_merge($this->getFilesInDir($p['root'].'/default-templates'),$this->tpl);
223          }
224
225          uksort($this->tpl,array($this,'sortFilesHelper'));
226     }
227
228     protected function findStyles()
229     {
230          $this->css = $this->getFilesInDir($this->user_theme,'css');
231          $this->css= array_merge($this->css,$this->getFilesInDir($this->user_theme.'/style','css','style/'));
232     }
233
234     protected function findScripts()
235     {
236          $this->js = $this->getFilesInDir($this->user_theme,'js');
237          $this->js = array_merge($this->js,$this->getFilesInDir($this->user_theme.'/js','js','js/'));
238     }
239
240     protected function findLocales()
241     {
242          $langs = l10n::getISOcodes(1,1);
243          foreach ($langs as $k => $v) {
244               if ($this->parent_theme) {
245                    $this->po = array_merge($this->po,$this->getFilesInDir($this->parent_theme.'/locales/'.$v,'po',$v.'/'));
246               }
247               $this->po = array_merge($this->po,$this->getFilesInDir($this->user_theme.'/locales/'.$v,'po',$v.'/'));
248          }
249     }
250
251     protected function getFilesInDir($dir,$ext=null,$prefix='',$model=null)
252     {
253          $dir = path::real($dir);
254          if (!$dir || !is_dir($dir) || !is_readable($dir)) {
255               return array();
256          }
257
258          $d = dir($dir);
259          $res = array();
260          while (($f = $d->read()) !== false)
261          {
262               if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f) && (!$ext || preg_match('/\.'.preg_quote($ext).'$/i',$f))) {
263                    if (!$model || preg_match('/^'.preg_quote($model).'$/i', $f)) {
264                         $res[$prefix.$f] = $dir.'/'.$f;
265                    }
266               }
267          }
268
269          return $res;
270     }
271
272     protected function sortFilesHelper($a,$b)
273     {
274          if ($a == $b) {
275               return 0;
276          }
277
278          $ext_a = files::getExtension($a);
279          $ext_b = files::getExtension($b);
280
281          return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b);
282     }
283}
Note: See TracBrowser for help on using the repository browser.

Sites map