Indefero

Indefero Git Source Tree


Root/src/IDF/Views/Admin.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
Pluf::loadFunction('Pluf_Shortcuts_RenderToResponse');
Pluf::loadFunction('Pluf_Shortcuts_GetObjectOr404');
Pluf::loadFunction('Pluf_Shortcuts_GetFormForModel');
 
/**
 * Administration's views.
 */
class IDF_Views_Admin
{
    /**
     * Home page of the administration.
     *
     * It should provide an overview of the forge status.
     */
    public $home_precond = array('Pluf_Precondition::staffRequired');
    public function home($request, $match)
    {
        $title = __('Administer');
        return Pluf_Shortcuts_RenderToResponse('idf/gadmin/home.html',
                                               array(
                                                     'page_title' => $title,
                                                     ),
                                               $request);
    }
 
    /**
     * Projects overview.
     *
     */
    public $projects_precond = array('Pluf_Precondition::staffRequired');
    public function projects($request, $match)
    {
        $title = __('Projects');
        $pag = new Pluf_Paginator(new IDF_Project());
        $pag->class = 'recent-issues';
        $pag->summary = __('This table shows the projects in the forge.');
        $pag->action = 'IDF_Views_Admin::projects';
        $pag->edit_action = array('IDF_Views_Admin::projectUpdate', 'id');
        $pag->sort_order = array('shortname', 'ASC');
        $list_display = array(
             'shortname' => __('Short Name'),
             'name' => __('Name'),
                              );
        $pag->configure($list_display, array(),
                        array('shortname'));
        $pag->items_per_page = 25;
        $pag->no_results_text = __('No projects were found.');
        $pag->setFromRequest($request);
        return Pluf_Shortcuts_RenderToResponse('idf/gadmin/projects/index.html',
                                               array(
                                                     'page_title' => $title,
                                                     'projects' => $pag,
                                                     ),
                                               $request);
    }
 
    /**
     * Edition of a project.
     *
     * One cannot switch from one source backend to another.
     */
    public $projectUpdate_precond = array('Pluf_Precondition::staffRequired');
    public function projectUpdate($request, $match)
    {
        $project = Pluf_Shortcuts_GetObjectOr404('IDF_Project', $match[1]);
        $title = sprintf(__('Update %s'), $project->name);
        $params = array(
                        'project' => $project,
                        );
        if ($request->method == 'POST') {
            $form = new IDF_Form_Admin_ProjectUpdate($request->POST, $params);
            if ($form->isValid()) {
                $form->save();
                $request->user->setMessage(__('The project has been updated.'));
                $url = Pluf_HTTP_URL_urlForView('IDF_Views_Admin::projectUpdate',
                                                array($project->id));
                return new Pluf_HTTP_Response_Redirect($url);
            }
        } else {
            $form = new IDF_Form_Admin_ProjectUpdate(null, $params);
        }
        return Pluf_Shortcuts_RenderToResponse('idf/gadmin/projects/update.html',
                                               array(
                                                     'page_title' => $title,
                                                     'project' => $project,
                                                     'form' => $form,
                                                     ),
                                               $request);
    }
 
    /**
     * Creation of a project.
     *
     */
    public $projectCreate_precond = array('Pluf_Precondition::staffRequired');
    public function projectCreate($request, $match)
    {
        $title = __('Create Project');
        $extra = array('user' => $request->user);
        if ($request->method == 'POST') {
            $form = new IDF_Form_Admin_ProjectCreate($request->POST, $extra);
            if ($form->isValid()) {
                $project = $form->save();
                $request->user->setMessage(__('The project has been created.'));
                $url = Pluf_HTTP_URL_urlForView('IDF_Views_Admin::projects');
                return new Pluf_HTTP_Response_Redirect($url);
            }
        } else {
            $form = new IDF_Form_Admin_ProjectCreate(null, $extra);
        }
        $base = Pluf::f('url_base').Pluf::f('idf_base').'/p/';
        return Pluf_Shortcuts_RenderToResponse('idf/gadmin/projects/create.html',
                                               array(
                                                     'page_title' => $title,
                                                     'form' => $form,
                                                     'base_url' => $base,
                                                     ),
                                               $request);
    }
 
