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

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

better control over session cache for multiview data

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