-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJqueryFileUpload.module
1755 lines (1466 loc) · 64.6 KB
/
JqueryFileUpload.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace ProcessWire;
/**
* This is a ProcessWire module implementation of the awesome jQuery File Upload plugin (https://github.com/blueimp/jQuery-File-Upload).
* In addition to providing (most of) the functionality found in the original plugin, this module comes with a couple of helper modules.
* Planned for the future:
* - create ProcessWire pages based on the uploads
* - transfer uploads to exising file/image fields in you site
*
* @author Francis Otieno (Kongondo)
*
* https:// github.com/kongondo/JqueryFileUpload
* Created 18 January 2016
*
* ProcessWire 3.x
* Copyright (C) 2016 by Ryan Cramer*
*
* All files under /js/ and related stylesheets @Copyright, Sebastian Tschan
* https://blueimp.net
*
* This module is Licensed under the MIT license
* http://www.opensource.org/licenses/MIT
*
*/
class JqueryFileUpload extends WireData implements Module {
/**
* Return information about this module (required).
*
* @access public
* @return array of information.
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Jquery File Upload',
'summary' => 'ProcessWire Wrapper for jQuery File Upload Plugin.',
'author' => 'Francis Otieno (Kongondo)',
'href' => 'https://processwire.com/talk/topic/12050-module-jquery-file-upload/',
'version' => "009",
'singular' => true,
'autoload' => false,
);
}
private $privateUploadsDir; // for private uploads [default]
private $defaultOptions = array(); // for processing jfu ajax requests
private $options = array(); // for final (default merged with custom) options for use in class operations.
private $backendMode; // check if in backend or frontend
private $uploadActionsRender; // actions: buttons/icons/text
private $uploads = array(); // for temporary storage of files from unzipping archives operations
/**
* Set some key properties for use throughout the class.
*
* @access public
*
*/
public function __construct() {
parent::__construct();
$config = $this->wire('config');
// by default, we upload to a non-accessible folder
$this->privateUploadsDir = $config->paths->files . 'jqfu/.files_no_show/';
if (!is_dir($this->privateUploadsDir)) $this->wire('files')->mkdir($this->privateUploadsDir, true);
$thumbsPrivateDir = $this->privateMediaUploadsDir . 'thumbnails/';
$uploadsDir = $config->paths->files . 'jqfu/files/'; // web-accessible directory
$thumbsDir = $config->paths->files . 'jqfu/files/thumbnails/'; // ditto
$uploadsURL = $config->urls->files . 'jqfu/files/'; // ditto
$thumbsURL = $config->urls->files . 'jqfu/files/thumbnails/'; // ditto
/* @note: The defaultOptions below ARE NOT the values we send to jQuery File Upload plugin!
for those, @see $this->configsJFU().
in total, there's 3 types of configurations/options we deal with:
1. Options/configurations that are passed on to the plugin for use on the client-side: @see $this->configsJFU($options)
2. Options/configurations that are passed determine how and which parts of the form to render.
This happens server-side: @see $this->render($options).
3. Options/configurations that are used to process upload/listing/deletion ajax requests sent by the form.
For security, this is all dealt with server-side: @see $this->processJFUAjax($options).
We don't rely on client-side validation. By default, no requests are honoured unless explicitly permitted server-side using this method. Hence, this is a very important method.
Optionally, developers can implement their own ajax-handling methods in their modules/template files.
*/
$this->defaultOptions = array(
// important for $this->processJFUAjax() @note: user configurable
'showUploaded' => 0, // custom option
// custom option. Allows the module to be used as a files lister only (e.g. allow uploads for registered users, otherwise show list only )
// if setting to true, also needs to be passed to $this->render to disable output of form widget
'disableUploads' => false,
'paramName' => 'files',
'acceptFileTypes' => 'gif jpeg jpg png mp3 pdf mp4 pdf',
'responseType' => 1, // if 1 we return [echo] a JSON response if 2, we return an array(useful) if further processing, e.g. in a module, is necessary before returning final response
'setMaxFiles' => 30, // WireUpload
'setOverwrite' => false, // WireUpload
# $this->render() @note: user configurable
'useCustomForm' => 0, // whether to use a custom form + action buttons for uploads + upload actions
'uploadsDeletable' => 0, // custom option: show buttons and enable deletion of uploaded files. Default=false
'enableDropZone' => 1, // render a drop zone where files could be uploaded via 'dropping'
'showGallery' => 1, // render blueimp gallery
'filesContainer' => 'files',
# thumbnails
// $this->createThumbnails()
/* 'thumbsWidth' => 100, */
'thumbsWidth' => 0, // @note: proportional width
'thumbsHeight' => 260,
'createThumb' => false, // whether to create image thumbnails for uploads
# uploads (read+write)
'privateUploadsDir' => $this->privateUploadsDir, // @note: non-web-accessible
'thumbsPrivateDir' => $thumbsPrivateDir, // -ditto-
'uploadsDir' => $uploadsDir, // @note: web-accessible
'thumbsDir' => $thumbsDir, // @note: web-accessible
'uploadsURL' => $uploadsURL, // @note: web accissible
'thumbsURL' => $thumbsURL, // @note: web-accessible
# image files validation @note: user configurable via $this->processJFUAjax()
'allowedImageMimeTypes' => array('image/gif', 'image/jpeg', 'image/png'),
'commonImageExts' => array('jpg', 'jpeg', 'gif', 'png', 'svg'),
'imageTypeConstants' => array('gif' => IMAGETYPE_GIF, 'jpeg' => IMAGETYPE_JPEG, 'jpg' => IMAGETYPE_JPEG, 'png' => IMAGETYPE_PNG),
# other files validation (pdf, mp3, mp4)
'allowedNonImageMimeTypes' => array('application/pdf', 'audio/mpeg', 'video/mp4'),
# custom settings
// what to render for upload actions: start/cancel/delete (1=buttons;2=icons,3=text)
// @note/@todo: not currently using but may revisit if there's demand. In that case, will add the combined button as an option as well? In that case we'll also have to revisit the JS in other places
'uploadActionsRender' => 1,
/*
- renders custom markup set by dev
- this can be used for custom actions, custom action buttons, etc.
- @note: JFU only provides the markup. Devs have to do their own form processing implementation
*/
'customJFUActionMarkup' => '',
// whether to decompress zip files after upload. contents will then be listable if listing
// @note: zip file is deleted after decompression
// @note: we only allow unzip if in the backend!
'unzipFiles' => false,
// the target HTML element we'll be calling the fileupload method on (@note: we'll use as selector since we need jQuery collection)
'targetElement' => '#fileupload',
// whether to move unzipped files in sub-folders to the main root uploads directory + delete the empty sub-folders
// @note: currently, mainly for internal use @TOD0!!
'moveToRoot' => true,
// for rendering string of allowed file type extensions for use in $this->renderAllowedFileExtensions()
// @note: this is probably verbose since we can always build from "acceptFileTypes"
'renderAllowedFileExtensions' => ''
);
// if option "renderAllowedFileExtensions" is empty, we fall back to "acceptFileTypes"
if (!$this->defaultOptions['renderAllowedFileExtensions']) {
$this->defaultOptions['renderAllowedFileExtensions'] = str_replace(" ", ", ", $this->defaultOptions['acceptFileTypes']);
}
$this->backendMode = $this->wire('page')->template == 'admin' ? true : false;
$this->extraCSSClass = $this->adminTheme == 'AdminThemeReno' ? 'Reno' : '';
}
/**
* Initialise the module. This is an optional initialisation method called before any execute methods.
*
* Initialises various class properties ready for use throughout the class.
*
* @access public
*
*/
public function init() {
}
/* ######################### - CONFIG - ######################### */
/**
* Add all needed scripts using $config->scripts->add().
*
* @access public
* @param array $options Options to disable outputting certain scripts if some features not in use.
* @see: https://github.com/blueimp/jQuery-File-Upload/wiki/Plugin-files.
*
*/
public function configJFUScripts($options = array()) {
// scripts needed by jQuery File Upload
$scripts = array(
// 1. iframe transport
// adds iframe transport support to jQuery.ajax()
'iframetransport|jquery.iframe-transport.min.js',
// 2. canvas to blob
// Canvas to Blob plugin is included for image resizing functionality
'canvastoblob|canvas-to-blob.min.js',
// 3. load image
// plugin included for the preview images and image resizing functionality
// ...also audio+video previews
'loadimage|load-image.all.min.js',
// 4. fileupload core
// the basic plugin: enhances file upload process, without making assumptions about user interface or content-type of response
'fileupload|jquery.fileupload.min.js',
// extends the basic version of the fileupload plugin and adds file processing functionality
'fileupload|jquery.fileupload-process.min.js',
// extends the file processing plugin and adds image preview & resize functionality
'fileupload|jquery.fileupload-image.min.js',
// extends the file processing plugin and adds audio preview functionality
'fileupload|jquery.fileupload-audio.min.js',
// extends the file processing plugin and adds video preview functionality
'fileupload|jquery.fileupload-video.min.js',
// extends the file processing plugin and adds file validation functionality
'fileupload|jquery.fileupload-validate.min.js',
// extends the file processing plugin and adds a complete user interface
'fileupload|jquery.fileupload-ui.min.js',
// extends the UI version of the fileupload plugin to use it with jQuery UI
//'fileupload|jquery.fileupload-jquery-ui.min.js',
// 5. Cross-origin Resource Sharing (CORS) @todo?
// adds XDomainRequest transport support to jQuery.ajax()
'cors|jquery.xdr-transport.min.js',
// adds postMessage transport support to jQuery.ajax()
'cors|jquery.postmessage-transport.min.js',
// 6. gallery
'gallery|jquery.blueimp-gallery.min.js',
);
$config = $this->wire('config');
$url = $config->urls->$this;
foreach ($scripts as $script) {
$src = explode('|', $script);
$folder = $src[0];
$s = $src[1];
if (in_array('noGallery', $options) && $folder == 'gallery') continue;
if (in_array('noIframeTransport', $options) && $folder == 'iframetransport') continue;
if (in_array('noResize', $options) && $folder == 'canvastoblob') continue;
if (in_array('noAudioPreview', $options) && $folder == 'fileupload' && strpos($s, 'audio') !== false) continue;
if (in_array('noVideoPreview', $options) && $folder == 'fileupload' && strpos($s, 'video') !== false) continue;
$config->scripts->add($url . "js/" . $folder . "/" . $s);
}
// also add our main js file unless custom script being used
if (!in_array('useCustomScript', $options)) $config->scripts->add($url . "js/jqueryfileupload.js");
}
/**
* Add all needed styles using $config->styles->add().
*
* @access public
* @param array $options Contains option to not output gallery CSS if gallery disabled.
*
*/
public function configJFUStyles($options = array()) {
$config = $this->wire('config');
$url = $config->urls->$this;
if (!in_array('useCustomStyle', $options)) $config->styles->add($url . 'css/jqueryfileupload.css');
if (!in_array('noGallery', $options)) $config->styles->add($url . 'css/blueimp-gallery.min.css');
}
/**
* Outputs javascript configuration settings for this module.
*
* This will need to be output before the page is rendered.
* See documentation for an example.
* @note: We can pass any options we need (if different from the defaults) through $options.
* @note: Different output depending on if in the front or backend.
*
* @access public
* @param array $options Options to override jQuery File Upload's plugin defaults.
* @return string $json Added to $config->js() array. We return JSON if in the frontend but in backend populate $config->js().
*
*/
public function configsJFU($options = null) {
$defaultJFUOptions = array(
"url" => "./", // by default we post to same page that sent the request
'acceptFileTypes' => 'gif jpeg jpg png mp3 pdf mp4', // @todo: why not just get from $this->defaultOptions()?
'showUploaded' => 0, // by default, don't show files that have already been uploaded
'filesContainer' => '#files',
'dropZone' => '.jfu_dropzone',
'uploadTemplateId' => 'null',
'downloadTemplateId' => 'null',
'maxFileSize' => 2000000000, // 2GB
'loadImageMaxFileSize' => 100000000, // 100MB: The maximum file size of images to load
'loadVideoMaxFileSize' => 1000000000, // 1GB: The maximum file size of video files to load
'previewMinWidth' => 0,
'previewMinHeight' => 260,
'previewMaxWidth' => 0,
'previewMaxHeight' => 260,
'previewCrop' => true, // Define if preview images should be cropped or only scaled
// messages
# @note: undocumented in JFU but works. we need these for customisation and translation
'messages' => array(
'maxNumberOfFiles' => $this->_("Maximum number of files exceeded"),
'acceptFileTypes' => $this->_("File type not allowed"),
'maxFileSize' => $this->_("File is too large"),
'minFileSize' => $this->_("File is too small")
),
// own/custom config @todo: still using?
'useCustomJFUActionMarkup' => false,
// the target HTML element we'll be calling the fileupload method on (@note: we'll use as selector since we need jQuery collection)
'targetElement' => '#fileupload',
);
// merge user options with default jfu configuration options
if ($options != null && is_array($options)) $options = array_merge($defaultJFUOptions, $options);
else $options = $defaultJFUOptions;
// reformat accepted file types string
$acceptFileTypes = explode(' ', $options['acceptFileTypes']); // convert into array
$options['acceptFileTypes'] = implode('|', $acceptFileTypes); // convert into pipe-separated string, i.e. 'gif|jpeg|jpg|....'
// if in the frontend, we return JSON ready for use in js
if ($this->wire('page')->template != 'admin') {
$jfuOptions = array();
$jfuOptions['JqueryFileUpload'] = $options;
$json = json_encode($jfuOptions);
return $json;
}
// else if in ProcessWire admin, we populate config->js()
else {
$jfuConfig = $this->wire('config')->js($this->className(), $options); // populate global config->js() with our jfu configs
}
}
/* ######################### - RENDER - ######################### */
/**
* Add markup for all needed scripts.
*
* This is an alternative to using configJFUScripts.
* Here we use <script></script>
*
* @access public
* @param array $options Options to disable outputting certain scripts if some features not in use.
* @see: https://github.com/blueimp/jQuery-File-Upload/wiki/Plugin-files.
* @return string $out Markup to add to <head> or just before </body>.
*
*/
public function renderJFUScripts($options = array()) {
$scripts = array(
// 1. iframe transport
'iframetransport|jquery.iframe-transport.min.js', // adds iframe transport support to jQuery.ajax()
// 2. canvas to blob
'canvastoblob|canvas-to-blob.min.js', // Canvas to Blob plugin is included for image resizing functionality
// 3. load image
'loadimage|load-image.all.min.js', // plugin included for the preview images and image resizing functionality (also audio+video previews)
// 4. fileupload core
'fileupload|jquery.fileupload.min.js', // the basic plugin: enhances file upload process, without making assumptions about user interface or content-type of response
'fileupload|jquery.fileupload-process.min.js', // extends the basic version of the fileupload plugin and adds file processing functionality
'fileupload|jquery.fileupload-image.min.js', // extends the file processing plugin and adds image preview & resize functionality
'fileupload|jquery.fileupload-audio.min.js', // extends the file processing plugin and adds audio preview functionality
'fileupload|jquery.fileupload-video.min.js', // extends the file processing plugin and adds video preview functionality
'fileupload|jquery.fileupload-validate.min.js', // extends the file processing plugin and adds file validation functionality
'fileupload|jquery.fileupload-ui.min.js', // extends the file processing plugin and adds a complete user interface
//'fileupload|jquery.fileupload-jquery-ui.min.js',// extends the UI version of the fileupload plugin to use it with jQuery UI
// 5. Cross-origin Resource Sharing (CORS) @todo!!
'cors|jquery.xdr-transport.min.js', // adds XDomainRequest transport support to jQuery.ajax()
'cors|jquery.postmessage-transport.min.js', // adds postMessage transport support to jQuery.ajax()
// 6. gallery
'gallery|jquery.blueimp-gallery.min.js',
);
$url = $this->wire('config')->urls->$this;
$out = '';
foreach ($scripts as $script) {
$src = explode('|', $script);
$folder = $src[0];
$s = $src[1];
if (in_array('noGallery', $options) && $folder == 'gallery') continue;
if (in_array('noIframeTransport', $options) && $folder == 'iframetransport') continue;
if (in_array('noResize', $options) && $folder == 'canvastoblob') continue;
if (in_array('noAudioPreview', $options) && $folder == 'fileupload' && strpos($s, 'audio') !== false) continue;
if (in_array('noVideoPreview', $options) && $folder == 'fileupload' && strpos($s, 'video') !== false) continue;
// @todo?:
/*<!-- The XDomainRequest Transport is included for cross-domain file deletion for IE 8 and IE 9 -->
<!--[if (gte IE 8)&(lt IE 10)]>
<script src="js/cors/jquery.xdr-transport.js"></script>
<![endif]-->*/
$out .= "\n\t<script type='text/javascript' src='" . $url . "js/" . $folder . "/" . $s . "'></script>";
}
// also add our main js file unless custom script being used
if (!in_array('useCustomScript', $options)) $out .= "\n\t<script type='text/javascript' src='" . $url . "js/jqueryfileupload.js'></script>";
return $out;
}
/**
* Add markup for all needed styles.
*
* This is an alternative to using configJFUStyles.
* Here we use <link></link>.
*
* @access public
* @param array $options Contains option to not output gallery CSS if gallery disabled.
* @return string $out Markup to add to <head>.
*
*/
public function renderJFUStyles($options = array()) {
$url = $this->wire('config')->urls->$this;
$out = '';
if (!in_array('useCustomStyle', $options)) $out .= "\n\t\t<link href='" . $url . "css/jqueryfileupload.css' type='text/css' rel='stylesheet'>\n";
if (!in_array('noGallery', $options)) $out .= "\n\t\t<link href='" . $url . "css/blueimp-gallery.min.css' type='text/css' rel='stylesheet'>\n";
return $out;
}
/**
* Builds and renders the uploads widget.
*
* Outputs the uploads form, buttons/input for the actions 'add files, start upload, cancel button and delete' and uploads table list.
* @note: Developers can add extra checks in their code to control the display of this form widget, e.g. allow only registered users to see and upload.
* @see also $this->processJFUAjax() that controls access server-side.
*
* @access public
* @param array $options Options to control rendering of uploads widget (e.g. whether to show download files or not, etc).
* @return strung $out Markup of upload widget and table list.
*
*/
public function render($options = null) {
$this->processOptions($options);
$options = $this->options;
$this->uploadActionsRender = $options['uploadActionsRender'];
// CSRF
$session = $this->wire('session');
$tokenName = $session->CSRF->getTokenName();
$tokenValue = $session->CSRF->getTokenValue();
$token = "\n\t\t<input type='hidden' id='_post_token' name='" . $tokenName . "' value='" . $tokenValue . "'>";
$out = '';
// if implemented in a third-party module, let developer wrap below output in their own ProcessWire form
// if using this module's form for uploads. @note: developers can use the module but using their own custom forms for uploads
if ((int) $options['useCustomForm'] === 0) {
// whether to output delete button.
// @note: this is only about displaying the button or not.
// In $this->processJFUAjax() we also check, server-side, if delete is allowed. The default is NOT ALLOWED.
// The setting can only be made via the API, server-side AND NOT client-side
$allowDelete = (int) $options["uploadsDeletable"];
// the file upload form used as target for the file upload widget
$out .= "\n\t<div class='jfu_files_container'>";
$disableUploads = $options['disableUploads'];
$targetElement = $options['targetElement'];
// @todo: for our purposes, we currently remove the leading "#". We also assume the targetElement will be an ID!
$targetElement = preg_replace('/#/', '', $targetElement, 1);
// if uploads disabled, we only output the table listing of uploads (if show uploads also enabled)
// @note: if no form output with id#fileupload, need this to trigger jquery file upload plugin
if ($disableUploads == true) $out .= "\n\t\t<input id='{$targetElement}' type='hidden'>";
else {
// if NOT in a module in ProcessWire admin, we'll use our form
if (!$this->backendMode) $out .= "\n\t\t<form id='{$targetElement}' action='./' method='POST' enctype='multipart/form-data'>";
#$out .= "<noscript><input type='hidden' name='redirect' value='./'></noscript>;// @todo: unsure?
$out .= $token;
$out .= "\n\t\t\t<div uk-grid class='row fileupload-buttonbar'>"; // buttons bar wrapper
$extraCSSClass = $this->extraCSSClass;
$extraClass = $extraCSSClass ? ' action-buttons-wrapper-' . $extraCSSClass : '';
$out .= "\n\t\t\t\t<div class='uk-width-1-1 jfu_actions InputfieldHeader action-buttons-wrapper uk-" . $extraClass . "'>\n"; // buttons wrapper
$out .= "\n\t\t\t\t" . $this->renderActionMarkup();
// the global file processing state
$out .= $this->renderFileProcessingState();
$out .= "\n\t\t\t\t</div>"; // end action-buttons-wrapper
// the global progress state
$out .= $this->renderGlobalProgressState();
$out .= "\n\t\t\t</div>"; // end div.fileupload-buttonbar
// drop zone
$out .= $this->renderUploadsArea();
}
$showGallery = (int) $options["showGallery"];
// the table listing the files available for upload/download
$filesContainer = $options['filesContainer'];
$out .= '<div uk-grid>' . $this->renderUploadDownloadList() . '</div>';
if (!$this->backendMode || !$disableUploads) $out .= "\n\t\t</form>";
// @todo: we also need to control delete uploads here?
$out .= $this->renderMarkupTemplates();
$out .= "\n\t</div><!-- end div.jfu_files_container -->"; // end div.container
// if photo gallery enabled (@see: downloadTemplate property in jqueryfileupload.js)
if ($showGallery === 1) $out .= $this->renderGalleryWidget();
}
return $out;
}
/**
* Renders the upload buttons/input for the actions 'add files, start upload, cancel button and delete'.
*
* For flexiblity, buttons/input can be rendered individually (see related methods).
*
* @access private
* @return string $out Markup of action buttons.
*
*/
private function renderActionMarkup() {
$out = '';
$options = $this->options;
$allowDelete = (int) $options["uploadsDeletable"];
// check if using custom action markup or using JFU inbuilt
$customActionMarkup = $options['customJFUActionMarkup'];
if ($customActionMarkup) $out .= $customActionMarkup;
else {
// start all uploads button
$out = "\n\t\t\t\t" . $this->renderStartUploads();
// cancel all uploads button
$out .= "\n\t\t\t\t" . $this->renderCancelUploads();
// delete selected uploads button
if ((int) $allowDelete === 1) $out .= "\n\t\t\t\t" . $this->renderDeleteUploads();
}
return $out;
}
/**
* Render the 'Choose files to upload' area.
*
* It is designed to resemble ProcessWire image uploads markup.
*
* @access private
* @return string $out Markup of the choose files area.
*
*/
private function renderChooseFiles() {
$options = $this->options;
$paramName = $this->wire('sanitizer')->text($options['paramName']);
if (!$paramName) $paramName = 'files';
$out =
"<div class='InputfieldImageUpload jfu_choose_files uk-width-1-2'>" .
// @todo: get the css for this from Fieldtypeimage and use instead of depending on InputMask?
"<div class='InputMask ui-button ui-state-default'>" .
"<span class='ui-button-text jfu_choose_files'><i class='fa fa-fw fa-folder-open-o'></i>" .
$this->_("Choose Files") .
"</span>" .
"\n\t\t\t\t<input type='file' name='{$paramName}[]' multiple='multiple'>" .
"</div>" . // end .InputMask
"</div>"; // .InputfieldImageUpload
return $out;
}
/**
* Renders the start all uploads button.
*
* @access private
* @return string $out Markup of start uploads action button.
*
*/
private function renderStartUploads() {
// @todo: fontawesome fallback here?
$actionsRenderModes = array(1 => ' class="uk-button uk-button-default"', 2 => ' uk-icon="cloud-upload"', 3 => '');
$actionType = $actionsRenderModes[$this->uploadActionsRender];
$startTitle = $this->_('Start');
$startText = '';
if ($this->uploadActionsRender != 2) $startText = $startTitle;
// if using buttons, remove title
if ($this->uploadActionsRender == 1) $startTitle = '';
$out =
"<span class='jfu_start start uk-margin-small-right'>" .
"<a class='ui-button ui-state-default jfu_action' href='#' title='{$startTitle}'{$actionType}>{$startText}</a>" .
"</span>";
return $out;
}
/**
* Renders the cancel all uploads button.
*
* @access private
* @return string $out Markup of cancel uploads action button.
*
*/
private function renderCancelUploads() {
// @todo: fontawesome fallback here?
$actionsRenderModes = array(1 => ' class="uk-button uk-button-default"', 2 => ' uk-icon="ban"', 3 => '');
$actionType = $actionsRenderModes[$this->uploadActionsRender];
$cancelTitle = $this->_('Cancel');
$cancelText = '';
if ($this->uploadActionsRender != 2) $cancelText = $cancelTitle;
// if using buttons, remove title
if ($this->uploadActionsRender == 1) $cancelTitle = '';
$out =
"<span class='jfu_cancel cancel uk-margin-small-right'>" .
"<a class='jfu_cancel_upload jfu_action ui-button ui-state-default ui-priority-secondary' href='#' title='{$cancelTitle}'{$actionType}>{$cancelText}</a>" .
"</span>";
return $out;
}
/**
* Renders the delete all uploads button unless custom action markup is in use.
*
* If custom markup is in use, that will be used instead.
*
* @access private
* @return string $out Markup of delete uploads action button.
*
*/
private function renderDeleteUploads() {
$out = '';
// @todo: fontawesome fallback here?
$actionsRenderModes = array(1 => ' class="uk-button uk-button-default"', 2 => ' uk-icon="trash"', 3 => '');
$actionType = $actionsRenderModes[$this->uploadActionsRender];
$deleteTitle = $this->_('Delete');
$deleteText = '';
if ($this->uploadActionsRender != 2) $deleteText = $deleteTitle;
$deleteTitleMarkup = $this->uploadActionsRender == 2 ? " title='{$deleteTitle}'" : '';
$options = $this->options;
// check if using custom action markup or using JFU inbuilt
// if custom, we don't show delete button; just the checkbox
$customActionMarkup = $options['customJFUActionMarkup'];
if (!$customActionMarkup) {
$out .=
"<span class='jfu_delete delete uk-margin-small-right'>" .
"<a class='jfu_delete_upload jfu_action' href='#'{$deleteTitleMarkup}{$actionType}>{$deleteText}</a>" .
"</span>";
}
// select uploads checkboxes
$out .= "<input type='checkbox' class='uploaded_file jfu_check toggle uk-checkbox uk-form-controls-text'>";
return $out;
}
/**
* Render markup for The global progress state.
*
* @code: taken from the blueimp jquery file upload demo.
*
* @access private
* @return string $out Markup for global progress state.
*
*/
private function renderGlobalProgressState() {
// the global progress state
$out =
"\n\t<div class='jfu_actions uk-width-1-1 fileupload-progress fade'>" .
// the global progress bar
"\n\t\t<div class='progress progress-striped active' role='progressbar' aria-valuemin='0' aria-valuemax='100'>" .
"\n\t\t\t<div class='progress-bar progress-bar-success' style='width:0%;'></div>" .
"\n\t\t</div>" .
// the extended global progress state
"\n\t\t<div class='progress-extended'> </div>" .
"\n\t</div>";
return $out;
}
/**
* Render markup for the table listing for uploads/donwloads.
*
* @code: taken from the blueimp jquery file upload demo.
*
* @access private
* @return string $out Markup for table listing for files ready for upload/download.
*
*/
private function renderUploadDownloadList() {
$options = $this->options;
$allowDelete = (int) $options["uploadsDeletable"];
$showGallery = (int) $options["showGallery"];
$filesContainer = $options['filesContainer'];
$out = '';
$label = $this->_('Uploaded Files');
$notes = $this->_('Files to action.');
// deleting uploads allowed. We need the checkbox
if ($allowDelete) {
$selectColumn =
"<th class='uk-width-medium jfu_check_action'>" .
"<input type='checkbox' class='toggle_all jfu_check uk-checkbox uk-form-controls-text' title='Select All'>" .
"</th>";
}
// deleting not allowed, no checkbox needed
else {
$selectColumn =
"<th class='uk-width-medium jfu_check_action'>" . $this->_('Action') . "</th>";
}
$out .=
"\n\t<div class='jfu-uploaded-files-list uk-overflow-auto uk-width-1-1'>" .
"<h3 class='InputfieldHeader uk-form-label'>{$label}</h3>" .
"<p class='jfu-uploaded-files-note notes'>{$notes}</p>" .
"<table id='jfu-files-list' role='presentation' class='files_list uk-table uk-table-divider uk-table-hover uk-table-justify uk-table uk-table-middle uk-table-responsive' data-deletable='{$allowDelete}' data-show-gallery='{$showGallery}'>" .
"<thead>" .
"<tr>" .
"<th class='uk-width-1-4 jfu_file_preview'>" . $this->_('Preview') . "</th>" .
"<th class='uk-table-expand jfu_file_name'>" . $this->_('Name') . "</th>" .
"<th class='uk-width-small jfu_file_size'>" . $this->_('Size') . "</th>" .
$selectColumn .
"</tr>" .
"</thead>" .
"\n\t\t<tbody id='" . $filesContainer . "'></tbody>\n" .
"\t</table></div>\n";
return $out;
}
/**
* Render markup for the global file processing state.
*
* @code: taken from the blueimp jquery file upload demo.
*
* @access private
* @return string $out Markup for file processing state.
*
*/
private function renderFileProcessingState() {
// the global file processing state
$out = "\n\t<span class='fileupload-process'></span>\n";
return $out;
}
/**
* Render icons for allowed file types.
*
* @note: not currently in use. Option being considered for future versions.
*
* @access private
* @return string $out Markup of icons.
*
*/
private function renderAllowedMediaIcons() {
$out =
"<span class='uk-margin-small-right' uk-icon='play' title='Audio'></span>" .
"<span class='uk-margin-small-right' uk-icon='file' title='Document'></span>" .
"<span class='uk-margin-small-right' uk-icon='image' title='Image'></span>" .
"<span class='uk-margin-small-right' uk-icon='video-camera' title='Video'></span>";
return $out;
}
/**
* Render string of allowed file type extensions.
*
* @access private
* @return string $out Markup of allowed file extensions.
*
*/
private function renderAllowedFileExtensions() {
$out =
'<div class="Allowed extensions uk-width-1-1">' .
'<span class="detail">' . $this->options['renderAllowedFileExtensions'] . '</span>' .
'</div>';
return $out;
}
/**
* Render fontawesome icons for allowed file types.
*
* @note: not currently in use. Option being considered for future versions.
*
* @access private
* @return string $out Markup of fontawesome icons.
*
*/
private function renderAllowedMediaIconsFA() {
$out =
"<i class='uk-margin-small-right fa fa-headphones' aria-hidden='true'></i>" .
"<i class='uk-margin-small-right fa fa-file-text-o' aria-hidden='true'></i>" .
"<i class='uk-margin-small-right fa fa-picture-o' aria-hidden='true'></i>" .
"<i class='uk-margin-small-right fa fa-video-camera' aria-hidden='true'></i>";
return $out;
}
/**
* Render the uploads area for choosing files and the dropzone.
*
* @access private
* @return string $out Markup of the uploads area.
*
*/
private function renderUploadsArea() {
$dropZoneClass = (int) $this->options["enableDropZone"] === 1 ? ' jfu_dropzone' : '';
$out =
"<div id='jfu_drop_area_wrapper'>" .
"<h3 class='InputfieldHeader uk-form-label'>Select Files</h3>" .
"<div class='drop-files-container{$dropZoneClass}' uk-grid>" .
// allowed extensions (@note: uk-width-1-1)
$this->renderAllowedFileExtensions() .
// masked file input (@note: uk-width-1-2)
$this->renderChooseFiles() .
// drop zone (@note: uk-width-1-2)
$this->renderDropZone() .
"</div>" .
"</div>";
return $out;
}
/**
* Render the JFU dropzone markup.
*
* @access private
* @return string $out Markup of the dropzone area.
*
*/
private function renderDropZone() {
$options = $this->options;
$out = '';
if ((int) $options["enableDropZone"] === 1) {
$out =
"<div class='jfu-drop-zone-text uk-position-center-right uk-text-right uk-width-1-2'>" .
"\n\t\t\t<span class='description' style='display: inline;'>" .
"\n\t\t\t\t<span><i class='fa fa-cloud-upload'></i> " . $this->_('drag and drop files here') . "</span>" .
"</span>" .
"</div>";
}
return $out;
}
/**
* Render markup of the JFU Upload Anywhere widget.
*
* @access private
* @return string $out Markup of upload anywhere widget.
*
*/
public function renderUploadAnywhereWidget() {
$spinner = '<span class="mm_upload_anywhere_spinner"><i class="fa fa-lg fa-spin fa-spinner"></i></span>';
$uploadPercentCounter = '<span class="jfu_upload_anywhere_percent_counter"></span>';
$uploadHeadline =
'<span class="jfu_upload_anywhere_uploading">' . $this->_('Uploading') .
' <span class="jfu_upload_anywhere_count">0</span> ' .
$this->_('media') . '</span>';
$uploadCompleteHeadline =
'<span class="jfu_upload_anywhere_complete">' .
$this->_('Upload complete') .
'</span>';
// show just in case upload complete but processing still going on server-sie
$processingHeadline =
'<span class="jfu_upload_anywhere_processing">' .
$this->_('Processing') . $spinner .
'</span>';
$out =
'<div class="jfu_upload_anywhere_widget ui-dialog">' .
'<div class="jfu_upload_anywhere_widget_header ui-dialog-titlebar">' .
'<p class="clearfix">' .
$uploadPercentCounter . $uploadHeadline . $uploadCompleteHeadline . $processingHeadline .
'<a class="jfu_upload_anywhere_cancel_all" href="#" >' . $this->_('Cancel') . '</a>' .
'</p>' .
'</div>' .
'<div id="jfu_upload_anywhere_widget_info">' .
'<ul class="jfu_upload_anywhere_widget_info_list" data-uploads-count="0"></ul>' .
'</div>' .
'</div>';
return $out;
}
/**
* Render markup for the blueimp Gallery Widget.
*
* @code: taken from the blueimp jquery file upload demo.
*
* @access public
* @param bool $filterDuplicates Whether to filter for duplicates
* @param string $id HTML ID for the gallery.
* @return string $out Markup of action buttons.
*
*/
public function renderGalleryWidget($filterDuplicates = true, $id = '') {
$galleryID = $id ? $id : 'blueimp-gallery';
// the blueimp Gallery Widget
// @note: 'The jQuery plugin also introduces the additional filter option, which is applied to the Gallery links via jQuery's filter method and allows to remove duplicates from the list:'
// @see: https://github.com/blueimp/Gallery#html5-data-attributes
$dataFilter = $filterDuplicates ? " data-filter=':even'" : '';
$out =
"\n\t<div id='{$galleryID}' class='blueimp-gallery blueimp-gallery-controls'$dataFilter>" . // ensure duplicates removed where necessary
"\n\t\t<div class='slides'></div>" .
"\n\t\t<h3 class='title'></h3>" .
"\n\t\t<p class='caption'></p>" . // placeholder for description label
"\n\t\t<a class='prev'>‹</a>" .
"\n\t\t<a class='next'>›</a>" .
"\n\t\t<a class='close'>×</a>" .
"\n\t\t<a class='play-pause'></a>" .
"\n\t\t<ol class='indicator'></ol>" .
"\n\t</div><!-- end div.blueimp-gallery -->";
return $out;
}
/* ######################### - TEMPLATE - ######################### */
/**
* Render template markup for the table listing for uploads/donwloads.
*
* @code: taken from the blueimp jquery file upload demo.
*
* @access private
* @return string $out Markup for table listing for files ready for upload/download.
*
*/
private function renderMarkupTemplates() {
$start = $this->_('Start upload');
$delete = $this->_('Delete');
$finished = $this->_('Upload finished');
$cancel = $this->_('Cancel');
$failed = $this->_('Upload failed');
$invalidFileType = $this->_('File type not allowed');
$serverError = $this->_('Server error.');
$out =
"\n\t\t<div id='jfu_markup_templates' class='jfu_hide'>" .
# start icon
"\n\t\t\t<div class='jfu_uploads_start jfu_single_action' title='{$start}'>" .
"\n\t\t\t\t" . $this->renderStartUploads() .
"\n\t\t\t</div>" .
# cancel icon
"\n\t\t\t<div class='jfu_uploads_cancel jfu_single_action' title='{$cancel}'>" .
"\n\t\t\t\t" . $this->renderCancelUploads() .
"\n\t\t\t</div>" .
# delete
"\n\t\t\t<div class='jfu_uploads_delete jfu_single_action jfu_hide' title='{$delete}'>" .
"\n\t\t\t\t" . $this->renderDeleteUploads() .
"\n\t\t\t</div>" .
# done/delete @todo: do we still need this?
"\n\t\t\t<div class='jfu_uploads_done jfu_single_action' title='{$delete}'>" .
"\n\t\t\t\t<i class='fa fa-check-circle jfu_action' aria-hidden='true'></i>" .
"\n\t\t\t\t<i class='fa fa-times-circle jfu_delete jfu_action' aria-hidden='true'></i>" .
"\n\t\t\t\t<input class='jfu_action_delete toggle jfu_hide' value='' name='delete' type='checkbox'>" .
"\n\t\t\t</div>" .
# fail