Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
FileUpload.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  class FileUpload extends ZurmoWidget
43  {
44  const DOWNLOAD_TEMPLATE_ID = 'template-download';
45 
46  const UPLOAD_TEMPLATE_ID = 'template-upload';
47 
48  public $scriptFile = array('jquery.fileupload.js',
49  'jquery.fileupload-ui.js', 'jquery.tmpl.min.js', 'jquery.iframe-transport.js');
50 
51  public $cssFile = null;
52 
53  public $assetFolderName = 'fileUpload';
54 
59  public $uploadUrl;
60 
65  public $deleteUrl;
66 
71  public $allowMultipleUpload = false;
72 
77  public $renderFileDownloadLinks = false;
78 
84  public $options;
85 
90  public $formName;
91 
98  public $formData;
99 
104  public $inputName;
105 
110  public $inputId;
111 
116  public $hiddenInputName;
117 
122  public $existingFiles;
123 
128  public $maxSize;
129 
134  public $beforeUploadAction;
135 
140  public $afterDeleteAction;
141 
146  public $onSuccessAction;
147 
152  public $showMaxSize = true;
153 
160  public function init()
161  {
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');
169  parent::init();
170  }
171 
172  public function run()
173  {
174  $id = $this->getId();
175  $jsonEncodedExistingFiles = CJSON::encode($this->existingFiles);
176 
177  if ($this->allowMultipleUpload)
178  {
179  $sendAction = null;
180  $addLabel = $this->getAddLabel();
181  }
182  else
183  {
184  $sendAction = "\$('#{$this->formName}').find('.files > tbody').children().remove();";
185  $addLabel = $this->getAddLabel();
186  }
187  // Begin Not Coding Standard
188  $javaScript = <<<EOD
189 $(function () {
190  'use strict';
191 
192  // Initialize the jQuery File Upload widget:
193  $('#fileUpload{$id}').fileupload({
194  dataType: 'json',
195  url: '{$this->uploadUrl}',
196  autoUpload: true,
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}
205  {$sendAction}
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 () {
212  // Fix for IE7 and lower:
213  $(this).show();
214  $(this).makeOrRemoveTogglableSpinner(true, '#'+'fileUpload{$id}');
215  }).data('data', data);
216  if ((that.options.autoUpload || data.autoUpload) &&
217  data.isValidated) {
218  data.jqXHR = data.submit();
219  }
220  }
221  });
222  // Open download dialogs via iframes,
223  // to prevent aborting current uploads:
224  $('#fileUpload{$id} .files a:not([target^=_blank])').live('click', function (e) {
225  e.preventDefault();
226  $('<iframe style="display:none;"></iframe>')
227  .prop('src', this.href)
228  .appendTo('body');
229  });
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}
234 
235  });
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))
239  {
240  setTimeout(function () {
241  $('#{$this->formName}').find('.files > tbody').children(':last').fadeOut('slow', function() { $(this).remove();});
242  {$this->afterDeleteAction}
243  }, 1000);
244  }
245  });
246  $('#fileUpload{$id}').bind('done', function (e, data) { {$this->onSuccessAction} });
247  //load existing files
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 () {
254  // Fix for IE7 and lower:
255  $(this).show();
256  });
257 });
258 
259 EOD;
260  // End Not Coding Standard
261  Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $id, $javaScript);
262  $htmlOptions = array('id' => $this->inputId);
263  if ($this->allowMultipleUpload)
264  {
265  $htmlOptions['multiple'] = 'multiple';
266  }
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();
276  echo $html;
277  }
278 
279  protected function getDropZoneContent()
280  {
281  return null;
282  }
283 
284  protected function getAddLabel()
285  {
286  return ZurmoHtml::tag('strong', array('class' => 'add-label'), Zurmo::t('Core', 'Add Files'));
287  }
288 
289  protected function makeDownloadRowScriptContent()
290  {
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}}">
299  {{if error}}
300  <td class="error" colspan="4">\${error}</td>
301  {{else}}
302  <td class="name">
303  {{if insert_link}}
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>
308  {{else}}
309  <span class="icon-attachment"></span>
310  <span class="ui-icon ui-icon-document" style="display:inline-block;"></span>
311  {{if $renderFileDownloadLinks}}
312  {{if filelink}}
313  <a href="\${filelink}">\${name}</a></span>
314  {{else}}
315  \${name}
316  {{/if}}
317  {{else}}
318  \${name}
319  {{/if}}
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>
323  </span>
324  {{/if}}
325  <input name="{$this->hiddenInputName}[]" type="hidden" value="\${id}"/>
326  </td>
327  {{/if}}
328  </tr>
329 </script>
330 EOD;
331  return $scriptContent;
332  }
333 
334  protected function makeUploadRowScriptContent()
335  {
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}}">
340  <td class="name">
341  <span class="z-spinner"></span>
342  \${name} <span class="file-size">(\${sizef})</span>
343  {{if error}}
344  <span class="upload-error">\${error}</span>
345  {{else}}
346  <span class="upload-actions cancel">
347  <button class="cancel" title="{$cancelLabel}"><span>{$cancelLabel}</span></button>
348  </span>
349  {{/if}}
350  </td>
351  </tr>
352 </script>
353 EOD;
354  return $scriptContent;
355  }
356 
357  protected static function renderMaxSizeContent($maxSize, $showMaxSize)
358  {
359  assert('is_int($maxSize) || $maxSize == null');
360  assert('is_bool($showMaxSize)');
361  if ($maxSize == null || !$showMaxSize)
362  {
363  return;
364  }
365  $content = '<span class="max-upload-size">' . Zurmo::t('Core', 'Max upload size: {maxSize}',
366  array('{maxSize}' => FileModelDisplayUtil::convertSizeToHumanReadableAndGet($maxSize))) . '</span>';
367  return $content;
368  }
369 
370  protected function getDownloadTemplateId()
371  {
372  return static::DOWNLOAD_TEMPLATE_ID;
373  }
374 
375  protected function getUploadTemplateId()
376  {
377  return static::UPLOAD_TEMPLATE_ID;
378  }
379 
380  protected function renderParamForInit($paramName)
381  {
382  $paramValue = $this->$paramName;
383  if (isset($paramValue))
384  {
385  $config = "{$paramName}: {$paramValue},"; // Not Coding Standard
386  return $config;
387  }
388  }
389  }
390 ?>
static convertSizeToHumanReadableAndGet($size)
Generated on Sat Jul 4 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.