|
- <%
- ' TerritoryController - CRUD controller for Territories
- ' Generated for NorthTerritory app
- '
- ' Dependencies:
- ' - app/models/POBO_Territories.asp
- ' - app/models/TerritoriesRepository.asp
- %>
- <!--#include file="../models/POBO_Territories.asp" -->
- <!--#include file="../models/TerritoriesRepository.asp" -->
- <%
-
- Class TerritoryController_Class
- Private m_useLayout
- Private m_title
-
- ' Public properties for views
- Public territories ' LinkedList for Index
- Public territory ' Single POBO for Show/Edit
- Public territoryHouseholdCounts
- Public territoryStreets ' LinkedList of street names for Show
-
- ' Pagination properties
- Public currentPage
- Public pageCount
- Public recordCount
- Public perPage
- Public searchTerm
-
- Private Sub Class_Initialize()
- m_useLayout = True
- m_title = "Territories"
- currentPage = 1
- pageCount = 0
- recordCount = 0
- perPage = 20
- searchTerm = ""
- Set territoryHouseholdCounts = Nothing
- End Sub
-
- Public Property Get useLayout
- useLayout = m_useLayout
- End Property
-
- Public Property Let useLayout(v)
- m_useLayout = v
- End Property
-
- Public Property Get Title
- Title = m_title
- End Property
-
- Public Property Let Title(v)
- m_title = v
- End Property
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Index - List all territories with pagination and search
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Index()
- ' Get pagination params
- If Request.QueryString("page") <> "" And IsNumeric(Request.QueryString("page")) Then
- currentPage = CInt(Request.QueryString("page"))
- If currentPage < 1 Then currentPage = 1
- End If
-
- ' Get search param
- searchTerm = Trim(Request.QueryString("q") & "")
-
- ' Fetch territories with pagination
- If searchTerm <> "" Then
- ' Search in Name and Description columns
- Set territories = TerritoriesRepository.SearchTablePaged( _
- Array("Name", "Description"), _
- searchTerm, _
- Empty, _
- perPage, _
- currentPage, _
- pageCount, _
- recordCount _
- )
- Else
- Set territories = TerritoriesRepository.FindPaged( _
- Empty, _
- Empty, _
- perPage, _
- currentPage, _
- pageCount, _
- recordCount _
- )
- End If
-
- Set territoryHouseholdCounts = HouseholdsRepository.GetCountsByTerritory()
-
- %> <!--#include file="../views/Territory/index.asp" --> <%
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Show - Display a single territory
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Show(id)
- On Error Resume Next
- Set territory = TerritoriesRepository.FindByID(id)
- If Err.Number <> 0 Or territory Is Nothing Then
- On Error GoTo 0
- Flash().AddError "Territory not found."
- Response.Redirect "/territories"
- Exit Sub
- End If
- On Error GoTo 0
-
- ' Load distinct street names for this territory
- Set territoryStreets = HouseholdsRepository.GetDistinctStreetsByTerritory(id)
-
- %> <!--#include file="../views/Territory/show.asp" --> <%
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Create - Display form for new territory
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Create()
- Set territory = New POBO_Territories
- %> <!--#include file="../views/Territory/create.asp" --> <%
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Store - Save new territory
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Store()
- Set territory = New POBO_Territories
- territory.Name = Trim(Request.Form("Name"))
- territory.Description = Trim(Request.Form("Description"))
- territory.Coordinates = Trim(Request.Form("Coordinates"))
-
- ' Validation
- If territory.Name = "" Then
- Flash().AddError "Name is required."
- Response.Redirect "/territories/new"
- Exit Sub
- End If
-
- TerritoriesRepository.AddNew territory
-
- Flash().Success = "Territory created successfully."
- Response.Redirect "/territories/" & territory.Id
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Edit - Display form to edit territory
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Edit(id)
- On Error Resume Next
- Set territory = TerritoriesRepository.FindByID(id)
- If Err.Number <> 0 Or territory Is Nothing Then
- On Error GoTo 0
- Flash().AddError "Territory not found."
- Response.Redirect "/territories"
- Exit Sub
- End If
- On Error GoTo 0
-
- %> <!--#include file="../views/Territory/edit.asp" --> <%
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Update - Save changes to territory
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Update(id)
- On Error Resume Next
- Set territory = TerritoriesRepository.FindByID(id)
- If Err.Number <> 0 Or territory Is Nothing Then
- On Error GoTo 0
- Flash().AddError "Territory not found."
- Response.Redirect "/territories"
- Exit Sub
- End If
- On Error GoTo 0
-
- territory.Name = Trim(Request.Form("Name"))
- territory.Description = Trim(Request.Form("Description"))
- territory.Coordinates = Trim(Request.Form("Coordinates"))
-
- ' Validation
- If territory.Name = "" Then
- Flash().AddError "Name is required."
- Response.Redirect "/territories/" & id & "/edit"
- Exit Sub
- End If
-
- TerritoriesRepository.Update territory
-
- Flash().Success = "Territory updated successfully."
- Response.Redirect "/territories/" & id
- End Sub
-
- '-------------------------------------------------------------------------------------------------------------------
- ' Delete - Remove a territory
- '-------------------------------------------------------------------------------------------------------------------
- Public Sub Delete(id)
- On Error Resume Next
- Set territory = TerritoriesRepository.FindByID(id)
- If Err.Number <> 0 Or territory Is Nothing Then
- On Error GoTo 0
- Flash().AddError "Territory not found."
- Response.Redirect "/territories"
- Exit Sub
- End If
- On Error GoTo 0
-
- TerritoriesRepository.Delete id
-
- Flash().Success = "Territory deleted successfully."
- Response.Redirect "/territories"
- End Sub
-
- End Class
-
- ' Singleton instance
- Dim TerritoryController_Class__Singleton
- Function TerritoryController()
- If IsEmpty(TerritoryController_Class__Singleton) Then
- Set TerritoryController_Class__Singleton = New TerritoryController_Class
- End If
- Set TerritoryController = TerritoryController_Class__Singleton
- End Function
- %>
|