|
- <%
- ' Auto-generated Controller: Posts
- ' Generated on 5/2/2026 9:47:38 PM
- ' Generator: generateController.vbs v1.0
- '
- ' Remember to:
- ' 1. Add to app/controllers/autoload_controllers.asp
- ' 2. Register in core/lib.ControllerRegistry.asp
- ' 3. Add routes in public/Default.asp
-
-
- Class PostsController_Class
- Private m_useLayout
- Private m_title
-
- Private Sub Class_Initialize()
- m_useLayout = True
- m_title = "Posts"
- 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
-
- '---------------------------------------------------------------
- ' Action: Index
- '---------------------------------------------------------------
- Public Sub Index()
- Dim posts
- Set posts = PostsRepository().FindAllWhere("IsPublished = 1", "PublishedDate DESC", 0, 20)
- %>
- <!--#include file="../views/Posts/index.asp" -->
- <%
- End Sub
-
- '---------------------------------------------------------------
- ' Action: Show
- '---------------------------------------------------------------
- Public Sub Show(ByVal slug)
- Dim requestedSlug : requestedSlug = Trim(CStr(slug))
- Dim canonicalSlug : canonicalSlug = NormalizeSlug(requestedSlug)
-
- Dim matches
- Set matches = PostsRepository().Find(Array("Slug", requestedSlug, "IsPublished", 1), Empty)
-
- If matches.Count = 0 And canonicalSlug <> requestedSlug Then
- Set matches = PostsRepository().Find(Array("Slug", canonicalSlug, "IsPublished", 1), Empty)
- End If
-
- If matches.Count = 0 Then
- Response.Status = "404 Not Found"
- %>
- <!--#include file="../views/Error/NotFound.asp" -->
- <%
- Exit Sub
- End If
-
- Dim post
- Set post = matches.Front()
-
- If Len(canonicalSlug) > 0 And canonicalSlug <> requestedSlug Then
- Response.Redirect PostPath(canonicalSlug)
- End If
-
- Dim comments
- Set comments = CommentsRepository().Find(Array("PostID", post.PostID, "IsApproved", 1), "CreatedDate")
- %>
- <!--#include file="../views/Posts/show.asp" -->
- <%
- End Sub
-
- '---------------------------------------------------------------
- ' Action: New
- '---------------------------------------------------------------
- Public Sub NewForm()
- %>
- <!--#include file="../views/Posts/new.asp" -->
- <%
- End Sub
-
- '---------------------------------------------------------------
- ' Action: Create
- '---------------------------------------------------------------
- Public Sub Create()
- Dim title : title = Trim(Request.Form("Title"))
- If Len(title) = 0 Then
- Flash().AddError "Title is required."
- Response.Redirect "/posts/new"
- Exit Sub
- End If
-
- Dim post
- Set post = New POBO_Posts
-
- post.Title = title
- post.Summary = Request.Form("Summary")
- post.Body = Request.Form("Body")
- post.CategoryID = FormNumberOrZero(Request.Form("CategoryID"))
- post.Slug = BuildSlug(title)
- post.CreatedDate = Now()
- post.UpdatedDate = Now()
- post.IsPublished = 0
-
- PostsRepository().AddNew post
- Flash().Success = "Post created."
- Response.Redirect "/posts"
- End Sub
-
- '---------------------------------------------------------------
- ' Action: Edit
- '---------------------------------------------------------------
- Public Sub Edit(ByVal id)
- Dim post
- On Error Resume Next
- Set post = PostsRepository().FindByID(id)
- If Err.Number <> 0 Then
- Err.Clear
- On Error GoTo 0
- Response.Status = "404 Not Found"
- %>
- <!--#include file="../views/Error/NotFound.asp" -->
- <%
- Exit Sub
- End If
- On Error GoTo 0
- %>
- <!--#include file="../views/Posts/edit.asp" -->
- <%
- End Sub
-
- '---------------------------------------------------------------
- ' Action: Update
- '---------------------------------------------------------------
- Public Sub Update(ByVal id)
- Dim post
- On Error Resume Next
- Set post = PostsRepository().FindByID(id)
- If Err.Number <> 0 Then
- Err.Clear
- On Error GoTo 0
- Response.Status = "404 Not Found"
- %>
- <!--#include file="../views/Error/NotFound.asp" -->
- <%
- Exit Sub
- End If
- On Error GoTo 0
-
- Dim title : title = Trim(Request.Form("Title"))
- If Len(title) = 0 Then
- Flash().AddError "Title is required."
- Response.Redirect "/posts/" & Server.URLEncode(CStr(id)) & "/edit"
- Exit Sub
- End If
-
- post.Title = title
- post.Summary = Request.Form("Summary")
- post.Body = Request.Form("Body")
- post.CategoryID = FormNumberOrZero(Request.Form("CategoryID"))
- post.Slug = BuildSlug(title)
- post.UpdatedDate = Now()
-
- PostsRepository().Update post
- Flash().Success = "Post updated."
- Response.Redirect "/posts"
- End Sub
-
- '---------------------------------------------------------------
- ' Action: Delete
- '---------------------------------------------------------------
- Public Sub Delete(ByVal id)
- PostsRepository().Delete id
- Flash().Success = "Post deleted."
- Response.Redirect "/posts"
- End Sub
-
- Private Function FormNumberOrZero(ByVal value)
- If IsNumeric(value) Then
- FormNumberOrZero = CLng(value)
- Else
- FormNumberOrZero = 0
- End If
- End Function
-
- Private Function BuildSlug(ByVal value)
- Dim raw : raw = LCase(Trim(CStr(value)))
- Dim i, ch, slug, previousDash
- slug = ""
- previousDash = False
-
- For i = 1 To Len(raw)
- ch = Mid(raw, i, 1)
- If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Then
- slug = slug & ch
- previousDash = False
- ElseIf Not previousDash And Len(slug) > 0 Then
- slug = slug & "-"
- previousDash = True
- End If
- Next
-
- Do While Right(slug, 1) = "-"
- slug = Left(slug, Len(slug) - 1)
- Loop
-
- If Len(slug) = 0 Then slug = "post"
- BuildSlug = slug
- End Function
-
- End Class
-
- ' Singleton instance
- Dim PostsController_Class__Singleton
- Function PostsController()
- If IsEmpty(PostsController_Class__Singleton) Then
- Set PostsController_Class__Singleton = New PostsController_Class
- End If
- Set PostsController = PostsController_Class__Singleton
- End Function
- %>
|