Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

manager.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 14.30 KiB


001  <?php
002  /**
003  *
004  * This file is part of the phpBB Forum Software package.
005  *
006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
007  * @license GNU General Public License, version 2 (GPL-2.0)
008  *
009  * For full copyright and license information, please see
010  * the docs/CREDITS.txt file.
011  *
012  */
013   
014  namespace phpbb\profilefields;
015   
016  /**
017  * Custom Profile Fields
018  */
019  class manager
020  {
021      /**
022      * Auth object
023      * @var \phpbb\auth\auth
024      */
025      protected $auth;
026   
027      /**
028      * Database object
029      * @var \phpbb\db\driver\driver_interface
030      */
031      protected $db;
032   
033      /**
034      * Event dispatcher object
035      * @var \phpbb\event\dispatcher_interface
036      */
037      protected $dispatcher;
038   
039      /**
040      * Request object
041      * @var \phpbb\request\request
042      */
043      protected $request;
044   
045      /**
046      * Template object
047      * @var \phpbb\template\template
048      */
049      protected $template;
050   
051      /**
052      * Service Collection object
053      * @var \phpbb\di\service_collection
054      */
055      protected $type_collection;
056   
057      /**
058      * User object
059      * @var \phpbb\user
060      */
061      protected $user;
062   
063      protected $fields_table;
064   
065      protected $fields_language_table;
066   
067      protected $fields_data_table;
068   
069      protected $profile_cache = array();
070   
071      /**
072      * Construct
073      *
074      * @param    \phpbb\auth\auth            $auth        Auth object
075      * @param    \phpbb\db\driver\driver_interface    $db            Database object
076      * @param    \phpbb\event\dispatcher_interface        $dispatcher    Event dispatcher object
077      * @param    \phpbb\request\request        $request    Request object
078      * @param    \phpbb\template\template    $template    Template object
079      * @param    \phpbb\di\service_collection $type_collection
080      * @param    \phpbb\user                    $user        User object
081      * @param    string                $fields_table
082      * @param    string                $fields_language_table
083      * @param    string                $fields_data_table
084      */
085      public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\di\service_collection $type_collection, \phpbb\user $user, $fields_table, $fields_language_table, $fields_data_table)
086      {
087          $this->auth = $auth;
088          $this->db = $db;
089          $this->dispatcher = $dispatcher;
090          $this->request = $request;
091          $this->template = $template;
092          $this->type_collection = $type_collection;
093          $this->user = $user;
094   
095          $this->fields_table = $fields_table;
096          $this->fields_language_table = $fields_language_table;
097          $this->fields_data_table = $fields_data_table;
098      }
099   
100      /**
101      * Assign editable fields to template, mode can be profile (for profile change) or register (for registration)
102      * Called by ucp_profile and ucp_register
103      */
104      public function generate_profile_fields($mode, $lang_id)
105      {
106          $sql_where = '';
107          switch ($mode)
108          {
109              case 'register':
110                  // If the field is required we show it on the registration page
111                  $sql_where .= ' AND f.field_show_on_reg = 1';
112              break;
113   
114              case 'profile':
115                  // Show hidden fields to moderators/admins
116                  if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_'))
117                  {
118                      $sql_where .= ' AND f.field_show_profile = 1';
119                  }
120              break;
121   
122              default:
123                  trigger_error('Wrong profile mode specified', E_USER_ERROR);
124              break;
125          }
126   
127          $sql = 'SELECT l.*, f.*
128              FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f
129              WHERE f.field_active = 1
130                  $sql_where
131                  AND l.lang_id = " . (int) $lang_id . '
132                  AND l.field_id = f.field_id
133              ORDER BY f.field_order';
134          $result = $this->db->sql_query($sql);
135   
136          while ($row = $this->db->sql_fetchrow($result))
137          {
138              // Return templated field
139              $profile_field = $this->type_collection[$row['field_type']];
140              $tpl_snippet = $profile_field->process_field_row('change', $row);
141   
142              $this->template->assign_block_vars('profile_fields', array(
143                  'LANG_NAME'        => $this->user->lang($row['lang_name']),
144                  'LANG_EXPLAIN'    => $this->user->lang($row['lang_explain']),
145                  'FIELD'            => $tpl_snippet,
146                  'FIELD_ID'        => $profile_field->get_field_ident($row),
147                  'S_REQUIRED'    => ($row['field_required']) ? true : false,
148              ));
149          }
150          $this->db->sql_freeresult($result);
151      }
152   
153      /**
154      * Build profile cache, used for display
155      */
156      protected function build_cache()
157      {
158          $this->profile_cache = array();
159   
160          // Display hidden/no_view fields for admin/moderator
161          $sql = 'SELECT l.*, f.*
162              FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f
163              WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . '
164                  AND f.field_active = 1 ' .
165                  ((!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) ? '    AND f.field_hide = 0 ' : '') . '
166                  AND f.field_no_view = 0
167                  AND l.field_id = f.field_id
168              ORDER BY f.field_order';
169          $result = $this->db->sql_query($sql);
170   
171          while ($row = $this->db->sql_fetchrow($result))
172          {
173              $this->profile_cache[$row['field_ident']] = $row;
174          }
175          $this->db->sql_freeresult($result);
176      }
177   
178      /**
179      * Submit profile field for validation
180      */
181      public function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
182      {
183          $sql_where = '';
184          switch ($mode)
185          {
186              case 'register':
187                  // If the field is required we show it on the registration page
188                  $sql_where .= ' AND f.field_show_on_reg = 1';
189              break;
190   
191              case 'profile':
192                  // Show hidden fields to moderators/admins
193                  if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_'))
194                  {
195                      $sql_where .= ' AND f.field_show_profile = 1';
196                  }
197              break;
198   
199              default:
200                  trigger_error('Wrong profile mode specified', E_USER_ERROR);
201              break;
202          }
203   
204          $sql = 'SELECT l.*, f.*
205              FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f
206              WHERE l.lang_id = ' . (int) $lang_id . "
207                  AND f.field_active = 1
208                  $sql_where
209                  AND l.field_id = f.field_id
210              ORDER BY f.field_order";
211          $result = $this->db->sql_query($sql);
212   
213          while ($row = $this->db->sql_fetchrow($result))
214          {
215              $profile_field = $this->type_collection[$row['field_type']];
216              $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row);
217              $check_value = $cp_data['pf_' . $row['field_ident']];
218   
219              if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false)
220              {
221                  // If the result is not false, it's an error message
222                  $cp_error[] = $cp_result;
223              }
224          }
225          $this->db->sql_freeresult($result);
226      }
227   
228      /**
229      * Update profile field data directly
230      */
231      public function update_profile_field_data($user_id, $cp_data)
232      {
233          if (!sizeof($cp_data))
234          {
235              return;
236          }
237   
238          $sql = 'UPDATE ' . $this->fields_data_table . '
239              SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . '
240              WHERE user_id = ' . (int) $user_id;
241          $this->db->sql_query($sql);
242   
243          if (!$this->db->sql_affectedrows())
244          {
245              $cp_data = $this->build_insert_sql_array($cp_data);
246              $cp_data['user_id'] = (int) $user_id;
247   
248              $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data);
249              $this->db->sql_query($sql);
250          }
251      }
252   
253      /**
254      * Generate the template arrays in order to display the column names
255      *
256      * @param string    $restrict_option    Restrict the published fields to a certain profile field option
257      * @return array        Returns an array with the template variables type, name and explain for the fields to display
258      */
259      public function generate_profile_fields_template_headlines($restrict_option = '')
260      {
261          if (!sizeof($this->profile_cache))
262          {
263              $this->build_cache();
264          }
265   
266          $tpl_fields = array();
267   
268          // Go through the fields in correct order
269          foreach ($this->profile_cache as $field_ident => $field_data)
270          {
271              if ($restrict_option && !$field_data[$restrict_option])
272              {
273                  continue;
274              }
275   
276              $profile_field = $this->type_collection[$field_data['field_type']];
277   
278              $tpl_fields[] = array(
279                  'PROFILE_FIELD_TYPE'    => $field_data['field_type'],
280                  'PROFILE_FIELD_NAME'    => $profile_field->get_field_name($field_data['lang_name']),
281                  'PROFILE_FIELD_EXPLAIN'    => $this->user->lang($field_data['lang_explain']),
282              );
283          }
284   
285          return $tpl_fields;
286      }
287   
288      /**
289      * Grab the user specific profile fields data
290      *
291      * @param    int|array    $user_ids    Single user id or an array of ids
292      * @return array        Users profile fields data
293      */
294      public function grab_profile_fields_data($user_ids = 0)
295      {
296          if (!is_array($user_ids))
297          {
298              $user_ids = array($user_ids);
299          }
300   
301          if (!sizeof($this->profile_cache))
302          {
303              $this->build_cache();
304          }
305   
306          if (!sizeof($user_ids))
307          {
308              return array();
309          }
310   
311          $sql = 'SELECT *
312              FROM ' . $this->fields_data_table . '
313              WHERE ' . $this->db->sql_in_set('user_id', array_map('intval', $user_ids));
314          $result = $this->db->sql_query($sql);
315   
316          $field_data = array();
317          while ($row = $this->db->sql_fetchrow($result))
318          {
319              $field_data[$row['user_id']] = $row;
320          }
321          $this->db->sql_freeresult($result);
322   
323          /**
324          * Event to modify profile fields data retrieved from the database
325          *
326          * @event core.grab_profile_fields_data
327          * @var    array    user_ids        Single user id or an array of ids
328          * @var    array    field_data        Array with profile fields data
329          * @since 3.1.0-b3
330          */
331          $vars = array('user_ids', 'field_data');
332          extract($this->dispatcher->trigger_event('core.grab_profile_fields_data', compact($vars)));
333   
334          $user_fields = array();
335   
336          // Go through the fields in correct order
337          foreach (array_keys($this->profile_cache) as $used_ident)
338          {
339              foreach ($field_data as $user_id => $row)
340              {
341                  $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
342                  $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
343              }
344   
345              foreach ($user_ids as $user_id)
346              {
347                  if (!isset($user_fields[$user_id][$used_ident]) && $this->profile_cache[$used_ident]['field_show_novalue'])
348                  {
349                      $user_fields[$user_id][$used_ident]['value'] = '';
350                      $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
351                  }
352              }
353          }
354   
355          return $user_fields;
356      }
357   
358      /**
359      * Assign the user's profile fields data to the template
360      *
361      * @param array    $profile_row        Array with users profile field data
362      * @param bool    $use_contact_fields    Should we display contact fields as such?
363      *            This requires special treatments (links should not be parsed in the values, and more)
364      * @return array
365      */
366      public function generate_profile_fields_template_data($profile_row, $use_contact_fields = true)
367      {
368          // $profile_row == $user_fields[$row['user_id']];
369          $tpl_fields = array();
370          $tpl_fields['row'] = $tpl_fields['blockrow'] = array();
371   
372          /**
373          * Event to modify data of the generated profile fields, before the template assignment loop
374          *
375          * @event core.generate_profile_fields_template_data_before
376          * @var    array    profile_row        Array with users profile field data
377          * @var    array    tpl_fields        Array with template data fields
378          * @var    bool    use_contact_fields    Should we display contact fields as such?
379          * @since 3.1.0-b3
380          */
381          $vars = array('profile_row', 'tpl_fields', 'use_contact_fields');
382          extract($this->dispatcher->trigger_event('core.generate_profile_fields_template_data_before', compact($vars)));
383   
384          foreach ($profile_row as $ident => $ident_ary)
385          {
386              $profile_field = $this->type_collection[$ident_ary['data']['field_type']];
387              $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']);
388              $value_raw = $profile_field->get_profile_value_raw($ident_ary['value'], $ident_ary['data']);
389   
390              if ($value === null)
391              {
392                  continue;
393              }
394   
395              $field_desc = $contact_url = '';
396              if ($use_contact_fields && $ident_ary['data']['field_is_contact'])
397              {
398                  $value = $profile_field->get_profile_contact_value($ident_ary['value'], $ident_ary['data']);
399                  $field_desc = $this->user->lang($ident_ary['data']['field_contact_desc']);
400                  if (strpos($field_desc, '%s') !== false)
401                  {
402                      $field_desc = sprintf($field_desc, $value);
403                  }
404                  $contact_url = '';
405                  if (strpos($ident_ary['data']['field_contact_url'], '%s') !== false)
406                  {
407                      $contact_url = sprintf($ident_ary['data']['field_contact_url'], $value);
408                  }
409              }
410   
411              $tpl_fields['row'] += array(
412                  'PROFILE_' . strtoupper($ident) . '_IDENT'        => $ident,
413                  'PROFILE_' . strtoupper($ident) . '_VALUE'        => $value,
414                  'PROFILE_' . strtoupper($ident) . '_VALUE_RAW'    => $value_raw,
415                  'PROFILE_' . strtoupper($ident) . '_CONTACT'    => $contact_url,
416                  'PROFILE_' . strtoupper($ident) . '_DESC'        => $field_desc,
417                  'PROFILE_' . strtoupper($ident) . '_TYPE'        => $ident_ary['data']['field_type'],
418                  'PROFILE_' . strtoupper($ident) . '_NAME'        => $this->user->lang($ident_ary['data']['lang_name']),
419                  'PROFILE_' . strtoupper($ident) . '_EXPLAIN'    => $this->user->lang($ident_ary['data']['lang_explain']),
420   
421                  'S_PROFILE_' . strtoupper($ident) . '_CONTACT'    => $ident_ary['data']['field_is_contact'],
422                  'S_PROFILE_' . strtoupper($ident)            => true,
423              );
424   
425              $tpl_fields['blockrow'][] = array(
426                  'PROFILE_FIELD_IDENT'        => $ident,
427                  'PROFILE_FIELD_VALUE'        => $value,
428                  'PROFILE_FIELD_VALUE_RAW'    => $value_raw,
429                  'PROFILE_FIELD_CONTACT'        => $contact_url,
430                  'PROFILE_FIELD_DESC'        => $field_desc,
431                  'PROFILE_FIELD_TYPE'        => $ident_ary['data']['field_type'],
432                  'PROFILE_FIELD_NAME'        => $this->user->lang($ident_ary['data']['lang_name']),
433                  'PROFILE_FIELD_EXPLAIN'        => $this->user->lang($ident_ary['data']['lang_explain']),
434   
435                  'S_PROFILE_CONTACT'                        => $ident_ary['data']['field_is_contact'],
436                  'S_PROFILE_' . strtoupper($ident)        => true,
437              );
438          }
439   
440          /**
441          * Event to modify template data of the generated profile fields
442          *
443          * @event core.generate_profile_fields_template_data
444          * @var    array    profile_row        Array with users profile field data
445          * @var    array    tpl_fields        Array with template data fields
446          * @var    bool    use_contact_fields    Should we display contact fields as such?
447          * @since 3.1.0-b3
448          */
449          $vars = array('profile_row', 'tpl_fields', 'use_contact_fields');
450          extract($this->dispatcher->trigger_event('core.generate_profile_fields_template_data', compact($vars)));
451   
452          return $tpl_fields;
453      }
454   
455      /**
456      * Build Array for user insertion into custom profile fields table
457      */
458      public function build_insert_sql_array($cp_data)
459      {
460          $sql_not_in = array();
461          foreach ($cp_data as $key => $null)
462          {
463              $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key;
464          }
465   
466          $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value
467              FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f
468              WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . '
469                  ' . ((sizeof($sql_not_in)) ? ' AND ' . $this->db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . '
470                  AND l.field_id = f.field_id';
471          $result = $this->db->sql_query($sql);
472   
473          while ($row = $this->db->sql_fetchrow($result))
474          {
475              $profile_field = $this->type_collection[$row['field_type']];
476              $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row);
477          }
478          $this->db->sql_freeresult($result);
479   
480          return $cp_data;
481      }
482  }
483