From a329bbc2159a35437d81b78f8176925904ddae08 Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Tue, 25 Aug 2015 23:22:05 +0800
Subject: [PATCH] new org dashboard issues

---
 cmd/web.go                           |  1 +
 models/issue.go                      |  8 ++++--
 modules/base/tool.go                 |  9 +++++++
 routers/repo/issue.go                |  2 +-
 routers/user/home.go                 | 40 ++++++++++++++--------------
 templates/user/dashboard/issues.tmpl |  2 ++
 6 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/cmd/web.go b/cmd/web.go
index b25bca84f4..6d78047700 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -384,6 +384,7 @@ func runWeb(ctx *cli.Context) {
 
 		m.Group("/:org", func() {
 			m.Get("/dashboard", user.Dashboard)
+			m.Get("/issues", user.Issues)
 			m.Get("/members", org.Members)
 			m.Get("/members/action/:action", org.MembersAction)
 
diff --git a/models/issue.go b/models/issue.go
index f5070d1ec4..325c2c938c 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -404,11 +404,13 @@ func GetIssueByID(id int64) (*Issue, error) {
 }
 
 // Issues returns a list of issues by given conditions.
-func Issues(uid, assigneeID, repoID, posterID, milestoneID int64, page int, isClosed, isMention bool, labels, sortType string) ([]*Issue, error) {
+func Issues(uid, assigneeID, repoID, posterID, milestoneID int64, repoIDs []int64, page int, isClosed, isMention bool, labels, sortType string) ([]*Issue, error) {
 	sess := x.Limit(setting.IssuePagingNum, (page-1)*setting.IssuePagingNum)
 
 	if repoID > 0 {
 		sess.Where("issue.repo_id=?", repoID).And("issue.is_closed=?", isClosed)
+	} else if repoIDs != nil {
+		sess.Where("issue.repo_id IN (?)", strings.Join(base.Int64sToStrings(repoIDs), ",")).And("issue.is_closed=?", isClosed)
 	} else {
 		sess.Where("issue.is_closed=?", isClosed)
 	}
@@ -682,7 +684,7 @@ func GetIssueStats(repoID, uid, labelID, milestoneID, assigneeID int64, filterMo
 }
 
 // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
-func GetUserIssueStats(repoID, uid int64, filterMode int) *IssueStats {
+func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int) *IssueStats {
 	stats := &IssueStats{}
 	issue := new(Issue)
 	stats.AssignCount, _ = x.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue)
@@ -692,6 +694,8 @@ func GetUserIssueStats(repoID, uid int64, filterMode int) *IssueStats {
 	baseCond := " WHERE issue.is_closed=?"
 	if repoID > 0 {
 		baseCond += " AND issue.repo_id=" + com.ToStr(repoID)
+	} else {
+		baseCond += " AND issue.repo_id IN (" + strings.Join(base.Int64sToStrings(repoIDs), ",") + ")"
 	}
 	switch filterMode {
 	case FM_ASSIGN:
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 16759f21f6..0e118552aa 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -434,6 +434,15 @@ func StringsToInt64s(strs []string) []int64 {
 	return ints
 }
 
+// Int64sToStrings converts a slice of int64 to a slice of string.
+func Int64sToStrings(ints []int64) []string {
+	strs := make([]string, len(ints))
+	for i := range ints {
+		strs[i] = com.ToStr(ints[i])
+	}
+	return strs
+}
+
 // Int64sToMap converts a slice of int64 to a int64 map.
 func Int64sToMap(ints []int64) map[int64]bool {
 	m := make(map[int64]bool)
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index f34ece7787..6a0cc2de3a 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -114,7 +114,7 @@ func Issues(ctx *middleware.Context) {
 
 	// Get issues.
 	issues, err := models.Issues(uid, assigneeID, repo.ID, posterID, milestoneID,
-		page, isShowClosed, filterMode == models.FM_MENTION, selectLabels, sortType)
+		nil, page, isShowClosed, filterMode == models.FM_MENTION, selectLabels, sortType)
 	if err != nil {
 		ctx.Handle(500, "Issues: %v", err)
 		return
diff --git a/routers/user/home.go b/routers/user/home.go
index 900c2a17da..6ae11bb17d 100644
--- a/routers/user/home.go
+++ b/routers/user/home.go
@@ -190,20 +190,6 @@ func Issues(ctx *middleware.Context) {
 
 	repoID := ctx.QueryInt64("repo")
 	isShowClosed := ctx.Query("state") == "closed"
-	issueStats := models.GetUserIssueStats(repoID, ctxUser.Id, filterMode)
-
-	page := ctx.QueryInt("page")
-	if page <= 1 {
-		page = 1
-	}
-
-	var total int
-	if !isShowClosed {
-		total = int(issueStats.OpenCount)
-	} else {
-		total = int(issueStats.ClosedCount)
-	}
-	ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5)
 
 	// Get repositories.
 	repos, err := models.GetRepositories(ctxUser.Id, true)
@@ -212,6 +198,7 @@ func Issues(ctx *middleware.Context) {
 		return
 	}
 
+	allCount := 0
 	repoIDs := make([]int64, 0, len(repos))
 	showRepos := make([]*models.Repository, 0, len(repos))
 	for _, repo := range repos {
@@ -221,12 +208,9 @@ func Issues(ctx *middleware.Context) {
 
 		repoIDs = append(repoIDs, repo.ID)
 		repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
-		issueStats.AllCount += int64(repo.NumOpenIssues)
+		allCount += repo.NumOpenIssues
 
-		if repo.ID == repoID {
-			repo.NumOpenIssues = int(issueStats.OpenCount)
-			repo.NumClosedIssues = int(issueStats.ClosedCount)
-		} else if filterMode != models.FM_ALL && repo.NumIssues > 0 {
+		if filterMode != models.FM_ALL {
 			// Calculate repository issue count with filter mode.
 			numOpen, numClosed := repo.IssueStats(ctxUser.Id, filterMode)
 			repo.NumOpenIssues, repo.NumClosedIssues = int(numOpen), int(numClosed)
@@ -244,9 +228,25 @@ func Issues(ctx *middleware.Context) {
 		repoIDs = []int64{repoID}
 	}
 
+	issueStats := models.GetUserIssueStats(repoID, ctxUser.Id, repoIDs, filterMode)
+	issueStats.AllCount = int64(allCount)
+
+	page := ctx.QueryInt("page")
+	if page <= 1 {
+		page = 1
+	}
+
+	var total int
+	if !isShowClosed {
+		total = int(issueStats.OpenCount)
+	} else {
+		total = int(issueStats.ClosedCount)
+	}
+	ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5)
+
 	// Get issues.
 	issues, err := models.Issues(ctxUser.Id, assigneeID, repoID, posterID, 0,
-		page, isShowClosed, false, "", "")
+		repoIDs, page, isShowClosed, false, "", "")
 	if err != nil {
 		ctx.Handle(500, "Issues: %v", err)
 		return
diff --git a/templates/user/dashboard/issues.tmpl b/templates/user/dashboard/issues.tmpl
index b104d69d16..c22b35481f 100644
--- a/templates/user/dashboard/issues.tmpl
+++ b/templates/user/dashboard/issues.tmpl
@@ -9,6 +9,7 @@
   					{{.i18n.Tr "home.issues.in_your_repos"}}
   					<strong class="ui right">{{.IssueStats.AllCount}}</strong>
   				</a>
+  				{{if not .ContextUser.IsOrganization}}
   				<a class="{{if eq .ViewType "assigned"}}active{{end}} item" href="{{.Link}}?type=assigned&repo={{.RepoID}}&state={{.State}}">
   					{{.i18n.Tr "repo.issues.filter_type.assigned_to_you"}}
   					<strong class="ui right">{{.IssueStats.AssignCount}}</strong>
@@ -17,6 +18,7 @@
   					{{.i18n.Tr "repo.issues.filter_type.created_by_you"}}
   					<strong class="ui right">{{.IssueStats.CreateCount}}</strong>
   				</a>
+  				{{end}}
   				<div class="ui divider"></div>
           {{range .Repos}}
           <a class="{{if eq $.RepoID .ID}}active{{end}} item" href="{{$.Link}}?type={{$.ViewType}}{{if not (eq $.RepoID .ID)}}&repo={{.ID}}{{end}}&state={{$.State}}">{{$.SignedUser.Name}}/{{.Name}} <strong class="ui right">{{if $.IsShowClosed}}{{.NumClosedIssues}}{{else}}{{.NumOpenIssues}}{{end}}</strong></a>