123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- package modules
- import (
- "html"
- "net/http"
- "strings"
- "golang-fave/engine/assets"
- "golang-fave/engine/builder"
- "golang-fave/engine/consts"
- "golang-fave/engine/fetdata"
- "golang-fave/engine/sqlw"
- "golang-fave/engine/utils"
- "golang-fave/engine/wrapper"
- )
- func (this *Modules) RegisterModule_Blog() *Module {
- return this.newModule(MInfo{
- Mount: "blog",
- Name: "Blog",
- Order: 1,
- System: false,
- Icon: assets.SysSvgIconList,
- Sub: &[]MISub{
- {Mount: "default", Name: "List of posts", Show: true, Icon: assets.SysSvgIconList},
- {Mount: "add", Name: "Add new post", Show: true, Icon: assets.SysSvgIconPlus},
- {Mount: "modify", Name: "Modify post", Show: false},
- {Sep: true, Show: true},
- {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
- {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
- {Mount: "categories-modify", Name: "Modify category", Show: false},
- },
- }, func(wrap *wrapper.Wrapper) {
- if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
- // Blog category
- row := &utils.MySql_blog_category{}
- rou := &utils.MySql_user{}
- err := wrap.DB.QueryRow(
- wrap.R.Context(),
- `SELECT
- main.id,
- main.user,
- main.name,
- main.alias,
- main.lft,
- main.rgt,
- main.depth,
- parent.id AS parent_id,
- fave_users.id,
- fave_users.first_name,
- fave_users.last_name,
- fave_users.email,
- fave_users.admin,
- fave_users.active
- FROM
- (
- SELECT
- node.id,
- node.user,
- node.name,
- node.alias,
- node.lft,
- node.rgt,
- (COUNT(parent.id) - 1) AS depth
- FROM
- fave_blog_cats AS node,
- fave_blog_cats AS parent
- WHERE
- node.lft BETWEEN parent.lft AND parent.rgt
- GROUP BY
- node.id
- ORDER BY
- node.lft ASC
- ) AS main
- LEFT JOIN (
- SELECT
- node.id,
- node.user,
- node.name,
- node.alias,
- node.lft,
- node.rgt,
- (COUNT(parent.id) - 0) AS depth
- FROM
- fave_blog_cats AS node,
- fave_blog_cats AS parent
- WHERE
- node.lft BETWEEN parent.lft AND parent.rgt
- GROUP BY
- node.id
- ORDER BY
- node.lft ASC
- ) AS parent ON
- parent.depth = main.depth AND
- main.lft > parent.lft AND
- main.rgt < parent.rgt
- LEFT JOIN fave_users ON fave_users.id = main.user
- WHERE
- main.id > 1 AND
- main.alias = ?
- ORDER BY
- main.lft ASC
- ;`,
- wrap.UrlArgs[2],
- ).Scan(
- &row.A_id,
- &row.A_user,
- &row.A_name,
- &row.A_alias,
- &row.A_lft,
- &row.A_rgt,
- &row.A_depth,
- &row.A_parent,
- &rou.A_id,
- &rou.A_first_name,
- &rou.A_last_name,
- &rou.A_email,
- &rou.A_admin,
- &rou.A_active,
- )
- if err != nil && err != wrapper.ErrNoRows {
- // System error 500
- wrap.LogCpError(&err)
- utils.SystemErrorPageEngine(wrap.W, err)
- return
- } else if err == wrapper.ErrNoRows {
- // User error 404 page
- wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
- return
- }
- // Fix url
- if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
- http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
- return
- }
- // Render template
- wrap.RenderFrontEnd("blog-category", fetdata.New(wrap, false, row, rou), http.StatusOK)
- return
- } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] != "" {
- // Blog post
- row := &utils.MySql_blog_post{}
- rou := &utils.MySql_user{}
- err := wrap.DB.QueryRow(
- wrap.R.Context(),
- `SELECT
- fave_blog_posts.id,
- fave_blog_posts.user,
- fave_blog_posts.name,
- fave_blog_posts.alias,
- fave_blog_posts.category,
- fave_blog_posts.briefly,
- fave_blog_posts.content,
- UNIX_TIMESTAMP(fave_blog_posts.datetime) as datetime,
- fave_blog_posts.active,
- fave_users.id,
- fave_users.first_name,
- fave_users.last_name,
- fave_users.email,
- fave_users.admin,
- fave_users.active
- FROM
- fave_blog_posts
- LEFT JOIN fave_users ON fave_users.id = fave_blog_posts.user
- WHERE
- fave_blog_posts.active = 1 and
- fave_blog_posts.alias = ?
- LIMIT 1;`,
- wrap.UrlArgs[1],
- ).Scan(
- &row.A_id,
- &row.A_user,
- &row.A_name,
- &row.A_alias,
- &row.A_category,
- &row.A_briefly,
- &row.A_content,
- &row.A_datetime,
- &row.A_active,
- &rou.A_id,
- &rou.A_first_name,
- &rou.A_last_name,
- &rou.A_email,
- &rou.A_admin,
- &rou.A_active,
- )
- if err != nil && err != wrapper.ErrNoRows {
- // System error 500
- wrap.LogCpError(&err)
- utils.SystemErrorPageEngine(wrap.W, err)
- return
- } else if err == wrapper.ErrNoRows {
- // User error 404 page
- wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
- return
- }
- // Fix url
- if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
- http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
- return
- }
- // Render template
- wrap.RenderFrontEnd("blog-post", fetdata.New(wrap, false, row, rou), http.StatusOK)
- return
- } else if len(wrap.UrlArgs) == 1 && wrap.UrlArgs[0] == "blog" {
- // Blog
- // Fix url
- if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
- http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
- return
- }
- // Render template
- wrap.RenderFrontEnd("blog", fetdata.New(wrap, false, nil, nil), http.StatusOK)
- return
- } else if (*wrap.Config).Engine.MainModule == 1 {
- // Render template
- wrap.RenderFrontEnd("blog", fetdata.New(wrap, false, nil, nil), http.StatusOK)
- return
- }
- // User error 404 page
- wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
- }, func(wrap *wrapper.Wrapper) (string, string, string) {
- content := ""
- sidebar := ""
- if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
- content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
- {Name: "List of posts"},
- })
- content += builder.DataTable(
- wrap,
- "fave_blog_posts",
- "id",
- "DESC",
- &[]builder.DataTableRow{
- {
- DBField: "id",
- },
- {
- DBField: "name",
- NameInTable: "Post / URL",
- CallBack: func(values *[]string) string {
- name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
- alias := html.EscapeString((*values)[2])
- return `<div>` + name + `</div><div><small>/blog/` + alias + `/</small></div>`
- },
- },
- {
- DBField: "alias",
- },
- {
- DBField: "datetime",
- DBExp: "UNIX_TIMESTAMP(`datetime`)",
- NameInTable: "Date / Time",
- Classes: "d-none d-md-table-cell",
- CallBack: func(values *[]string) string {
- t := int64(utils.StrToInt((*values)[3]))
- return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
- `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
- },
- },
- {
- DBField: "active",
- NameInTable: "Active",
- Classes: "d-none d-sm-table-cell",
- CallBack: func(values *[]string) string {
- return builder.CheckBox(utils.StrToInt((*values)[4]))
- },
- },
- },
- func(values *[]string) string {
- return builder.DataTableAction(&[]builder.DataTableActionRow{
- {
- Icon: assets.SysSvgIconView,
- Href: `/blog/` + (*values)[2] + `/`,
- Hint: "View",
- Target: "_blank",
- },
- {
- Icon: assets.SysSvgIconEdit,
- Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
- Hint: "Edit",
- },
- {
- Icon: assets.SysSvgIconRemove,
- Href: "javascript:fave.ActionDataTableDelete(this,'blog-delete','" +
- (*values)[0] + "','Are you sure want to delete post?');",
- Hint: "Delete",
- Classes: "delete",
- },
- })
- },
- "/cp/"+wrap.CurrModule+"/",
- nil,
- nil,
- true,
- )
- } else if wrap.CurrSubModule == "categories" {
- content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
- {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
- {Name: "List of categories"},
- })
- content += builder.DataTable(
- wrap,
- "fave_blog_cats",
- "id",
- "ASC",
- &[]builder.DataTableRow{
- {
- DBField: "id",
- },
- {
- DBField: "user",
- },
- {
- DBField: "name",
- NameInTable: "Category",
- CallBack: func(values *[]string) string {
- depth := utils.StrToInt((*values)[4]) - 1
- if depth < 0 {
- depth = 0
- }
- sub := strings.Repeat("— ", depth)
- name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
- return `<div>` + name + `</div>`
- },
- },
- {
- DBField: "alias",
- },
- {
- DBField: "depth",
- },
- },
- func(values *[]string) string {
- return builder.DataTableAction(&[]builder.DataTableActionRow{
- {
- Icon: assets.SysSvgIconView,
- Href: `/blog/category/` + (*values)[3] + `/`,
- Hint: "View",
- Target: "_blank",
- },
- {
- Icon: assets.SysSvgIconEdit,
- Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
- Hint: "Edit",
- },
- {
- Icon: assets.SysSvgIconRemove,
- Href: "javascript:fave.ActionDataTableDelete(this,'blog-categories-delete','" +
- (*values)[0] + "','Are you sure want to delete category?');",
- Hint: "Delete",
- Classes: "delete",
- },
- })
- },
- "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
- nil,
- func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
- return wrap.DB.Query(
- wrap.R.Context(),
- `SELECT
- node.id,
- node.user,
- node.name,
- node.alias,
- (COUNT(parent.id) - 1) AS depth
- FROM
- fave_blog_cats AS node,
- fave_blog_cats AS parent
- WHERE
- node.lft BETWEEN parent.lft AND parent.rgt AND
- node.id > 1
- GROUP BY
- node.id
- ORDER BY
- node.lft ASC
- ;`,
- )
- },
- false,
- )
- } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
- if wrap.CurrSubModule == "add" {
- content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
- {Name: "Add new post"},
- })
- } else {
- content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
- {Name: "Modify post"},
- })
- }
- data := utils.MySql_blog_post{
- A_id: 0,
- A_user: 0,
- A_name: "",
- A_alias: "",
- A_category: 0,
- A_content: "",
- A_datetime: 0,
- A_active: 0,
- }
- if wrap.CurrSubModule == "modify" {
- if len(wrap.UrlArgs) != 3 {
- return "", "", ""
- }
- if !utils.IsNumeric(wrap.UrlArgs[2]) {
- return "", "", ""
- }
- err := wrap.DB.QueryRow(
- wrap.R.Context(),
- `SELECT
- id,
- user,
- name,
- alias,
- category,
- briefly,
- content,
- active
- FROM
- fave_blog_posts
- WHERE
- id = ?
- LIMIT 1;`,
- utils.StrToInt(wrap.UrlArgs[2]),
- ).Scan(
- &data.A_id,
- &data.A_user,
- &data.A_name,
- &data.A_alias,
- &data.A_category,
- &data.A_briefly,
- &data.A_content,
- &data.A_active,
- )
- if *wrap.LogCpError(&err) != nil {
- return "", "", ""
- }
- }
- // All post current categories
- var selids []int
- if data.A_id > 0 {
- rows, err := wrap.DB.Query(wrap.R.Context(), "SELECT category_id FROM fave_blog_cat_post_rel WHERE post_id = ?;", data.A_id)
- if err == nil {
- defer rows.Close()
- values := make([]int, 1)
- scan := make([]interface{}, len(values))
- for i := range values {
- scan[i] = &values[i]
- }
- for rows.Next() {
- err = rows.Scan(scan...)
- if *wrap.LogCpError(&err) == nil {
- selids = append(selids, int(values[0]))
- }
- }
- }
- }
- btn_caption := "Add"
- if wrap.CurrSubModule == "modify" {
- btn_caption = "Save"
- }
- content += builder.DataForm(wrap, []builder.DataFormField{
- {
- Kind: builder.DFKHidden,
- Name: "action",
- Value: "blog-modify",
- },
- {
- Kind: builder.DFKHidden,
- Name: "id",
- Value: utils.IntToStr(data.A_id),
- },
- {
- Kind: builder.DFKText,
- Caption: "Post name",
- Name: "name",
- Value: data.A_name,
- Required: true,
- Min: "1",
- Max: "255",
- },
- {
- Kind: builder.DFKText,
- Caption: "Post alias",
- Name: "alias",
- Value: data.A_alias,
- Hint: "Example: our-news",
- Max: "255",
- },
- {
- Kind: builder.DFKText,
- Caption: "Category",
- Name: "category",
- Value: "0",
- CallBack: func(field *builder.DataFormField) string {
- return `<div class="form-group n4">` +
- `<div class="row">` +
- `<div class="col-md-3">` +
- `<label for="lbl_category">Category</label>` +
- `</div>` +
- `<div class="col-md-9">` +
- `<div>` +
- `<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
- `<option title="Nothing selected" value="0">—</option>` +
- this.blog_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
- `</select>` +
- `</div>` +
- `</div>` +
- `</div>` +
- `</div>`
- },
- },
- {
- Kind: builder.DFKText,
- Caption: "Categories",
- Name: "cats",
- Value: "0",
- CallBack: func(field *builder.DataFormField) string {
- return `<div class="form-group n5">` +
- `<div class="row">` +
- `<div class="col-md-3">` +
- `<label for="lbl_parent">Categories</label>` +
- `</div>` +
- `<div class="col-md-9">` +
- `<div>` +
- `<select class="selectpicker form-control" id="lbl_cats" name="cats[]" data-live-search="true" multiple>` +
- this.blog_GetCategorySelectOptions(wrap, 0, 0, selids) +
- `</select>` +
- `</div>` +
- `</div>` +
- `</div>` +
- `</div>`
- },
- },
- {
- Kind: builder.DFKTextArea,
- Caption: "Briefly",
- Name: "briefly",
- Value: data.A_briefly,
- Classes: "briefly wysiwyg",
- },
- {
- Kind: builder.DFKTextArea,
- Caption: "Post content",
- Name: "content",
- Value: data.A_content,
- Classes: "wysiwyg",
- },
- {
- Kind: builder.DFKCheckBox,
- Caption: "Active",
- Name: "active",
- Value: utils.IntToStr(data.A_active),
- },
- {
- Kind: builder.DFKSubmit,
- Value: btn_caption,
- Target: "add-edit-button",
- },
- })
- if wrap.CurrSubModule == "add" {
- sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
- } else {
- sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
- }
- } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
- if wrap.CurrSubModule == "categories-add" {
- content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
- {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
- {Name: "Add new category"},
- })
- } else {
- content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
- {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
- {Name: "Modify category"},
- })
- }
- data := utils.MySql_blog_category{
- A_id: 0,
- A_user: 0,
- A_name: "",
- A_alias: "",
- A_lft: 0,
- A_rgt: 0,
- }
- if wrap.CurrSubModule == "categories-modify" {
- if len(wrap.UrlArgs) != 3 {
- return "", "", ""
- }
- if !utils.IsNumeric(wrap.UrlArgs[2]) {
- return "", "", ""
- }
- err := wrap.DB.QueryRow(
- wrap.R.Context(),
- `SELECT
- id,
- user,
- name,
- alias,
- lft,
- rgt
- FROM
- fave_blog_cats
- WHERE
- id = ?
- LIMIT 1;`,
- utils.StrToInt(wrap.UrlArgs[2]),
- ).Scan(
- &data.A_id,
- &data.A_user,
- &data.A_name,
- &data.A_alias,
- &data.A_lft,
- &data.A_rgt,
- )
- if *wrap.LogCpError(&err) != nil {
- return "", "", ""
- }
- }
- btn_caption := "Add"
- if wrap.CurrSubModule == "categories-modify" {
- btn_caption = "Save"
- }
- parentId := 0
- if wrap.CurrSubModule == "categories-modify" {
- parentId = this.blog_GetCategoryParentId(wrap, data.A_id)
- }
- content += builder.DataForm(wrap, []builder.DataFormField{
- {
- Kind: builder.DFKHidden,
- Name: "action",
- Value: "blog-categories-modify",
- },
- {
- Kind: builder.DFKHidden,
- Name: "id",
- Value: utils.IntToStr(data.A_id),
- },
- {
- Kind: builder.DFKText,
- Caption: "Parent",
- Name: "parent",
- Value: "0",
- CallBack: func(field *builder.DataFormField) string {
- return `<div class="form-group n2">` +
- `<div class="row">` +
- `<div class="col-md-3">` +
- `<label for="lbl_parent">Parent</label>` +
- `</div>` +
- `<div class="col-md-9">` +
- `<div>` +
- `<select class="selectpicker form-control" id="lbl_parent" name="parent" data-live-search="true">` +
- `<option title="Nothing selected" value="0">—</option>` +
- this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) +
- `</select>` +
- `</div>` +
- `</div>` +
- `</div>` +
- `</div>`
- },
- },
- {
- Kind: builder.DFKText,
- Caption: "Name",
- Name: "name",
- Value: data.A_name,
- Required: true,
- Min: "1",
- Max: "255",
- },
- {
- Kind: builder.DFKText,
- Caption: "Alias",
- Name: "alias",
- Value: data.A_alias,
- Hint: "Example: popular-posts",
- Max: "255",
- },
- {
- Kind: builder.DFKSubmit,
- Value: btn_caption,
- Target: "add-edit-button",
- },
- })
- if wrap.CurrSubModule == "categories-add" {
- sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
- } else {
- sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
- }
- }
- return this.getSidebarModules(wrap), content, sidebar
- })
- }
|