| 1 | <?php |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 3 | /* |
| 4 | # ***** BEGIN LICENSE BLOCK ***** |
| 5 | # This file is part of InDefero, an open source project management application. |
| 6 | # Copyright (C) 2008 Céondo Ltd and contributors. |
| 7 | # |
| 8 | # InDefero is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # InDefero is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program; if not, write to the Free Software |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | # |
| 22 | # ***** END LICENSE BLOCK ***** */ |
| 23 | |
| 24 | /** |
| 25 | * A comment to an issue. |
| 26 | * |
| 27 | * The first description of an issue is also stored as a comment. |
| 28 | * |
| 29 | * A comment is also tracking the changes in the main issue. |
| 30 | */ |
| 31 | class IDF_IssueComment extends Pluf_Model |
| 32 | { |
| 33 | public $_model = __CLASS__; |
| 34 | |
| 35 | function init() |
| 36 | { |
| 37 | $this->_a['table'] = 'idf_issuecomments'; |
| 38 | $this->_a['model'] = __CLASS__; |
| 39 | $this->_a['cols'] = array( |
| 40 | // It is mandatory to have an "id" column. |
| 41 | 'id' => |
| 42 | array( |
| 43 | 'type' => 'Pluf_DB_Field_Sequence', |
| 44 | 'blank' => true, |
| 45 | ), |
| 46 | 'issue' => |
| 47 | array( |
| 48 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 49 | 'model' => 'IDF_Issue', |
| 50 | 'blank' => false, |
| 51 | 'verbose' => __('issue'), |
| 52 | 'relate_name' => 'comments', |
| 53 | ), |
| 54 | 'content' => |
| 55 | array( |
| 56 | 'type' => 'Pluf_DB_Field_Text', |
| 57 | 'blank' => false, |
| 58 | 'verbose' => __('comment'), |
| 59 | ), |
| 60 | 'submitter' => |
| 61 | array( |
| 62 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 63 | 'model' => 'Pluf_User', |
| 64 | 'blank' => false, |
| 65 | 'verbose' => __('submitter'), |
| 66 | 'relate_name' => 'commented_issue', |
| 67 | ), |
| 68 | 'changes' => |
| 69 | array( |
| 70 | 'type' => 'Pluf_DB_Field_Serialized', |
| 71 | 'blank' => true, |
| 72 | 'verbose' => __('changes'), |
| 73 | 'help_text' => __('Serialized array of the changes in the issue.'), |
| 74 | ), |
| 75 | 'creation_dtime' => |
| 76 | array( |
| 77 | 'type' => 'Pluf_DB_Field_Datetime', |
| 78 | 'blank' => true, |
| 79 | 'verbose' => __('creation date'), |
| 80 | ), |
| 81 | ); |
| 82 | $this->_a['idx'] = array( |
| 83 | 'creation_dtime_idx' => |
| 84 | array( |
| 85 | 'col' => 'creation_dtime', |
| 86 | 'type' => 'normal', |
| 87 | ), |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | function changedIssue() |
| 92 | { |
| 93 | return (is_array($this->changes) and count($this->changes) > 0); |
| 94 | } |
| 95 | |
| 96 | function _toIndex() |
| 97 | { |
| 98 | return $this->content; |
| 99 | } |
| 100 | |
| 101 | function preDelete() |
| 102 | { |
| 103 | IDF_Timeline::remove($this); |
| 104 | } |
| 105 | |
| 106 | function preSave($create=false) |
| 107 | { |
| 108 | if ($this->id == '') { |
| 109 | $this->creation_dtime = gmdate('Y-m-d H:i:s'); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function postSave($create=false) |
| 114 | { |
| 115 | if ($create) { |
| 116 | // Check if more than one comment for this issue. We do |
| 117 | // not want to insert the first comment in the timeline as |
| 118 | // the issue itself is inserted. |
| 119 | $sql = new Pluf_SQL('issue=%s', array($this->issue)); |
| 120 | $co = Pluf::factory('IDF_IssueComment')->getList(array('filter'=>$sql->gen())); |
| 121 | if ($co->count() > 1) { |
| 122 | IDF_Timeline::insert($this, $this->get_issue()->get_project(), |
| 123 | $this->get_submitter()); |
| 124 | } |
| 125 | } |
| 126 | IDF_Search::index($this->get_issue()); |
| 127 | } |
| 128 | |
| 129 | public function timelineFragment($request) |
| 130 | { |
| 131 | $issue = $this->get_issue(); |
| 132 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Issue::view', |
| 133 | array($request->project->shortname, |
| 134 | $issue->id)); |
| 135 | $url .= '#ic'.$this->id; |
| 136 | $out = "\n".'<tr class="log"><td><a href="'.$url.'">'. |
| 137 | Pluf_esc(Pluf_Template_dateAgo($this->creation_dtime, 'without')). |
| 138 | '</a></td><td>'; |
| 139 | $stag = new IDF_Template_ShowUser(); |
| 140 | $user = $stag->start($this->get_submitter(), $request, '', false); |
| 141 | |
| 142 | $ic = (in_array($issue->status, $request->project->getTagIdsByStatus('closed'))) ? 'issue-c' : 'issue-o'; |
| 143 | $out .= sprintf(__('<a href="%1$s" class="%2$s" title="View issue">Issue %3$d</a>, %4$s'), $url, $ic, $issue->id, Pluf_esc($issue->summary)); |
| 144 | |
| 145 | if ($this->changedIssue()) { |
| 146 | $out .= '<div class="issue-changes-timeline">'; |
| 147 | foreach ($this->changes as $w => $v) { |
| 148 | $out .= '<strong>'; |
| 149 | switch ($w) { |
| 150 | case 'su': |
| 151 | $out .= __('Summary:'); break; |
| 152 | case 'st': |
| 153 | $out .= __('Status:'); break; |
| 154 | case 'ow': |
| 155 | $out .= __('Owner:'); break; |
| 156 | case 'lb': |
| 157 | $out .= __('Labels:'); break; |
| 158 | } |
| 159 | $out .= '</strong> '; |
| 160 | if ($w == 'lb') { |
| 161 | $out .= Pluf_esc(implode(', ', $v)); |
| 162 | } else { |
| 163 | $out .= Pluf_esc($v); |
| 164 | } |
| 165 | $out .= ' '; |
| 166 | } |
| 167 | $out .= '</div>'; |
| 168 | } |
| 169 | $out .= '</td></tr>'; |
| 170 | $out .= "\n".'<tr class="extra"><td colspan="2"> |
| 171 | <div class="helptext right">'.sprintf(__('Comment on <a href="%s" class="%s">issue %d</a>, by %s'), $url, $ic, $issue->id, $user).'</div></td></tr>'; |
| 172 | return Pluf_Template::markSafe($out); |
| 173 | } |
| 174 | |
| 175 | public function feedFragment($request) |
| 176 | { |
| 177 | $issue = $this->get_issue(); |
| 178 | $url = Pluf::f('url_base') |
| 179 | .Pluf_HTTP_URL_urlForView('IDF_Views_Issue::view', |
| 180 | array($request->project->shortname, |
| 181 | $issue->id)); |
| 182 | $title = sprintf(__('%s: Comment on issue %d - %s'), |
| 183 | Pluf_esc($request->project->name), |
| 184 | $issue->id, Pluf_esc($issue->summary)); |
| 185 | $url .= '#ic'.$this->id; |
| 186 | $date = Pluf_Date::gmDateToGmString($this->creation_dtime); |
| 187 | $context = new Pluf_Template_Context_Request( |
| 188 | $request, |
| 189 | array('url' => $url, |
| 190 | 'author' => $issue->get_submitter(), |
| 191 | 'title' => $title, |
| 192 | 'c' => $this, |
| 193 | 'issue' => $issue, |
| 194 | 'date' => $date) |
| 195 | ); |
| 196 | $tmpl = new Pluf_Template('idf/issues/feedfragment.xml'); |
| 197 | return $tmpl->render($context); |
| 198 | } |
| 199 | } |
| 200 | |