<commit_before># Copyright (c) 2008-2013 <NAME> and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
module ApplicationHelper
  def tabs(tabs = nil)
    tabs ||= controller_path =~ /admin/ ? FatFreeCRM::Tabs.admin : FatFreeCRM::Tabs.main
    if tabs
      @current_tab ||= tabs.first[:text] # Select first tab by default.
      tabs.each { |tab| tab[:active] = (@current_tab == tab[:text] || @current_tab == tab[:url][:controller]) }
    else
      fail FatFreeCRM::MissingSettings, "Tab settings are missing, please run <b>rake ffcrm:setup</b> command."
    end
  end

  #----------------------------------------------------------------------------
  def tabless_layout?
    %w(authentications passwords).include?(controller.controller_name) ||
      ((controller.controller_name == "users") && (%w(create new).include?(controller.action_name)))
  end

  # Show existing flash or embed hidden paragraph ready for flash[:notice]
  #----------------------------------------------------------------------------
  def show_flash(options = { sticky: false })
    [:error, :warning, :info, :notice].each do |type|
      if flash[type]
        html = content_tag(:div, h(flash[type]), id: "flash")
        flash[type] = nil
        return html << content_tag(:script, "crm.flash('#{type}', #{options[:sticky]})".html_safe, type: "text/javascript")
      end
    end
    content_tag(:p, nil, id: "flash", style: "display:none;")
  end

  #----------------------------------------------------------------------------
  def subtitle(id, hidden = true, text = id.to_s.split("_").last.capitalize)
    content_tag("div",
                link_to("<small>#{ hidden ? '&#9658;' : '&#9660;' }</small> #{sanitize text}".html_safe,
                        url_for(controller: :home, action: :toggle, id: id),
                        remote: true,
                        onclick: "crm.flip_subtitle(this)"
                ), class: "subtitle")
  end

  #----------------------------------------------------------------------------
  def section(related, assets)
    asset = assets.to_s.singularize
    create_id  = "create_#{asset}"
    select_id  = "select_#{asset}"
    assest_path = controller.send(:"new_#{asset}_path")

    html = tag(:br)
    html << content_tag(:div, link_to(t(select_id), "#", id: select_id), class: "subtitle_tools")
    html << content_tag(:div, "&nbsp;|&nbsp;".html_safe, class: "subtitle_tools")

    # html << content_tag(:div, link_to_inline(create_id, create_url, related: dom_id(related), text: t(create_id)), class: "subtitle_tools")
    related = "related=#{dom_id(related)}"
    assest_path = "assest_path=#{assest_path}"
    url="/create_assest?#{related}&#{assest_path}"
    html << content_tag(:div, link_to(create_id, url, text: t(create_id)), class: "subtitle_tools")

    html << content_tag(:div, t(assets), class: :subtitle, id: "create_#{asset}_title")
    html << content_tag(:div, "", class: :remote, id: create_id, style: "display:none;")
  end

  #----------------------------------------------------------------------------
  def load_select_popups_for(related, *assets)
    js = generate_js_for_popups(related, *assets)
    content_for(:javascript_epilogue) do
      raw "$(function() { #{js} });"
    end
  end

  def generate_js_for_popups(related, *assets)
    assets.map do |asset|
      render(partial: "shared/select_popup", locals: { related: related, popup: asset })
    end.join
  end

  # We need this because standard Rails [select] turns &#9733; into &amp;#9733;
  #----------------------------------------------------------------------------
  def rating_select(name, options = {}, edit)
    if edit
      stars = Hash[(1..5).map { |star| [star, "&#9733;" * star] }].sort
      options_for_select = %(<option value="0"#{options[:selected].to_i == 0 ? ' selected="selected"' : ''}>#{t :select_none}</option>)
      options_for_select << stars.map { |star| %(<option value="#{star.first}"#{options[:selected] == star.first ? ' selected="selected"' : ''}>#{star.last}</option>) }.join
      select_tag name, options_for_select.html_safe, options
    else
      data = ""
      if options[:selected].to_i == 0
        data << "None"
      else
        data << "&#9733;" * options[:selected].to_i
      end
      data.html_safe
    end
  end

  #----------------------------------------------------------------------------
  def link_to_inline(id, url, options = {})
    text = options[:text] || t(id, default: id.to_s.titleize)

    text = (arrow_for(id) + text) unless options[:plain]
    text = "test"
    related = (options[:related] ? "&related=#{options[:related]}" : '')
    data_kind = options["data-kind"] || ""

    # todo: terrible javascript
    if options.include?(:edit)
      edit = options[:edit]

      link_to(text,<commit_msg>revert wrong commit<commit_after># Copyright (c) 2008-2013 <NAME> and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
module ApplicationHelper
  def tabs(tabs = nil)
    tabs ||= controller_path =~ /admin/ ? FatFreeCRM::Tabs.admin : FatFreeCRM::Tabs.main
    if tabs
      @current_tab ||= tabs.first[:text] # Select first tab by default.
      tabs.each { |tab| tab[:active] = (@current_tab == tab[:text] || @current_tab == tab[:url][:controller]) }
    else
      fail FatFreeCRM::MissingSettings, "Tab settings are missing, please run <b>rake ffcrm:setup</b> command."
    end
  end

  #----------------------------------------------------------------------------
  def tabless_layout?
    %w(authentications passwords).include?(controller.controller_name) ||
      ((controller.controller_name == "users") && (%w(create new).include?(controller.action_name)))
  end

  # Show existing flash or embed hidden paragraph ready for flash[:notice]
  #----------------------------------------------------------------------------
  def show_flash(options = { sticky: false })
    [:error, :warning, :info, :notice].each do |type|
      if flash[type]
        html = content_tag(:div, h(flash[type]), id: "flash")
        flash[type] = nil
        return html << content_tag(:script, "crm.flash('#{type}', #{options[:sticky]})".html_safe, type: "text/javascript")
      end
    end
    content_tag(:p, nil, id: "flash", style: "display:none;")
  end

  #----------------------------------------------------------------------------
  def subtitle(id, hidden = true, text = id.to_s.split("_").last.capitalize)
    content_tag("div",
                link_to("<small>#{ hidden ? '&#9658;' : '&#9660;' }</small> #{sanitize text}".html_safe,
                        url_for(controller: :home, action: :toggle, id: id),
                        remote: true,
                        onclick: "crm.flip_subtitle(this)"
                ), class: "subtitle")
  end

  #----------------------------------------------------------------------------
  def section(related, assets)
    asset = assets.to_s.singularize
    create_id  = "create_#{asset}"
    select_id  = "select_#{asset}"
    assest_path = controller.send(:"new_#{asset}_path")

    html = tag(:br)
    html << content_tag(:div, link_to(t(select_id), "#", id: select_id), class: "subtitle_tools")
    html << content_tag(:div, "&nbsp;|&nbsp;".html_safe, class: "subtitle_tools")

    # html << content_tag(:div, link_to_inline(create_id, create_url, related: dom_id(related), text: t(create_id)), class: "subtitle_tools")
    related = "related=#{dom_id(related)}"
    assest_path = "assest_path=#{assest_path}"
    url="/create_assest?#{related}&#{assest_path}"
    html << content_tag(:div, link_to(create_id, url, text: t(create_id)), class: "subtitle_tools")

    html << content_tag(:div, t(assets), class: :subtitle, id: "create_#{asset}_title")
    html << content_tag(:div, "", class: :remote, id: create_id, style: "display:none;")
  end

  #----------------------------------------------------------------------------
  def load_select_popups_for(related, *assets)
    js = generate_js_for_popups(related, *assets)
    content_for(:javascript_epilogue) do
      raw "$(function() { #{js} });"
    end
  end

  def generate_js_for_popups(related, *assets)
    assets.map do |asset|
      render(partial: "shared/select_popup", locals: { related: related, popup: asset })
    end.join
  end

  # We need this because standard Rails [select] turns &#9733; into &amp;#9733;
  #----------------------------------------------------------------------------
  def rating_select(name, options = {}, edit)
    if edit
      stars = Hash[(1..5).map { |star| [star, "&#9733;" * star] }].sort
      options_for_select = %(<option value="0"#{options[:selected].to_i == 0 ? ' selected="selected"' : ''}>#{t :select_none}</option>)
      options_for_select << stars.map { |star| %(<option value="#{star.first}"#{options[:selected] == star.first ? ' selected="selected"' : ''}>#{star.last}</option>) }.join
      select_tag name, options_for_select.html_safe, options
    else
      data = ""
      if options[:selected].to_i == 0
        data << "None"
      else
        data << "&#9733;" * options[:selected].to_i
      end
      data.html_safe
    end
  end

  #----------------------------------------------------------------------------
  def link_to_inline(id, url, options = {})
    text = options[:text] || t(id, default: id.to_s.titleize)
    text = (arrow_for(id) + text) unless options[:plain]
    related = (options[:related] ? "&related=#{options[:related]}" : '')
    data_kind = options["data-kind"] || ""

    # todo: terrible javascript
    if options.include?(:edit)
      edit = options[:edit]

      link_to(text,
{'fraction_non_alphanumeric': 0.2684691928837069, 'fraction_numerical': 0.007437933460649311, 'mean_word_length': 2.161741340959644, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6866660', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2900, 'n_words': 649}