{% block ks_attachments_list %}
{% set url = url('frontend.attachment.execute') %}
{% set atachmentsPluginOptions = {
url: url
} %}
{% set maxItems = maxItems ? maxItems : 100 %}
{% set iconSize = iconSize is null ? 'w40' : iconSize %}
{# initial ID array #}
{% set attachmentsMediaIds = [] %}
{% for attachment in attachmentsDisplay %}
{# merge IDs to a single array #}
{% set attachmentsMediaIds = attachmentsMediaIds|merge([attachment.mediaIconId]) %}
{% set attachmentsMediaIds = attachmentsMediaIds|merge([attachment.mediaFileId ? attachment.mediaFileId : attachment.translated.mediaFileId]) %}
{% endfor %}
{# do a single fetch from database #}
{% set mediaFileCollection = searchMedia(attachmentsMediaIds, context.context) %}
{% if displayOn == '3' %}
<div class="ks-attachment-container{% if containerClass %} {{ containerClass }}{% endif %}" data-ks-attachments data-attachments-plugin-options='{{ atachmentsPluginOptions|json_encode }}' data-product-id="{{ page.product.id }}">
<ul class="ks-attachments-list ks-attachments-list--product-icons">
{% if attachmentsDisplay %}
{% for attachment in attachmentsDisplay %}
{% if loop.index - 1 < maxItems %}
<li>
{% set mediaFileId = attachment.mediaFileId ? attachment.mediaFileId : attachment.translated.mediaFileId %}
{% set mediaFile = mediaFileCollection.get(mediaFileId) %}
<img src="{{ mediaFile.url }}" alt="{{ attachment.title }}" title="{{ attachment.title }}" width="50" height="50">
</li>
{% endif %}
{% endfor %}
{% endif %}
</ul>
</div>
{% else %}
<div class="ks-attachment-container{% if containerClass %} {{ containerClass }}{% endif %}" data-ks-attachments data-attachments-plugin-options='{{ atachmentsPluginOptions|json_encode }}' data-product-id="{{ page.product.id }}">
<ul class="ks-attachments-list">
{% if attachmentsDisplay %}
{% for attachment in attachmentsDisplay %}
<li>
{% set IconAttachment = mediaFileCollection.get(attachment.mediaIconId) %}
{% set displayAttachment = attachment.display|split(',') %}
{% set limited = attachment.limited %}
{% set downloadsRemaining = attachment.downloadsRemaining %}
{% if limited and downloadsRemaining > 0 %}
{% set downloadsRemaining = downloadsRemaining %}
{% elseif limited == false %}
{% set downloadsRemaining = "" %}
{% else %}
{% set downloadsRemaining = "Out of " %}
{% endif %}
{% set mediaFile = mediaFileCollection.get(attachment.mediaFileId) %}
{% set mediaFileExtension = mediaFile.fileExtension %}
{% set mediaFileIcon = mediaFileExtension %}
<div class="ks-media-attachment" data-attachment-id="{{ attachment.id }}" data-attachment-file-icon="{{ mediaFileIcon }}">
{% if IconAttachment %}
{% sw_thumbnails 'minimal-image-thumbnails' with {
media: IconAttachment,
sizes: {
'default': '30px'
},
attributes: {
'class': 'ks-icon-attachment',
'alt': ( attachment.title ?: ''),
'title': (attachment.title ?: '')
}
} %}
{% else %}
{% if mediaFileIcon == 'jpeg' %}
{% set mediaFileIcon = 'jpg' %}
{% endif %}
{% set mediaFileIconArray = ['pdf', 'png', 'txt', 'doc', 'xls', 'jpg', 'svg'] %}
{% set iconName = 'file-icon' %}
{% if mediaFileIcon in mediaFileIconArray %}
{% set iconName = mediaFileIcon ~ '-icon' %}
{% endif %}
{% sw_include "@Storefront/storefront/utilities/icon.html.twig" with {
'name' : iconName,
'pack' : 'ks',
'namespace' : 'KsAttachments',
} %}
{% set mediaImageFileIconArray = ['jpg', 'jpeg', 'png', 'svg'] %}
{% if mediaFileIcon in mediaImageFileIconArray %}
<span class="ks-attachments-popup"></span>
{% endif %}
{% endif %}
<span class="ks-title-attachment">{{ attachment.title }}
{% if limited %}
(<span class="download-remaining-{{ attachment.ids }}">{{ downloadsRemaining }}</span>
downloads)
{% else %}
<span class="download-remaining-{{ attachment.ids }}">{{ downloadsRemaining }}</span>
{% endif %}
</span>
</div>
</li>
{% endfor %}
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %}