|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Override last checked at: 04.04.2025 |
| 4 | +Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessesController.class_eval do |
| 5 | + def edit |
| 6 | + enforce_permission_to :update, :process, process: current_participatory_process |
| 7 | + |
| 8 | + @form = form(Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessForm).from_model(current_participatory_process) |
| 9 | + |
| 10 | + @form[:description_de] = reset_url_target(@form[:description_de]) |
| 11 | + @form[:description_en] = reset_url_target(@form[:description_en]) |
| 12 | + |
| 13 | + render layout: "decidim/admin/participatory_process" |
| 14 | + end |
| 15 | + |
| 16 | + private |
| 17 | + |
| 18 | + def participatory_process_params |
| 19 | + ppp = { id: params[:slug] }.merge(params[:participatory_process].to_unsafe_h) |
| 20 | + |
| 21 | + ppp['description_de'] = apply_url_target(ppp['description_de']) |
| 22 | + ppp['description_en'] = apply_url_target(ppp['description_en']) |
| 23 | + |
| 24 | + ppp |
| 25 | + end |
| 26 | + |
| 27 | + def apply_url_target(text) |
| 28 | + pattern = %r{<a href="([^"]+?)(:?\?|\&)target=([^"]+?)"(.+?)(:?target="_blank")?>} |
| 29 | + matches = matches(text, pattern) |
| 30 | + |
| 31 | + matches |
| 32 | + .reduce(text) do |text, match| |
| 33 | + href, _, target, params = match.captures.map(&:strip) |
| 34 | + |
| 35 | + text.gsub(match[0], %(<a href="#{href}" #{params} target="#{target}">)) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def reset_url_target(text) |
| 40 | + pattern = %r{<a href="(.+?)"(.+?)target="(.+?)">} |
| 41 | + matches = matches(text, pattern) |
| 42 | + |
| 43 | + matches |
| 44 | + .reject { |match| match[3] == '_blank' } |
| 45 | + .reduce(text) do |text, match| |
| 46 | + href, params, target = match.captures.map(&:strip) |
| 47 | + next if target == '_blank' |
| 48 | + |
| 49 | + divider = href.split('/').last.include?('?') ? '&' : '?' |
| 50 | + new_href = "#{href}#{divider}target=#{target}" |
| 51 | + |
| 52 | + text.gsub(match[0], %(<a href="#{new_href}" #{params} target="_blank">)) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def matches(str, regexp) |
| 57 | + start_at = 0 |
| 58 | + matches = [] |
| 59 | + while(match = str.match(regexp, start_at)) |
| 60 | + matches.push(match) |
| 61 | + start_at = match.end(0) |
| 62 | + end |
| 63 | + matches |
| 64 | + end |
| 65 | +end |
0 commit comments