srchub

srchub Git Source Tree


Root/indefero/src/IDF/Form/ReviewFileComment.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
<?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-2011 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 ***** */
 
/**
 * Add comments to files in a review.
 *
 */
class IDF_Form_ReviewFileComment extends Pluf_Form
{
    public $files = null;
    public $patch = null;
    public $user = null;
    public $project = null;
 
    public function initFields($extra=array())
    {
        $this->files = $extra['files'];
        $this->patch = $extra['patch'];
        $this->user = $extra['user'];
        $this->project = $extra['project'];
 
        foreach ($this->files as $filename => $def) {
            $this->fields[md5($filename)] = new Pluf_Form_Field_Varchar(
                                      array('required' => false,
                                            'label' => __('Comment'),
                                            'initial' => '',
                                            'widget' => 'Pluf_Form_Widget_TextareaInput',
                                            'widget_attrs' => array(
                                                       'cols' => 58,
                                                       'rows' => 9,
                                                                    ),
                                            ));
        }
        $this->fields['content'] = new Pluf_Form_Field_Varchar(
                                      array('required' => false,
                                            'label' => __('General comment'),
                                            'initial' => '',
                                            'widget' => 'Pluf_Form_Widget_TextareaInput',
                                            'widget_attrs' => array(
                                                       'cols' => 58,
                                                       'rows' => 9,
                                                                    ),
                                            ));
        if ($this->user->hasPerm('IDF.project-owner', $this->project)
            or $this->user->hasPerm('IDF.project-member', $this->project)) {
            $this->show_full = true;
        } else {
            $this->show_full = false;
        }
        if ($this->show_full) {
            $this->fields['summary'] = new Pluf_Form_Field_Varchar(
                                      array('required' => true,
                                            'label' => __('Summary'),
                                            'initial' => $this->patch->get_review()->summary,
                                            'widget_attrs' => array(
                                                       'maxlength' => 200,
                                                       'size' => 67,
                                                                    ),
                                            ));
 
            $this->fields['status'] = new Pluf_Form_Field_Varchar(
                                      array('required' => true,
                                            'label' => __('Status'),
                                            'initial' => $this->patch->get_review()->get_status()->name,
                                            'widget_attrs' => array(
                                                       'maxlength' => 20,
                                                       'size' => 15,
                                                                    ),
                                            ));
        }
    }
 
 
    /**
     * Validate the interconnection in the form.
     */
    public function clean()
    {
        $isOk = false;
         
        foreach($this->files as $filename => $def) {
            $this->cleaned_data[md5($filename)] = trim($this->cleaned_data[md5($filename)]);
            if(!empty($this->cleaned_data[md5($filename)])) {
                $isOk = true;
            }
        }
         
        if(!empty($this->cleaned_data['content'])) {
            $isOk = true;
        }
         
        if (!$isOk) {
            throw new Pluf_Form_Invalid(__('You need to provide your general comment about the proposal, or comments on at least one file.'));
        }
         
        return $this->cleaned_data;
    }
 
    function clean_content()
    {
        $content = trim($this->cleaned_data['content']);
        if(empty($content)) {
            if ($this->fields['status']->initial != $this->fields['status']->value) {
                return __('The status have been updated.');
            }
        } else {
            return $content;
        }
         
        throw new Pluf_Form_Invalid(__('This field is required.'));
    }
 
    /**
     * Save the model in the database.
     *
     * @param bool Commit in the database or not. If not, the object
     *             is returned but not saved in the database.
     * @return Object Model with data set from the form.
     */
    function save($commit=true)
    {
        if (!$this->isValid()) {
            throw new Exception(__('Cannot save the model from an invalid form.'));
        }
        // create a base comment
        $bc = new IDF_Review_Comment();
        $bc->patch = $this->patch;
        $bc->submitter = $this->user;
        $bc->content = $this->cleaned_data['content'];
        $review = $this->patch->get_review();
        if ($this->show_full) {
            // Compare between the old and the new data
            // Status, summary
            $changes = array();
            $status = IDF_Tag::add(trim($this->cleaned_data['status']), $this->project, 'Status');
            if ($status->id != $this->patch->get_review()->status) {
                $changes['st'] = $status->name;
            }
            if (trim($this->patch->get_review()->summary) != trim($this->cleaned_data['summary'])) {
                $changes['su'] = trim($this->cleaned_data['summary']);
            }
            // Update the review
            $review->summary = trim($this->cleaned_data['summary']);
            $review->status = $status;
            $bc->changes = $changes;
        }
        $bc->create();
        foreach ($this->files as $filename => $def) {
            if (!empty($this->cleaned_data[md5($filename)])) {
                // Add a comment.
                $c = new IDF_Review_FileComment();
                $c->comment = $bc;
                $c->cfile = $filename;
                $c->content = $this->cleaned_data[md5($filename)];
                $c->create();
            }
        }
        $review->update(); // reindex and put up in the list.
        return $bc;
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.20927s using 11 queries.