HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/openproject/db/migrate/20180830120550_migrate_light_background_themes.rb
class MigrateLightBackgroundThemes < ActiveRecord::Migration[5.1]
  NEW_COLOR_VARIABLES = %w( main-menu-font-color
                            main-menu-bg-selected-background
                            main-menu-selected-font-color
                            main-menu-bg-hover-background
                            main-menu-hover-font-color
                            main-menu-border-color ).freeze
  def down
    # This migration is not revertible.
  end

  def up
    # This migration is "nice to have". There is no harm if it does not get applied.
    return unless apply?

    # Main menu was set to white
    if DesignColor.find_by(variable: 'main-menu-bg-color')&.hexcode == '#FFFFFF'
      set_old_default_menu_colors
    end

    # Header is white and main menu was default light grey
    if DesignColor.find_by(variable: 'header-bg-color')&.hexcode == '#FFFFFF' &&
       DesignColor.find_by(variable: 'main-menu-bg-color').nil?
      set_variable('main-menu-bg-color', '#F8F8F8')
      set_old_default_menu_colors
    end
  end

  def set_old_default_menu_colors
    content_link_color = DesignColor.find_by(variable: 'content-link-color')&.hexcode ||
                         DesignColor.find_by(variable: 'primary-color-dark')&.hexcode ||
                         "#175A8E"

    set_variable('main-menu-font-color',             '#333333')
    set_variable('main-menu-bg-selected-background', '#E9E9E9')
    set_variable('main-menu-selected-font-color',    content_link_color)
    set_variable('main-menu-bg-hover-background',    '#F0F0F0')
    set_variable('main-menu-hover-font-color',       '#333333')
    set_variable('main-menu-border-color',           '#E7E7E7')
  end

  def set_variable(variable_name, hexcode)
    DesignColor.create(variable: variable_name, hexcode: hexcode)
  end

  def apply?
    # Future safety: Check that all necessary variables are still in use.
    %w( content-link-color
        header-bg-color
        header-border-bottom-color
        main-menu-bg-color
        main-menu-font-color
        main-menu-bg-selected-background
        main-menu-selected-font-color
        main-menu-bg-hover-background
        main-menu-hover-font-color
        main-menu-border-color ).each do |variable_name|
      return false unless OpenProject::CustomStyles::Design.customizable_variables.include? variable_name
    end

    # Never ever overwrite variables that were already set.
    # The existence of one variable is sufficient to better abort this migration.
    false if DesignColor.where(variable: NEW_COLOR_VARIABLES).any?

    true
  end
end