<commit_before>﻿' Fw Controller base class
'
' Part of ASP.NET osa framework  www.osalabs.com/osafw/asp.net
' (c) 2009-2017 <NAME> www.osalabs.com

Public MustInherit Class FwController
    Public Shared access_level As Integer = -1 'access level for the controller. fw.config("access_levels") overrides this. -1 (public access), 0(min logged level), 100(max admin level)

    Public Shared route_default_action As String = "" 'supported values - "" (use Default Parser for unknown actions), index (use IndexAction for unknown actions), show (assume action is id and use ShowAction)
    Public base_url As String 'base url for the controller

    Public form_new_defaults As Hashtable   'optional, defaults for the fields in new form
    Public required_fields As String        'optional, default required fields, space-separated
    Public save_fields As String            'required, fields to save from the form to db, space-separated
    Public save_fields_checkboxes As String 'optional, checkboxes fields to save from the form to db, qw string: "field|def_value field2|def_value2"

    Protected fw As FW
    Protected db As DB
    Protected model0 As FwModel
    Protected config As Hashtable                  ' controller config, loaded from template dir/config.json

    Protected list_view As String                  ' table/view to use in list sql, if empty model0.table_name used
    Protected list_orderby As String               ' orderby for the list screen
    Protected list_filter As Hashtable             ' filter values for the list screen
    Protected list_where As String = " 1=1 "       ' where to use in list sql, default is non-deleted records (see setListSearch() )
    Protected list_count As Integer                ' count of list rows returned from db
    Protected list_rows As ArrayList               ' list rows returned from db (array of hashes)
    Protected list_pager As ArrayList              ' pager for the list from FormUtils.get_pager
    Protected list_sortdef As String               ' required for Index, default list sorting: name asc|desc
    Protected list_sortmap As Hashtable            ' required for Index, sortmap fields
    Protected search_fields As String              ' optional, search fields, space-separated 
    'fields to search via $s=list_filter("s"), ! - means exact match, not "like"
    'format: "field1 field2,!field3 field4" => field1 LIKE '%$s%' or (field2 LIKE '%$s%' and field3='$s') or field4 LIKE '%$s%'

    'support of customizable view list
    'map of fileld names to screen names
    Protected is_dynamic As Boolean = False         'true if controller is dynamic, then define below:
    Protected view_list_defaults As String = ""     'qw list of default columns
    Protected view_list_map As Hashtable            'list of all available columns fieldname|visiblename
    Protected view_list_custom As String = ""       'qw list of custom-formatted fields for the list_table

    Protected return_url As String                 ' url to return after SaveAction successfully completed, passed via request
    Protected related_id As String                 ' related id, passed via request. Controller should limit view to items related to this id
    Protected related_field_name As String         ' if set (in Controller) and $related_id passed - list will be filtered on this field

    Public Sub New(Optional fw As FW = Nothing)
        If fw IsNot Nothing Then
            Me.fw = fw
            Me.db = fw.db
        End If
    End Sub

    Public Overridable Sub init(fw As FW)
        Me.fw = fw
        Me.db = fw.db

        return_url = reqs("return_url")
        related_id = reqs("related_id")
    End Sub

    'load controller config from json in template dir (based on base_url)
    Public Overridable Sub loadControllerConfig(Optional config_filename As String = "config.json")
        Dim conf_file0 = base_url.ToLower() & "/" & config_filename
        Dim conf_file = fw.config("template") & "/" & conf_file0
        If Not IO.File.Exists(conf_file) Then Throw New ApplicationException("Controller Config file not found in templates: " & conf_file0)

        Me.config = Utils.jsonDecode(FW.get_file_content(conf_file))
        If Me.config Is Nothing Then Throw New ApplicationException("Controller Config is invalid, check json in templates: " & conf_file0)
        'logger("loaded config:")
        'logger(Me.config)

        'check/conv to str
        required_fields = Utils.f2str(Me.config("required_fields"))

        'save_fields could be defined as qw string - check and convert
        Dim save_fields_raw = Me.config("save_fields")
        If TypeOf save_fields_raw Is IList Then
            save_fields = Utils.qwRevert(save_fields_raw) 'not optimal, but simplest for now
        Else
            save_fields = Utils.f2str(save_fields_raw)
        End If

        'save_fields_checkboxes could be defined as qw string - check and convert
        Dim save_fields_checkboxes_raw = Me.config("save_fields_checkboxes")
        If TypeOf save_fields_checkboxes_raw Is IDictionary Then
            save_fields_checkboxes = Utils.qhRevert(save_fields_checkboxes_raw) 'not optimal, but simplest for now
        Else
            save_fields_checkboxes = Utils.f2str(save_fields_checkboxes_raw)
        End If

        search_fields = Utils.f2str(Me.config("search_fields"))
        list_sortdef = Utils.f2str(Me.config("list_sortdef"))
        list_sortmap = Utils.qh(Utils.f2str(Me.config("list_sortmap")))

        related_field_name = Utils.f2str(Me.config("related_field_name"))

        list_view = Utils.f2str(Me.config("list_view"))

        is_dynamic = Utils.f2bool(Me.config("is_dynamic"))
        If is_dynamic Then
            'Whoah! this is fully dynamic form
            view_list_defaults = Utils.f2str(Me.config("view_list_defaults"))

            'since view_list_map could be defined as qw string or as hashtable - check and convert
            Dim raw_view_list_map = Me.config("view_list_map")
            If TypeOf raw_view_list_map Is IDictionary Then
                view_list_map = raw_view_list_map
            Else
                view_list_map = Utils.qh(raw_view_list_map)
            End If

            view_list_custom = Utils.f2str(Me.config("view_list_custom"))

            list_sortmap = getViewListSortmap() 'just add all fields from view_list_map
            search_fields = getViewListUserFields() 'just search in all visible fields
        End If

    End Sub

    'set of helper functions to return string, integer, date values from request (fw.FORM)
    Public Function req(iname As String) As Object
        Return fw.FORM(iname)
    End Function<commit_msg>Dynamic controller - if search_fields defined - use it instead of search in all list view fields<commit_after>﻿' Fw Controller base class