    /**
     * Users overview.
     *
     */
    public $users_precond = array('Pluf_Precondition::staffRequired');
    public function users($request, $match, $not_validated=false)
    {
        $pag = new Pluf_Paginator(new Pluf_User());
        $db =& Pluf::db();
        $true = Pluf_DB_BooleanToDb(true, $db);
        if ($not_validated) {
            $pag->forced_where = new Pluf_SQL('first_name = \'---\' AND active!='.$true);
            $title = __('Not Validated User List');
        } else {
            $pag->forced_where = new Pluf_SQL('first_name != \'---\'');
            $title = __('User List');
        }
        $pag->class = 'recent-issues';
        $pag->summary = __('This table shows the users in the forge.');
        $pag->action = 'IDF_Views_Admin::users';
        $pag->edit_action = array('IDF_Views_Admin::userUpdate', 'id');
        $pag->sort_order = array('login', 'ASC');
        $list_display = array(
             'login' => __('login'),
             array('last_name', 'Pluf_Paginator_ToString', __('Name')),
             array('staff', 'IDF_Views_Admin_bool', __('Staff')),
             array('administrator', 'IDF_Views_Admin_bool', __('Admin')),
             array('active', 'IDF_Views_Admin_bool', __('Active')),
             array('last_login', 'Pluf_Paginator_DateYMDHM', __('Last Login')),
                              );
        $pag->extra_classes = array('', '', 'a-c', 'a-c', 'a-c', 'a-c');
        $pag->configure($list_display, array(), array('login', 'last_login'));
        $pag->items_per_page = 50;
        $pag->no_results_text = __('No users were found.');
        $pag->setFromRequest($request);
        return Pluf_Shortcuts_RenderToResponse('idf/gadmin/users/index.html',
                                               array(
                                                     'page_title' => $title,
                                                     'users' => $pag,
                                                     'not_validated' => $not_validated,
                                                     ),
                                               $request);
    }
     
    /**
     * Not validated users.
     */
    public $usersNotValidated_precond = array('Pluf_Precondition::staffRequired');
    public function usersNotValidated($request, $match)
    {
        return $this->users($request, $match, true);
    }
 
    /**
     * Edition of a user.
     *
     * Staff cannot edit other staff people and only admin can edit
     * staff.
     */
    public $userUpdate_precond = array('Pluf_Precondition::staffRequired');
    public function userUpdate($request, $match)
    {
        $user = Pluf_Shortcuts_GetObjectOr404('Pluf_User', $match[1]);
        $title = sprintf(__('Update %s'), $user->__toString());
        $params = array(
                        'user' => $user,
                        'request' => $request,
                        );
        // Check the rights.
        $url = Pluf_HTTP_URL_urlForView('IDF_Views_Admin::users');
        $error = __('You do not have the rights to update this user.');
        if ($user->administrator and $request->user->id != $user->id) {
            $request->user->setMessage($error);
            return new Pluf_HTTP_Response_Redirect($url);
        }
        if ($user->staff) {
            if (!$request->user->administrator and $request->user->id != $user->id) {
                $request->user->setMessage($error);
                return new Pluf_HTTP_Response_Redirect($url);
            }
        }
 
        if ($request->method == 'POST') {
            $form = new IDF_Form_Admin_UserUpdate($request->POST, $params);
            if ($form->isValid()) {
                $form->save();
                $request->user->setMessage(__('The user has been updated.'));
                return new Pluf_HTTP_Response_Redirect($url);
            }
        } else {
            $form = new IDF_Form_Admin_UserUpdate(null, $params);
        }
        return Pluf_Shortcuts_RenderToResponse('idf/gadmin/users/update.html',
                                               array(
                                                     'page_title' => $title,
                                                     'cuser' => $user,
                                                     'form' => $form,
                                                     ),
                                               $request);
    }
}
 
function IDF_Views_Admin_bool($field, $item)
{
    $img = ($item->$field) ? 'day' : 'night';
    $text = ($item->$field) ? __('Yes') : __('No');
    return sprintf('<img src="'.Pluf::f('url_media').'/idf/img/%s.png" alt="%s" /> ', $img, $text);
}

Archive Download this file

Page rendered in 0.20784s using 11 queries.