project;
$title = sprintf(__('%s Downloads'), (string) $prj);
// Paginator to paginate the files to download.
$pag = new Pluf_Paginator(new IDF_Upload());
$pag->class = 'recent-issues';
$pag->item_extra_props = array('project_m' => $prj,
'shortname' => $prj->shortname);
$pag->summary = __('This table shows the files to download.');
$pag->action = array('IDF_Views_Download::index', array($prj->shortname));
$pag->edit_action = array('IDF_Views_Download::view', 'shortname', 'id');
$list_display = array(
'file' => __('File'),
array('summary', 'IDF_Views_Download_SummaryAndLabels', __('Summary')),
array('filesize', 'IDF_Views_Download_Size', __('Size')),
array('modif_dtime', 'Pluf_Paginator_DateYMD', __('Uploaded')),
);
$pag->configure($list_display, array(), array('file', 'filesize', 'modif_dtime'));
$pag->items_per_page = 10;
$pag->no_results_text = __('No downloads were found.');
$pag->sort_order = array('file', 'ASC');
$pag->setFromRequest($request);
return Pluf_Shortcuts_RenderToResponse('downloads/index.html',
array('project' => $prj,
'page_title' => $title,
'downloads' => $pag,
),
$request);
}
/**
* View details of a file.
*/
public function view($request, $match)
{
$prj = $request->project;
$upload = Pluf_Shortcuts_GetObjectOr404('IDF_Upload', $match[2]);
$prj->inOr404($upload);
$title = sprintf(__('Download %s'), $upload->summary);
$form = false;
if ($request->method == 'POST' and
true === IDF_Precondition::projectMemberOrOwner($request)) {
$form = new IDF_Form_UpdateUpload($request->POST,
array('project' => $prj,
'upload' => $upload,
'user' => $request->user));
if ($form->isValid()) {
$upload = $form->save();
$urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view',
array($prj->shortname, $upload->id));
$request->user->setMessage(sprintf(__('The file %2$s has been updated.'), $urlfile, Pluf_esc($upload->file)));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index',
array($prj->shortname));
return new Pluf_HTTP_Response_Redirect($url);
}
} elseif (true === IDF_Precondition::projectMemberOrOwner($request)) {
$form = new IDF_Form_UpdateUpload(null,
array('upload' => $upload,
'project' => $prj,
'user' => $request->user));
}
return Pluf_Shortcuts_RenderToResponse('downloads/view.html',
array(
'file' => $upload,
'auto_labels' => self::autoCompleteArrays($prj),
'page_title' => $title,
'form' => $form,
),
$request);
}
/**
* Download a file.
*/
public function download($request, $match)
{
$prj = $request->project;
$upload = Pluf_Shortcuts_GetObjectOr404('IDF_Upload', $match[2]);
$prj->inOr404($upload);
$upload->downloads += 1;
$upload->update();
return new Pluf_HTTP_Response_Redirect($upload->getAbsoluteUrl($prj));
}
/**
* Submit a new file for download.
*/
public $submit_precond = array('IDF_Precondition::projectMemberOrOwner');
public function submit($request, $match)
{
$prj = $request->project;
$title = __('New Download');
if ($request->method == 'POST') {
$form = new IDF_Form_Upload(array_merge($request->POST, $request->FILES),
array('project' => $prj,
'user' => $request->user));
if ($form->isValid()) {
$upload = $form->save();
$urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view',
array($prj->shortname, $upload->id));
$request->user->setMessage(sprintf(__('The file has been uploaded.'), $urlfile));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index',
array($prj->shortname));
return new Pluf_HTTP_Response_Redirect($url);
}
} else {
$form = new IDF_Form_Upload(null,
array('project' => $prj,
'user' => $request->user));
}
return Pluf_Shortcuts_RenderToResponse('downloads/submit.html',
array(
'auto_labels' => self::autoCompleteArrays($prj),
'page_title' => $title,
'form' => $form,
),
$request);
}
/**
* Create the autocomplete arrays for the little AJAX stuff.
*/
public static function autoCompleteArrays($project)
{
$conf = new IDF_Conf();
$conf->setProject($project);
$st = preg_split("/\015\012|\015|\012/",
$conf->getVal('labels_downloads_predefined', IDF_Form_UploadConf::init_predefined), -1, PREG_SPLIT_NO_EMPTY);
$auto = '';
foreach ($st as $s) {
$v = '';
$d = '';
$_s = split('=', $s, 2);
if (count($_s) > 1) {
$v = trim($_s[0]);
$d = trim($_s[1]);
} else {
$v = trim($_s[0]);
}
$auto .= sprintf('{ name: "%s", to: "%s" }, ',
Pluf_esc($d), Pluf_esc($v));
}
return substr($auto, 0, -1);
}
}
/**
* Display the summary of a download, then on a new line, display the
* list of labels.
*
* The summary of the download is linking to the download.
*/
function IDF_Views_Download_SummaryAndLabels($field, $down, $extra='')
{
$tags = array();
foreach ($down->get_tags_list() as $tag) {
$tags[] = sprintf('%s', Pluf_esc((string) $tag));
}
$out = '';
if (count($tags)) {
$out = '
'.implode(', ', $tags).'';
}
return Pluf_esc($down->summary).$out;
}
function IDF_Views_Download_Size($field, $down)
{
return Pluf_Utils::prettySize($down->$field);
}