<commit_before># All methods (helpers) in this helper are used by Alchemy to render elements, contents and layouts on the Page.
#
# TODO: list all important infos here.
# 
# Most Important Infos:
# ---
#
# 1. The most important helpers for webdevelopers are the render_navigation(), render_elements() and the render_page_layout() helpers.
# 2. The currently displayed page can be accessed via the @page variable.
# 3. All important meta data from @page will be rendered via the render_meta_data() helper.

module PagesHelper

  # This helper renders the Element partial for either the view or the editor part.
  # Generate element partials with ./script/generate elements
  def render_element(element, part = :view, options = {}, i = 1)
    begin
      if element.blank?
        warning('Element is nil')
        render :partial => "elements/#{part}_not_found", :locals => {:name => 'nil'}
      else
        default_options = {
          :shorten_to => nil,
          :render_format => "html"
        }
        options = default_options.merge(options)
        element.store_page(@page) if part == :view
        path1 = "#{Rails.root}/app/views/elements/"
        path2 = "#{Rails.root}/vendor/plugins/alchemy/app/views/elements/"
        partial_name = "_#{element.name.underscore}_#{part}.html.erb"
        locals = options.delete(:locals)
        render(
          :partial => "elements/#{element.name.underscore}_#{part}.#{options[:render_format]}.erb",
          :locals => {
            :element => element, 
            :options => options, 
            :counter => i
          }.merge(locals || {})
        )
      end
    rescue ActionView::MissingTemplate
      warning(%(
        Element #{part} partial not found for #{element.name}.\n
        Looking for #{partial_name}, but not found
        neither in #{path1}
        nor in #{path2}
        Use rails generate alchemy:elements to generate them.
        Maybe you still have old style partial names? (like .rhtml). Then please rename them in .html.erb'
      ))
      render :partial => "elements/#{part}_not_found", :locals => {:name => element.name, :error => "Element #{part} partial not found. Use rails generate alchemy:elements to generate them."}
    end
  end

  # DEPRICATED! Use Element.preview_text instead.
  def get_preview_text element
    element.preview_text
  end

  def render_classes classes=[]
    s = classes.uniq.delete_if{|x| x == nil || x.blank?}.join(" ")
    s.blank? ? "" : "class='#{s}'"
  end

  def picture_essence_caption(content)
    return "" if content.nil?
    return "" if content.essence.nil?
    content.essence.caption
  end

  def alchemy_form_select(name, select_options, options={})
    select "mail_data", name, select_options, :selected => (session[:mail_data][name.to_sym] rescue "")
  end

  def alchemy_form_input_field(name, options = {})
    if options[:value].blank? && session[:mail_data].blank?
      value = nil
    elsif options[:value].blank? && !session[:mail_data].blank?
      value = session[:mail_data][name.to_sym]
    else
      value = options[:value]
    end
    text_field("mail_data", name, {:value => value}.merge(options))
  end

  def alchemy_form_text_area(name, options={})
    text_area "mail_data", name, :class => options[:class], :value => (session[:mail_data][name.to_sym] rescue "")
  end

  def alchemy_form_check_box(name, options={})
    box = hidden_field_tag "mail_data[#{name}]", 0, :id => nil
    box += check_box_tag("mail_data[#{name}]", 1, (session[:mail_data] && session[:mail_data][name.to_sym] == "1"))
    box
  end

  def alchemy_form_label(element, name, options={})
    label_tag "mail_data_#{name}", render_essence_view_by_name(element, name), options
  end

  def alchemy_form_reset_button(name, options={})
    button_to_function(
      name,
      remote_function(
        :url => {
          :controller => "contact_form",
          :action => "clear_session"
        },
        :before => %(
          this.form.reset();
          this.form.descendants().each(
            function(d){
              if ((d.type!='button') && (d.type!='submit') && (d.type!='hidden') && !d.disabled) {
                d.value = '';
                if (d.type == 'checkbox') {
                  d.checked = false;
                }
              }
            }
          )
        )
      ),
      options
    )
  end

  # helper for language switching
  def language_switches(options={})
    default_options = {
      :linkname => :name,
      :spacer => "",
      :link_to_public_child => configuration(:redirect_to_public_child),
      :link_to_page_with_layout => nil,
      :show_title => true,
      :reverse => false,
      :as_select_box => false
    }
    options = default_options.merge(options)
    if multi_language?
      language_links = []
      pages = (options[:link_to_public_child] == true) ? Page.language_roots : Page.public_language_roots
      pages.each_with_index do |page, i|
        if(options[:link_to_page_with_layout] != nil)
          page_found_by_layout = Page.find_by_page_layout_and_language_id(options[:link_to_page_with_layout].to_s, page.language)
        end
        page = page_found_by_layout || page
        page = (options[:link_to_public_child] ? (page.first_public_child.blank? ? nil : page.first_public_child) : nil) if !page.public?
        
        if !page.blank?
          active = session[:language_id] == page.language.id
          linkname = page.language.label(options[:linkname])
          if options[:as_select_box]
            language_links << [linkname, show_page_url(:urlname => page.urlname, :lang => page.language.code)]
          else
            language_links << link_to(
              "#{content_tag(:span, '', :class => "flag")}#{ content_tag(:span, linkname)}".html_safe,
              show_page_path(:urlname => page.urlname, :lang => page.language.code),
              :class => "#{(active ? 'active ' : nil)}#{page.language.code} #{(i == 0) ? 'first' : (i==pages.length-1) ? 'last' : nil}",
              :title => options[:show_title] ? I18n.t("alchemy.language_links.#{page.language.code}.title", :default => page.language.name) : nil
            )
          end
        end
      end
      language_links.reverse! if options[:reverse]
      if options[:as_select_box]
        return select_tag(
          'language',
          options_for_select(
            language_links,
            show_page_url(:urlname => @page.urlname, :lang => @page.language.code)
          ),
          :onchange => "window.location=this.value"
        )
      else
        raw(language_links.join(options[:spacer]))
      end
    end
  end

  def sitename_from_header_page
    header_page = Page.find_by_page_layout_and_layoutpage('layout_header', true)
    return "" if header_page.nil?
    page_title = header_page.elements.find_by_name('sitename')
    return "" if page_title.nil?
    page_title.ingredient('name')
  end

