Root/
| Source at commit 7383e18dff19e7e6a1e56d55cdfbfa109e91bfb3 created 2 years 1 month ago. By Loic d'Anterroches, Fixed issue 4, with fine control over the tabs access. | |
|---|---|
| 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_Shortcuts_RenderToResponse'); |
| 26 | Pluf::loadFunction('Pluf_Shortcuts_GetObjectOr404'); |
| 27 | Pluf::loadFunction('Pluf_Shortcuts_GetFormForModel'); |
| 28 | |
| 29 | /** |
| 30 | * Download's views. |
| 31 | * |
| 32 | * - List all the files. |
| 33 | * - Upload a file. |
| 34 | * - See the details of a file. |
| 35 | */ |
| 36 | class IDF_Views_Download |
| 37 | { |
| 38 | /** |
| 39 | * List the files available for download. |
| 40 | */ |
| 41 | public $index_precond = array('IDF_Precondition::accessDownloads'); |
| 42 | public function index($request, $match) |
| 43 | { |
| 44 | $prj = $request->project; |
| 45 | $title = sprintf(__('%s Downloads'), (string) $prj); |
| 46 | // Paginator to paginate the files to download. |
| 47 | $pag = new Pluf_Paginator(new IDF_Upload()); |
| 48 | $pag->class = 'recent-issues'; |
| 49 | $pag->item_extra_props = array('project_m' => $prj, |
| 50 | 'shortname' => $prj->shortname); |
| 51 | $pag->summary = __('This table shows the files to download.'); |
| 52 | $pag->action = array('IDF_Views_Download::index', array($prj->shortname)); |
| 53 | $pag->edit_action = array('IDF_Views_Download::view', 'shortname', 'id'); |
| 54 | $list_display = array( |
| 55 | 'file' => __('File'), |
| 56 | array('summary', 'IDF_Views_Download_SummaryAndLabels', __('Summary')), |
| 57 | array('filesize', 'IDF_Views_Download_Size', __('Size')), |
| 58 | array('creation_dtime', 'Pluf_Paginator_DateYMD', __('Uploaded')), |
| 59 | ); |
| 60 | $pag->configure($list_display, array(), array('file', 'filesize', 'creation_dtime')); |
| 61 | $pag->items_per_page = 10; |
| 62 | $pag->no_results_text = __('No downloads were found.'); |
| 63 | $pag->sort_order = array('creation_dtime', 'DESC'); |
| 64 | $pag->setFromRequest($request); |
| 65 | $tags = $prj->getTagCloud('downloads'); |
| 66 | return Pluf_Shortcuts_RenderToResponse('downloads/index.html', |
| 67 | array( |
| 68 | 'page_title' => $title, |
| 69 | 'downloads' => $pag, |
| 70 | 'tags' => $tags, |
| 71 | ), |
| 72 | $request); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * View details of a file. |
| 78 | */ |
| 79 | public $view_precond = array('IDF_Precondition::accessDownloads'); |
| 80 | public function view($request, $match) |
| 81 | { |
| 82 | $prj = $request->project; |
| 83 | $upload = Pluf_Shortcuts_GetObjectOr404('IDF_Upload', $match[2]); |
| 84 | $prj->inOr404($upload); |
| 85 | $title = sprintf(__('Download %s'), $upload->summary); |
| 86 | $form = false; |
| 87 | $ptags = self::getDownloadTags($prj); |
| 88 | $dtag = array_pop($ptags); // The last tag is the deprecated tag. |
| 89 | $tags = $upload->get_tags_list(); |
| 90 | $deprecated = Pluf_Model_InArray($dtag, $tags); |
| 91 | if ($request->method == 'POST' and |
| 92 | true === IDF_Precondition::projectMemberOrOwner($request)) { |
| 93 | |
| 94 | $form = new IDF_Form_UpdateUpload($request->POST, |
| 95 | array('project' => $prj, |
| 96 | 'upload' => $upload, |
| 97 | 'user' => $request->user)); |
| 98 | if ($form->isValid()) { |
| 99 | $upload = $form->save(); |
| 100 | $urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view', |
| 101 | array($prj->shortname, $upload->id)); |
| 102 | $request->user->setMessage(sprintf(__('The file <a href="%1$s">%2$s</a> has been updated.'), $urlfile, Pluf_esc($upload->file))); |
| 103 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index', |
| 104 | array($prj->shortname)); |
| 105 | return new Pluf_HTTP_Response_Redirect($url); |
| 106 | } |
| 107 | } elseif (true === IDF_Precondition::projectMemberOrOwner($request)) { |
| 108 | $form = new IDF_Form_UpdateUpload(null, |
| 109 | array('upload' => $upload, |
| 110 | 'project' => $prj, |
| 111 | 'user' => $request->user)); |
| 112 | } |
| 113 | return Pluf_Shortcuts_RenderToResponse('downloads/view.html', |
| 114 | array( |
| 115 | 'file' => $upload, |
| 116 | 'deprecated' => $deprecated, |
| 117 | 'tags' => $tags, |
| 118 | 'auto_labels' => self::autoCompleteArrays($prj), |
| 119 | 'page_title' => $title, |
| 120 | 'form' => $form, |
| 121 | ), |
| 122 | $request); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Download a file. |
| 127 | */ |
| 128 | public $download_precond = array('IDF_Precondition::accessDownloads'); |
| 129 | public function download($request, $match) |
| 130 | { |
| 131 | $prj = $request->project; |
| 132 | $upload = Pluf_Shortcuts_GetObjectOr404('IDF_Upload', $match[2]); |
| 133 | $prj->inOr404($upload); |
| 134 | $upload->downloads += 1; |
| 135 | $upload->update(); |
| 136 | return new Pluf_HTTP_Response_Redirect($upload->getAbsoluteUrl($prj)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Submit a new file for download. |
| 141 | */ |
| 142 | public $submit_precond = array('IDF_Precondition::accessDownloads', |
| 143 | 'IDF_Precondition::projectMemberOrOwner'); |
| 144 | public function submit($request, $match) |
| 145 | { |
| 146 | $prj = $request->project; |
| 147 | $title = __('New Download'); |
| 148 | if ($request->method == 'POST') { |
| 149 | $form = new IDF_Form_Upload(array_merge($request->POST, $request->FILES), |
| 150 | array('project' => $prj, |
| 151 | 'user' => $request->user)); |
| 152 | if ($form->isValid()) { |
| 153 | $upload = $form->save(); |
| 154 | $urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view', |
| 155 | array($prj->shortname, $upload->id)); |
| 156 | $request->user->setMessage(sprintf(__('The <a href="%s">file</a> has been uploaded.'), $urlfile)); |
| 157 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index', |
| 158 | array($prj->shortname)); |
| 159 | return new Pluf_HTTP_Response_Redirect($url); |
| 160 | } |
| 161 | } else { |
| 162 | $form = new IDF_Form_Upload(null, |
| 163 | array('project' => $prj, |
| 164 | 'user' => $request->user)); |
| 165 | } |
| 166 | return Pluf_Shortcuts_RenderToResponse('downloads/submit.html', |
| 167 | array( |
| 168 | 'auto_labels' => self::autoCompleteArrays($prj), |
| 169 | 'page_title' => $title, |
| 170 | 'form' => $form, |
| 171 | ), |
| 172 | $request); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Create the autocomplete arrays for the little AJAX stuff. |
| 177 | */ |
| 178 | public static function autoCompleteArrays($project) |
| 179 | { |
| 180 | $conf = new IDF_Conf(); |
| 181 | $conf->setProject($project); |
| 182 | $st = preg_split("/\015\012|\015|\012/", |
| 183 | $conf->getVal('labels_downloads_predefined', IDF_Form_UploadConf::init_predefined), -1, PREG_SPLIT_NO_EMPTY); |
| 184 | $auto = ''; |
| 185 | foreach ($st as $s) { |
| 186 | $v = ''; |
| 187 | $d = ''; |
| 188 | $_s = split('=', $s, 2); |
| 189 | if (count($_s) > 1) { |
| 190 | $v = trim($_s[0]); |
| 191 | $d = trim($_s[1]); |
| 192 | } else { |
| 193 | $v = trim($_s[0]); |
| 194 | } |
| 195 | $auto .= sprintf('{ name: "%s", to: "%s" }, ', |
| 196 | Pluf_esc($d), Pluf_esc($v)); |
| 197 | } |
| 198 | return substr($auto, 0, -1); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * View list of downloads with a given label. |
| 203 | */ |
| 204 | public $listLabel_precond = array('IDF_Precondition::accessDownloads'); |
| 205 | public function listLabel($request, $match) |
| 206 | { |
| 207 | $prj = $request->project; |
| 208 | $tag = Pluf_Shortcuts_GetObjectOr404('IDF_Tag', $match[2]); |
| 209 | $prj->inOr404($tag); |
| 210 | $title = sprintf(__('%1$s Downloads with Label %2$s'), (string) $prj, |
| 211 | (string) $tag); |
| 212 | // Paginator to paginate the downloads |
| 213 | $pag = new Pluf_Paginator(new IDF_Upload()); |
| 214 | $pag->model_view = 'join_tags'; |
| 215 | $pag->class = 'recent-issues'; |
| 216 | $pag->item_extra_props = array('project_m' => $prj, |
| 217 | 'shortname' => $prj->shortname); |
| 218 | $pag->summary = sprintf(__('This table shows the downloads with label %s.'), (string) $tag); |
| 219 | $pag->forced_where = new Pluf_SQL('project=%s AND idf_tag_id=%s', array($prj->id, $tag->id)); |
| 220 | $pag->action = array('IDF_Views_Download::index', array($prj->shortname)); |
| 221 | $pag->edit_action = array('IDF_Views_Download::view', 'shortname', 'id'); |
| 222 | $list_display = array( |
| 223 | 'file' => __('File'), |
| 224 | array('summary', 'IDF_Views_Download_SummaryAndLabels', __('Summary')), |
| 225 | array('filesize', 'IDF_Views_Download_Size', __('Size')), |
| 226 | array('creation_dtime', 'Pluf_Paginator_DateYMD', __('Uploaded')), |
| 227 | ); |
| 228 | $pag->configure($list_display, array(), array('file', 'filesize', 'creation_dtime')); |
| 229 | $pag->items_per_page = 10; |
| 230 | $pag->no_results_text = __('No downloads were found.'); |
| 231 | $pag->sort_order = array('creation_dtime', 'DESC'); |
| 232 | $pag->setFromRequest($request); |
| 233 | $tags = $prj->getTagCloud('downloads'); |
| 234 | return Pluf_Shortcuts_RenderToResponse('downloads/index.html', |
| 235 | array( |
| 236 | 'page_title' => $title, |
| 237 | 'label' => $tag, |
| 238 | 'downloads' => $pag, |
| 239 | 'tags' => $tags, |
| 240 | ), |
| 241 | $request); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Get the download tags. |
| 246 | * |
| 247 | * @param IDF_Project |
| 248 | * @return ArrayObject The tags |
| 249 | */ |
| 250 | public static function getDownloadTags($project) |
| 251 | { |
| 252 | return $project->getTagsFromConfig('labels_downloads_predefined', |
| 253 | IDF_Form_UploadConf::init_predefined); |
| 254 | |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Display the summary of a download, then on a new line, display the |
| 260 | * list of labels. |
| 261 | * |
| 262 | * The summary of the download is linking to the download. |
| 263 | */ |
| 264 | function IDF_Views_Download_SummaryAndLabels($field, $down, $extra='') |
| 265 | { |
| 266 | $tags = array(); |
| 267 | foreach ($down->get_tags_list() as $tag) { |
| 268 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::listLabel', |
| 269 | array($down->shortname, $tag->id)); |
| 270 | $tags[] = sprintf('<a href="%s" class="label">%s</a>', $url, Pluf_esc((string) $tag)); |
| 271 | } |
| 272 | $out = ''; |
| 273 | if (count($tags)) { |
| 274 | $out = '<br /><span class="note">'.implode(', ', $tags).'</span>'; |
| 275 | } |
| 276 | return Pluf_esc($down->summary).$out; |
| 277 | } |
| 278 | |
| 279 | function IDF_Views_Download_Size($field, $down) |
| 280 | { |
| 281 | return Pluf_Utils::prettySize($down->$field); |
| 282 | } |
