source: extensions/gally/gally-default/admin/GallyDefault.class.inc.php

Last change on this file was 16016, checked in by grum, 12 years ago

feature:2664- compatibility with Piwigo 2.4

File size: 8.9 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Theme      : Gally/default
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.fr
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10
11  This is the default class for the Gally themes interfaces managment
12
13  --------------------------------------------------------------------------- */
14
15include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
16include_once('Conf.class.inc.php');
17
18load_language('theme.lang', PHPWG_THEMES_PATH.'gally-default/');
19
20class GallyDefault
21{
22  const THEME_VERSION = '1.5.0';
23
24  private $directories=Array(
25    'templates.parent' => '',
26    'templates.local' => '',
27    'conf.parent' => '',
28    'conf.local' => ''
29  );
30  private $pageLink;
31  private $authorizedValues=Array();
32  protected $config;
33  protected $tabsheet;
34  protected $themeProperties = Array(
35      'name' => 'Gally/Default',
36      'version' => self::THEME_VERSION
37    );
38
39
40  /**
41   * function used to activate a theme
42   *
43   * @param String $themeId : theme's id (for example 'gally-lapis-lazulis')
44   * @param String $themeVersion : theme's release (not gally-default release)
45   * @param String $gallyDefaultNeeded : gally-default release needed
46   * @param &Array $errors : array of error messages
47   */
48  static public function activate($themeId, $themeVersion, $gallyDefaultNeeded, $errors)
49  {
50    if(strnatcmp(self::THEME_VERSION, $gallyDefaultNeeded)<0)
51    {
52      if(!is_array($errors))
53      {
54        if($errors!='')
55        {
56          $errors=array($errors);
57        }
58        else
59        {
60          $errors=array();
61        }
62      }
63
64      $errors[]=sprintf(l10n('This theme needs Gally-default theme v%s; actually, Gally-default version is %s.'), $gallyDefaultNeeded, self::THEME_VERSION);
65    }
66    else
67    {
68      Conf::init($themeId);
69    }
70    return($errors);
71  }
72
73  /**
74   * function used to deactivate a theme
75   *
76   */
77  static public function deactivate()
78  {
79  }
80
81  /**
82   *
83   * @param String $themeName : the name of the theme
84   * @param String $templateDirectory : directory for the admin.tpl template
85   *                                    file ; if no directory is given, assume
86   *                                    to use the gally/default admin.tpl file
87   * @param String $confDirectory     : directory for the conf files
88   *                                    if no directory is given, assume to use
89   *                                    the gally/default conf files
90   * @param Boolean $manage           : if true, manage display interface when
91   *                                    the class is instancied
92   */
93  public function __construct($themeName, $templateDirectory="", $confDirectory="", $manage=true)
94  {
95    $this->setDirectoryPrivate('templates.parent', dirname(__FILE__));
96    $this->setDirectoryPrivate('templates.local', $templateDirectory);
97
98
99    $this->setDirectoryPrivate('conf.parent', dirname(dirname(__FILE__)).'/conf');
100    $this->setDirectoryPrivate('conf.local', $confDirectory);
101
102    if($themeName!="")
103    {
104      $this->themeProperties['name']=$themeName;
105    }
106
107    $this->pageLink="admin.php?page=theme&amp;theme=".$_REQUEST['theme'];
108
109    $this->authorizedValues['fGally_tabsheet']=Array('img_interface', 'img_high_res', 'img_other');
110
111    $this->loadConfig();
112    $this->initTabSheet();
113
114    if($manage)
115    {
116      $this->manage();
117    }
118  }
119
120  public function __destruct()
121  {
122    unset($this->tabsheet);
123    unset($this->config);
124    unset($this->directories);
125    unset($this->authorizedValues);
126    unset($this->themeProperties);
127  }
128
129  /**
130   *
131   */
132  public function loadConfig()
133  {
134    $this->config = new Conf();
135    $this->config->setFileName($this->directories['conf.parent']."/default.conf");
136    $this->config->read();
137    $this->config->setFileName($this->directories['conf.local']."/default.conf");
138    $this->config->read(false);
139    $this->config->setFileName(PHPWG_ROOT_PATH.PWG_LOCAL_DIR."themes/".basename(dirname($this->directories['conf.local']))."/conf/local.conf");
140    $this->config->read(false);
141  }
142
143
144  /**
145   *
146   */
147  public function initTabSheet()
148  {
149    $this->tabsheet = new tabsheet();
150    $this->tabsheet->add('img_interface',
151                          l10n('gally_img_interface'),
152                          $this->getAdminThemeLink().'&amp;fGally_tabsheet=img_interface');
153    $this->tabsheet->add('img_high_res',
154                          l10n('gally_img_high_res'),
155                          $this->getAdminThemeLink().'&amp;fGally_tabsheet=img_high_res');
156    $this->tabsheet->add('img_other',
157                          l10n('gally_img_other'),
158                          $this->getAdminThemeLink().'&amp;fGally_tabsheet=img_other');
159  }
160
161  /**
162   * manage the display of the admin theme interface
163   *
164   */
165  public function manage()
166  {
167    global $template;
168
169    $template->set_filenames(array(
170        'theme_admin_content' => $this->getFile('templates', 'admin.tpl')));
171
172    $this->initRequest();
173    $this->manageConfig();
174
175    $this->tabsheet->select($_REQUEST['fGally_tabsheet']);
176    $this->tabsheet->assign();
177
178    $file=$this->getFile('templates', 'gally_'.$_REQUEST['fGally_tabsheet'].'.tpl');
179    if($file!==false)
180    {
181      $template->set_filename('body_page', $file);
182
183      $template->assign('options', $this->config->getConf());
184      $template->assign_var_from_handle('GALLY_BODY_PAGE', 'body_page');
185
186      $template->assign('gally', $this->themeProperties);
187      $template->assign_var_from_handle('ADMIN_CONTENT', 'theme_admin_content');
188    }
189  }
190
191
192  /**
193   * @return String : the url of the theme admin page
194   */
195  public function getAdminThemeLink()
196  {
197    return($this->pageLink);
198  }
199
200  /**
201   *
202   */
203  public function getDirectory($type)
204  {
205    if($type=='templates.local' or $type=='templates.parent' or
206       $type=='conf.local' or $type=='conf.parent')
207    {
208      return($this->directories[$type]);
209    }
210    else
211    {
212      return(false);
213    }
214  }
215
216  /**
217   *
218   */
219  public function setDirectory($type, $directory)
220  {
221    if($type=='templates.local' or $type=='conf.local')
222    {
223      $this->setDirectoryPrivate($type, $directory);
224    }
225    return($this->directories[$type]);
226  }
227
228  /**
229   *
230   */
231  private function setDirectoryPrivate($type, $directory)
232  {
233    if(substr($directory, -1)=="/") $directory=substr($directory, 0, -1);
234
235    if(file_exists($directory)) $this->directories[$type]=$directory;
236
237    return($this->directories[$type]);
238  }
239
240  /**
241   *
242   *
243   */
244  public function getFile($type, $fileName)
245  {
246    if(@file_exists($this->directories[$type.'.local'].'/'.$fileName))
247    {
248      return($this->directories[$type.'.local'].'/'.$fileName);
249    }
250    elseif(@file_exists($this->directories[$type.'.parent'].'/'.$fileName))
251    {
252      return($this->directories[$type.'.parent'].'/'.$fileName);
253    }
254    return(false);
255  }
256
257
258
259  /**
260   * check and initialize all needed properties in the $_REQUEST var
261   */
262  protected function initRequest()
263  {
264    //initialise $REQUEST values if not defined
265    if(!array_key_exists('fGally_tabsheet', $_REQUEST))
266    {
267      $_REQUEST['fGally_tabsheet']='img_interface';
268    }
269
270    if(!$this->isAuthorizedValues('fGally_tabsheet', $_REQUEST['fGally_tabsheet']))
271    {
272      $_REQUEST['fGally_tabsheet']='img_interface';
273    }
274  }
275
276
277  protected function manageConfig()
278  {
279    $config=$this->config->getConf();
280
281    if(isset($_POST['submit_gally']))
282    {
283      foreach($config as $key => $val)
284      {
285        if(isset($_POST['f_'.$key]))
286        {
287          $config[$key] = stripslashes($_REQUEST['f_'.$key]);
288        }
289      }
290      $this->config->setConf($config, false);
291      $this->displayResult(l10n("gally_save_local_conf"), $this->config->write());
292    }
293  }
294
295  protected function displayResult($action_msg, $result)
296  {
297    global $page;
298
299    if($result)
300    {
301      array_push($page['infos'], $action_msg);
302      array_push($page['infos'], l10n("gally_action_ok"));
303    }
304    else
305    {
306      array_push($page['errors'], $action_msg);
307      array_push($page['errors'], l10n("gally_action_ko"));
308    }
309  }
310
311  protected function isAuthorizedValues($key, $value)
312  {
313    if(array_key_exists($key, $this->authorizedValues))
314    {
315      if(in_array($value, $this->authorizedValues[$key]))
316      {
317        return(true);
318      }
319    }
320    return(false);
321  }
322
323  protected function addAuthorizedValue($key, $value)
324  {
325    if(array_key_exists($key, $this->authorizedValues))
326    {
327      if(!in_array($value, $this->authorizedValues[$key]))
328      {
329        $this->authorizedValues[$key][]=$value;
330      }
331    }
332    else
333    {
334      $this->authorizedValues[$key]=Array($value);
335    }
336  }
337
338  protected function removeAuthorizedValue($key, $value)
339  {
340    if(array_key_exists($key, $this->authorizedValues))
341    {
342      $key2=array_search($value, $this->authorizedValues[$key]);
343      if($key2!==false)
344      {
345        unset($this->authorizedValues[$key][$key2]);
346      }
347    }
348  }
349
350}
351
352
353?>
Note: See TracBrowser for help on using the repository browser.