<% Class IndustriesController_Class Private m_useLayout Private m_title Private Sub Class_Initialize() m_useLayout = True m_title = "Industries" 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 '------------------------------------------------------------------------------------------------------------------- ' GET /industries '------------------------------------------------------------------------------------------------------------------- Public Sub index() Dim list, uncategorized, csrf Set list = IndustryRepository().All() uncategorized = IndustryRepository().UncategorizedCount() csrf = Csrf_Token("industries") %> <% End Sub '------------------------------------------------------------------------------------------------------------------- ' POST /industries/create '------------------------------------------------------------------------------------------------------------------- Public Sub create() Dim name GuardCsrf name = Left(Trim(F("name")), 100) If Len(name) = 0 Then Fail "Enter an industry name." If IndustryRepository().NameTaken(name, 0) Then Fail """" & name & """ already exists." IndustryRepository().Create name Flash().Success = "Added """ & name & """." Response.Redirect "/industries" End Sub '------------------------------------------------------------------------------------------------------------------- ' POST /industries/:id/rename '------------------------------------------------------------------------------------------------------------------- Public Sub rename(id) Dim ind, name Set ind = Load(id) name = Left(Trim(F("name")), 100) If Len(name) = 0 Then Fail "The name can't be blank." If IndustryRepository().NameTaken(name, ind("id")) Then Fail """" & name & """ already exists. Use Merge to combine them." End If IndustryRepository().Rename ind("id"), name Flash().Success = "Renamed to """ & name & """." Response.Redirect "/industries" End Sub '------------------------------------------------------------------------------------------------------------------- ' POST /industries/:id/merge : move everything from this industry into another one, then delete this one '------------------------------------------------------------------------------------------------------------------- Public Sub merge(id) Dim ind, target, moved Set ind = Load(id) If Not Valid_Id(F("target_id")) Then Fail "Choose the industry to merge into." If CLng(F("target_id")) = ind("id") Then Fail "Choose a different industry to merge into." Set target = IndustryRepository().Find(CLng(F("target_id"))) If target Is Nothing Then Fail "That industry no longer exists." moved = IndustryRepository().Merge(ind("id"), target("id")) Flash().Success = "Merged """ & ind("name") & """ into """ & target("name") & """ (" & moved & " businesses moved)." Response.Redirect "/industries" End Sub '------------------------------------------------------------------------------------------------------------------- ' POST /industries/:id/delete : only when no business uses it '------------------------------------------------------------------------------------------------------------------- Public Sub delete(id) Dim ind Set ind = Load(id) If ind("n") > 0 Then Fail "Can't delete """ & ind("name") & """ while " & ind("n") & " businesses use it. Merge it instead." IndustryRepository().Delete ind("id") Flash().Success = "Deleted """ & ind("name") & """." Response.Redirect "/industries" End Sub '------------------------------------------------------------------------------------------------------------------- Private Function Load(id) GuardCsrf If Not Valid_Id(id) Then Response.Redirect "/404" Dim ind : Set ind = IndustryRepository().Find(CLng(id)) If ind Is Nothing Then Response.Redirect "/404" Set Load = ind End Function Private Sub GuardCsrf() If Not Csrf_Valid("industries", F("csrf")) Then Fail "Invalid form state. Please try again." End Sub Private Sub Fail(msg) Flash().AddError msg Response.Redirect "/industries" End Sub Private Function F(name) F = CStr(Request.Form(name)) End Function End Class Dim IndustriesController_Class__Singleton Function IndustriesController() If IsEmpty(IndustriesController_Class__Singleton) Then Set IndustriesController_Class__Singleton = New IndustriesController_Class End If Set IndustriesController = IndustriesController_Class__Singleton End Function %>