All Data Structures Functions Variables Pages
LogoFileUpload.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 
41  class LogoFileUpload extends FileUpload
42  {
43  public function run()
44  {
45  $id = $this->getId();
46  $jsonEncodedExistingFiles = CJSON::encode($this->existingFiles);
47 
48  if ($this->allowMultipleUpload)
49  {
50  $sendAction = null;
51  $addLabel = ZurmoHtml::tag('strong', array('class' => 'add-label'), Zurmo::t('Core', 'Add Files'));
52  }
53  else
54  {
55  $sendAction = "\$('#{$this->formName}').find('.files > tbody').children().remove();";
56  $addLabel = ZurmoHtml::tag('strong', array('class' => 'add-label'), Zurmo::t('Core', 'Add File'));
57  }
58  $this->registerScriptForLogoFileElement($id, $sendAction, $jsonEncodedExistingFiles);
59 
60  $htmlOptions = array('id' => $this->inputId);
61  $html = '<div id="fileUpload' . $id . '">';
62  $html .= '<div class="fileupload-buttonbar clearfix">';
63  $html .= '<div class="addfileinput-button"><span>Y</span>' . $addLabel;
64  $html .= ZurmoHtml::fileField($this->inputName, null, $htmlOptions);
65  $html .= '</div>' . self::renderMaxSizeContent($this->maxSize, $this->showMaxSize);
66  $html .= '</div><div class="fileupload-content"><table class="files"><tbody></tbody></table></div></div>';
67  $html .= $this->makeUploadRowScriptContent();
68  $html .= $this->makeDownloadRowScriptContent();
69  echo $html;
70  }
71 
72  protected function registerScriptForLogoFileElement($id, $sendAction, $jsonEncodedExistingFiles)
73  {
74  // Begin Not Coding Standard
75  $javaScript = <<<EOD
76 $(function () {
77  'use strict';
78 
79  // Initialize the jQuery File Upload widget:
80  $('#fileUpload{$id}').fileupload({
81  dataType: 'json',
82  url: '{$this->uploadUrl}',
83  autoUpload: true,
84  sequentialUploads: true,
85  maxFileSize: {$this->maxSize},
86  add: function (e, data) {
87  {$this->beforeUploadAction}
88  {$sendAction}
89  var that = $(this).data('blueimpUI-fileupload') || $(this).data('fileupload');
90  that._adjustMaxNumberOfFiles(-data.files.length);
91  data.isAdjusted = true;
92  data.isValidated = that._validate(data.files);
93  data.context = that._renderUpload(data.files)
94  .appendTo($(this).find('.files')).fadeIn(function () {
95  // Fix for IE7 and lower:
96  $(this).show();
97  $(this).makeOrRemoveTogglableSpinner(true, '#'+'fileUpload{$id}');
98  }).data('data', data);
99  if ((that.options.autoUpload || data.autoUpload) &&
100  data.isValidated) {
101  data.jqXHR = data.submit();
102  }
103  }
104  });
105  // Open download dialogs via iframes,
106  // to prevent aborting current uploads:
107  $('#fileUpload{$id} .files a:not([target^=_blank])').live('click', function (e) {
108  e.preventDefault();
109  $('<iframe style="display:none;"></iframe>')
110  .prop('src', this.href)
111  .appendTo('body');
112  });
113  $('.fileupload-buttonbar').removeClass('ui-widget-header ui-corner-top');
114  $('.fileupload-content').removeClass('ui-widget-content ui-corner-bottom');
115  $('#fileUpload{$id}').bind('fileuploaddestroy', function (e, data) {
116  {$this->afterDeleteAction}
117 
118  });
119  $('#fileUpload{$id}').bind('fileuploadalways', function (e, data) {
120  if (data == undefined || data.result == undefined ||
121  ((data.result[0] != undefined && data.result[0].error != undefined) || data.result.error != undefined))
122  {
123  setTimeout(function () {
124  $('#{$this->formName}').find('.files > tbody').children(':last').fadeOut('slow', function() { $(this).remove();});
125  {$this->afterDeleteAction}
126  }, 1000);
127  }
128  });
129  //load existing files
130  var existingFiles = {$jsonEncodedExistingFiles};
131  var fu = $('#fileUpload{$id}').data('blueimpUI-fileupload');
132  fu._adjustMaxNumberOfFiles(-existingFiles.length);
133  fu._renderDownload(existingFiles)
134  .appendTo($('#fileUpload{$id} .files'))
135  .fadeIn(function () {
136  // Fix for IE7 and lower:
137  $(this).show();
138  });
139 });
140 
141 EOD;
142  // End Not Coding Standard
143  Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $id, $javaScript);
144  }
145 
146  protected function makeDownloadRowScriptContent()
147  {
148  $deleteLabel = 'Delete';
149  $removeLabel = Zurmo::t('Core', 'Remove');
150  $scriptContent = <<<EOD
151 <script id="template-download" type="text/x-jquery-tmpl">
152  <tr class="template-download uploaded-logo-template{{if error}} ui-state-error{{/if}}">
153  {{if error}}
154  <td class="error" colspan="4">\${error}</td>
155  {{else}}
156  <td class="name">
157  <span class="uploaded-logo"><img src="\${thumbnail_url}"/></span>
158  \${name} <span class="file-size">(\${sizef})</span>
159  <span class="upload-actions delete">
160  <button class="icon-delete" title="{$removeLabel}" data-url="{$this->deleteUrl}"><span><!--{$deleteLabel}--><span></button>
161  </span>
162  <input name="{$this->hiddenInputName}[]" type="hidden" value="\${id}"/>
163  </td>
164  {{/if}}
165  </tr>
166 </script>
167 EOD;
168  return $scriptContent;
169  }
170  }
171 ?>
Generated on Sat Feb 22 2020 07:10:26