Indefero

Indefero Git Source Tree

Root/src/IDF/Views/Download.php

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

Archive Download this file