github.com/kotovmak/go-admin@v1.1.1/plugins/admin/controller/detail.go (about)

     1  package controller
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/kotovmak/go-admin/context"
     7  	"github.com/kotovmak/go-admin/modules/auth"
     8  	"github.com/kotovmak/go-admin/modules/language"
     9  	"github.com/kotovmak/go-admin/plugins/admin/modules"
    10  	"github.com/kotovmak/go-admin/plugins/admin/modules/constant"
    11  	form2 "github.com/kotovmak/go-admin/plugins/admin/modules/form"
    12  	"github.com/kotovmak/go-admin/plugins/admin/modules/parameter"
    13  	"github.com/kotovmak/go-admin/template"
    14  	"github.com/kotovmak/go-admin/template/types"
    15  	"github.com/kotovmak/go-admin/template/types/form"
    16  )
    17  
    18  func (h *Handler) ShowDetail(ctx *context.Context) {
    19  
    20  	var (
    21  		prefix    = ctx.Query(constant.PrefixKey)
    22  		id        = ctx.Query(constant.DetailPKKey)
    23  		panel     = h.table(prefix, ctx)
    24  		user      = auth.Auth(ctx)
    25  		newPanel  = panel.Copy()
    26  		detail    = panel.GetDetail()
    27  		info      = panel.GetInfo()
    28  		formModel = newPanel.GetForm()
    29  		fieldList = make(types.FieldList, 0)
    30  	)
    31  
    32  	if len(detail.FieldList) == 0 {
    33  		fieldList = info.FieldList
    34  	} else {
    35  		fieldList = detail.FieldList
    36  	}
    37  
    38  	formModel.FieldList = make([]types.FormField, len(fieldList))
    39  
    40  	for i, field := range fieldList {
    41  		formModel.FieldList[i] = types.FormField{
    42  			Field:        field.Field,
    43  			FieldClass:   field.Field,
    44  			TypeName:     field.TypeName,
    45  			Head:         field.Head,
    46  			Hide:         field.Hide,
    47  			Joins:        field.Joins,
    48  			FormType:     form.Default,
    49  			FieldDisplay: field.FieldDisplay,
    50  		}
    51  	}
    52  
    53  	if detail.Table != "" {
    54  		formModel.Table = detail.Table
    55  	} else {
    56  		formModel.Table = info.Table
    57  	}
    58  
    59  	param := parameter.GetParam(ctx.Request.URL,
    60  		info.DefaultPageSize,
    61  		info.SortField,
    62  		info.GetSort())
    63  
    64  	paramStr := param.DeleteDetailPk().GetRouteParamStr()
    65  
    66  	editUrl := modules.AorEmpty(!info.IsHideEditButton, h.routePathWithPrefix("show_edit", prefix)+paramStr+
    67  		"&"+constant.EditPKKey+"="+ctx.Query(constant.DetailPKKey))
    68  	deleteUrl := modules.AorEmpty(!info.IsHideDeleteButton, h.routePathWithPrefix("delete", prefix)+paramStr)
    69  	infoUrl := h.routePathWithPrefix("info", prefix) + paramStr
    70  
    71  	editUrl = user.GetCheckPermissionByUrlMethod(editUrl, h.route("show_edit").Method())
    72  	deleteUrl = user.GetCheckPermissionByUrlMethod(deleteUrl, h.route("delete").Method())
    73  
    74  	deleteJs := ""
    75  
    76  	if deleteUrl != "" {
    77  		deleteJs = fmt.Sprintf(`<script>
    78  function DeletePost(id) {
    79  	swal({
    80  			title: '%s',
    81  			type: "warning",
    82  			showCancelButton: true,
    83  			confirmButtonColor: "#DD6B55",
    84  			confirmButtonText: '%s',
    85  			closeOnConfirm: false,
    86  			cancelButtonText: '%s',
    87  		},
    88  		function () {
    89  			$.ajax({
    90  				method: 'post',
    91  				url: '%s',
    92  				data: {
    93  					id: id
    94  				},
    95  				success: function (data) {
    96  					if (typeof (data) === "string") {
    97  						data = JSON.parse(data);
    98  					}
    99  					if (data.code === 200) {
   100  						location.href = '%s'
   101  					} else {
   102  						swal(data.msg, '', 'error');
   103  					}
   104  				}
   105  			});
   106  		});
   107  }
   108  
   109  $('.delete-btn').on('click', function (event) {
   110  	DeletePost('%s')
   111  });
   112  
   113  </script>`, language.Get("are you sure to delete"), language.Get("yes"),
   114  			language.Get("cancel"), deleteUrl, infoUrl, id)
   115  	}
   116  
   117  	title := ""
   118  	desc := ""
   119  
   120  	isNotIframe := ctx.Query(constant.IframeKey) != "true"
   121  
   122  	if isNotIframe {
   123  		title = detail.Title
   124  
   125  		if title == "" {
   126  			title = info.Title + language.Get("Detail")
   127  		}
   128  
   129  		desc = detail.Description
   130  
   131  		if desc == "" {
   132  			desc = info.Description + language.Get("Detail")
   133  		}
   134  	}
   135  
   136  	formInfo, err := newPanel.GetDataWithId(param.WithPKs(id))
   137  
   138  	if err != nil {
   139  		h.HTML(ctx, user, template.WarningPanelWithDescAndTitle(err.Error(), desc, title),
   140  			template.ExecuteOptions{Animation: param.Animation})
   141  		return
   142  	}
   143  
   144  	h.HTML(ctx, user, types.Panel{
   145  		Content: detailContent(aForm().
   146  			SetTitle(template.HTML(title)).
   147  			SetContent(formInfo.FieldList).
   148  			SetHeader(detail.HeaderHtml).
   149  			SetFooter(template.HTML(deleteJs)+detail.FooterHtml).
   150  			SetHiddenFields(map[string]string{
   151  				form2.PreviousKey: infoUrl,
   152  			}).
   153  			SetPrefix(h.config.PrefixFixSlash()), editUrl, deleteUrl, !isNotIframe),
   154  		Description: template.HTML(desc),
   155  		Title:       template.HTML(title),
   156  	}, template.ExecuteOptions{Animation: param.Animation})
   157  }