ASP Classic blog framework - BrainOrdure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.0KB

  1. <%
  2. ' Auto-generated Controller: Posts
  3. ' Generated on 5/2/2026 9:47:38 PM
  4. ' Generator: generateController.vbs v1.0
  5. '
  6. ' Remember to:
  7. ' 1. Add to app/controllers/autoload_controllers.asp
  8. ' 2. Register in core/lib.ControllerRegistry.asp
  9. ' 3. Add routes in public/Default.asp
  10. Class PostsController_Class
  11. Private m_useLayout
  12. Private m_title
  13. Private Sub Class_Initialize()
  14. m_useLayout = True
  15. m_title = "Posts"
  16. End Sub
  17. Public Property Get useLayout
  18. useLayout = m_useLayout
  19. End Property
  20. Public Property Let useLayout(v)
  21. m_useLayout = v
  22. End Property
  23. Public Property Get Title
  24. Title = m_title
  25. End Property
  26. Public Property Let Title(v)
  27. m_title = v
  28. End Property
  29. '---------------------------------------------------------------
  30. ' Action: Index
  31. '---------------------------------------------------------------
  32. Public Sub Index()
  33. Dim posts
  34. Set posts = PostsRepository().FindAllWhere("IsPublished = 1", "PublishedDate DESC", 0, 20)
  35. %>
  36. <!--#include file="../views/Posts/index.asp" -->
  37. <%
  38. End Sub
  39. '---------------------------------------------------------------
  40. ' Action: Show
  41. '---------------------------------------------------------------
  42. Public Sub Show(ByVal slug)
  43. Dim requestedSlug : requestedSlug = Trim(CStr(slug))
  44. Dim canonicalSlug : canonicalSlug = NormalizeSlug(requestedSlug)
  45. Dim matches
  46. Set matches = PostsRepository().Find(Array("Slug", requestedSlug, "IsPublished", 1), Empty)
  47. If matches.Count = 0 And canonicalSlug <> requestedSlug Then
  48. Set matches = PostsRepository().Find(Array("Slug", canonicalSlug, "IsPublished", 1), Empty)
  49. End If
  50. If matches.Count = 0 Then
  51. Response.Status = "404 Not Found"
  52. %>
  53. <!--#include file="../views/Error/NotFound.asp" -->
  54. <%
  55. Exit Sub
  56. End If
  57. Dim post
  58. Set post = matches.Front()
  59. If Len(canonicalSlug) > 0 And canonicalSlug <> requestedSlug Then
  60. Response.Redirect PostUrl(canonicalSlug)
  61. End If
  62. Dim comments
  63. Set comments = CommentsRepository().Find(Array("PostID", post.PostID, "IsApproved", 1), "CreatedDate")
  64. %>
  65. <!--#include file="../views/Posts/show.asp" -->
  66. <%
  67. End Sub
  68. '---------------------------------------------------------------
  69. ' Action: New
  70. '---------------------------------------------------------------
  71. Public Sub NewForm()
  72. %>
  73. <!--#include file="../views/Posts/new.asp" -->
  74. <%
  75. End Sub
  76. '---------------------------------------------------------------
  77. ' Action: Create
  78. '---------------------------------------------------------------
  79. Public Sub Create()
  80. Dim title : title = Trim(Request.Form("Title"))
  81. If Len(title) = 0 Then
  82. Flash().AddError "Title is required."
  83. Response.Redirect "/posts/new"
  84. Exit Sub
  85. End If
  86. Dim post
  87. Set post = New POBO_Posts
  88. post.Title = title
  89. post.Summary = Request.Form("Summary")
  90. post.Body = Request.Form("Body")
  91. post.CategoryID = FormNumberOrZero(Request.Form("CategoryID"))
  92. post.Slug = BuildSlug(title)
  93. post.CreatedDate = Now()
  94. post.UpdatedDate = Now()
  95. post.IsPublished = 0
  96. PostsRepository().AddNew post
  97. Flash().Success = "Post created."
  98. Response.Redirect "/posts"
  99. End Sub
  100. '---------------------------------------------------------------
  101. ' Action: Edit
  102. '---------------------------------------------------------------
  103. Public Sub Edit(ByVal id)
  104. Dim post
  105. On Error Resume Next
  106. Set post = PostsRepository().FindByID(id)
  107. If Err.Number <> 0 Then
  108. Err.Clear
  109. On Error GoTo 0
  110. Response.Status = "404 Not Found"
  111. %>
  112. <!--#include file="../views/Error/NotFound.asp" -->
  113. <%
  114. Exit Sub
  115. End If
  116. On Error GoTo 0
  117. %>
  118. <!--#include file="../views/Posts/edit.asp" -->
  119. <%
  120. End Sub
  121. '---------------------------------------------------------------
  122. ' Action: Update
  123. '---------------------------------------------------------------
  124. Public Sub Update(ByVal id)
  125. Dim post
  126. On Error Resume Next
  127. Set post = PostsRepository().FindByID(id)
  128. If Err.Number <> 0 Then
  129. Err.Clear
  130. On Error GoTo 0
  131. Response.Status = "404 Not Found"
  132. %>
  133. <!--#include file="../views/Error/NotFound.asp" -->
  134. <%
  135. Exit Sub
  136. End If
  137. On Error GoTo 0
  138. Dim title : title = Trim(Request.Form("Title"))
  139. If Len(title) = 0 Then
  140. Flash().AddError "Title is required."
  141. Response.Redirect "/posts/" & Server.URLEncode(CStr(id)) & "/edit"
  142. Exit Sub
  143. End If
  144. post.Title = title
  145. post.Summary = Request.Form("Summary")
  146. post.Body = Request.Form("Body")
  147. post.CategoryID = FormNumberOrZero(Request.Form("CategoryID"))
  148. post.Slug = BuildSlug(title)
  149. post.UpdatedDate = Now()
  150. PostsRepository().Update post
  151. Flash().Success = "Post updated."
  152. Response.Redirect "/posts"
  153. End Sub
  154. '---------------------------------------------------------------
  155. ' Action: Delete
  156. '---------------------------------------------------------------
  157. Public Sub Delete(ByVal id)
  158. PostsRepository().Delete id
  159. Flash().Success = "Post deleted."
  160. Response.Redirect "/posts"
  161. End Sub
  162. Private Function FormNumberOrZero(ByVal value)
  163. If IsNumeric(value) Then
  164. FormNumberOrZero = CLng(value)
  165. Else
  166. FormNumberOrZero = 0
  167. End If
  168. End Function
  169. Private Function BuildSlug(ByVal value)
  170. Dim raw : raw = LCase(Trim(CStr(value)))
  171. Dim i, ch, slug, previousDash
  172. slug = ""
  173. previousDash = False
  174. For i = 1 To Len(raw)
  175. ch = Mid(raw, i, 1)
  176. If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Then
  177. slug = slug & ch
  178. previousDash = False
  179. ElseIf Not previousDash And Len(slug) > 0 Then
  180. slug = slug & "-"
  181. previousDash = True
  182. End If
  183. Next
  184. Do While Right(slug, 1) = "-"
  185. slug = Left(slug, Len(slug) - 1)
  186. Loop
  187. If Len(slug) = 0 Then slug = "post"
  188. BuildSlug = slug
  189. End Function
  190. End Class
  191. ' Singleton instance
  192. Dim PostsController_Class__Singleton
  193. Function PostsController()
  194. If IsEmpty(PostsController_Class__Singleton) Then
  195. Set PostsController_Class__Singleton = New PostsController_Class
  196. End If
  197. Set PostsController = PostsController_Class__Singleton
  198. End Function
  199. %>

Powered by TurnKey Linux.