<commit_before>module Spider; module Forms
    
    # Widget for managing forms.
    #
    # Attributes:
    # *:model*  model to use
    # *:elements*   array of elements to display
    # *:widget_types*   hash of widget classes to use for inputs
    # *:read_only*  array of read only elements
    # *:disabled*   array of disabled elements (will not be shown)
    # *:pk*     primary key element
    # *:save_submit_text*   text for the save button when updating
    # *:insert_submit_text* text for the save button when inserting
    # *:show_submit_and_new*    bool, whether to show the submit and new button (default: true)
    # *:show_submit_and_stay*   book, whether to show the submit and stay button (default: true)
    # *:submit_and_new_text*    text for the save and insert new button
    # *:submit_and_stay_text*   text for the submit and stay button
    #
    # Content:
    # replaces <form:input id="element_name" /> tags with appropriate inputs
    
    class Form < Spider::Widget
        tag 'form'
        is_attribute :form_action
        i_attribute :model
        i_attribute :elements
        i_attribute :widget_types
        i_attribute :read_only
        i_attribute :disabled
        attribute :save_submit_text, :default => lambda{ _('Save') }
        attribute :insert_submit_text, :default => lambda{ _('Insert') }
        is_attribute :show_submit_and_new, :default => false
        is_attribute :show_submit_and_stay, :default => false
        is_attribute :show_additional_buttons, :default => false
        attribute :submit_and_new_text, :default => lambda{ _('%s and insert new') }
        attribute :submit_and_stay_text, :default => lambda{ _('%s and stay') }
        is_attr_accessor :pk
        attr_to_scene :inputs, :names, :hidden_inputs, :labels, :error, :errors, :sub_links
        attribute :show_related, :type => TrueClass, :default => false
        i_attribute :auto_redirect, :default => false
        attr_accessor :save_actions
        attr_accessor :fixed
        attr_accessor :before_save, :after_save
        
        attr_accessor :inputs
        attr_accessor :pk
        attr_reader :obj
        
        def init
            @inputs = {}
            @names = []
            @hidden_inputs = []
            @errors = {}
            @labels = {}
            @save_actions ||= {}
            @sub_links = {}
            @disabled = []
            @read_only = []
            @requested_elements = []
            @obj = nil
        end
        
        def route_widget
            if (@action == :sub)
                [@crud.id, @_action.split('/', 3)[2]]
            end
        end
        
        def prepare(action='')
            @form_action = @request.path
            @pk ||= @_action_local
            @pk ||= params['pk']
            @pk = nil if @pk == 'new'
            @model = const_get_full(@model) if @model.is_a?(String)
            if (@elements.is_a?(String))
                @elements = @elements.split(',').map{ |e| @model.elements[e.strip.to_sym] }.reject{ |i| i.nil? }
                @requested_elements = @elements
            end
            @elements = @model.elements_array unless @elements
            @model.each_element do |el|
                @labels[el.name] = el.label
            end
            wt = @widget_types || {}
            @widget_types = {}
            wt.each do |key, value|
                value = const_get_full(value) if value.is_a?(String)
                @widget_types[key.to_sym] = value
            end
            if (@read_only.is_a?(String))
                @read_only = @read_only.split(',').map{ |el| el.strip.to_sym }
            end
            @read_only ||= []
            if (@disabled.is_a?(String))
                @disabled = @disabled.split(',').map{ |el| e.strip.to_sym }
            end
            @disabled ||= []
