Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow themes and variables to be updated by service

Oliver Güntherb419398fa6fc
diff --git a/app/controllers/custom_styles_controller.rb b/app/controllers/custom_styles_controller.rb
index c996aac0c9b..fce4dbe484c 100644
--- a/app/controllers/custom_styles_controller.rb
+++ b/app/controllers/custom_styles_controller.rb
@@ -89,20 +89,28 @@ class CustomStylesController < ApplicationController
def update_colors
variable_params = params[:design_colors].first
- set_colors(variable_params)
- set_theme(params)
+
+ ::Design::UpdateDesignService
+ .new(colors: variable_params, theme: params[:theme])
+ .call
redirect_to action: :show
end
def update_themes
- theme = OpenProject::CustomStyles::ColorThemes::THEMES.find { |t| t[:name] == params[:theme] }
- color_params = theme[:colors]
- logo = theme[:logo]
+ theme = OpenProject::CustomStyles::ColorThemes::THEMES.find { |t| t[:theme] == params[:theme] }
+
+ call = ::Design::UpdateDesignService
+ .new(theme)
+ .call
+
+ call.on_success do
+ flash[:notice] = I18n.t(:notice_successful_update)
+ end
- set_logo(logo)
- set_colors(color_params)
- set_theme(params)
+ call.on_failure do
+ flash[:error] = call.message
+ end
redirect_to action: :show
end
@@ -114,40 +122,12 @@ class CustomStylesController < ApplicationController
private
def options_for_theme_select
- options = OpenProject::CustomStyles::ColorThemes::THEMES.map { |val| val[:name] }
+ options = OpenProject::CustomStyles::ColorThemes::THEMES.map { |val| val[:theme] }
options << [t('admin.custom_styles.color_theme_custom'), '', disabled: true] if @current_theme.empty?
options
end
- def set_logo(logo)
- get_or_create_custom_style.update(theme_logo: logo)
- end
-
- def set_colors(variable_params)
- variable_params.each do |param_variable, param_hexcode|
- if design_color = DesignColor.find_by(variable: param_variable)
- if param_hexcode.blank?
- design_color.destroy
- elsif design_color.hexcode != param_hexcode
- design_color.hexcode = param_hexcode
- design_color.save
- end
- else
- # create that design_color
- design_color = DesignColor.new variable: param_variable, hexcode: param_hexcode
- design_color.save
- end
- end
- end
-
- def set_theme(params)
- theme = ActionController::Parameters.new(theme: params[:theme] || '').permit(:theme)
-
- @custom_style = get_or_create_custom_style
- @custom_style.update(theme)
- end
-
def get_or_create_custom_style
CustomStyle.current || CustomStyle.create!
end
diff --git a/app/services/design/update_design_service.rb b/app/services/design/update_design_service.rb
new file mode 100644
index 00000000000..13cbf1e408b
--- /dev/null
+++ b/app/services/design/update_design_service.rb
@@ -0,0 +1,83 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Design
+ class UpdateDesignService
+ attr_reader :params
+
+ def initialize(params)
+ @params = params
+ end
+
+ def call
+ set_logo
+ set_colors
+ set_theme
+
+ ServiceResult.new success: custom_style.save,
+ result: custom_style
+ end
+
+ private
+
+ def set_logo
+ return unless params[:logo]
+
+ custom_style.theme_logo = params[:logo]
+ end
+
+ def set_colors
+ return unless params[:colors]
+
+ params[:colors].each do |param_variable, param_hexcode|
+ if design_color = DesignColor.find_by(variable: param_variable)
+ if param_hexcode.blank?
+ design_color.destroy
+ elsif design_color.hexcode != param_hexcode
+ design_color.hexcode = param_hexcode
+ design_color.save
+ end
+ else
+ # create that design_color
+ design_color = DesignColor.new variable: param_variable, hexcode: param_hexcode
+ design_color.save
+ end
+ end
+ end
+
+ def set_theme
+ return unless params[:theme]
+
+ custom_style.theme = params[:theme]
+ end
+
+ def custom_style
+ @custom_style ||= (CustomStyle.current || CustomStyle.create!)
+ end
+ end
+end
diff --git a/lib/open_project/custom_styles/color_themes.rb b/lib/open_project/custom_styles/color_themes.rb
index 88eba9c7fd3..7ac6e8dfa73 100644
--- a/lib/open_project/custom_styles/color_themes.rb
+++ b/lib/open_project/custom_styles/color_themes.rb
@@ -31,69 +31,73 @@ module OpenProject::CustomStyles
class ColorThemes
THEMES = [
{
- name: 'OpenProject',
+ theme: 'OpenProject',
colors: {
- 'primary-color' => "#1A67A3",
- 'primary-color-dark' => "#175A8E",
- 'alternative-color' => "#35C53F",
- 'content-link-color' => "#175A8E",
- 'header-bg-color' => "#1A67A3",
- 'header-item-bg-hover-color' => "#175A8E",
- 'header-item-font-color' => "#FFFFFF",
- 'header-item-font-hover-color' => "#FFFFFF",
- 'header-border-bottom-color' => "",
- 'main-menu-bg-color' => "#333739",
- 'main-menu-bg-selected-background' => "#175A8E",
- 'main-menu-bg-hover-background' => "#124E7C",
- 'main-menu-font-color' => "#FFFFFF",
- 'main-menu-hover-font-color' => "#FFFFFF",
- 'main-menu-selected-font-color' => "#FFFFFF",
- 'main-menu-border-color' => "#EAEAEA"
+ 'primary-color' => "#1A67A3",
+ 'primary-color-dark' => "#175A8E",
+ 'alternative-color' => "#35C53F",
+ 'content-link-color' => "#175A8E",
+ 'header-bg-color' => "#1A67A3",
+ 'header-item-bg-hover-color' => "#175A8E",
+ 'header-item-font-color' => "#FFFFFF",
+ 'header-item-font-hover-color' => "#FFFFFF",
+ 'header-border-bottom-color' => "",
+ 'main-menu-bg-color' => "#333739",
+ 'main-menu-bg-selected-background' => "#175A8E",
+ 'main-menu-bg-hover-background' => "#124E7C",
+ 'main-menu-font-color' => "#FFFFFF",
+ 'main-menu-hover-font-color' => "#FFFFFF",
+ 'main-menu-selected-font-color' => "#FFFFFF",
+ 'main-menu-border-color' => "#EAEAEA"
}
},
{
- name: 'OpenProject Light',
+ theme: 'OpenProject Light',
colors: {
- 'primary-color' => "#1A67A3",
- 'primary-color-dark' => "#175A8E",
- 'alternative-color' => "#138E1B",
- 'content-link-color' => "#175A8E",
- 'header-bg-color' => "#FAFAFA",
- 'header-item-bg-hover-color' => "#E1E1E1",
- 'header-item-font-color' => "#313131",
- 'header-item-font-hover-color' => "#313131",
- 'header-border-bottom-color' => "#E1E1E1",
- 'main-menu-bg-color' => "#ECECEC",
- 'main-menu-bg-selected-background' => "#A9A9A9",
- 'main-menu-bg-hover-background' => "#FFFFFF",
- 'main-menu-font-color' => "#000000",
- 'main-menu-hover-font-color' => "#000000",
- 'main-menu-selected-font-color' => "#000000",
- 'main-menu-border-color' => "#EAEAEA"
+ 'primary-color' => "#1A67A3",
+ 'primary-color-dark' => "#175A8E",
+ 'alternative-color' => "#138E1B",
+ 'content-link-color' => "#175A8E",
+ 'header-bg-color' => "#FAFAFA",
+ 'header-item-bg-hover-color' => "#E1E1E1",
+ 'header-item-font-color' => "#313131",
+ 'header-item-font-hover-color' => "#313131",
+ 'header-border-bottom-color' => "#E1E1E1",
+ 'main-menu-bg-color' => "#ECECEC",
+ 'main-menu-bg-selected-background' => "#A9A9A9",
+ 'main-menu-bg-hover-background' => "#FFFFFF",
+ 'main-menu-font-color' => "#000000",
+ 'main-menu-hover-font-color' => "#000000",
+ 'main-menu-selected-font-color' => "#000000",
+ 'main-menu-border-color' => "#EAEAEA"
},
- logo: 'logo_openproject.png'
+ logo: 'logo_openproject.png'
},
{
- name: 'OpenProject Dark',
+ theme: 'OpenProject Dark',
colors: {
- 'primary-color' => "#3270DB",
- 'primary-color-dark' => "#163473",
- 'alternative-color' => "#349939",
- 'content-link-color' => "#275BB5",
- 'header-bg-color' => "#05002C",
- 'header-item-bg-hover-color' => "#163473",
- 'header-item-font-color' => "#FFFFFF",
- 'header-item-font-hover-color' => "#FFFFFF",
- 'header-border-bottom-color' => "",
- 'main-menu-bg-color' => "#0E2045",
- 'main-menu-bg-selected-background' => "#3270DB",
- 'main-menu-bg-hover-background' => "#163473",
- 'main-menu-font-color' => "#FFFFFF",
- 'main-menu-hover-font-color' => "#FFFFFF",
- 'main-menu-selected-font-color' => "#FFFFFF",
- 'main-menu-border-color' => "#EAEAEA"
+ 'primary-color' => "#3270DB",
+ 'primary-color-dark' => "#163473",
+ 'alternative-color' => "#349939",
+ 'content-link-color' => "#275BB5",
+ 'header-bg-color' => "#05002C",
+ 'header-item-bg-hover-color' => "#163473",
+ 'header-item-font-color' => "#FFFFFF",
+ 'header-item-font-hover-color' => "#FFFFFF",
+ 'header-border-bottom-color' => "",
+ 'main-menu-bg-color' => "#0E2045",
+ 'main-menu-bg-selected-background' => "#3270DB",
+ 'main-menu-bg-hover-background' => "#163473",
+ 'main-menu-font-color' => "#FFFFFF",
+ 'main-menu-hover-font-color' => "#FFFFFF",
+ 'main-menu-selected-font-color' => "#FFFFFF",
+ 'main-menu-border-color' => "#EAEAEA"
}
}
].freeze
+
+ def themes
+ THEMES
+ end
end
end