Exemplo n.º 1
0
func (self *MainHandler) Get() {
	//fmt.Println("im MainHandler")
	self.Data["catpage"] = "home"
	self.TplNames = "sdc/main.html"

	ipage, _ := self.GetInt(":page")
	page := int(ipage)

	tab := template.HTMLEscapeString(strings.TrimSpace(self.GetString(":tab")))

	url := "/"
	if tab == "lastest" {
		url = "/lastest/"
		tab = "id"
		self.Data["tab"] = "lastest"
	} else if tab == "hotness" {
		url = "/hotness/"
		tab = "hotness"
		self.Data["tab"] = "hotness"
	} else if tab == "unanswered" {
		url = "/unanswered/"
		tab = "unanswered"
		self.Data["tab"] = "unanswered"
	} else {
		url = "/lastest/"
		tab = "id"
		self.Data["tab"] = "lastest"
	}

	pagesize := 30
	results_count, err := model.GetQuestionsCount(0, pagesize, tab)
	if err != nil {
		return
	}
	pages, page, beginnum, endnum, offset := helper.Pages(int(results_count), page, pagesize)
	/*
	   <article class="post" id="q-1010000000444736">
	       <div class="status">
	           <span class="answer answered" title="18 个答案">18</span>
	           <span class="vote voted" title="3 个投票">3</span>
	       </div>
	       <div class="p-summary">
	           <a class="author" data-toggle="tooltip" data-placement="bottom" rel="tooltip" href="http://segmentfault.com/u/tonyx" title="TonyX &bull; 658">
	               <img class="avatar-40" src="http://sfault-avatar.b0.upaiyun.com/262/496/2624967969-1030000000395062_medium40" alt="TonyX" />
	           </a>
	           <h2>
	               <a href="http://segmentfault.com/q/1010000000444736" title="你最喜欢的开发工具是什么?">你最喜欢的开发工具是什么?</a>
	           </h2>
	           <div class="meta">
	               <span class="views"> <i class="i-view"></i>
	                   912 次浏览
	               </span>
	               &nbsp;
	               <ul class="meta-tags">
	                   <li> <i class="i-tag"></i>
	                   </li>
	                   <li>
	                       <a data-tid="1040000000090473" href="/t/ide">ide</a>
	                   </li>
	               </ul>
	               &nbsp;
	               <span class="datetime">
	                   <i class="i-time"></i>
	                   17分钟前
	               </span>
	           </div>
	       </div>
	   </article>
	*/
	if qts, err := model.GetQuestions(offset, pagesize, tab); err == nil {
		results_count := len(*qts)
		if results_count > 0 {
			i := 1
			output := ""
			for _, v := range *qts {

				i += i

				output = output + `<article class="post" id="` + strconv.Itoa(int(v.Id)) + `"><div class="status">`

				if v.Ctype == 2 {
					output = output + `<span class="answer answered-accepted" title="` + strconv.Itoa(int(v.ReplyCount)) + ` 个答案">` + strconv.Itoa(int(v.ReplyCount)) + `</span>`
				} else if v.Ctype == -1 {
					output = output + `<span class="answer answered closed" title="` + strconv.Itoa(int(v.ReplyCount)) + ` 个答案">` + strconv.Itoa(int(v.ReplyCount)) + `</span>`
				} else if v.ReplyCount == 0 {
					output = output + `<span class="answer" title="0 个答案">0</span>`
				} else {
					output = output + `<span class="answer answered" title="` + strconv.Itoa(int(v.ReplyCount)) + ` 个答案">` + strconv.Itoa(int(v.ReplyCount)) + `</span>`
				}

				if v.Hotvote == 0 {
					output = output + `<span class="vote" title="0 个投票">0</span>`
				} else {
					output = output + `<span class="vote voted" title="` + strconv.Itoa(int(v.Hotvote)) + ` 个投票">` + strconv.Itoa(int(v.Hotvote)) + `</span>`
				}
				output = output + `</div><div class="p-summary">`
				avatar := ""
				if usr, err := model.GetUser(v.Uid); err == nil && usr != nil {
					avatar = helper.Gravatar(usr.Email, 40)
				}
				if avatar == "" {
					output = output + `<a class="author" data-toggle="tooltip" data-placement="bottom" rel="tooltip" href="###" title="&bull; ` + v.Author + ` &bull;">
	               <img class="avatar-40" src="http://sfault-avatar.b0.upaiyun.com/262/496/2624967969-1030000000395062_medium40" alt="` + v.Author + `" />
	           </a>`
				} else {
					output = output + `<a class="author" data-toggle="tooltip" data-placement="bottom" rel="tooltip" href="###/user/` + v.Author + `/" title="&bull; ` + v.Author + ` &bull;">
	               <img class="avatar-40" src="` + avatar + `" alt="` + v.Author + `" />
	           </a>`
				}

				output = output + `
	           <h2>
	               <a href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">` + v.Title + `</a>
	           </h2>
	           <div class="meta">
	               <span class="views"> <i class="i-view"></i>
	                   ` + strconv.Itoa(int(v.Views)) + ` 次浏览
	               </span>
	               &nbsp;`

				if v.Tags != "" {
					output = output + `<ul class="meta-tags"><li><i class="i-tag"></i></li>`
					tags := helper.Tags(v.Tags, ",")
					itags := len(tags)

					for _, tag := range tags {
						itags = itags - 1
						if itags == 0 {
							//等于0则不添加逗号
							output = output + `<li><a data-tid="` + strconv.Itoa(int(v.Id)) + `" href="/tag/` + tag + `/">` + tag + `</a></li>`

						} else {
							output = output + `<li><a data-tid="` + strconv.Itoa(int(v.Id)) + `" href="/tag/` + tag + `/">` + tag + `</a> , </li>`

						}
					}
					output = output + `</ul>`
				}
				output = output + `&nbsp;<span class="datetime"><i class="i-time"></i>` + " " + v.Author + " " + helper.TimeSince(v.Created) + `提问` + `</span></div></div></article>`
			}
			self.Data["questions"] = output
			self.Data["pagesbar"] = helper.Pagesbar(url, "", results_count, pages, page, beginnum, endnum, 0)
		}

	} else {
		fmt.Println("首页推荐榜单 数据查询出错", err)
	}

	self.Data["replys_8s"] = model.GetAnswersByPid(0, 1, 0, 8, "id")

	//侧栏九宫格推荐榜单
	//先行取出最热门的9个节点 然后根据节点获取该节点下最热门的话题
	/*
		if nd, err := model.GetNodes(0, 9, "hotness"); err == nil {
			if len(*nd) > 0 {
				for _, v := range *nd {

					i := 0
					output_start := `<ul class="widgets-popular widgets-similar clx">`
					output := ""
					if qts := model.GetTopicsByNid(v.Id, 0, 1, 0, "hotness"); err == nil {

						if len(*qts) > 0 {
							for _, v := range *qts {

								i += 1
								if i == 3 {
									output = output + `<li class="similar similar-third">`
									i = 0
								} else {
									output = output + `<li class="similar">`
								}
								output = output + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
							}
						}
					}
					output_end := "</ul>"
					if len(output) > 0 {
						output = output_start + output + output_end
						self.Data["topic_hotness_9_module"] = template.HTML(output)
					} else {
						self.Data["topic_hotness_9_module"] = nil
					}

				}
			}
		} else {
			fmt.Println("节点数据查询出错", err)
		}
	*/

	//侧栏九宫格推荐榜单
	//根据最热的1个分类查找该分类下级9个话题
	/*
		if cats, err := model.GetCategorys(0, 1, "hotness"); err == nil && len(cats) > 0 {
			for _, v := range cats {
				output_start := `<ul class="widgets-popular widgets-similar clx">`
				output := ""
				i := 0
				//根据CID 获取同分类下的最热的9个话题
				if qts := model.GetTopicsByCid(v.Id, 0, 9, 0, "hotness"); len(*qts) > 0 {

					for _, v := range *qts {

						i += 1
						if i == 3 {
							output = output + `<li class="similar similar-third">`
							i = 0
						} else {
							output = output + `<li class="similar">`
						}
						output = output + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
					}
				}
				output_end := "</ul>"

				if len(output) > 0 {
					output = output_start + output + output_end
					self.Data["topic_hotness_9_module"] = template.HTML(output)
				} else {
					self.Data["topic_hotness_9_module"] = nil
				}
			}

		}
	*/

	//根据最热的1个节点查找该节点的上级分类下的9个话题
	/*
		if nd, err := model.GetNodes(0, 1, "hotness"); err == nil && len(*nd) > 0 {
			for _, v := range *nd {

				output_start := `<ul class="widgets-popular widgets-similar clx">`
				output := ""
				i := 0
				//根据节点的上级PID 获取同分类下的最热话题
				if qts := model.GetQuestionsByCid(v.Pid, 0, 9, 0, "hotness"); len(*qts) > 0 {

					for _, v := range *qts {

						i += 1
						if i == 3 {
							output = output + `<li class="similar similar-third">`
							i = 0
						} else {
							output = output + `<li class="similar">`
						}
						output = output + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
														<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
													</a>
												</li>`
					}
				}
				output_end := "</ul>"

				if len(output) > 0 {
					output = output_start + output + output_end
					self.Data["topic_hotness_9_module"] = template.HTML(output)
				} else {
					self.Data["topic_hotness_9_module"] = nil
				}

			}
		}
	*/
}
Exemplo n.º 2
0
func (self *QuestionHandler) Get() {
	self.TplNames = "sdc/question.html"

	qid, _ := self.GetInt(":qid")

	if qid > 0 {

		if qs, err := model.GetQuestion(qid); qs != nil && err == nil {
			avatar := ""
			if usr, err := model.GetUser(qs.Uid); err == nil && usr != nil {
				avatar = helper.Gravatar(usr.Email, 32)
			}
			self.Data["avatar"] = avatar
			self.Data["article"] = *qs
			self.Data["replys"] = *model.GetAnswersByPid(qid, 0, 0, 0, "hotness")

			/*
				if qss := model.GetTopicsByCidOnBetween(qs.Cid, qid-5, qid+5, 0, 11, 0, "asc"); qss != nil && qid != 0 && len(qss) > 0 {

					for i, v := range qss {

						if v.Id == qid {
							//两侧的翻页按钮参数 初始化 s
							prev := i - 1
							next := i + 1
							//两侧的翻页按钮参数 初始化 e

							//话题内容部位 页码  初始化 s
							ipagesbar := `<div class="link_pages">`
							h := 5
							ipagesbar_start := i - h
							ipagesbar_end := i + h
							j := 0
							//话题内容部位 页码  初始化 e
							for i, v := range qss {
								//两侧的翻页按钮 s
								if prev == i {
									self.Data["previd"] = v.Id
									self.Data["prev"] = v.Title
								}
								if next == i {
									self.Data["nexqid"] = v.Id
									self.Data["next"] = v.Title
								}
								//两侧的翻页按钮 e

								//话题内容部位 页码 s
								if ipagesbar_start == i {
									ipagesbar = ipagesbar + `<a href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `"><span><</span></a>`
								}
								if i > ipagesbar_start && i < ipagesbar_end {
									j += 1
									if v.Id == qid { // current

										ipagesbar = ipagesbar + `<span>` + strconv.Itoa(int(v.Id)) + `</span>`

									} else { //loop

										ipagesbar = ipagesbar + `<a href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `"><span>` + strconv.Itoa(int(v.Id)) + `</span></a>`

									}
									if j > (2 * h) {
										break
									}
								}
								if ipagesbar_end == i {
									ipagesbar = ipagesbar + `<a href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `"><span>></span></a>`
								}
								//话题内容部位 页码 e
							}
							self.Data["ipagesbar"] = template.HTML(ipagesbar + "</div>")
						}
					}
					// (qss []*Topic)

				}
			*/

			//侧栏你可能喜欢 推荐同节点下的最热话题
			/*
				if qss := model.GetTopicsByNid(qs.Nid, 0, 6, 0, "hotness"); *qss != nil {

					if len(*qss) > 0 {
						i := 0
						ouqsut := `<ul class="widgets-similar clx">`
						for _, v := range *qss {

							i += 1
							if i == 3 {
								ouqsut = ouqsut + `<li class="similar similar-third">`
								i = 0
							} else {
								ouqsut = ouqsut + `<li class="similar">`
							}
							ouqsut = ouqsut + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
						}
						ouqsut = ouqsut + `</ul>`
						self.Data["topic_sidebar_hotness_6_module"] = template.HTML(ouqsut)
					}

				}
			*/

			//侧栏九宫格推荐榜单
			//先行取出最热门的一个节点 然后根据节点获取该节点下最热门的话题
			/*
				if nd, err := model.GetNodes(0, 1, "hotness"); err == nil {
					if len(*nd) == 1 {
						for _, v := range *nd {

							if qss := model.GetTopicsByNid(v.Id, 0, 9, 0, "hotness"); err == nil {

								if len(*qss) > 0 {
									i := 0
									ouqsut := `<ul class="widgets-popular widgets-similar clx">`
									for _, v := range *qss {

										i += 1
										if i == 3 {
											ouqsut = ouqsut + `<li class="similar similar-third">`
											i = 0
										} else {
											ouqsut = ouqsut + `<li class="similar">`
										}
										ouqsut = ouqsut + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
									}
									ouqsut = ouqsut + `</ul>`
									self.Data["topic_hotness_9_module"] = template.HTML(ouqsut)
								}
							} else {
								fmt.Println("推荐榜单(9)数据查询出错", err)
							}
						}
					}
				} else {
					fmt.Println("节点数据查询出错", err)
				}
			*/

			//底部六格推荐
			//推荐同一作者的最热话题
			/*
				if qss := model.GetTopicsByUid(qs.Uid, 0, 6, 0, "hotness"); len(*qss) > 0 {
					i := 0
					ouqsut := `<ul class="widgets-similar clx">`
					for _, v := range *qss {

						i += 1
						if i == 3 {
							ouqsut = ouqsut + `<li class="likesimilar likesimilar-3">`
							i = 0
						} else {
							ouqsut = ouqsut + `<li class="likesimilar">`
						}
						ouqsut = ouqsut + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `" alt="` + v.Title + `" class="likeimglink">
													<img src="` + v.ThumbnailsMedium + `" wdith="150" height="150" />
													<span class="bg">` + v.Title + `</span>
												</a>
											</li>`
					}
					ouqsut = ouqsut + `</ul>`
					self.Data["topic_hotness_6_module"] = template.HTML(ouqsut)
				}
			*/
		} else {
			self.Redirect("/", 302)
		}
	} else {
		self.Redirect("/", 302)
	}

}
Exemplo n.º 3
0
func (self *SearchHandler) Get() {
	self.TplNames = "sdc/search.html"
	keyword := template.HTMLEscapeString(strings.TrimSpace(self.GetString(":keyword")))

	if keyword == "" {
		keyword = template.HTMLEscapeString(strings.TrimSpace(self.GetString("keyword")))
	}

	ipage, _ := self.GetInt(":page")
	if ipage <= 0 {
		ipage, _ = self.GetInt("page")
	}
	page := int(ipage)

	limit := 9 //每页显示数目

	//如果已经登录登录
	sess_username, _ := self.GetSession("username").(string)
	if sess_username != "" {
		limit = 30
	}

	if keyword != "" {
		if qts, err := model.SearchQuestion(keyword, 0, 0, "id"); err == nil {

			rcs := len(*qts)
			pages, pageout, beginnum, endnum, offset := helper.Pages(rcs, page, limit)

			if st, err := model.SearchQuestion(keyword, offset, limit, "hotness"); err == nil {
				results_count := len(*st)
				if results_count > 0 {
					i := 1
					output := ""
					for _, v := range *qts {

						i += i

						output = output + `<article class="post" id="` + strconv.Itoa(int(v.Id)) + `"><div class="status">`

						if v.Ctype == 2 {
							output = output + `<span class="answer answered-accepted" title="` + strconv.Itoa(int(v.ReplyCount)) + ` 个答案">` + strconv.Itoa(int(v.ReplyCount)) + `</span>`
						} else if v.Ctype == -1 {
							output = output + `<span class="answer answered closed" title="` + strconv.Itoa(int(v.ReplyCount)) + ` 个答案">` + strconv.Itoa(int(v.ReplyCount)) + `</span>`
						} else if v.ReplyCount == 0 {
							output = output + `<span class="answer" title="0 个答案">0</span>`
						} else {
							output = output + `<span class="answer answered" title="` + strconv.Itoa(int(v.ReplyCount)) + ` 个答案">` + strconv.Itoa(int(v.ReplyCount)) + `</span>`
						}

						if v.Hotvote == 0 {
							output = output + `<span class="vote" title="0 个投票">0</span>`
						} else {
							output = output + `<span class="vote voted" title="` + strconv.Itoa(int(v.Hotvote)) + ` 个投票">` + strconv.Itoa(int(v.Hotvote)) + `</span>`
						}
						output = output + `</div><div class="p-summary">`
						avatar := ""
						if usr, err := model.GetUser(v.Uid); err == nil && usr != nil {
							avatar = helper.Gravatar(usr.Email, 40)
						}
						if avatar == "" {
							output = output + `<a class="author" data-toggle="tooltip" data-placement="bottom" rel="tooltip" href="###" title="&bull; ` + v.Author + ` &bull;">
			               <img class="avatar-40" src="http://sfault-avatar.b0.upaiyun.com/262/496/2624967969-1030000000395062_medium40" alt="` + v.Author + `" />
			           </a>`
						} else {
							output = output + `<a class="author" data-toggle="tooltip" data-placement="bottom" rel="tooltip" href="###/user/` + v.Author + `/" title="&bull; ` + v.Author + ` &bull;">
			               <img class="avatar-40" src="` + avatar + `" alt="` + v.Author + `" />
			           </a>`
						}

						output = output + `
			           <h2>
			               <a href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">` + v.Title + `</a>
			           </h2>
			           <div class="meta">
			               <span class="views"> <i class="i-view"></i>
			                   ` + strconv.Itoa(int(v.Views)) + ` 次浏览
			               </span>
			               &nbsp;`

						if v.Tags != "" {
							output = output + `<ul class="meta-tags"><li><i class="i-tag"></i></li>`
							tags := helper.Tags(v.Tags, ",")
							itags := len(tags)

							for _, tag := range tags {
								itags = itags - 1
								if itags == 0 {
									//等于0则不添加逗号
									output = output + `<li><a data-tid="` + strconv.Itoa(int(v.Id)) + `" href="/tag/` + tag + `/">` + tag + `</a></li>`

								} else {
									output = output + `<li><a data-tid="` + strconv.Itoa(int(v.Id)) + `" href="/tag/` + tag + `/">` + tag + `</a> , </li>`

								}
							}
							output = output + `</ul>`
						}
						output = output + `&nbsp;<span class="datetime"><i class="i-time"></i>` + " " + v.Author + " " + helper.TimeSince(v.Created) + `提问` + `</span></div></div></article>`
					}
					self.Data["questions"] = output

				}

			}

			if k := self.GetString("keyword"); k != "" {
				self.Data["search_keyword"] = k
			} else {
				self.Data["search_keyword"] = keyword
			}

			self.Data["pagesbar"] = helper.Pagesbar("/search/", keyword, rcs, pages, pageout, beginnum, endnum, 0)
		} else {
			fmt.Println("SearchQuestion errors:", err)
			return
		}

		self.Data["replys_8s"] = model.GetAnswersByPid(0, 1, 0, 8, "id")

		//侧栏九宫格推荐榜单
		//根据用户的关键词推荐
		/*
			nds, ndserr := model.SearchNode(keyword, 0, 9, "hotness")
			cats, catserr := model.SearchCategory(keyword, 0, 9, "hotness")
			//如果在节点找到关键词
			if (ndserr == catserr) && (ndserr == nil) {

				output_start := `<ul class="widgets-popular widgets-similar clx">`
				output := ""
				i := 0
				if len(*nds) >= len(*cats) && len(*nds) > 0 {
					for _, v := range *nds {

						if tps := model.GetTopicsByNid(v.Id, 0, 1, 0, "hotness"); len(*tps) > 0 {

							for _, v := range *tps {

								i += 1
								if i == 3 {
									output = output + `<li class="similar similar-third">`
									i = 0
								} else {
									output = output + `<li class="similar">`
								}
								output = output + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
							}
						}

					}
				} else if len(*cats) > len(*nds) && len(*cats) > 0 {
					for _, v := range *cats {

						if tps := model.GetTopicsByCid(v.Id, 0, 1, 0, "hotness"); len(*tps) > 0 {

							for _, v := range *tps {

								i += 1
								if i == 3 {
									output = output + `<li class="similar similar-third">`
									i = 0
								} else {
									output = output + `<li class="similar">`
								}
								output = output + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
							}
						}

					}
				}
				output_end := "</ul>"
				if len(output) > 0 {
					output = output_start + output + output_end
					self.Data["topic_hotness_9_module"] = template.HTML(output)
				} else {
					self.Data["topic_hotness_9_module"] = nil
				}
			}
		*/

		//侧栏九宫格推荐榜单
		//先行取出最热门的9个节点 然后根据节点获取该节点下最热门的话题
		/*
			if nd, err := model.GetNodes(0, 9, "hotness"); err == nil {
				if len(*nd) > 0 {
					for _, v := range *nd {

						i := 0
						output_start := `<ul class="widgets-popular widgets-similar clx">`
						output := ""
						if tps := model.GetTopicsByNid(v.Id, 0, 1, 0, "hotness"); err == nil {

							if len(*tps) > 0 {
								for _, v := range *tps {

									i += 1
									if i == 3 {
										output = output + `<li class="similar similar-third">`
										i = 0
									} else {
										output = output + `<li class="similar">`
									}
									output = output + `<a target="_blank" href="/` + strconv.Itoa(int(v.Id)) + `/" title="` + v.Title + `">
													<img src="` + v.ThumbnailsSmall + `" wdith="70" height="70" />
												</a>
											</li>`
								}
							}
						}
						output_end := "</ul>"
						if len(output) > 0 {
							output = output_start + output + output_end
							self.Data["topic_hotness_9_module"] = template.HTML(output)
						} else {
							self.Data["topic_hotness_9_module"] = nil
						}

					}
				}
			} else {
				fmt.Println("节点数据查询出错", err)
			}
		*/
	} else {
		self.Redirect("/", 302)
	}

}