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

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

use new update pattern

File size: 6.9 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();
[25979]14  private $data_url_params = array();
[25617]15
[25619]16  /** @var array $user */
17  private $user = array();
18
[25618]19  /**
20   * Constructor, load $data from session
21   */
[25617]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      );
[25979]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);
[25617]42  }
43
[25618]44  /**
45   * @return bool
46   */
[25617]47  public function is_admin()
48  {
49    return $this->is_admin;
50  }
51
[25618]52  /**
53   * @return array
54   */
[25617]55  public function get_data()
56  {
57    return $this->data;
58  }
59
[25618]60  /**
[25619]61   * @return array
62   */
63  public function get_user()
64  {
65    return $this->user;
66  }
67
68  /**
[25618]69   * Save $data in session
70   */
[25617]71  private function save()
72  {
73    pwg_set_session_var('multiview', $this->data);
74  }
75
[25618]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   */
[25617]82  public function get_clean_url($with_amp=false)
83  {
84    if (script_basename() == 'picture')
85    {
[25979]86      $url = duplicate_picture_url(array(), $this->data_url_params);
[25617]87    }
88    else if (script_basename() == 'index')
89    {
[25979]90      $url = duplicate_index_url(array(), $this->data_url_params);
[25617]91    }
92    else
93    {
[25979]94      $url = get_query_string_diff($this->data_url_params);
[25617]95    }
96
97    if ($with_amp)
98    {
99      $url.= strpos($url, '?')!==false ? '&' : '?';
100    }
101
102    return $url;
103  }
104
[25618]105  /**
106   * Triggered on "user_init", change current view depending of URL params.
107   */
[25617]108  public function user_init()
109  {
[25619]110    global $user, $conf;
111
[25617]112    $this->is_admin = is_admin();
113
[25619]114    $this->user = array(
115      'id' => $user['id'],
116      'username' => $user['username'],
[25979]117      'language' => $user['language'],
118      'theme' => $user['theme'],
[25619]119      );
120
[25791]121    // inactive on ws.php to allow AJAX admin tasks
122    if ($this->is_admin && script_basename() != 'ws')
[25617]123    {
[25619]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      }
[25617]136
137      // view_as
[25979]138      if (!defined('IN_ADMIN'))
[25617]139      {
[25979]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        }
[25617]153      }
154
155      // theme
[25655]156      if (isset($_GET['ato_theme']))
[25617]157      {
[25655]158        $this->data['theme'] = $_GET['ato_theme'];
[25617]159      }
[25619]160      $user['theme'] = $this->data['theme'];
[25617]161
162      // lang
[25655]163      if (isset($_GET['ato_lang']))
[25617]164      {
[25655]165        $this->data['lang'] = $_GET['ato_lang'];
[25617]166      }
[25619]167      $user['language'] = $this->data['lang'];
[25617]168
169      // show_queries
[25655]170      if (isset($_GET['ato_show_queries']))
[25617]171      {
[25655]172        $this->data['show_queries'] = (bool)$_GET['ato_show_queries'];
[25617]173      }
174      $conf['show_queries'] = $this->data['show_queries'];
175
176      // debug_l10n
[25655]177      if (isset($_GET['ato_debug_l10n']))
[25617]178      {
[25655]179        $this->data['debug_l10n'] = (bool)$_GET['ato_debug_l10n'];
[25617]180      }
181      $conf['debug_l10n'] = $this->data['debug_l10n'];
182
183      // debug_template
[25655]184      if (isset($_GET['ato_debug_template']))
[25617]185      {
[25655]186        $this->data['debug_template'] = (bool)$_GET['ato_debug_template'];
[25617]187      }
188      $conf['debug_template'] = $this->data['debug_template'];
189
190      // template_combine_files
[25655]191      if (isset($_GET['ato_template_combine_files']))
[25617]192      {
[25655]193        $this->data['template_combine_files'] = (bool)$_GET['ato_template_combine_files'];
[25617]194      }
195      $conf['template_combine_files'] = $this->data['template_combine_files'];
196
197      // no_history
[25655]198      if (isset($_GET['ato_no_history']))
[25617]199      {
[25655]200        $this->data['no_history'] = (bool)$_GET['ato_no_history'];
[25617]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
[25618]211  /**
[25979]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  /**
[25618]227   * Triggered on "init", in order to clean template files (not initialized on "user_init")
228   */
[25617]229  public function init()
230  {
[25791]231    if ($this->is_admin)
[25617]232    {
[25791]233      if (isset($_GET['ato_purge_template']))
234      {
235        global $template;
236        $template->delete_compiled_templates();
237        FileCombiner::clear_combined_files();
238      }
[25617]239    }
240  }
[25915]241
[25817]242  /**
[25818]243   * Mark browser session cache for deletion
244   */
245  public static function invalidate_cache()
246  {
247    global $conf;
[28692]248    conf_update_param('multiview_invalidate_cache', true, true);
[25818]249  }
[25915]250
[25818]251  /**
[25817]252   * Register custom API methods
253   */
254  public static function register_ws($arr)
255  {
256    $service = &$arr[0];
[25915]257
[25817]258    $service->addMethod(
259      'multiView.getData',
260      array('MultiView', 'ws_get_data'),
261      array(),
262      'AdminTools private method.',
263      null,
264      array('admin_only' => true, 'hidden' => true)
265      );
266  }
[25915]267
[25817]268  /**
269   * API method
270   * Return full list of users, themes and languages
271   */
272  public static function ws_get_data($params)
273  {
274    global $conf;
[25915]275
[25817]276    // get users
277    $query = '
278SELECT
279  '.$conf['user_fields']['id'].' AS id,
280  '.$conf['user_fields']['username'].' AS username
281FROM '.USERS_TABLE.'
282  ORDER BY CONVERT('.$conf['user_fields']['username'].', CHAR)
283;';
284    $out['users'] = array_from_query($query);
285
286    // get themes
287    include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php');
288    $themes = new themes();
289    foreach (array_keys($themes->db_themes_by_id) as $theme)
290    {
291      if (!empty($theme))
292      {
293        $out['themes'][] = $theme;
294      }
295    }
296
297    // get languages
298    foreach (get_languages() as $code => $name)
299    {
300      $out['languages'][] = array(
301        'id' => $code,
302        'name' => $name,
303        );
304    }
[25915]305
[25818]306    conf_delete_param('multiview_invalidate_cache');
[25915]307
[25817]308    return $out;
309  }
[25617]310}
Note: See TracBrowser for help on using the repository browser.