Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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

profilefield_base_migration.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 6.25 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\db\migration;
015   
016  abstract class profilefield_base_migration extends container_aware_migration
017  {
018      protected $profilefield_name;
019   
020      protected $profilefield_database_type;
021   
022      protected $profilefield_data;
023   
024      /**
025      * Language data should be in array -> each language_data in separate key
026      * array(
027      *    array(
028      *        'option_id'    => value,
029      *        'field_type'    => value,
030      *        'lang_value'    => value,
031      *    ),
032      *    array(
033      *        'option_id'    => value,
034      *        'field_type'    => value,
035      *        'lang_value'    => value,
036      *    ),
037      * )
038      */
039      protected $profilefield_language_data;
040   
041      protected $user_column_name;
042   
043      public function effectively_installed()
044      {
045          return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_' . $this->profilefield_name);
046      }
047   
048      public function update_schema()
049      {
050          return array(
051              'add_columns'    => array(
052                  $this->table_prefix . 'profile_fields_data'            => array(
053                      'pf_' . $this->profilefield_name        => $this->profilefield_database_type,
054                  ),
055              ),
056          );
057      }
058   
059      public function revert_schema()
060      {
061          return array(
062              'drop_columns'    => array(
063                  $this->table_prefix . 'profile_fields_data'            => array(
064                      'pf_' . $this->profilefield_name,
065                  ),
066              ),
067          );
068      }
069   
070      public function update_data()
071      {
072          return array(
073              array('custom', array(array($this, 'create_custom_field'))),
074              array('custom', array(array($this, 'convert_user_field_to_custom_field'))),
075          );
076      }
077   
078      public function revert_data()
079      {
080          return array(
081              array('custom', array(array($this, 'delete_custom_profile_field_data'))),
082          );
083      }
084   
085      public function create_custom_field()
086      {
087          $sql = 'SELECT MAX(field_order) as max_field_order
088              FROM ' . PROFILE_FIELDS_TABLE;
089          $result = $this->db->sql_query($sql);
090          $max_field_order = (int) $this->db->sql_fetchfield('max_field_order');
091          $this->db->sql_freeresult($result);
092   
093          $sql_ary = array_merge($this->profilefield_data, array(
094              'field_order'            => $max_field_order + 1,
095          ));
096   
097          $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
098          $this->db->sql_query($sql);
099          $field_id = (int) $this->db->sql_nextid();
100   
101          $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE);
102   
103          $sql = 'SELECT lang_id
104              FROM ' . LANG_TABLE;
105          $result = $this->db->sql_query($sql);
106          $lang_name = (strpos($this->profilefield_name, 'phpbb_') === 0) ? strtoupper(substr($this->profilefield_name, 6)) : strtoupper($this->profilefield_name);
107          while ($lang_id = (int) $this->db->sql_fetchfield('lang_id'))
108          {
109              $insert_buffer->insert(array(
110                  'field_id'                => (int) $field_id,
111                  'lang_id'                => (int) $lang_id,
112                  'lang_name'                => $lang_name,
113                  'lang_explain'            => '',
114                  'lang_default_value'    => '',
115              ));
116          }
117          $this->db->sql_freeresult($result);
118   
119          $insert_buffer->flush();
120      }
121   
122      /**
123      * Create Custom profile fields languguage entries
124      */
125      public function create_language_entries()
126      {
127          $field_id = $this->get_custom_profile_field_id();
128   
129          $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_FIELDS_LANG_TABLE);
130   
131          $sql = 'SELECT lang_id
132              FROM ' . LANG_TABLE;
133          $result = $this->db->sql_query($sql);
134          while ($lang_id = (int) $this->db->sql_fetchfield('lang_id'))
135          {
136              foreach ($this->profilefield_language_data as $language_data)
137              {
138                  $insert_buffer->insert(array_merge(array(
139                      'field_id'    => (int) $field_id,
140                      'lang_id'    => (int) $lang_id,
141                  ), $language_data));
142              }
143          }
144          $this->db->sql_freeresult($result);
145   
146          $insert_buffer->flush();
147      }
148   
149      /**
150      * Clean database when reverting the migration
151      */
152      public function delete_custom_profile_field_data()
153      {
154          $field_id = $this->get_custom_profile_field_id();
155   
156          $sql = 'DELETE FROM ' . PROFILE_FIELDS_TABLE . '
157              WHERE field_id = ' . (int) $field_id;
158          $this->db->sql_query($sql);
159   
160          $sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . '
161              WHERE field_id = ' . (int) $field_id;
162          $this->db->sql_query($sql);
163   
164          $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . '
165              WHERE field_id = ' . (int) $field_id;
166          $this->db->sql_query($sql);
167      }
168   
169      /**
170      * Get custom profile field id
171      * @return    int    custom profile filed id
172      */
173      public function get_custom_profile_field_id()
174      {
175          $sql = 'SELECT field_id
176              FROM ' . PROFILE_FIELDS_TABLE . "
177              WHERE field_name = '" . $this->profilefield_name . "'";
178          $result = $this->db->sql_query($sql);
179          $field_id = (int) $this->db->sql_fetchfield('field_id');
180          $this->db->sql_freeresult($result);
181   
182          return $field_id;
183      }
184   
185      /**
186      * @param int            $start        Start of staggering step
187      * @return        mixed        int start of the next step, null if the end was reached
188      */
189      public function convert_user_field_to_custom_field($start)
190      {
191          $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data');
192          $limit = 250;
193          $converted_users = 0;
194          $start = $start ?: 0;
195   
196          $sql = 'SELECT user_id, ' . $this->user_column_name . '
197              FROM ' . $this->table_prefix . 'users
198              WHERE ' . $this->user_column_name . " <> ''
199              ORDER BY user_id";
200          $result = $this->db->sql_query_limit($sql, $limit, $start);
201   
202          while ($row = $this->db->sql_fetchrow($result))
203          {
204              $converted_users++;
205   
206              $cp_data = array(
207                  'pf_' . $this->profilefield_name        => $row[$this->user_column_name],
208              );
209   
210              $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data
211                  SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . '
212                  WHERE user_id = ' . (int) $row['user_id'];
213              $this->db->sql_query($sql);
214   
215              if (!$this->db->sql_affectedrows())
216              {
217                  $cp_data['user_id'] = (int) $row['user_id'];
218                  $cp_data = array_merge($this->get_insert_sql_array(), $cp_data);
219                  $insert_buffer->insert($cp_data);
220              }
221          }
222          $this->db->sql_freeresult($result);
223   
224          $insert_buffer->flush();
225   
226          if ($converted_users < $limit)
227          {
228              // No more users left, we are done...
229              return;
230          }
231   
232          return $start + $limit;
233      }
234   
235      protected function get_insert_sql_array()
236      {
237          static $profile_row;
238   
239          if ($profile_row === null)
240          {
241              /* @var $manager \phpbb\profilefields\manager */
242              $manager = $this->container->get('profilefields.manager');
243              $profile_row = $manager->build_insert_sql_array(array());
244          }
245   
246          return $profile_row;
247      }
248  }
249