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

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

various minor issues

File size: 6.2 KB
RevLine 
[25617]1<?php
[25655]2defined('ADMINTOOLS_PATH') or die('Hacking attempt!');
[25617]3
[25618]4/**
5 * Class managing multi views system
6 */
[25617]7class MultiView
8{
[25618]9  /** @var bool $is_admin */
[25619]10  private $is_admin = false;
[25617]11
[25618]12  /** @var array $data */
[25617]13  private $data = array();
14
[25619]15  /** @var array $user */
16  private $user = array();
17
[25618]18  /**
19   * Constructor, load $data from session
20   */
[25617]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
[25618]40  /**
41   * @return bool
42   */
[25617]43  public function is_admin()
44  {
45    return $this->is_admin;
46  }
47
[25618]48  /**
49   * @return array
50   */
[25617]51  public function get_data()
52  {
53    return $this->data;
54  }
55
[25618]56  /**
[25619]57   * @return array
58   */
59  public function get_user()
60  {
61    return $this->user;
62  }
63
64  /**
[25618]65   * Save $data in session
66   */
[25617]67  private function save()
68  {
69    pwg_set_session_var('multiview', $this->data);
70  }
71
[25618]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   */
[25617]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
[25618]101  /**
102   * Triggered on "user_init", change current view depending of URL params.
103   */
[25617]104  public function user_init()
105  {
[25619]106    global $user, $conf;
107
[25617]108    $this->is_admin = is_admin();
109
[25619]110    $this->user = array(
111      'id' => $user['id'],
112      'username' => $user['username'],
113      );
114
[25791]115    // inactive on ws.php to allow AJAX admin tasks
116    if ($this->is_admin && script_basename() != 'ws')
[25617]117    {
[25619]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      }
[25617]130
131      // view_as
[25655]132      if (isset($_GET['ato_view_as']))
[25617]133      {
[25655]134        $this->data['view_as'] = (int)$_GET['ato_view_as'];
[25617]135      }
[25619]136      if ($this->data['view_as'] != $user['id'])
[25617]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
[25655]144      if (isset($_GET['ato_theme']))
[25617]145      {
[25655]146        $this->data['theme'] = $_GET['ato_theme'];
[25617]147      }
[25619]148      $user['theme'] = $this->data['theme'];
[25617]149
150      // lang
[25655]151      if (isset($_GET['ato_lang']))
[25617]152      {
[25655]153        $this->data['lang'] = $_GET['ato_lang'];
[25617]154      }
[25619]155      $user['language'] = $this->data['lang'];
[25617]156
157      // show_queries
[25655]158      if (isset($_GET['ato_show_queries']))
[25617]159      {
[25655]160        $this->data['show_queries'] = (bool)$_GET['ato_show_queries'];
[25617]161      }
162      $conf['show_queries'] = $this->data['show_queries'];
163
164      // debug_l10n
[25655]165      if (isset($_GET['ato_debug_l10n']))
[25617]166      {
[25655]167        $this->data['debug_l10n'] = (bool)$_GET['ato_debug_l10n'];
[25617]168      }
169      $conf['debug_l10n'] = $this->data['debug_l10n'];
170
171      // debug_template
[25655]172      if (isset($_GET['ato_debug_template']))
[25617]173      {
[25655]174        $this->data['debug_template'] = (bool)$_GET['ato_debug_template'];
[25617]175      }
176      $conf['debug_template'] = $this->data['debug_template'];
177
178      // template_combine_files
[25655]179      if (isset($_GET['ato_template_combine_files']))
[25617]180      {
[25655]181        $this->data['template_combine_files'] = (bool)$_GET['ato_template_combine_files'];
[25617]182      }
183      $conf['template_combine_files'] = $this->data['template_combine_files'];
184
185      // no_history
[25655]186      if (isset($_GET['ato_no_history']))
[25617]187      {
[25655]188        $this->data['no_history'] = (bool)$_GET['ato_no_history'];
[25617]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
[25618]199  /**
200   * Triggered on "init", in order to clean template files (not initialized on "user_init")
201   */
[25617]202  public function init()
203  {
[25791]204    if ($this->is_admin)
[25617]205    {
[25791]206      if (isset($_GET['ato_purge_template']))
207      {
208        global $template;
209        $template->delete_compiled_templates();
210        FileCombiner::clear_combined_files();
211      }
[25617]212    }
213  }
[25915]214
[25817]215  /**
[25818]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  }
[25915]224
[25818]225  /**
[25817]226   * Register custom API methods
227   */
228  public static function register_ws($arr)
229  {
230    $service = &$arr[0];
[25915]231
[25817]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  }
[25915]241
[25817]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;
[25915]249
[25817]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    }
[25915]279
[25818]280    conf_delete_param('multiview_invalidate_cache');
[25915]281
[25817]282    return $out;
283  }
[25617]284}
Note: See TracBrowser for help on using the repository browser.