ASP Classic blog framework - BrainOrdure
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

219 行
6.4KB

  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 matches
  44. Set matches = PostsRepository().Find(Array("Slug", slug, "IsPublished", 1), Empty)
  45. If matches.Count = 0 Then
  46. Response.Status = "404 Not Found"
  47. %>
  48. <!--#include file="../views/Error/NotFound.asp" -->
  49. <%
  50. Exit Sub
  51. End If
  52. Dim post
  53. Set post = matches.Front()
  54. %>
  55. <!--#include file="../views/Posts/show.asp" -->
  56. <%
  57. End Sub
  58. '---------------------------------------------------------------
  59. ' Action: New
  60. '---------------------------------------------------------------
  61. Public Sub NewForm()
  62. %>
  63. <!--#include file="../views/Posts/new.asp" -->
  64. <%
  65. End Sub
  66. '---------------------------------------------------------------
  67. ' Action: Create
  68. '---------------------------------------------------------------
  69. Public Sub Create()
  70. Dim title : title = Trim(Request.Form("Title"))
  71. If Len(title) = 0 Then
  72. Flash().AddError "Title is required."
  73. Response.Redirect "/posts/new"
  74. Exit Sub
  75. End If
  76. Dim post
  77. Set post = New POBO_Posts
  78. post.Title = title
  79. post.Summary = Request.Form("Summary")
  80. post.Body = Request.Form("Body")
  81. post.CategoryID = FormNumberOrZero(Request.Form("CategoryID"))
  82. post.Slug = BuildSlug(title)
  83. post.CreatedDate = Now()
  84. post.UpdatedDate = Now()
  85. post.IsPublished = 0
  86. PostsRepository().AddNew post
  87. Flash().Success = "Post created."
  88. Response.Redirect "/posts"
  89. End Sub
  90. '---------------------------------------------------------------
  91. ' Action: Edit
  92. '---------------------------------------------------------------
  93. Public Sub Edit(ByVal id)
  94. Dim post
  95. On Error Resume Next
  96. Set post = PostsRepository().FindByID(id)
  97. If Err.Number <> 0 Then
  98. Err.Clear
  99. On Error GoTo 0
  100. Response.Status = "404 Not Found"
  101. %>
  102. <!--#include file="../views/Error/NotFound.asp" -->
  103. <%
  104. Exit Sub
  105. End If
  106. On Error GoTo 0
  107. %>
  108. <!--#include file="../views/Posts/edit.asp" -->
  109. <%
  110. End Sub
  111. '---------------------------------------------------------------
  112. ' Action: Update
  113. '---------------------------------------------------------------
  114. Public Sub Update(ByVal id)
  115. Dim post
  116. On Error Resume Next
  117. Set post = PostsRepository().FindByID(id)
  118. If Err.Number <> 0 Then
  119. Err.Clear
  120. On Error GoTo 0
  121. Response.Status = "404 Not Found"
  122. %>
  123. <!--#include file="../views/Error/NotFound.asp" -->
  124. <%
  125. Exit Sub
  126. End If
  127. On Error GoTo 0
  128. Dim title : title = Trim(Request.Form("Title"))
  129. If Len(title) = 0 Then
  130. Flash().AddError "Title is required."
  131. Response.Redirect "/posts/" & Server.URLEncode(CStr(id)) & "/edit"
  132. Exit Sub
  133. End If
  134. post.Title = title
  135. post.Summary = Request.Form("Summary")
  136. post.Body = Request.Form("Body")
  137. post.CategoryID = FormNumberOrZero(Request.Form("CategoryID"))
  138. post.Slug = BuildSlug(title)
  139. post.UpdatedDate = Now()
  140. PostsRepository().Update post
  141. Flash().Success = "Post updated."
  142. Response.Redirect "/posts"
  143. End Sub
  144. '---------------------------------------------------------------
  145. ' Action: Delete
  146. '---------------------------------------------------------------
  147. Public Sub Delete(ByVal id)
  148. PostsRepository().Delete id
  149. Flash().Success = "Post deleted."
  150. Response.Redirect "/posts"
  151. End Sub
  152. Private Function FormNumberOrZero(ByVal value)
  153. If IsNumeric(value) Then
  154. FormNumberOrZero = CLng(value)
  155. Else
  156. FormNumberOrZero = 0
  157. End If
  158. End Function
  159. Private Function BuildSlug(ByVal value)
  160. Dim raw : raw = LCase(Trim(CStr(value)))
  161. Dim i, ch, slug, previousDash
  162. slug = ""
  163. previousDash = False
  164. For i = 1 To Len(raw)
  165. ch = Mid(raw, i, 1)
  166. If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Then
  167. slug = slug & ch
  168. previousDash = False
  169. ElseIf Not previousDash And Len(slug) > 0 Then
  170. slug = slug & "-"
  171. previousDash = True
  172. End If
  173. Next
  174. Do While Right(slug, 1) = "-"
  175. slug = Left(slug, Len(slug) - 1)
  176. Loop
  177. If Len(slug) = 0 Then slug = "post"
  178. BuildSlug = slug
  179. End Function
  180. End Class
  181. ' Singleton instance
  182. Dim PostsController_Class__Singleton
  183. Function PostsController()
  184. If IsEmpty(PostsController_Class__Singleton) Then
  185. Set PostsController_Class__Singleton = New PostsController_Class
  186. End If
  187. Set PostsController = PostsController_Class__Singleton
  188. End Function
  189. %>

Powered by TurnKey Linux.