source: extensions/AdminTools/include/MultiView.class.php @ 25979

Last change on this file since 25979 was 25979, checked in by mistic100, 10 years ago

remove useless SQL queries, don't change user on admin pages, fix theme and language overwrites, fix toolbar overlap, colorbox max width

File size: 7.0 KB
Line 
1<?php
2defined('ADMINTOOLS_PATH') or die('Hacking attempt!');
3
4/**
5 * Class managing multi views system
6 */
7class MultiView
8{
9  /** @var bool $is_admin */
10  private $is_admin = false;
11
12  /** @var array $data */
13  private $data = array();
14  private $data_url_params = array();
15
16  /** @var array $user */
17  private $user = array();
18
19  /**
20   * Constructor, load $data from session
21   */
22  function __construct()
23  {
24    global $conf;
25
26    $this->data = array_merge(
27      array(
28        'view_as' => 0,
29        'theme' => '',
30        'lang' => '',
31        'show_queries' => $conf['show_queries'],
32        'debug_l10n' => $conf['debug_l10n'],
33        'debug_template' => $conf['debug_template'],
34        'template_combine_files' => $conf['template_combine_files'],
35        'no_history' => false,
36        ),
37      pwg_get_session_var('multiview', array())
38      );
39
40    $this->data_url_params = array_keys($this->data);
41    $this->data_url_params = array_map(create_function('$d', 'return "ato_".$d;'), $this->data_url_params);
42  }
43
44  /**
45   * @return bool
46   */
47  public function is_admin()
48  {
49    return $this->is_admin;
50  }
51
52  /**
53   * @return array
54   */
55  public function get_data()
56  {
57    return $this->data;
58  }
59
60  /**
61   * @return array
62   */
63  public function get_user()
64  {
65    return $this->user;
66  }
67
68  /**
69   * Save $data in session
70   */
71  private function save()
72  {
73    pwg_set_session_var('multiview', $this->data);
74  }
75
76  /**
77   * Returns the current url minus MultiView params
78   *
79   * @param bool $with_amp - adds ? or & at the end of the url
80   * @return string
81   */
82  public function get_clean_url($with_amp=false)
83  {
84    if (script_basename() == 'picture')
85    {
86      $url = duplicate_picture_url(array(), $this->data_url_params);
87    }
88    else if (script_basename() == 'index')
89    {
90      $url = duplicate_index_url(array(), $this->data_url_params);
91    }
92    else
93    {
94      $url = get_query_string_diff($this->data_url_params);
95    }
96
97    if ($with_amp)
98    {
99      $url.= strpos($url, '?')!==false ? '&' : '?';
100    }
101
102    return $url;
103  }
104
105  /**
106   * Triggered on "user_init", change current view depending of URL params.
107   */
108  public function user_init()
109  {
110    global $user, $conf;
111
112    $this->is_admin = is_admin();
113
114    $this->user = array(
115      'id' => $user['id'],
116      'username' => $user['username'],
117      'language' => $user['language'],
118      'theme' => $user['theme'],
119      );
120
121    // inactive on ws.php to allow AJAX admin tasks
122    if ($this->is_admin && script_basename() != 'ws')
123    {
124      if ($this->data['view_as'] == 0)
125      {
126        $this->data['view_as'] = $user['id'];
127      }
128      if (empty($this->data['lang']))
129      {
130        $this->data['lang'] = $user['language'];
131      }
132      if (empty($this->data['theme']))
133      {
134        $this->data['theme'] = $user['theme'];
135      }
136
137      // view_as
138      if (!defined('IN_ADMIN'))
139      {
140        if (isset($_GET['ato_view_as']))
141        {
142          $this->data['view_as'] = (int)$_GET['ato_view_as'];
143        }
144        if ($this->data['view_as'] != $user['id'])
145        {
146          $user = build_user($this->data['view_as'], true);
147          if (isset($_GET['ato_view_as']))
148          {
149            $this->data['theme'] = $user['theme'];
150            $this->data['lang'] = $user['language'];
151          }
152        }
153      }
154
155      // theme
156      if (isset($_GET['ato_theme']))
157      {
158        $this->data['theme'] = $_GET['ato_theme'];
159      }
160      $user['theme'] = $this->data['theme'];
161
162      // lang
163      if (isset($_GET['ato_lang']))
164      {
165        $this->data['lang'] = $_GET['ato_lang'];
166      }
167      $user['language'] = $this->data['lang'];
168
169      // show_queries
170      if (isset($_GET['ato_show_queries']))
171      {
172        $this->data['show_queries'] = (bool)$_GET['ato_show_queries'];
173      }
174      $conf['show_queries'] = $this->data['show_queries'];
175
176      // debug_l10n
177      if (isset($_GET['ato_debug_l10n']))
178      {
179        $this->data['debug_l10n'] = (bool)$_GET['ato_debug_l10n'];
180      }
181      $conf['debug_l10n'] = $this->data['debug_l10n'];
182
183      // debug_template
184      if (isset($_GET['ato_debug_template']))
185      {
186        $this->data['debug_template'] = (bool)$_GET['ato_debug_template'];
187      }
188      $conf['debug_template'] = $this->data['debug_template'];
189
190      // template_combine_files
191      if (isset($_GET['ato_template_combine_files']))
192      {
193        $this->data['template_combine_files'] = (bool)$_GET['ato_template_combine_files'];
194      }
195      $conf['template_combine_files'] = $this->data['template_combine_files'];
196
197      // no_history
198      if (isset($_GET['ato_no_history']))
199      {
200        $this->data['no_history'] = (bool)$_GET['ato_no_history'];
201      }
202      if ($this->data['no_history'])
203      {
204        add_event_handler('pwg_log_allowed', create_function('', 'return false;'));
205      }
206
207      $this->save();
208    }
209  }
210
211  /**
212   * Returns the language of the current user if different from the current language
213   * false otherwise
214   */
215  function get_user_language()
216  {
217    if (isset($this->user['language']) && isset($this->data['lang'])
218        && $this->user['language'] != $this->data['lang']
219      )
220    {
221      return $this->user['language'];
222    }
223    return false;
224  }
225
226  /**
227   * Triggered on "init", in order to clean template files (not initialized on "user_init")
228   */
229  public function init()
230  {
231    if ($this->is_admin)
232    {
233      if (isset($_GET['ato_purge_template']))
234      {
235        global $template;
236        $template->delete_compiled_templates();
237        FileCombiner::clear_combined_files();
238      }
239    }
240  }
241
242  /**
243   * Mark browser session cache for deletion
244   */
245  public static function invalidate_cache()
246  {
247    global $conf;
248    conf_update_param('multiview_invalidate_cache', true);
249    $conf['multiview_invalidate_cache'] = true;
250  }
251
252  /**
253   * Register custom API methods
254   */
255  public static function register_ws($arr)
256  {
257    $service = &$arr[0];
258
259    $service->addMethod(
260      'multiView.getData',
261      array('MultiView', 'ws_get_data'),
262      array(),
263      'AdminTools private method.',
264      null,
265      array('admin_only' => true, 'hidden' => true)
266      );
267  }
268
269  /**
270   * API method
271   * Return full list of users, themes and languages
272   */
273  public static function ws_get_data($params)
274  {
275    global $conf;
276
277    // get users
278    $query = '
279SELECT
280  '.$conf['user_fields']['id'].' AS id,
281  '.$conf['user_fields']['username'].' AS username
282FROM '.USERS_TABLE.'
283  ORDER BY CONVERT('.$conf['user_fields']['username'].', CHAR)
284;';
285    $out['users'] = array_from_query($query);
286
287    // get themes
288    include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php');
289    $themes = new themes();
290    foreach (array_keys($themes->db_themes_by_id) as $theme)
291    {
292      if (!empty($theme))
293      {
294        $out['themes'][] = $theme;
295      }
296    }
297
298    // get languages
299    foreach (get_languages() as $code => $name)
300    {
301      $out['languages'][] = array(
302        'id' => $code,
303        'name' => $name,
304        );
305    }
306
307    conf_delete_param('multiview_invalidate_cache');
308
309    return $out;
310  }
311}
Note: See TracBrowser for help on using the repository browser.