end
<commit_msg>Removes deprecated `get_preview_text` helper<commit_after># All methods (helpers) in this helper are used by Alchemy to render elements, contents and layouts on the Page.
#
# TODO: list all important infos here.
# 
# Most Important Infos:
# ---
#
# 1. The most important helpers for webdevelopers are the render_navigation(), render_elements() and the render_page_layout() helpers.
# 2. The currently displayed page can be accessed via the @page variable.
# 3. All important meta data from @page will be rendered via the render_meta_data() helper.

module PagesHelper

  # This helper renders the Element partial for either the view or the editor part.
  # Generate element partials with ./script/generate elements
  def render_element(element, part = :view, options = {}, i = 1)
    begin
      if element.blank?
        warning('Element is nil')
        render :partial => "elements/#{part}_not_found", :locals => {:name => 'nil'}
      else
        default_options = {
          :shorten_to => nil,
          :render_format => "html"
        }
        options = default_options.merge(options)
        element.store_page(@page) if part == :view
        path1 = "#{Rails.root}/app/views/elements/"
        path2 = "#{Rails.root}/vendor/plugins/alchemy/app/views/elements/"
        partial_name = "_#{element.name.underscore}_#{part}.html.erb"
        locals = options.delete(:locals)
        render(
          :partial => "elements/#{element.name.underscore}_#{part}.#{options[:render_format]}.erb",
          :locals => {
            :element => element, 
            :options => options, 
            :counter => i
          }.merge(locals || {})
        )
      end
    rescue ActionView::MissingTemplate
      warning(%(
        Element #{part} partial not found for #{element.name}.\n
        Looking for #{partial_name}, but not found
        neither in #{path1}
        nor in #{path2}
        Use rails generate alchemy:elements to generate them.
        Maybe you still have old style partial names? (like .rhtml). Then please rename them in .html.erb'
      ))
      render :partial => "elements/#{part}_not_found", :locals => {:name => element.name, :error => "Element #{part} partial not found. Use rails generate alchemy:elements to generate them."}
    end
  end

  def render_classes classes=[]
    s = classes.uniq.delete_if{|x| x == nil || x.blank?}.join(" ")
    s.blank? ? "" : "class='#{s}'"
  end

  def picture_essence_caption(content)
    return "" if content.nil?
    return "" if content.essence.nil?
    content.essence.caption
  end

  def alchemy_form_select(name, select_options, options={})
    select "mail_data", name, select_options, :selected => (session[:mail_data][name.to_sym] rescue "")
  end

  def alchemy_form_input_field(name, options = {})
    if options[:value].blank? && session[:mail_data].blank?
      value = nil
    elsif options[:value].blank? && !session[:mail_data].blank?
      value = session[:mail_data][name.to_sym]
    else
      value = options[:value]
    end
    text_field("mail_data", name, {:value => value}.merge(options))
  end

  def alchemy_form_text_area(name, options={})
    text_area "mail_data", name, :class => options[:class], :value => (session[:mail_data][name.to_sym] rescue "")
  end

  def alchemy_form_check_box(name, options={})
    box = hidden_field_tag "mail_data[#{name}]", 0, :id => nil
    box += check_box_tag("mail_data[#{name}]", 1, (session[:mail_data] && session[:mail_data][name.to_sym] == "1"))
    box
  end

  def alchemy_form_label(element, name, options={})
    label_tag "mail_data_#{name}", render_essence_view_by_name(element, name), options
  end

  def alchemy_form_reset_button(name, options={})
    button_to_function(
      name,
      remote_function(
        :url => {
          :controller => "contact_form",
          :action => "clear_session"
        },
        :before => %(
          this.form.reset();
          this.form.descendants().each(
            function(d){
              if ((d.type!='button') && (d.type!='submit') && (d.type!='hidden') && !d.disabled) {
                d.value = '';
                if (d.type == 'checkbox') {
                  d.checked = false;
                }
              }
            }
          )
        )
      ),
      options
    )
  end

  # helper for language switching
  def language_switches(options={})
    default_options = {
      :linkname => :name,
      :spacer => "",
      :link_to_public_child => configuration(:redirect_to_public_child),
      :link_to_page_with_layout => nil,
      :show_title => true,
      :reverse => false,
      :as_select_box => false
    }
    options = default_options.merge(options)
    if multi_language?
      language_links = []
      pages = (options[:link_to_public_child] == true) ? Page.language_roots : Page.public_language_roots
      pages.each_with_index do |page, i|
        if(options[:link_to_page_with_layout] != nil)
          page_found_by_layout = Page.find_by_page_layout_and_language_id(options[:link_to_page_with_layout].to_s, page.language)
        end
        page = page_found_by_layout || page
        page = (options[:link_to_public_child] ? (page.first_public_child.blank? ? nil : page.first_public_child) : nil) if !page.public?
        
        if !page.blank?
          active = session[:language_id] == page.language.id
          linkname = page.language.label(options[:linkname])
          if options[:as_select_box]
            language_links << [linkname, show_page_url(:urlname => page.urlname, :lang => page.language.code)]
          else
            language_links << link_to(
              "#{content_tag(:span, '', :class => "flag")}#{ content_tag(:span, linkname)}".html_safe,
              show_page_path(:urlname => page.urlname, :lang => page.language.code),
              :class => "#{(active ? 'active ' : nil)}#{page.language.code} #{(i == 0) ? 'first' : (i==pages.length-1) ? 'last' : nil}",
              :title => options[:show_title] ? I18n.t("alchemy.language_links.#{page.language.code}.title", :default => page.language.name) : nil
            )
          end
        end
      end
      language_links.reverse! if options[:reverse]
      if options[:as_select_box]
        return select_tag(
          'language',
          options_for_select(
            language_links,
            show_page_url(:urlname => @page.urlname, :lang => @page.language.code)
          ),
          :onchange => "window.location=this.value"
        )
      else
        raw(language_links.join(options[:spacer]))
      end
    end
  end

  def sitename_from_header_page
    header_page = Page.find_by_page_layout_and_layoutpage('layout_header', true)
    return "" if header_page.nil?
    page_title = header_page.elements.find_by_name('sitename')
    return "" if page_title.nil?
    page_title.ingredient('name')
  end

end

{'fraction_non_alphanumeric': 0.1337824134434304, 'fraction_numerical': 0.0021729682746631897, 'mean_word_length': 2.7235706580366776, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7559283', 'n_tokens_mistral': 4264, 'n_tokens_neox': 4187, 'n_words': 942}