44 const DOWNLOAD_TEMPLATE_ID =
'template-download';
46 const UPLOAD_TEMPLATE_ID =
'template-upload';
48 public $scriptFile = array(
'jquery.fileupload.js',
49 'jquery.fileupload-ui.js',
'jquery.tmpl.min.js',
'jquery.iframe-transport.js');
51 public $cssFile = null;
53 public $assetFolderName =
'fileUpload';
71 public $allowMultipleUpload =
false;
77 public $renderFileDownloadLinks =
false;
116 public $hiddenInputName;
122 public $existingFiles;
134 public $beforeUploadAction;
140 public $afterDeleteAction;
146 public $onSuccessAction;
152 public $showMaxSize =
true;
162 assert(
'is_string($this->uploadUrl) && $this->uploadUrl != ""');
163 assert(
'is_string($this->deleteUrl) && $this->deleteUrl != ""');
164 assert(
'is_string($this->inputId) && $this->inputId != ""');
165 assert(
'is_string($this->inputName) && $this->inputName != ""');
166 assert(
'is_string($this->hiddenInputName) && $this->hiddenInputName != ""');
167 assert(
'is_array($this->existingFiles)');
168 Yii::app()->getClientScript()->registerCoreScript(
'jquery.ui');
172 public function run()
174 $id = $this->getId();
175 $jsonEncodedExistingFiles = CJSON::encode($this->existingFiles);
177 if ($this->allowMultipleUpload)
180 $addLabel = $this->getAddLabel();
184 $sendAction =
"\$('#{$this->formName}').find('.files > tbody').children().remove();";
185 $addLabel = $this->getAddLabel();
193 $(
'#fileUpload{$id}').fileupload({
195 url:
'{$this->uploadUrl}',
197 sequentialUploads:
true,
198 maxFileSize: {$this->maxSize},
199 dropZone: $(
'#dropzone{$id}'),
200 uploadTemplateId:
'{$this->getUploadTemplateId()}',
201 downloadTemplateId:
'{$this->getDownloadTemplateId()}',
202 {$this->renderParamForInit(
'formData')}
203 add:
function (e, data) {
204 {$this->beforeUploadAction}
206 var that = $(
this).data(
'blueimpUI-fileupload') || $(
this).data(
'fileupload');
207 that._adjustMaxNumberOfFiles(-data.files.length);
208 data.isAdjusted =
true;
209 data.isValidated = that._validate(data.files);
210 data.context = that._renderUpload(data.files)
211 .appendTo($(
this).find(
'.files')).fadeIn(
function () {
214 $(
this).makeOrRemoveTogglableSpinner(
true,
'#'+
'fileUpload{$id}');
215 }).data(
'data', data);
216 if ((that.options.autoUpload || data.autoUpload) &&
218 data.jqXHR = data.submit();
224 $(
'#fileUpload{$id} .files a:not([target^=_blank])').live(
'click',
function (e) {
226 $(
'<iframe style="display:none;"></iframe>')
227 .prop(
'src', this.href)
230 $(
'.fileupload-buttonbar').removeClass(
'ui-widget-header ui-corner-top');
231 $(
'.fileupload-content').removeClass(
'ui-widget-content ui-corner-bottom');
232 $(
'#fileUpload{$id}').bind(
'fileuploaddestroy',
function (e, data) {
233 {$this->afterDeleteAction}
236 $(
'#fileUpload{$id}').bind(
'fileuploadalways',
function (e, data) {
237 if (data == undefined || data.result == undefined ||
238 ((data.result[0] != undefined && data.result[0].error != undefined) || data.result.error != undefined))
240 setTimeout(
function () {
241 $(
'#{$this->formName}').find(
'.files > tbody').children(
':last').fadeOut(
'slow',
function() { $(
this).
remove();});
242 {$this->afterDeleteAction}
246 $(
'#fileUpload{$id}').bind(
'done',
function (e, data) { {$this->onSuccessAction} });
248 var existingFiles = {$jsonEncodedExistingFiles};
249 var fu = $(
'#fileUpload{$id}').data(
'blueimpUI-fileupload');
250 fu._adjustMaxNumberOfFiles(-existingFiles.length);
251 fu._renderDownload(existingFiles)
252 .appendTo($(
'#fileUpload{$id} .files'))
253 .fadeIn(
function () {
261 Yii::app()->getClientScript()->registerScript(__CLASS__ .
'#' . $id, $javaScript);
262 $htmlOptions = array(
'id' => $this->inputId);
263 if ($this->allowMultipleUpload)
265 $htmlOptions[
'multiple'] =
'multiple';
267 $html =
'<div id="dropzone' . $id .
'">' . $this->getDropZoneContent() .
'</div>';
268 $html .=
'<div id="fileUpload' . $id .
'">';
269 $html .=
'<div class="fileupload-buttonbar clearfix">';
270 $html .=
'<div class="addfileinput-button"><span>Y</span>' . $addLabel;
271 $html .= ZurmoHtml::fileField($this->inputName, null, $htmlOptions);
272 $html .=
'</div>' . self::renderMaxSizeContent($this->maxSize, $this->showMaxSize);
273 $html .=
'</div><div class="fileupload-content"><table class="files"><tbody></tbody></table></div></div>';
274 $html .= $this->makeUploadRowScriptContent();
275 $html .= $this->makeDownloadRowScriptContent();
279 protected function getDropZoneContent()
284 protected function getAddLabel()
286 return ZurmoHtml::tag(
'strong', array(
'class' =>
'add-label'), Zurmo::t(
'Core',
'Add Files'));
289 protected function makeDownloadRowScriptContent()
291 $deleteLabel =
'Delete';
292 $removeLabel = Zurmo::t(
'Core',
'Remove');
293 $insertLabel = Zurmo::t(
'ZurmoModule',
'Insert Image');
294 $linkForInsertClass = Redactor::LINK_FOR_INSERT_CLASS;
295 $renderFileDownloadLinks = intval($this->renderFileDownloadLinks);
296 $scriptContent = <<<EOD
297 <script
id=
"{$this->getDownloadTemplateId()}" type=
"text/x-jquery-tmpl">
298 <tr
class=
"{$this->getDownloadTemplateId()}{{if error}} ui-state-error{{/if}}">
300 <td
class=
"error" colspan=
"4">\${error}</td>
304 {{
if thumbnail_url}} <span
class=
"uploaded-logo"><img src=
"\${thumbnail_url}"/></span>{{/
if}}
305 <span
class=
"file-size">(\${size})</span>
306 <span
class=
"file-name"><strong>\${name}</strong></span>
307 <span
class=
"insert-link"><a href=
"\${insert_link}" class=
"mini-button {$linkForInsertClass}" data-url=
"\${filelink}">{$insertLabel}</a></span>
309 <span
class=
"icon-attachment"></span>
310 <span
class=
"ui-icon ui-icon-document" style=
"display:inline-block;"></span>
311 {{
if $renderFileDownloadLinks}}
313 <a href=
"\${filelink}">\${name}</a></span>
320 <span
class=
"file-size">(\${size})</span>
321 <span
class=
"upload-actions delete">
322 <button
class=
"icon-delete" title=
"{$removeLabel}" data-url=
"{$this->deleteUrl}?id=\${id}"><span><!--{$deleteLabel}--><span></button>
325 <input name=
"{$this->hiddenInputName}[]" type=
"hidden" value=
"\${id}"/>
331 return $scriptContent;
334 protected function makeUploadRowScriptContent()
336 $cancelLabel = Zurmo::t(
'Core',
'Cancel');
337 $scriptContent = <<<EOD
338 <script
id=
"{$this->getUploadTemplateId()}" type=
"text/x-jquery-tmpl">
339 <tr
class=
"{$this->getUploadTemplateId()}{{if error}} ui-state-error{{/if}}">
341 <span
class=
"z-spinner"></span>
342 \${name} <span
class=
"file-size">(\${sizef})</span>
344 <span
class=
"upload-error">\${error}</span>
346 <span
class=
"upload-actions cancel">
347 <button
class=
"cancel" title=
"{$cancelLabel}"><span>{$cancelLabel}</span></button>
354 return $scriptContent;
357 protected static function renderMaxSizeContent($maxSize, $showMaxSize)
359 assert(
'is_int($maxSize) || $maxSize == null');
360 assert(
'is_bool($showMaxSize)');
361 if ($maxSize == null || !$showMaxSize)
365 $content =
'<span class="max-upload-size">' . Zurmo::t(
'Core',
'Max upload size: {maxSize}',
370 protected function getDownloadTemplateId()
372 return static::DOWNLOAD_TEMPLATE_ID;
375 protected function getUploadTemplateId()
377 return static::UPLOAD_TEMPLATE_ID;
380 protected function renderParamForInit($paramName)
382 $paramValue = $this->$paramName;
383 if (isset($paramValue))
385 $config =
"{$paramName}: {$paramValue},";
static convertSizeToHumanReadableAndGet($size)