| 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 | Pluf::loadFunction('Pluf_HTTP_URL_urlForView'); |
| 25 | Pluf::loadFunction('Pluf_Template_dateAgo'); |
| 26 | |
| 27 | /** |
| 28 | * Base definition of an issue. |
| 29 | * |
| 30 | * An issue can have labels, comments, can be starred by people. |
| 31 | */ |
| 32 | class IDF_Issue extends Pluf_Model |
| 33 | { |
| 34 | public $_model = __CLASS__; |
| 35 | |
| 36 | function init() |
| 37 | { |
| 38 | $this->_a['table'] = 'idf_issues'; |
| 39 | $this->_a['model'] = __CLASS__; |
| 40 | $this->_a['cols'] = array( |
| 41 | // It is mandatory to have an "id" column. |
| 42 | 'id' => |
| 43 | array( |
| 44 | 'type' => 'Pluf_DB_Field_Sequence', |
| 45 | 'blank' => true, |
| 46 | ), |
| 47 | 'project' => |
| 48 | array( |
| 49 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 50 | 'model' => 'IDF_Project', |
| 51 | 'blank' => false, |
| 52 | 'verbose' => __('project'), |
| 53 | 'relate_name' => 'issues', |
| 54 | ), |
| 55 | 'summary' => |
| 56 | array( |
| 57 | 'type' => 'Pluf_DB_Field_Varchar', |
| 58 | 'blank' => false, |
| 59 | 'size' => 250, |
| 60 | 'verbose' => __('summary'), |
| 61 | ), |
| 62 | 'submitter' => |
| 63 | array( |
| 64 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 65 | 'model' => 'Pluf_User', |
| 66 | 'blank' => false, |
| 67 | 'verbose' => __('submitter'), |
| 68 | 'relate_name' => 'submitted_issue', |
| 69 | ), |
| 70 | 'owner' => |
| 71 | array( |
| 72 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 73 | 'model' => 'Pluf_User', |
| 74 | 'blank' => true, // no owner when submitted. |
| 75 | 'is_null' => true, |
| 76 | 'verbose' => __('owner'), |
| 77 | 'relate_name' => 'owned_issue', |
| 78 | ), |
| 79 | 'interested' => |
| 80 | array( |
| 81 | 'type' => 'Pluf_DB_Field_Manytomany', |
| 82 | 'model' => 'Pluf_User', |
| 83 | 'blank' => true, |
| 84 | 'verbose' => __('interested users'), |
| 85 | 'help_text' => __('Interested users will get an email notification when the issue is changed.'), |
| 86 | ), |
| 87 | 'tags' => |
| 88 | array( |
| 89 | 'type' => 'Pluf_DB_Field_Manytomany', |
| 90 | 'blank' => true, |
| 91 | 'model' => 'IDF_Tag', |
| 92 | 'verbose' => __('labels'), |
| 93 | ), |
| 94 | 'status' => |
| 95 | array( |
| 96 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 97 | 'blank' => false, |
| 98 | 'model' => 'IDF_Tag', |
| 99 | 'verbose' => __('status'), |
| 100 | ), |
| 101 | 'creation_dtime' => |
| 102 | array( |
| 103 | 'type' => 'Pluf_DB_Field_Datetime', |
| 104 | 'blank' => true, |
| 105 | 'verbose' => __('creation date'), |
| 106 | ), |
| 107 | 'modif_dtime' => |
| 108 | array( |
| 109 | 'type' => 'Pluf_DB_Field_Datetime', |
| 110 | 'blank' => true, |
| 111 | 'verbose' => __('modification date'), |
| 112 | ), |
| 113 | ); |
| 114 | $this->_a['idx'] = array( |
| 115 | 'modif_dtime_idx' => |
| 116 | array( |
| 117 | 'col' => 'modif_dtime', |
| 118 | 'type' => 'normal', |
| 119 | ), |
| 120 | ); |
| 121 | $table = $this->_con->pfx.'idf_issue_idf_tag_assoc'; |
| 122 | $this->_a['views'] = array( |
| 123 | 'join_tags' => |
| 124 | array( |
| 125 | 'join' => 'LEFT JOIN '.$table |
| 126 | .' ON idf_issue_id=id', |
| 127 | ), |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | function __toString() |
| 132 | { |
| 133 | return $this->id.' - '.$this->summary; |
| 134 | } |
| 135 | |
| 136 | function _toIndex() |
| 137 | { |
| 138 | $r = array(); |
| 139 | foreach ($this->get_comments_list() as $c) { |
| 140 | $r[] = $c->_toIndex(); |
| 141 | } |
| 142 | $str = str_repeat($this->summary.' ', 4).' '.implode(' ', $r); |
| 143 | return Pluf_Text::cleanString(html_entity_decode($str, ENT_QUOTES, 'UTF-8')); |
| 144 | } |
| 145 | |
| 146 | function preDelete() |
| 147 | { |
| 148 | IDF_Timeline::remove($this); |
| 149 | IDF_Search::remove($this); |
| 150 | } |
| 151 | |
| 152 | function preSave($create=false) |
| 153 | { |
| 154 | if ($this->id == '') { |
| 155 | $this->creation_dtime = gmdate('Y-m-d H:i:s'); |
| 156 | } |
| 157 | $this->modif_dtime = gmdate('Y-m-d H:i:s'); |
| 158 | } |
| 159 | |
| 160 | function postSave($create=false) |
| 161 | { |
| 162 | // Note: No indexing is performed here. The indexing is |
| 163 | // triggered in the postSave step of the comment to ensure |
| 164 | // that the issue as at least one comment in the database when |
| 165 | // doing the indexing. |
| 166 | if ($create) { |
| 167 | IDF_Timeline::insert($this, $this->get_project(), |
| 168 | $this->get_submitter()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns an HTML fragment used to display this issue in the |
| 174 | * timeline. |
| 175 | * |
| 176 | * The request object is given to be able to check the rights and |
| 177 | * as such create links to other items etc. You can consider that |
| 178 | * if displayed, you can create a link to it. |
| 179 | * |
| 180 | * @param Pluf_HTTP_Request |
| 181 | * @return Pluf_Template_SafeString |
| 182 | */ |
| 183 | public function timelineFragment($request) |
| 184 | { |
| 185 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Issue::view', |
| 186 | array($request->project->shortname, |
| 187 | $this->id)); |
| 188 | $out = '<tr class="log"><td><a href="'.$url.'">'. |
| 189 | Pluf_esc(Pluf_Template_dateAgo($this->creation_dtime, 'without')). |
| 190 | '</a></td><td>'; |
| 191 | $stag = new IDF_Template_ShowUser(); |
| 192 | $user = $stag->start($this->get_submitter(), $request, '', false); |
| 193 | $ic = (in_array($this->status, $request->project->getTagIdsByStatus('closed'))) ? 'issue-c' : 'issue-o'; |
| 194 | $out .= sprintf(__('<a href="%1$s" class="%2$s" title="View issue">Issue %3$d</a>, %4$s'), $url, $ic, $this->id, Pluf_esc($this->summary)).'</td>'; |
| 195 | $out .= "\n".'<tr class="extra"><td colspan="2"> |
| 196 | <div class="helptext right">'.sprintf(__('Creation of <a href="%s" class="%s">issue %d</a>, by %s'), $url, $ic, $this->id, $user).'</div></td></tr>'; |
| 197 | return Pluf_Template::markSafe($out); |
| 198 | } |
| 199 | |
| 200 | public function feedFragment($request) |
| 201 | { |
| 202 | $url = Pluf::f('url_base') |
| 203 | .Pluf_HTTP_URL_urlForView('IDF_Views_Issue::view', |
| 204 | array($request->project->shortname, |
| 205 | $this->id)); |
| 206 | $title = sprintf(__('%s: Issue %d created - %s'), |
| 207 | $request->project->name, |
| 208 | $this->id, $this->summary); |
| 209 | $cts = $this->get_comments_list(array('order' => 'id ASC', |
| 210 | 'nb' => 1)); |
| 211 | $date = Pluf_Date::gmDateToGmString($this->creation_dtime); |
| 212 | $context = new Pluf_Template_Context_Request( |
| 213 | $request, |
| 214 | array('url' => $url, |
| 215 | 'author' => $this->get_submitter(), |
| 216 | 'title' => $title, |
| 217 | 'c' => $cts[0], |
| 218 | 'issue' => $this, |
| 219 | 'date' => $date) |
| 220 | ); |
| 221 | $tmpl = new Pluf_Template('idf/issues/feedfragment.xml'); |
| 222 | return $tmpl->render($context); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Notification of change of the object. |
| 227 | * |
| 228 | * For the moment, only email, but one can add webhooks later. |
| 229 | * |
| 230 | * Usage: |
| 231 | * <pre> |
| 232 | * $this->notify($conf); // Notify the creation |
| 233 | * $this->notify($conf, false); // Notify the update of the object |
| 234 | * </pre> |
| 235 | * |
| 236 | * @param IDF_Conf Current configuration |
| 237 | * @param bool Creation (true) |
| 238 | */ |
| 239 | public function notify($conf, $create=true) |
| 240 | { |
| 241 | $prj = $this->get_project(); |
| 242 | $to_email = array(); |
| 243 | if ('' != $conf->getVal('issues_notification_email', '')) { |
| 244 | $langs = Pluf::f('languages', array('en')); |
| 245 | $to_email[] = array($conf->getVal('issues_notification_email'), |
| 246 | $langs[0]); |
| 247 | } |
| 248 | $current_locale = Pluf_Translation::getLocale(); |
| 249 | |
| 250 | if ($create) { |
| 251 | if (null != $this->get_owner() and $this->owner != $this->submitter) { |
| 252 | $email_lang = array($this->get_owner()->email, |
| 253 | $this->get_owner()->language); |
| 254 | if (!in_array($email_lang, $to_email)) { |
| 255 | $to_email[] = $email_lang; |
| 256 | } |
| 257 | } |
| 258 | $comments = $this->get_comments_list(array('order' => 'id ASC')); |
| 259 | $context = new Pluf_Template_Context( |
| 260 | array( |
| 261 | 'issue' => $this, |
| 262 | 'comment' => $comments[0], |
| 263 | 'project' => $prj, |
| 264 | 'url_base' => Pluf::f('url_base'), |
| 265 | ) |
| 266 | ); |
| 267 | foreach ($to_email as $email_lang) { |
| 268 | Pluf_Translation::loadSetLocale($email_lang[1]); |
| 269 | $email = new Pluf_Mail(Pluf::f('from_email'), $email_lang[0], |
| 270 | sprintf(__('Issue %s - %s (%s)'), |
| 271 | $this->id, $this->summary, $prj->shortname)); |
| 272 | $tmpl = new Pluf_Template('idf/issues/issue-created-email.txt'); |
| 273 | $email->addTextMessage($tmpl->render($context)); |
| 274 | $email->sendMail(); |
| 275 | } |
| 276 | } else { |
| 277 | $comments = $this->get_comments_list(array('order' => 'id DESC')); |
| 278 | $email_sender = ''; |
| 279 | if (isset($comments[0])) { |
| 280 | $email_sender = $comments[0]->get_submitter()->email; |
| 281 | } |
| 282 | foreach ($this->get_interested_list() as $interested) { |
| 283 | $email_lang = array($interested->email, |
| 284 | $interested->language); |
| 285 | if (!in_array($email_lang, $to_email)) { |
| 286 | $to_email[] = $email_lang; |
| 287 | } |
| 288 | } |
| 289 | $email_lang = array($this->get_submitter()->email, |
| 290 | $this->get_submitter()->language); |
| 291 | if (!in_array($email_lang, $to_email)) { |
| 292 | $to_email[] = $email_lang; |
| 293 | } |
| 294 | if (null != $this->get_owner()) { |
| 295 | $email_lang = array($this->get_owner()->email, |
| 296 | $this->get_owner()->language); |
| 297 | if (!in_array($email_lang, $to_email)) { |
| 298 | $to_email[] = $email_lang; |
| 299 | } |
| 300 | } |
| 301 | $context = new Pluf_Template_Context( |
| 302 | array( |
| 303 | 'issue' => $this, |
| 304 | 'comments' => $comments, |
| 305 | 'project' => $prj, |
| 306 | 'url_base' => Pluf::f('url_base'), |
| 307 | )); |
| 308 | foreach ($to_email as $email_lang) { |
| 309 | if ($email_lang[0] == $email_sender) { |
| 310 | continue; // Do not notify the one having created |
| 311 | // the comment |
| 312 | } |
| 313 | Pluf_Translation::loadSetLocale($email_lang[1]); |
| 314 | $email = new Pluf_Mail(Pluf::f('from_email'), $email_lang[0], |
| 315 | sprintf(__('Updated Issue %s - %s (%s)'), |
| 316 | $this->id, $this->summary, $prj->shortname)); |
| 317 | $tmpl = new Pluf_Template('idf/issues/issue-updated-email.txt'); |
| 318 | $email->addTextMessage($tmpl->render($context)); |
| 319 | $email->sendMail(); |
| 320 | } |
| 321 | } |
| 322 | Pluf_Translation::loadSetLocale($current_locale); |
| 323 | } |
| 324 | } |