'
' Part of ASP.NET osa framework  www.osalabs.com/osafw/asp.net
' (c) 2009-2017 <NAME> www.osalabs.com

Public MustInherit Class FwController
    Public Shared access_level As Integer = -1 'access level for the controller. fw.config("access_levels") overrides this. -1 (public access), 0(min logged level), 100(max admin level)

    Public Shared route_default_action As String = "" 'supported values - "" (use Default Parser for unknown actions), index (use IndexAction for unknown actions), show (assume action is id and use ShowAction)
    Public base_url As String 'base url for the controller

    Public form_new_defaults As Hashtable   'optional, defaults for the fields in new form
    Public required_fields As String        'optional, default required fields, space-separated
    Public save_fields As String            'required, fields to save from the form to db, space-separated
    Public save_fields_checkboxes As String 'optional, checkboxes fields to save from the form to db, qw string: "field|def_value field2|def_value2"

    Protected fw As FW
    Protected db As DB
    Protected model0 As FwModel
    Protected config As Hashtable                  ' controller config, loaded from template dir/config.json

    Protected list_view As String                  ' table/view to use in list sql, if empty model0.table_name used
    Protected list_orderby As String               ' orderby for the list screen
    Protected list_filter As Hashtable             ' filter values for the list screen
    Protected list_where As String = " 1=1 "       ' where to use in list sql, default is non-deleted records (see setListSearch() )
    Protected list_count As Integer                ' count of list rows returned from db
    Protected list_rows As ArrayList               ' list rows returned from db (array of hashes)
    Protected list_pager As ArrayList              ' pager for the list from FormUtils.get_pager
    Protected list_sortdef As String               ' required for Index, default list sorting: name asc|desc
    Protected list_sortmap As Hashtable            ' required for Index, sortmap fields
    Protected search_fields As String              ' optional, search fields, space-separated 
    'fields to search via $s=list_filter("s"), ! - means exact match, not "like"
    'format: "field1 field2,!field3 field4" => field1 LIKE '%$s%' or (field2 LIKE '%$s%' and field3='$s') or field4 LIKE '%$s%'

    'support of customizable view list
    'map of fileld names to screen names
    Protected is_dynamic As Boolean = False         'true if controller is dynamic, then define below:
    Protected view_list_defaults As String = ""     'qw list of default columns
    Protected view_list_map As Hashtable            'list of all available columns fieldname|visiblename
    Protected view_list_custom As String = ""       'qw list of custom-formatted fields for the list_table

    Protected return_url As String                 ' url to return after SaveAction successfully completed, passed via request
    Protected related_id As String                 ' related id, passed via request. Controller should limit view to items related to this id
    Protected related_field_name As String         ' if set (in Controller) and $related_id passed - list will be filtered on this field

    Public Sub New(Optional fw As FW = Nothing)
        If fw IsNot Nothing Then
            Me.fw = fw
            Me.db = fw.db
        End If
    End Sub

    Public Overridable Sub init(fw As FW)
        Me.fw = fw
        Me.db = fw.db

        return_url = reqs("return_url")
        related_id = reqs("related_id")
    End Sub

    'load controller config from json in template dir (based on base_url)
    Public Overridable Sub loadControllerConfig(Optional config_filename As String = "config.json")
        Dim conf_file0 = base_url.ToLower() & "/" & config_filename
        Dim conf_file = fw.config("template") & "/" & conf_file0
        If Not IO.File.Exists(conf_file) Then Throw New ApplicationException("Controller Config file not found in templates: " & conf_file0)

        Me.config = Utils.jsonDecode(FW.get_file_content(conf_file))
        If Me.config Is Nothing Then Throw New ApplicationException("Controller Config is invalid, check json in templates: " & conf_file0)
        'logger("loaded config:")
        'logger(Me.config)

        'check/conv to str
        required_fields = Utils.f2str(Me.config("required_fields"))

        'save_fields could be defined as qw string - check and convert
        Dim save_fields_raw = Me.config("save_fields")
        If TypeOf save_fields_raw Is IList Then
            save_fields = Utils.qwRevert(save_fields_raw) 'not optimal, but simplest for now
        Else
            save_fields = Utils.f2str(save_fields_raw)
        End If

        'save_fields_checkboxes could be defined as qw string - check and convert
        Dim save_fields_checkboxes_raw = Me.config("save_fields_checkboxes")
        If TypeOf save_fields_checkboxes_raw Is IDictionary Then
            save_fields_checkboxes = Utils.qhRevert(save_fields_checkboxes_raw) 'not optimal, but simplest for now
        Else
            save_fields_checkboxes = Utils.f2str(save_fields_checkboxes_raw)
        End If

        search_fields = Utils.f2str(Me.config("search_fields"))
        list_sortdef = Utils.f2str(Me.config("list_sortdef"))
        list_sortmap = Utils.qh(Utils.f2str(Me.config("list_sortmap")))

        related_field_name = Utils.f2str(Me.config("related_field_name"))

        list_view = Utils.f2str(Me.config("list_view"))

        is_dynamic = Utils.f2bool(Me.config("is_dynamic"))
        If is_dynamic Then
            'Whoah! this is fully dynamic form
            view_list_defaults = Utils.f2str(Me.config("view_list_defaults"))

            'since view_list_map could be defined as qw string or as hashtable - check and convert
            Dim raw_view_list_map = Me.config("view_list_map")
            If TypeOf raw_view_list_map Is IDictionary Then
                view_list_map = raw_view_list_map
            Else
                view_list_map = Utils.qh(raw_view_list_map)
            End If

            view_list_custom = Utils.f2str(Me.config("view_list_custom"))

            list_sortmap = getViewListSortmap() 'just add all fields from view_list_map
            If search_fields = "" Then search_fields = getViewListUserFields() 'just search in all visible fields if no specific fields defined
        End If

    End Sub

    'set of helper functions to return string, integer, date values from request (fw.FORM)
    Public Function req(iname As String) As Object
        Return fw.FORM(iname)
    End Function
{'fraction_non_alphanumeric': 0.06330329443049194, 'fraction_numerical': 0.006352489289407594, 'mean_word_length': 2.843031507238149, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3080798', 'n_tokens_mistral': 3813, 'n_tokens_neox': 3688, 'n_words': 1351}