#            @data = params['data'] || {}
            if @_action_rest
                el_label, sub_rest = @_action_rest.split('/', 2)
                sub_rest ||= ''
                @sub_element = @elements.find{ |el| el.label.downcase.gsub(/\s+/, '_') == el_label}
            end
            if (@sub_element)
                @action = :sub
                @scene.sub_element = @sub_element
                @_pass_action = sub_rest
            else
                @action = :form
            end
            @scene.action = @action
            if (@attributes[:show_additional_buttons])
                @attributes[:show_submit_and_new] = true
                @attributes[:show_submit_and_stay] = true
            end
            if (params['submit_and_new'])
                @submit_action = 'submit_and_new'
            elsif (params['submit_and_stay'])
                @submit_action = 'submit_and_stay'
            else
                @submit_action = params['submit']
            end
            @obj ||= load
            @scene.obj = @obj
            init_widgets
            # if (@submit_action)
            # else
            if @obj
                @fixed.each {|k, v| @obj.set(k, v)} if (@fixed)
                set_values(@obj) if @action == :form
            end
#            end
            super
            save(@submit_action) if @submit_action
        end
        
        def init_widgets
            super
            if (@action == :sub)
                @crud = Spider::Components::Crud.new(@request, @response)
                @crud.id = "crud_#{@sub_element.name.to_s}"
                @crud.model = @sub_element.model
                add_widget(@crud)
                @scene.crud = @crud
                @obj = load
                cond = {}
                @model.primary_keys.each do |key|
                    cond[@sub_element.reverse.to_s+'.'+key.name.to_s] = @obj.get(key)
                end
                @crud.fixed = cond
                sub_elements = []
                #sub_elements += @sub_element.model.primary_keys.map{ |k| k.name }
                @sub_element.model.elements_array.each do |el|
                    sub_elements << el.name unless el.integrated? || el.model == @model
                end
                @crud.attributes[:table_elements] = sub_elements
            else
                create_inputs
            end
        end
        
        def run
            Spider::Logger.debug("FORM EXECUTING")
            if (@obj)
                
                @scene.form_desc = @model.label.downcase+' '+ (@obj.to_s || '')
                if (@action == :sub)
                    <commit_msg>Fixed sub_element table condition for integrated elements<commit_after>module Spider; module Forms
    
    # Widget for managing forms.
    #
    # Attributes:
    # *:model*  model to use
    # *:elements*   array of elements to display
    # *:widget_types*   hash of widget classes to use for inputs
    # *:read_only*  array of read only elements
    # *:disabled*   array of disabled elements (will not be shown)
    # *:pk*     primary key element
    # *:save_submit_text*   text for the save button when updating
    # *:insert_submit_text* text for the save button when inserting
    # *:show_submit_and_new*    bool, whether to show the submit and new button (default: true)
    # *:show_submit_and_stay*   book, whether to show the submit and stay button (default: true)
    # *:submit_and_new_text*    text for the save and insert new button
    # *:submit_and_stay_text*   text for the submit and stay button
    #
    # Content:
    # replaces <form:input id="element_name" /> tags with appropriate inputs
    
    class Form < Spider::Widget
        tag 'form'
        is_attribute :form_action
        i_attribute :model
        i_attribute :elements
        i_attribute :widget_types
        i_attribute :read_only
        i_attribute :disabled
        attribute :save_submit_text, :default => lambda{ _('Save') }
        attribute :insert_submit_text, :default => lambda{ _('Insert') }
        is_attribute :show_submit_and_new, :default => false
        is_attribute :show_submit_and_stay, :default => false
        is_attribute :show_additional_buttons, :default => false
        attribute :submit_and_new_text, :default => lambda{ _('%s and insert new') }
        attribute :submit_and_stay_text, :default => lambda{ _('%s and stay') }
        is_attr_accessor :pk
        attr_to_scene :inputs, :names, :hidden_inputs, :labels, :error, :errors, :sub_links
        attribute :show_related, :type => TrueClass, :default => false
        i_attribute :auto_redirect, :default => false
        attr_accessor :save_actions
        attr_accessor :fixed
        attr_accessor :before_save, :after_save
        
        attr_accessor :inputs
        attr_accessor :pk
        attr_reader :obj
        
        def init
            @inputs = {}
            @names = []
            @hidden_inputs = []
            @errors = {}
            @labels = {}
            @save_actions ||= {}
            @sub_links = {}
            @disabled = []
            @read_only = []
            @requested_elements = []
            @obj = nil
        end
        
        def route_widget
            if (@action == :sub)
                [@crud.id, @_action.split('/', 3)[2]]
            end
        end
        
        def prepare(action='')
            @form_action = @request.path
            @pk ||= @_action_local
            @pk ||= params['pk']
            @pk = nil if @pk == 'new'
            @model = const_get_full(@model) if @model.is_a?(String)
            if (@elements.is_a?(String))
                @elements = @elements.split(',').map{ |e| @model.elements[e.strip.to_sym] }.reject{ |i| i.nil? }
                @requested_elements = @elements
            end
            @elements = @model.elements_array unless @elements
            @model.each_element do |el|
                @labels[el.name] = el.label
            end
            wt = @widget_types || {}
            @widget_types = {}
            wt.each do |key, value|
                value = const_get_full(value) if value.is_a?(String)
                @widget_types[key.to_sym] = value
            end
            if (@read_only.is_a?(String))
                @read_only = @read_only.split(',').map{ |el| el.strip.to_sym }
            end
            @read_only ||= []
            if (@disabled.is_a?(String))
                @disabled = @disabled.split(',').map{ |el| e.strip.to_sym }
            end
            @disabled ||= []
