Skip to content

Commit 74b9a1b

Browse files
committed
Twig extension
1 parent 7fd6e97 commit 74b9a1b

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ PunkAveFileUploaderBundle
44
Introduction
55
============
66

7-
This bundle provides multiple file uploads, based on the [BlueImp jQuery file uploader](https://github.com/blueimp/jQuery-File-Upload/) package. Both drag and drop and multiple file selection are fully supported in compatible browsers. We chose BlueImp because it has excellent backwards and forwards browser compatibility.
7+
This bundle provides multiple file uploads, based on the [BlueImp jQuery file uploader](https://github.com/blueimp/jQuery-File-Upload/) package. Both drag and drop and multiple file selection are fully supported in compatible browsers. We chose BlueImp because it has excellent backwards and forwards browser compatibility.
88

99
This bundle is a fairly thin wrapper because the existing PHP uploader class provided by BlueImp is very good already and does so many excellent things straight out of the box. We provided a way to integrate it into a Symfony 2 project.
1010

11-
The uploader delivers files to a folder that you specify. If that folder already contains files, they are displayed side by side with new files, as existing files that can be removed.
11+
The uploader delivers files to a folder that you specify. If that folder already contains files, they are displayed side by side with new files, as existing files that can be removed.
1212

1313
The bundle can automatically scale images to sizes you specify. The provided synchronization methods make it possible to create forms in which attached files respect "save" and "cancel" operations.
1414

1515
Note on Internet Explorer
1616
=========================
1717

18-
Versions of Internet Explorer prior to 10 have no support for multiple file uploads. However IE users will be able to add a single file at a time and will still be able to build a collection of attached files.
18+
Versions of Internet Explorer prior to 10 have no support for multiple file uploads. However IE users will be able to add a single file at a time and will still be able to build a collection of attached files.
1919

2020
Requirements
2121
============
@@ -31,7 +31,7 @@ Installation
3131
Symfony 2.0
3232
-----------
3333
1) Add the following line to your Symfony2 deps file:
34-
34+
3535
[FileUploaderBundle]
3636
git=http://github.com/punkave/symfony2-file-uploader-bundle.git
3737
target=/bundles/PunkAve/FileUploaderBundle
@@ -58,7 +58,7 @@ Symfony 2.2
5858
"branch-alias": {
5959
"dev-master": "2.2-dev"
6060
}
61-
```
61+
```
6262

6363
2) Modify your AppKernel with the following line:
6464

@@ -88,15 +88,15 @@ This code takes creat of creating an editId on the first pass through the form a
8888
(Fetching $posting and validating that the user is allowed to edit that posting is up to you.)
8989

