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

profilefield_base_migration.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 6.21 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 \phpbb\db\migration\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   
 195          $sql = 'SELECT user_id, ' . $this->user_column_name . '
 196              FROM ' . $this->table_prefix . 'users
 197              WHERE ' . $this->user_column_name . " <> ''
 198              ORDER BY user_id";
 199          $result = $this->db->sql_query_limit($sql, $limit, $start);
 200   
 201          while ($row = $this->db->sql_fetchrow($result))
 202          {
 203              $converted_users++;
 204   
 205              $cp_data = array(
 206                  'pf_' . $this->profilefield_name        => $row[$this->user_column_name],
 207              );
 208   
 209              $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data
 210                  SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . '
 211                  WHERE user_id = ' . (int) $row['user_id'];
 212              $this->db->sql_query($sql);
 213   
 214              if (!$this->db->sql_affectedrows())
 215              {
 216                  $cp_data['user_id'] = (int) $row['user_id'];
 217                  $cp_data = array_merge($this->get_insert_sql_array(), $cp_data);
 218                  $insert_buffer->insert($cp_data);
 219              }
 220          }
 221          $this->db->sql_freeresult($result);
 222   
 223          $insert_buffer->flush();
 224   
 225          if ($converted_users < $limit)
 226          {
 227              // No more users left, we are done...
 228              return;
 229          }
 230   
 231          return $start + $limit;
 232      }
 233   
 234      protected function get_insert_sql_array()
 235      {
 236          static $profile_row;
 237   
 238          if ($profile_row === null)
 239          {
 240              global $phpbb_container;
 241              $manager = $phpbb_container->get('profilefields.manager');
 242              $profile_row = $manager->build_insert_sql_array(array());
 243          }
 244   
 245          return $profile_row;
 246      }
 247  }
 248