#            @data = params['data'] || {}
            if @_action_rest
                el_label, sub_rest = @_action_rest.split('/', 2)
                sub_rest ||= ''
                @sub_element = @elements.find{ |el| el.label.downcase.gsub(/\s+/, '_') == el_label}
            end
            if (@sub_element)
                @action = :sub
                @scene.sub_element = @sub_element
                @_pass_action = sub_rest
            else
                @action = :form
            end
            @scene.action = @action
            if (@attributes[:show_additional_buttons])
                @attributes[:show_submit_and_new] = true
                @attributes[:show_submit_and_stay] = true
            end
            if (params['submit_and_new'])
                @submit_action = 'submit_and_new'
            elsif (params['submit_and_stay'])
                @submit_action = 'submit_and_stay'
            else
                @submit_action = params['submit']
            end
            @obj ||= load
            @scene.obj = @obj
            init_widgets
            # if (@submit_action)
            # else
            if @obj
                @fixed.each {|k, v| @obj.set(k, v)} if (@fixed)
                set_values(@obj) if @action == :form
            end
#            end
            super
            save(@submit_action) if @submit_action
        end
        
        def init_widgets
            super
            if (@action == :sub)
                @crud = Spider::Components::Crud.new(@request, @response)
                @crud.id = "crud_#{@sub_element.name.to_s}"
                @crud.model = @sub_element.model
                add_widget(@crud)
                @scene.crud = @crud
                @obj = load
                cond = {}
                if @sub_element.integrated?
                    @sub_element.integrated_from.model.primary_keys.each do |key|
                        cond[@sub_element.reverse.to_s+'.'+key.name.to_s] = @obj.get("#{@sub_element.integrated_from.name}.#{key.name}")
                    end
                else
                    @model.primary_keys.each do |key|
                        cond[@sub_element.reverse.to_s+'.'+key.name.to_s] = @obj.get(key)
                    end
                end
                @crud.fixed = cond
                sub_elements = []
                #sub_elements += @sub_element.model.primary_keys.map{ |k| k.name }
                @sub_element.model.elements_array.each do |el|
                    sub_elements << el.name unless el.integrated? || el.model == @model
                end
                @crud.attributes[:table_elements] = sub_elements
            else
                create_inputs
            end
        end
        
        def run
            Spider::Logger.debug("FORM EXECUTING")
            if (@obj)
                
                @scene.form_desc = @model.label.downcase+' '+ (@obj.to_s || '')
                if (@action == :sub)
                    
{'fraction_non_alphanumeric': 0.11051373954599761, 'fraction_numerical': 0.00044802867383512545, 'mean_word_length': 1.5721144613020934, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '1348923', 'n_tokens_mistral': 3868, 'n_tokens_neox': 3745, 'n_words': 874}