3 * Widget_MyLatestCommits
5 * Copyright (c) Xerox Corporation, Codendi 2001-2009 - marc.nazarian@xrce.xerox.com
6 * Copyright 2014, 2018, Franck Villaume - TrivialDev
8 * This file is a part of Fusionforge.
10 * Fusionforge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * Fusionforge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
24 class Widget_MyLatestCommits extends Widget {
27 * Default number of commits to display (if user did not change/set preferences)
29 const NB_COMMITS_TO_DISPLAY = 5;
32 * Number of commits to display (user preferences)
36 function __construct() {
37 parent::__construct('mylatestcommits');
38 $this->_nb_commits = UserManager::instance()->getCurrentUser()->getPreference('my_latests_commits_nb_display');
39 if($this->_nb_commits === false) {
40 $this->_nb_commits = self::NB_COMMITS_TO_DISPLAY;
41 UserManager::instance()->getCurrentUser()->setPreference('my_latests_commits_nb_display', $this->_nb_commits);
45 public function getTitle() {
46 return _('My Latest Commits');
49 public function _getLinkToCommit($project, $commit_id, $pluginName, $repo_name) {
50 return util_make_link('/scm/browser.php?group_id='.$project->getID().'&scm_plugin='.$pluginName.'&commit='.$commit_id.'&extra='.$repo_name, _('commit')._(': ').$commit_id);
53 static function commit_dateorder($a, $b) {
54 if ($a['date'] == $b['date']) {
57 return ($a['date'] > $b['date']) ? -1 : 1;
60 public function getContent() {
63 //$uh = new UserHelper();
64 $request = HTTPRequest::instance();
65 $hp = Codendi_HTMLPurifier::instance();
66 $user = UserManager::instance()->getCurrentUser();
67 $projects = $user->getGroups();
68 $global_nb_revisions = 0;
69 foreach ($projects as $project) {
70 $vItemId = new Valid_UInt('hide_item_id');
72 if ($request->valid($vItemId)) {
73 $hide_item_id = $request->get('hide_item_id');
77 $vProject = new Valid_WhiteList('hide_scm', array(0, 1));
78 $vProject->required();
79 if ($request->valid($vProject)) {
80 $hide_scm = $request->get('hide_scm');
85 if ($project->usesPlugin('scmsvn') && forge_check_perm('scm', $project->getID(), 'read')) {
86 $scmPlugin = plugin_get_object('scmsvn');
87 $revisions = array_merge($revisions, $scmPlugin->getCommits($project, $user, $this->_nb_commits));
89 if ($project->usesPlugin('scmgit') && forge_check_perm('scm', $project->getID(), 'read')) {
90 $scmPlugin = plugin_get_object('scmgit');
91 $revisions = array_merge($revisions, $scmPlugin->getCommits($project, $user, $this->_nb_commits));
93 if (count($revisions) > 0) {
94 usort($revisions, array($this, 'commit_dateorder'));
95 $revisions = array_slice($revisions, 0, $this->_nb_commits, true);
96 $global_nb_revisions += count($revisions);
97 list($hide_now, $count_diff, $hide_url) = my_hide_url('scm', $project->getID(), $hide_item_id, count($projects), $hide_scm);
98 $html .= html_e('div', array(), $hide_url.util_make_link('/scm/?group_id='.$project->getID(), $project->getPublicName()));
101 foreach ($revisions as $key => $revision) {
102 $revisionDescription = substr($revision['description'], 0, 255);
103 if (strlen($revision['description']) > 255) {
104 $revisionDescription .= ' [...]';
106 $divattr = array('class' => '', 'style' => 'border-bottom:1px solid #ddd');
107 if ((($key + 1) % 2) == 1) {
108 $divattr['class'] = 'bgcolor-white';
110 $divattr['class'] = 'bgcolor-grey';
112 $html .= html_e('div', $divattr,
113 html_e('div', array('style' => 'font-size:0.98em'),
114 $this->_getLinkToCommit($project, $revision['commit_id'], $revision['pluginName'], $revision['repo_name']).
115 ' '._('on repository').' '.$revision['repo_name'].' '.
116 date(_("Y-m-d H:i"), $revision['date'])).
117 html_e('div', array('style' => 'padding-left:20px; padding-bottom:4px; color:#555'),
118 $revisionDescription));
123 if (!$global_nb_revisions) {
124 $html .= $HTML->warning_msg(_('No commit found.'));
129 function getPreferences() {
130 $prefs = _('Maximum number of commits to display per project.');
131 $prefs .= html_e('input', array('name' => 'nb_commits', 'type' => 'number', 'size' => 2, 'maxlenght' => 3, 'value' => UserManager::instance()->getCurrentUser()->getPreference('my_latests_commits_nb_display')));
135 function updatePreferences(&$request) {
136 $request->valid(new Valid_String('cancel'));
137 $nbShow = new Valid_UInt('nb_commits');
139 if (!$request->exist('cancel')) {
140 if ($request->valid($nbShow)) {
141 $this->_nb_commits = $request->get('nb_commits');
143 $this->_nb_commits = self::NB_COMMITS_TO_DISPLAY;
145 UserManager::instance()->getCurrentUser()->setPreference('my_latests_commits_nb_display', $this->_nb_commits);
151 function hasPreferences() {
155 function getCategory() {
159 function getDescription() {
160 return _('List Commits you have done, by project.');
167 function getAjaxUrl($owner_id, $owner_type) {
168 $request =& HTTPRequest::instance();
169 $ajax_url = parent::getAjaxUrl($owner_id, $owner_type);
170 if ($request->exist('hide_item_id') || $request->exist('hide_scm')) {
171 $ajax_url .= '&hide_item_id='.$request->get('hide_item_id').'&hide_scm='.$request->get('hide_scm');
176 function isAvailable() {
177 if (!forge_get_config('use_scm')) {
180 foreach (UserManager::instance()->getCurrentUser()->getGroups(false) as $p) {
181 if ($p->usesPlugin('scmsvn') || $p->usesPlugin('scmgit')) {