Skip to content

Commit

Permalink
👹 fix theme options store
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Sep 27, 2017
1 parent 5b84a53 commit 74fdcd5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/tale/controller/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public String categories(Request request, @PathParam String keyword,
return this.render_404();
}

Page<Contents> contentsPaginator = contentsService.getArticles(metaDto.getMid(), page, limit);

request.attribute("articles", contentsPaginator);
Page<Contents> contentsPage = contentsService.getArticles(metaDto.getMid(), page, limit);
request.attribute("articles", contentsPage);
request.attribute("meta", metaDto);
request.attribute("type", "分类");
request.attribute("keyword", keyword);
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/com/tale/controller/admin/ThemeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.blade.Environment;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.JsonKit;
import com.blade.kit.StringKit;
import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.ui.RestResponse;
Expand All @@ -22,6 +23,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -67,8 +69,15 @@ public String index(Request request) {
*/
@GetRoute(value = "setting")
public String setting(Request request) {
Map<String, String> themeOptions = optionsService.getOptions("theme_option_");
request.attribute("theme_options", themeOptions);
String currentTheme = TaleConst.OPTIONS.get("site_theme", "default");
String key = "theme_" + currentTheme + "_options";

String option = optionsService.getOption(key);
Map<String, Object> map = new HashMap<>();
if (StringKit.isNotBlank(option)) {
map = JsonKit.toAson(option);
}
request.attribute("theme_options", map);
return this.render("setting");
}

Expand All @@ -82,10 +91,19 @@ public String setting(Request request) {
@JSON
public RestResponse saveSetting(Request request) {
try {
Map<String, List<String>> querys = request.parameters();
optionsService.saveOptions(querys);
Map<String, List<String>> query = request.parameters();

// theme_milk_options => { }
String currentTheme = TaleConst.OPTIONS.get("site_theme", "default");
String key = "theme_" + currentTheme + "_options";

Map<String, String> options = new HashMap<>();
query.forEach((k, v) -> options.put(k, v.get(0)));

optionsService.saveOption(key, JsonKit.toString(options));

TaleConst.OPTIONS = Environment.of(optionsService.getOptions());
new Logs(LogActions.THEME_SETTING, JsonKit.toString(querys), request.address(), this.getUid()).save();
new Logs(LogActions.THEME_SETTING, JsonKit.toString(query), request.address(), this.getUid()).save();
return RestResponse.ok();
} catch (Exception e) {
String msg = "主题设置失败";
Expand Down
31 changes: 22 additions & 9 deletions src/main/java/com/tale/extension/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class Commons {
private static final List EMPTY = new ArrayList(0);

private static final Random rand = new Random();

private static final String TEMPLATES = "/templates/";

public static void setSiteService(SiteService ss) {
Expand All @@ -45,6 +45,16 @@ public static boolean is_empty(Page paginator) {
return null == paginator || BladeKit.isEmpty(paginator.getRows());
}

/**
* 判断字符串不为空
*
* @param str
* @return
*/
public static boolean not_empty(String str) {
return StringKit.isNotBlank(str);
}

/**
* 返回网站首页链接,如:http://tale.biezhi.me
*
Expand Down Expand Up @@ -108,9 +118,10 @@ public static String site_option(String key, String defalutValue) {

/**
* 返回站点设置的描述信息
*
* @return
*/
public static String site_description(){
public static String site_description() {
return site_option("site_description");
}

Expand Down Expand Up @@ -175,6 +186,7 @@ public static String fmtdate(Integer unixTime) {

/**
* 格式化日期
*
* @param date
* @param fmt
* @return
Expand All @@ -199,23 +211,24 @@ public static String fmtdate(Integer unixTime, String patten) {

/**
* 获取随机数
*
* @param max
* @param str
* @return
*/
public static String random(int max, String str){
public static String random(int max, String str) {
return UUID.random(1, max) + str;
}

/**
* An :grinning:awesome :smiley:string &#128516;with a few :wink:emojis!
*
* <p>
* 这种格式的字符转换为emoji表情
*
* @param value
* @return
*/
public static String emoji(String value){
public static String emoji(String value) {
return EmojiParser.parseToUnicode(value);
}

Expand All @@ -227,10 +240,10 @@ public static String emoji(String value){
public static String show_thumb(String content) {
content = TaleUtils.mdToHtml(content);
if (content.contains("<img")) {
String img = "";
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
Matcher m_image = p_image.matcher(content);
String img = "";
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
Matcher m_image = p_image.matcher(content);
if (m_image.find()) {
img = img + "," + m_image.group();
// //匹配src
Expand Down
44 changes: 39 additions & 5 deletions src/main/java/com/tale/extension/Theme.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.tale.extension;

import com.blade.jdbc.page.Page;
import com.blade.kit.JsonKit;
import com.blade.kit.StringKit;
import com.blade.kit.json.Ason;
import com.blade.mvc.WebContext;
import com.blade.mvc.http.Request;
import com.tale.init.TaleConst;
import com.tale.model.dto.Comment;
import com.tale.model.dto.Types;
Expand All @@ -14,10 +18,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;

/**
* 主题函数
Expand Down Expand Up @@ -620,6 +621,29 @@ public static Page<Comment> comments(int limit) {
return siteService.getComments(contents.getCid(), page, limit);
}

/**
* 分页
*
* @param limit
* @return
*/
public static Page<Contents> articles(int limit) {
Request request = WebContext.request();

int page = request.queryInt("page", 1);
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;

Page<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).page(page, limit, "created desc");

request.attribute("articles", articles);
if (page > 1) {
WebContext.request().attribute("title", "第" + page + "页");
}
request.attribute("is_home", true);
request.attribute("page_prefix", "/page");
return articles;
}

/**
* 获取当前上下文的文章对象
*
Expand Down Expand Up @@ -656,7 +680,17 @@ public static String comments_num(String noComment, String value) {
* @return
*/
public static String theme_option(String key) {
return TaleConst.OPTIONS.get("theme_option_" + key, null);
String theme = TaleConst.OPTIONS.get("site_theme", "default");
return TaleConst.OPTIONS.get("theme_" + theme + "_options")
.filter(StringKit::isNotBlank)
.map((String json) -> {
Ason ason = JsonKit.toAson(json);
if(!ason.containsKey(key)){
return "";
}
return ason.getString(key);
})
.orElse("");
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/tale/service/MetasService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import com.tale.model.entity.Metas;
import com.tale.model.entity.Relationships;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -54,8 +51,11 @@ public Map<String, List<Contents>> getMetaMapping(String type) {
private List<Contents> getMetaContents(Metas m) {
Integer mid = m.getMid();
List<Relationships> relationships = new Relationships().where("mid", mid).findAll();
List<Integer> cidList = relationships.stream().map(Relationships::getCid).collect(Collectors.toList());
List<Contents> contents = new Contents().in("cid", cidList).findAll(OrderBy.desc("created"));
if (null == relationships || relationships.size() == 0) {
return new ArrayList<>();
}
List<Integer> cidList = relationships.stream().map(Relationships::getCid).collect(Collectors.toList());
List<Contents> contents = new Contents().queryAll("select * from t_contents where cid in (" + cidList.stream().map(Object::toString).collect(Collectors.joining(",")) + ") order by created desc");
return contents;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/tale/service/OptionsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public Map<String, String> getOptions(String key) {
return options;
}

public String getOption(String key) {
Options options = new Options().where("name", key).find();
if (null != options) {
return options.getValue();
}
return null;
}

/**
* 根据key删除配置项
*
Expand Down

0 comments on commit 74fdcd5

Please sign in to comment.