Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
samples.phtml
Go to the documentation of this file.
1 <?php
7 // @codingStandardsIgnoreFile
8 
9 ?>
10 <?php
15 $_product = $block->getProduct();
16 $block->getConfigJson();
17 ?>
18 <fieldset class="admin__fieldset downloadable-form" data-ui-id="downloadable-samples">
19  <legend class="admin__legend"><span><?= /* @escapeNotVerified */ __('Samples') ?></span></legend><br>
20  <p class="note"><?= /* @escapeNotVerified */ __('Add product preview files here.') ?></p>
21  <div class="admin__field"<?= !$block->isSingleStoreMode() ? ' data-config-scope="' . __('[STORE VIEW]') . '"' : '' ?>>
22  <label class="admin__field-label" for="downloadable_samples_title"><span><?= /* @noEscape */ __('Title') ?></span></label>
23  <div class="admin__field-control">
24  <input type="text" class="admin__control-text" id="downloadable_samples_title" name="product[samples_title]" value="<?= $block->escapeHtml($block->getSamplesTitle()) ?>" <?= /* @noEscape */ ($_product->getStoreId() && $block->getUsedDefault()) ? 'disabled="disabled"' : '' ?>>
25  <?php if ($_product->getStoreId()): ?>
26  <div class="admin__field admin__field-option">
27  <input id="sample_title_default" class="admin__control-checkbox" type="checkbox" name="use_default[]" value="samples_title" onclick="toggleValueElements(this, this.parentNode.parentNode)" <?= /* @noEscape */ $block->getUsedDefault() ? 'checked="checked"' : '' ?> />
28  <label class="admin__field-label" for="sample_title_default"><span>Use Default Value</span></label>
29  </div>
30  <?php endif; ?>
31  </div>
32  </div>
33  <div class="admin__field admin__field-wide">
34  <div class="admin__field-control">
35  <div class="admin__control-table-wrapper">
36  <table class="admin__control-table">
37  <thead>
38  <tr>
39  <th class="col-sort"><span><?= /* @noEscape */ __('Sort Order') ?></span></th>
40  <th class="_required col-title"><span><?= /* @noEscape */ __('Title') ?></span></th>
41  <th class="col-file"><span><?= /* @noEscape */ __('Attach File or Enter Link') ?></span></th>
42  <th class="col-actions">&nbsp;</th>
43  </tr>
44  </thead>
45  <tfoot>
46  <tr>
47  <td colspan="4" class="col-actions-add"><?= $block->getAddButtonHtml() ?></td>
48  </tr>
49  </tfoot>
50  <tbody id="sample_items_body">
51  </tbody>
52  </table>
53  </div>
54  <div class="admin__field-note">
55  <?= /* @noEscape */ __('Alphanumeric, dash and underscore characters are recommended for filenames. Improper characters are replaced with \'_\'.') ?>
56  </div>
57  </div>
58  </div>
59 </fieldset>
60 <script>
61 require([
62  'jquery',
63  'uiRegistry',
64  'mage/template',
65  'jquery/file-uploader',
66  'prototype',
67  'mage/translate'
68 ], function (jQuery, registry, mageTemplate) {
69  registry.get('downloadable', function (Downloadable) {
70  var sampleTemplate = '<tr>'+
71  '<td class="col-sort" data-role="draggable-handle">' +
72  '<input data-container="link-order" type="hidden" name="downloadable[sample][<%- data.id %>][sort_order]" value="<%- data.sort_order %>" class="sort" />' +
73  '<span class="draggable-handle" title="<?= /* @escapeNotVerified */ $block->escapeHtml(__('Sort Variations')) ?>"></span>' +
74  '</td>'+
75  '<td class="col-title">'+
76  '<input type="hidden" class="__delete__" name="downloadable[sample][<%- data.id %>][is_delete]" value="" />'+
77  '<input type="hidden" name="downloadable[sample][<%- data.id %>][sample_id]" value="<%- data.sample_id %>" />'+
78  '<input type="text" class="required-entry input-text admin__control-text" name="downloadable[sample][<%- data.id %>][title]" value="<%- data.title %>" />'+
79  <?php if($_product->getStoreId()): ?>
80  '<div class="admin__field admin__field-option">'+
81  '<input type="checkbox" id="downloadable_sample_<%- data.id %>_title" name="downloadable[sample][<%- data.id %>][use_default_title]" value="1" class="admin__control-checkbox" />'+
82  '<label for="downloadable_link_<%- data.id %>_price" class="admin__field-label"><span><?= /* @escapeNotVerified */ __('Use Default Value') ?></span></label>'+
83  '</div>' +
84  <?php endif; ?>
85  '</td>'+
86  '<td class="col-file">'+
87  '<div class="admin__field admin__field-option">'+
88  '<input type="radio" class="admin__control-radio validate-one-required-by-name" id="downloadable_sample_<%- data.id %>_file_type" name="downloadable[sample][<%- data.id %>][type]" value="file"<%- data.file_checked %> />' +
89  '<label for="downloadable_sample_<%- data.id %>_file_type" class="admin__field-label"><span><?= /* @escapeNotVerified */ __('File') ?>:</span></label>'+
90  '<input type="hidden" class="validate-downloadable-file" id="downloadable_sample_<%- data.id %>_file_save" name="downloadable[sample][<%- data.id %>][file]" value="<%- data.file_save %>" />'+
91  '<div id="downloadable_sample_<%- data.id %>_file" class="admin__field-uploader">'+
92  '<div id="downloadable_sample_<%- data.id %>_file-old" class="file-row-info"></div>'+
93  '<div id="downloadable_sample_<%- data.id %>_file-new" class="file-row-info new-file"></div>'+
94  '<div class="fileinput-button">'+
95  '<span><?= /* @noEscape */ __('Browse Files...') ?></span>' +
96  '<input id="downloadable_sample_<%- data.id %>_file" type="file" name="<?= /* @noEscape */ $block->getConfig()->getFileField() ?>" data-url="<?= $block->escapeHtml($block->getConfig()->getUrl()) ?>">' +
97  '<script>' +
98  '/*<![CDATA[*/' +
99  'sampleUploader("#downloadable_sample_<%- data.id %>_file"); ' +
100  '/*]]>*/' +
101  '</scr'+'ipt>'+
102  '</div>'+
103  '</div>'+
104  '</div>'+
105  '<div class="admin__field admin__field-option admin__field-file-url">'+
106  '<input type="radio" class="admin__control-radio validate-one-required-by-name" id="downloadable_sample_<%- data.id %>_url_type" name="downloadable[sample][<%- data.id %>][type]" value="url"<%- data.url_checked %> />' +
107  '<label for="downloadable_sample_<%- data.id %>_url_type" class="admin__field-label"><span><?= /* @escapeNotVerified */ __('URL') ?></span></label>' +
108  '<input type="text" class="validate-downloadable-url validate-url admin__control-text" name="downloadable[sample][<%- data.id %>][sample_url]" value="<%- data.sample_url %>" placeholder="<?= /* @escapeNotVerified */ __('URL') ?>" />'+
109  '</div>'+
110  '<div>'+
111  '<span id="downloadable_sample_<%- data.id %>_container"></span>'+
112  '</div>'+
113  '</td>'+
114  '<td class="col-actions">'+
115  '<button type="button" class="action-delete" title="<?= /* @escapeNotVerified */ $block->escapeHtml(__('Delete')) ?>"><span><?= /* @escapeNotVerified */ __('Delete') ?></span></button>'+
116  '</td>'+
117  '</tr>';
118  sampleItems = {
119  tbody: $('sample_items_body'),
120  templateText: sampleTemplate,
121  itemCount: 0,
122  add: function(data) {
123  alertAlreadyDisplayed = false;
124  this.template = mageTemplate(this.templateText);
125 
126  if(!data.sample_id) {
127  data = {};
128  data.sample_type = 'file';
129  data.sample_id = 0;
130  data.sort_order = this.itemCount + 1;
131  }
132 
133  data.id = this.itemCount;
134 
135  if (data.sample_type == 'url') {
136  data.url_checked = ' checked="checked"';
137  } else if (data.sample_type == 'file') {
138  data.file_checked = ' checked="checked"';
139  }
140 
141  Element.insert(this.tbody, {
142  'bottom': this.template({
143  data: data
144  })
145  });
146 
147  scopeTitle = $('downloadable_sample_'+data.id+'_title');
148  if (scopeTitle) {
149  Event.observe(scopeTitle, 'click', function(event){
150  scopeElm = $(Event.findElement(event, 'input'));
151  titleField = scopeElm.up(0).down('input[type="text"]');
152  if (scopeElm.checked == true) {
153  titleField.disabled = true;
154  } else {
155  titleField.disabled = false;
156  }
157  });
158  }
159  if (!data.store_title && scopeTitle) {
160  scopeTitle.up(0).down('input[type="text"]').disabled = true;
161  scopeTitle.checked = true;
162  }
163 
164  sampleUrl = $('downloadable_sample_'+data.id+'_url_type');
165 
166  if (!data.file_save) {
167  data.file_save = [];
168  }
169  new Downloadable.FileUploader(
170  'samples',
171  data.id,
172  sampleUrl.up('td').down('div.admin__field-uploader'),
173  'downloadable[sample]['+data.id+']',
174  data.file_save,
175  'downloadable_sample_'+data.id+'_file',
176  <?= /* @noEscape */ $block->getConfigJson() ?>
177  );
178  sampleUrl.advaiceContainer = 'downloadable_sample_'+data.id+'_container';
179  sampleFile = $('downloadable_sample_'+data.id+'_file_type');
180  sampleFile.advaiceContainer = 'downloadable_sample_'+data.id+'_container';
181  $('downloadable_sample_'+data.id+'_file_save').advaiceContainer = 'downloadable_sample_'+data.id+'_container';
182 
183  this.itemCount++;
184  this.bindRemoveButtons();
185  },
186  sorting: function () {
187  var list = jQuery(this.tbody);
188  list.sortable({
189  axis: 'y',
190  handle: '[data-role=draggable-handle]',
191  items: 'tr',
192  update: function (event, data) {
193  list.find('[data-container=link-order]').each(function (i, el) {
194  jQuery(el).val(i + 1);
195  });
196  },
197  tolerance: 'pointer'
198  });
199  },
200  remove: function(event) {
201  var element = $(Event.findElement(event, 'tr'));
202  alertAlreadyDisplayed = false;
203  if(element){
204  element.down('input[type="hidden"].__delete__').value = '1';
205  element.addClassName('no-display');
206  element.addClassName('ignore-validate');
207  element.hide();
208  }
209  },
210  bindRemoveButtons: function() {
211  var buttons = $$('tbody#sample_items_body .action-delete');
212  for(var i=0;i<buttons.length;i++){
213  if(!$(buttons[i]).binded){
214  $(buttons[i]).binded = true;
215  Event.observe(buttons[i], 'click', this.remove.bind(this));
216  }
217  }
218  }
219  };
220 
221  sampleItems.sorting();
222  sampleItems.bindRemoveButtons();
223 
224  sampleUploader = function (id) {
225  (function ($, id) {
226  $(id).fileupload({
227  dataType: 'json',
228  url: '<?= /* @noEscape */ $block->getConfig()->getUrl() ?>',
229  sequentialUploads: true,
230  maxFileSize: 2000000,
231  add: function (e, data) {
232  var progressTmpl = mageTemplate(id + '-template'),
233  fileSize,
234  tmpl;
235 
236  $.each(data.files, function (index, file) {
237  fileSize = typeof file.size == "undefined" ?
238  $.mage.__('We could not detect a size.') :
239  byteConvert(file.size);
240 
241  data.fileId = Math.random().toString(36).substr(2, 9);
242 
243  tmpl = progressTmpl({
244  data: {
245  name: file.name,
246  size: fileSize,
247  id: data.fileId
248  }
249  });
250 
251  $(tmpl).appendTo(id);
252  });
253 
254  $(this).fileupload('process', data).done(function () {
255  data.submit();
256  });
257  },
258  done: function (e, data) {
259  var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
260  $(progressSelector).css('width', '100%');
261  if (data.result && !data.result.hasOwnProperty('errorcode')) {
262  $(progressSelector).removeClass('upload-progress').addClass('upload-success');
263  new Downloadable.FileList(id.substr(1), null).handleUploadComplete(data.result);
264  } else {
265  $(progressSelector).removeClass('upload-progress').addClass('upload-failure');
266  var errorMsg = '<span class="file-info-error">' + data.result.error + '</span>';
267  $('#' + data.fileId + ' .file-info').append(errorMsg);
268  }
269  },
270  progress: function (e, data) {
271  var progress = parseInt(data.loaded / data.total * 100, 10);
272  var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
273  $(progressSelector).css('width', progress + '%');
274  },
275  fail: function (e, data) {
276  var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
277  $(progressSelector).removeClass('upload-progress').addClass('upload-failure');
278  if (data.result && data.result.hasOwnProperty('errorcode')) {
279  var errorMsg = '<span class="file-info-error">' + data.result.error + '</span>';
280  $('#' + data.fileId + ' .file-info').append(errorMsg);
281  }
282  }
283  });
284  })(jQuery, id);
285  };
286 
287  if($('add_sample_item')){
288  Event.observe('add_sample_item', 'click', sampleItems.add.bind(sampleItems));
289  }
290 
291  <?php foreach ($block->getSampleData() as $item): ?>
292  sampleItems.add(<?= /* @noEscape */ $item->toJson() ?>);
293  <?php endforeach; ?>
294 
295  });
296 });
297 </script>
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
__()
Definition: __.php:13
$block
Definition: block.php:8
jquery ui
Definition: tree.phtml:41
$_count $_index $_prevOptionId if($block->getOrderOptions()|| $_item->getDescription()) foreach( $items as $_item)( $_item) ?><?php $attributes if( $_item->getOrderItem() ->getParentItem())($_prevOptionId !=$attributes['option_id'])</td >< td > & nbsp
Definition: renderer.phtml:33
endforeach
Definition: samples.phtml:293
if( $_product->getStoreId())(this, this.parentNode.parentNode)" <? endif
Definition: samples.phtml:25
else function()
jQuery('.store-switcher .dropdown-menu li a').each(function()
Definition: switcher.phtml:203
$_product
Definition: samples.phtml:15