9090
$request = $this->getRequest();
91-
91+
9292
$editId = $this->getRequest()->get('editId');
9393
if (!preg_match('/^\d+$/', $editId))
9494
{
9595
$editId = sprintf('%09d', mt_rand(0, 1999999999));
9696
if ($posting->getId())
9797
{
9898
$this->get('punk_ave.file_uploader')->syncFiles(
99-
array('from_folder' => 'attachments/' . $posting->getId(),
99+
array('from_folder' => 'attachments/' . $posting->getId(),
100100
'to_folder' => 'tmp/attachments/' . $editId,
101101
'create_to_folder' => true));
102102
}
@@ -128,7 +128,7 @@ In Your Layout
128128

129129
To make the necessary JavaScript available via Assetic (note that you must supply jQuery, jQuery UI and Underscore):
130130

131-
{% javascripts
131+
{% javascripts
132132
'@MyBundle/Resources/public/js/jquery-1.7.2.min.js'
133133
'@MyBundle/Resources/public/js/jquery-ui-1.8.22.custom.min.js'
134134
'@MyBundle/Resources/public/js/underscore-min.js'
@@ -158,7 +158,7 @@ Let's assume there is an edit.html.twig template associated with the edit action
158158
{# Hydrated by javascript #}
159159
<div class="file-uploader"></div>
160160

161-
<button class="btn btn-primary" type="submit">{{ isNew ? "Save New Listing" : "Save Changes" }}</button>
161+
<button class="btn btn-primary" type="submit">{{ isNew ? "Save New Listing" : "Save Changes" }}</button>
162162
<a class="btn" href="{{ cancel }}">Cancel</a>
163163
{% if not isNew %}
164164
<a class="btn btn-danger" href="{{ path('delete', { id: posting.id } ) }}">Delete</a>
@@ -171,11 +171,11 @@ Let's assume there is an edit.html.twig template associated with the edit action
171171
// Enable the file uploader
172172

173173
$(function() {
174-
new PunkAveFileUploader({
174+
new PunkAveFileUploader({
175175
'uploadUrl': {{ path('upload', { editId: editId }) | json_encode | raw }},
176176
'viewUrl': {{ '/uploads/tmp/attachments/' ~ editId | json_encode | raw }},
177177
'el': '.file-uploader',
178-
'existingFiles': {{ existingFiles | json_encode | raw }},
178+
'existingFiles': {{ punkave_get_files('tmp/attachments/' ~ editId) | json_encode | raw }},
179179
'delaySubmitWhileUploading': '.edit-form'
180180
});
181181
});
@@ -231,8 +231,8 @@ Again, handleFileUpload DOES NOT RETURN as the response is generated in native P
231231

232232
Setting the allowed file types
233233
------------------------------
234-
You can specify custom file types to divert from the default ones (which are defined in Resources/config/services.yml) by either specifing
235-
them in the handleFileUpload method or parameters.yml.
234+
You can specify custom file types to divert from the default ones (which are defined in Resources/config/services.yml) by either specifing
235+
them in the handleFileUpload method or parameters.yml.
236236

237237
***In the handleFileUpload:***
238238

@@ -280,9 +280,9 @@ Consider installing this shell script as a cron job to be run nightly. This shel
280280
Configuration Parameters
281281
========================
282282

283-
See `Resources/config/services.yml` in this bundle. You can easily decide what the parent folder of uploads will be and what file extensions are accepted, as well as what sizes you'd like image files to be automatically scaled to.
283+
See `Resources/config/services.yml` in this bundle. You can easily decide what the parent folder of uploads will be and what file extensions are accepted, as well as what sizes you'd like image files to be automatically scaled to.
284284

285-
The `from_folder`, `to_folder`, and `folder` options seen above are all appended after `file_uploader.file_base_path` when dealing with files.
285+
The `from_folder`, `to_folder`, and `folder` options seen above are all appended after `file_uploader.file_base_path` when dealing with files.
286286

287287
If `file_uploader.file_base_path` is set as follows (the default):
288288

@@ -305,21 +305,21 @@ So all of these can be readily accessed via the following URLs:
305305

306306
And so on.
307307

308-
The original names and file extensions of the files uploaded are preserved as much as possible without introducing security risks.
308+
The original names and file extensions of the files uploaded are preserved as much as possible without introducing security risks.
309309

310310
Limit number of uploads
311311
-----------------------
312312

313313
You can limit the number of uploaded files by setting the `max_no_of_files` property. You could set this in parameters.yml like this:
314-
314+
315315
parameters:
316316
file_uploader.max_number_of_files: 4
317317

318318
You'll probably want to add an error handler for this case. In the template where you initialize PunkAveFileUploader set `errorCallback`
319319

320320
// Enable the file uploader
321321
$(function() {
322-
new PunkAveFileUploader({
322+
new PunkAveFileUploader({
323323
// ... other required options,
324324

325325
'errorCallback': function(errorObj) {
@@ -333,7 +333,7 @@ You'll probably want to add an error handler for this case. In the template wher
333333
Limitations
334334
===========
335335

336-
This bundle accesses the file system via the `glob()` function. It won't work out of the box with an S3 stream wrapper.
336+
This bundle accesses the file system via the `glob()` function. It won't work out of the box with an S3 stream wrapper.
337337

338338
Syncing files back and forth to follow the editId pattern might not be agreeable if your attachments are very large. In that case, don't use the editId pattern. One alternative is to create objects immediately in the database and not show them in the list view until you mark them live. This way your edit action can use the permanent id of the object as part of the `folder` option, and nothing has to be synced. In this scenario you should probably move the attachments list below the form to hint to the user that there is no such thing as "cancelling" those actions.
339339

Resources/config/services.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ parameters:
33
file_uploader.file_base_path: "%kernel.root_dir%/../web/uploads"
44
file_uploader.web_base_path: "/uploads"
55
file_uploader.allowed_extensions:
6-
# Mime types are just for documentation's sake.
6+
# Mime types are just for documentation's sake.
77
# BlueImp filters by supplied extension. No checking of the
8-
# actual file contents is done. If your mime types are
8+
# actual file contents is done. If your mime types are
99
# configured correctly in Apache the browser will have the
1010
# correct expectations for each file
1111
- gif #image/gif
@@ -27,17 +27,17 @@ parameters:
2727
- dotx #application/vnd.openxmlformats-officedocument.wordprocessingml.template
2828
- txt #text/plain
2929
- rtf #text/rtf
30-
30+
3131
# Folder where originals are uploaded. This is the only folder populated for
3232
# uploads that are not images
3333
file_uploader.originals:
3434
folder: originals
3535

36-
# Scaled versions of images. These image sizes are pretty great for
36+
# Scaled versions of images. These image sizes are pretty great for
3737
# 1140 grid / responsive / bootstrap projects, but you can override as you see fit
3838
#
3939
# Width and height here are maximums to be enforced, NOT an aspect ratio to be enforced.
40-
# UploadHandler renders the smallest size that doesn't violate one of the limits.
40+
# UploadHandler renders the smallest size that doesn't violate one of the limits.
4141
#
4242
# If an original is too small it is simply copied for that particular size. In short,
4343
# BlueImp did a good job here.
@@ -84,3 +84,9 @@ services:
8484
arguments:
8585
# For bc reasons we're not changing the names of the parameters
8686
- file_base_path: '%file_uploader.file_base_path%'
87+
88+
punk_ave.twig.file_extension:
89+
class: PunkAve\FileUploaderBundle\Twig\FileExtension
90+
arguments: [@service_container]
91+
tags:
92+
- { name: twig.extension }

Twig/FileExtension.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PunkAve\FileUploaderBundle\Twig;
4+
5+
use Twig_Extension;
6+
use Twig_Function_Method;
7+
use Symfony\Component\DependencyInjection\Container;
8+
9+
class FileExtension extends Twig_Extension
10+
{
11+
protected $container;
12+
13+
public function __construct(Container $container)
14+
{
15+
$this->container = $container;
16+
}
17+
18+
public function getFunctions()
19+
{
20+
return array(
21+
'punkave_get_files' => new Twig_Function_Method($this, 'getFiles'),
22+
);
23+
}
24+
25+
public function getFiles($folder)
26+
{
27+
return $this->container->get('punk_ave.file_uploader')->getFiles(array('folder' => $folder));
28+
}
29+
30+
public function getName()
31+
{
32+
return 'punkave_file';
33+
}
34+
}

0 commit comments

Comments
 (0)