[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/db/migration/ -> profilefield_base_migration.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\db\migration;
  15  
  16  abstract class profilefield_base_migration extends container_aware_migration
  17  {
  18      protected $profilefield_name;
  19  
  20      protected $profilefield_database_type;
  21  
  22      protected $profilefield_data;
  23  
  24      /**
  25      * Language data should be in array -> each language_data in separate key
  26      * array(
  27      *    array(
  28      *        'option_id'    => value,
  29      *        'field_type'    => value,
  30      *        'lang_value'    => value,
  31      *    ),
  32      *    array(
  33      *        'option_id'    => value,
  34      *        'field_type'    => value,
  35      *        'lang_value'    => value,
  36      *    ),
  37      * )
  38      */
  39      protected $profilefield_language_data;
  40  
  41      protected $user_column_name;
  42  
  43  	public function effectively_installed()
  44      {
  45          return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_' . $this->profilefield_name);
  46      }
  47  
  48  	public function update_schema()
  49      {
  50          return array(
  51              'add_columns'    => array(
  52                  $this->table_prefix . 'profile_fields_data'            => array(
  53                      'pf_' . $this->profilefield_name        => $this->profilefield_database_type,
  54                  ),
  55              ),
  56          );
  57      }
  58  
  59  	public function revert_schema()
  60      {
  61          return array(
  62              'drop_columns'    => array(
  63                  $this->table_prefix . 'profile_fields_data'            => array(
  64                      'pf_' . $this->profilefield_name,
  65                  ),
  66              ),
  67          );
  68      }
  69  
  70  	public function update_data()
  71      {
  72          return array(
  73              array('custom', array(array($this, 'create_custom_field'))),
  74              array('custom', array(array($this, 'convert_user_field_to_custom_field'))),
  75          );
  76      }
  77  
  78  	public function revert_data()
  79      {
  80          return array(
  81              array('custom', array(array($this, 'delete_custom_profile_field_data'))),
  82          );
  83      }
  84  
  85  	public function create_custom_field()
  86      {
  87          $sql = 'SELECT MAX(field_order) as max_field_order
  88              FROM ' . PROFILE_FIELDS_TABLE;
  89          $result = $this->db->sql_query($sql);
  90          $max_field_order = (int) $this->db->sql_fetchfield('max_field_order');
  91          $this->db->sql_freeresult($result);
  92  
  93          $sql_ary = array_merge($this->profilefield_data, array(
  94              'field_order'            => $max_field_order + 1,
  95          ));
  96  
  97          $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
  98          $this->db->sql_query($sql);
  99          $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  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1