3 * Widget_MyLatestCommits
5 * Copyright (c) Xerox Corporation, Codendi 2001-2009 - marc.nazarian@xrce.xerox.com
6 * Copyright 2014,2018,2021, 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 $user = UserManager::instance()->getCurrentUser();
64 $projects = $user->getGroups();
65 $global_nb_revisions = 0;
66 foreach ($projects as $project) {
67 $hide_item_id = getIntFromRequest('hide_item_id', 0);
68 $hide_scm = getIntFromRequest('hide_scm', 0);
70 if ($project->usesPlugin('scmsvn') && forge_check_perm('scm', $project->getID(), 'read')) {
71 $scmPlugin = plugin_get_object('scmsvn');
72 $revisions = array_merge($revisions, $scmPlugin->getCommits($project, $user, $this->_nb_commits));
74 if ($project->usesPlugin('scmgit') && forge_check_perm('scm', $project->getID(), 'read')) {
75 $scmPlugin = plugin_get_object('scmgit');
76 $revisions = array_merge($revisions, $scmPlugin->getCommits($project, $user, $this->_nb_commits));
78 if (count($revisions) > 0) {
79 usort($revisions, array($this, 'commit_dateorder'));
80 $revisions = array_slice($revisions, 0, $this->_nb_commits, true);
81 $global_nb_revisions += count($revisions);
82 list($hide_now, $count_diff, $hide_url) = my_hide_url('scm', $project->getID(), $hide_item_id, count($projects), $hide_scm);
83 $html .= html_e('div', array(), $hide_url.util_make_link('/scm/?group_id='.$project->getID(), $project->getPublicName()));
86 foreach ($revisions as $key => $revision) {
87 $revisionDescription = substr($revision['description'], 0, 255);
88 if (strlen($revision['description']) > 255) {
89 $revisionDescription .= ' [...]';
91 $divattr = array('class' => '', 'style' => 'border-bottom:1px solid #ddd');
92 if ((($key + 1) % 2) == 1) {
93 $divattr['class'] = 'bgcolor-white';
95 $divattr['class'] = 'bgcolor-grey';
97 $html .= html_e('div', $divattr,
98 html_e('div', array('style' => 'font-size:0.98em'),
99 $this->_getLinkToCommit($project, $revision['commit_id'], $revision['pluginName'], $revision['repo_name']).
100 ' '._('on repository').' '.$revision['repo_name'].' '.
101 date(_("Y-m-d H:i"), $revision['date'])).
102 html_e('div', array('style' => 'padding-left:20px; padding-bottom:4px; color:#555'),
103 $revisionDescription));
108 if (!$global_nb_revisions) {
109 $html .= $HTML->warning_msg(_('No commit found.'));
114 function getPreferences() {
115 $prefs = _('Maximum number of commits to display per project.');
116 $prefs .= html_e('input', array('name' => 'nb_commits', 'type' => 'number', 'size' => 2, 'maxlength' => 3, 'value' => UserManager::instance()->getCurrentUser()->getPreference('my_latests_commits_nb_display')));
120 function updatePreferences() {
121 $cancel = getStringFromRequest('cancel');
122 if (strlen($cancel) > 0) {
123 $this->_nb_commits = getIntFromRequest('nb_commits', self::NB_COMMITS_TO_DISPLAY);
124 UserManager::instance()->getCurrentUser()->setPreference('my_latests_commits_nb_display', $this->_nb_commits);
129 function hasPreferences() {
133 function getCategory() {
137 function getDescription() {
138 return _('List Commits you have done, by project.');
145 function getAjaxUrl($owner_id, $owner_type) {
146 $ajax_url = parent::getAjaxUrl($owner_id, $owner_type);
147 if (existInRequest('hide_item_id') || existInRequest('hide_scm')) {
148 $ajax_url .= '&hide_item_id='.getIntFromRequest('hide_item_id').'&hide_scm='.getIntFromRequest('hide_scm');
153 function isAvailable() {
154 if (!forge_get_config('use_scm')) {
157 foreach (UserManager::instance()->getCurrentUser()->getGroups(false) as $p) {
158 if ($p->usesPlugin('scmsvn') || $p->usesPlugin('scmgit')) {