Skip to content

Commit cda317b

Browse files
committed
added Gallery selector form field
1 parent 4ca14e3 commit cda317b

File tree

5 files changed

+265
-33
lines changed

5 files changed

+265
-33
lines changed

app/base/models/GiftCard.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Base\Interfaces\Model\ProductInterface;
77
use App\Base\Traits\ProductTrait;
88
use App\Base\GraphQl\GraphQLExport;
9+
use App\Site\Models\MediaElement;
910

1011
/**
1112
* @method string getSku()
@@ -46,6 +47,8 @@ class GiftCard extends FrontendModel implements ProductInterface
4647
{
4748
use ProductTrait;
4849

50+
protected ?MediaElement $media = null;
51+
4952
/**
5053
* {@inheritdoc}
5154
*
@@ -56,6 +59,26 @@ public function isPhysical(): bool
5659
return false;
5760
}
5861

62+
#[GraphQLExport]
63+
public function getMedia(): ?MediaElement
64+
{
65+
if (!is_null($this->media)) {
66+
return $this->media;
67+
}
68+
69+
if (is_null($this->getMediaId())) {
70+
return null;
71+
}
72+
73+
return $this->setMedia(MediaElement::load($this->getMediaId()))->media;
74+
}
75+
76+
public function setMedia(?MediaElement $media): self
77+
{
78+
$this->media = $media;
79+
return $this;
80+
}
81+
5982
/**
6083
* {@inheritdoc}
6184
*

app/site/controllers/Admin/Cms/Media.php

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
307307
]);
308308
}
309309

310+
if ($this->getRequest()->query->get('page_id') || $this->getRequest()->query->get('product_id') || ($form_state['input_values']['gallery'] ?? false)) {
311+
$galleryField = $this->containerMake(\App\Site\Tools\FAPI\Fields\Gallery::class, [ 'options' => [
312+
'title' => 'Select an existing media',
313+
// 'multiple' => true,
314+
]]);
315+
316+
$form->addField('gallery', $galleryField);
317+
}
318+
310319
$form
311320
->addField('upload_file', [
312321
'type' => 'file',
@@ -617,41 +626,46 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed
617626
// intentional fall trough
618627
// no break
619628
case 'edit':
620-
if ($form->getField('upload_file')->isUploaded()) {
621-
if ($values->upload_file->filepath) {
622-
$media->setPath($values->upload_file->filepath);
623-
}
624-
if ($values->upload_file->filename) {
625-
$media->setFilename($values->upload_file->filename);
626-
}
627-
if ($values->upload_file->mimetype) {
628-
$media->setMimetype($values->upload_file->mimetype);
629-
}
630-
if ($values->upload_file->filesize) {
631-
$media->setFilesize($values->upload_file->filesize);
632-
}
633-
if ($values->upload_file->renamed) {
634-
$this->addInfoFlashMessage(
635-
$this->getUtils()->translate(
636-
"File was renamed to %s",
637-
[$values->upload_file->filename]
638-
)
639-
);
629+
630+
if (is_numeric($values->gallery)) {
631+
$media = $this->containerCall([MediaElement::class, 'load'], ['id' => $values->gallery]);
632+
} else {
633+
if ($form->getField('upload_file')->isUploaded()) {
634+
if ($values->upload_file->filepath) {
635+
$media->setPath($values->upload_file->filepath);
636+
}
637+
if ($values->upload_file->filename) {
638+
$media->setFilename($values->upload_file->filename);
639+
}
640+
if ($values->upload_file->mimetype) {
641+
$media->setMimetype($values->upload_file->mimetype);
642+
}
643+
if ($values->upload_file->filesize) {
644+
$media->setFilesize($values->upload_file->filesize);
645+
}
646+
if ($values->upload_file->renamed) {
647+
$this->addInfoFlashMessage(
648+
$this->getUtils()->translate(
649+
"File was renamed to %s",
650+
[$values->upload_file->filename]
651+
)
652+
);
653+
}
640654
}
641-
}
642655

643-
$media->setLazyload($values->lazyload);
656+
$media->setLazyload($values->lazyload);
644657

645-
$this->setAdminActionLogData($media->getChangedData());
658+
$this->setAdminActionLogData($media->getChangedData());
646659

647-
$media->persist();
660+
$media->persist();
661+
}
648662

649-
if ($values['page_id'] != null) {
650-
$this->containerCall([Page::class, 'load'], ['id' => $values['page_id']])->addMedia($media);
651-
} else if ($values['product_type'] == 'downloadable' && $values['product_id'] != null) {
652-
$this->containerCall([DownloadableProduct::class, 'load'], ['id' => $values['product_id']])->addMedia($media);
653-
} else if ($values['product_type'] == 'book' && $values['product_id'] != null) {
654-
$this->containerCall([Book::class, 'load'], ['id' => $values['product_id']])->addMedia($media);
663+
if ($form_state['input_values']['page_id'] != null) {
664+
$this->containerCall([Page::class, 'load'], ['id' => $form_state['input_values']['page_id']])->addMedia($media);
665+
} else if ($form_state['input_values']['product_type'] == 'downloadable' && $form_state['input_values']['product_id'] != null) {
666+
$this->containerCall([DownloadableProduct::class, 'load'], ['id' => $form_state['input_values']['product_id']])->addMedia($media);
667+
} else if ($form_state['input_values']['product_type'] == 'book' && $form_state['input_values']['product_id'] != null) {
668+
$this->containerCall([Book::class, 'load'], ['id' => $form_state['input_values']['product_id']])->addMedia($media);
655669
} else {
656670
$this->addSuccessFlashMessage($this->getUtils()->translate("Media Saved."));
657671
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
/**
4+
* SiteBase
5+
* PHP Version 8.3
6+
*
7+
* @category CMS / Framework
8+
* @package Degami\Sitebase
9+
* @author Mirko De Grandis <degami@github.com>
10+
* @license MIT https://opensource.org/licenses/mit-license.php
11+
* @link https://github.com/degami/sitebase
12+
*/
13+
14+
namespace App\Site\Tools\FAPI\Fields;
15+
16+
use App\App;
17+
use Degami\PHPFormsApi\Abstracts\Base\Field;
18+
use Degami\PHPFormsApi\Form;
19+
use App\Site\Models\MediaElement;
20+
use App\Base\Abstracts\Models\BaseCollection;
21+
use Degami\Basics\Html\TagElement;
22+
use Exception;
23+
24+
class Gallery extends Field
25+
{
26+
protected $multiple = false;
27+
28+
public function isMultiple(?bool $multiple = null) : bool
29+
{
30+
if (is_null($multiple)) {
31+
return $this->multiple;
32+
}
33+
34+
return $this->multiple = $multiple;
35+
}
36+
37+
/**
38+
* this function tells to the form if this element is a value that needs to be
39+
* included into parent values() function call result
40+
*
41+
* @return boolean include_me
42+
*/
43+
public function isAValue() : bool // tells if component value is passed on the parent values() function call
44+
{
45+
return true;
46+
}
47+
48+
/**
49+
* The function that actually renders the html field
50+
*
51+
* @param Form $form form object
52+
*
53+
* @return string|BaseElement the field html
54+
*/
55+
public function renderField(Form $form) // renders html
56+
{
57+
$id = $this->getHtmlId();
58+
59+
/** @var TagElement $field */
60+
$field = App::getInstance()->containerMake(TagElement::class, ['options' => [
61+
'tag' => 'div',
62+
'id' => $id,
63+
'attributes' => [
64+
'class' => 'row row-cols-4',
65+
],
66+
]]);
67+
68+
$parent_id = App::getInstance()->getEnvironment()->getRequest()->query->get('parent_id');
69+
if (empty($parent_id) || !is_numeric($parent_id)) {
70+
$parent_id = null;
71+
}
72+
$medias = $this->getCollection($parent_id);
73+
74+
if (!is_null($parent_id)) {
75+
76+
$parent = App::getInstance()->containerCall([MediaElement::class, 'load'], ['id' => $parent_id]);
77+
if ($parent->getParentId()) {
78+
$grandparent = App::getInstance()->containerCall([MediaElement::class, 'load'], ['id' => $parent->getParentId()]);
79+
} else {
80+
$grandparent = null;
81+
}
82+
83+
$field->addChild(
84+
App::getInstance()->containerMake(TagElement::class, ['options' => [
85+
'tag' => 'div',
86+
'attributes' => ['class' => 'col text-center'],
87+
'text' => $this->renderMediaElement(
88+
$grandparent, App::getInstance()->getHtmlRenderer()->getIcon('corner-left-up') . ' ' . __('Up')
89+
),
90+
]])
91+
);
92+
}
93+
94+
foreach ($medias as $media) {
95+
/** @var MediaElement $media */
96+
$field->addChild(
97+
App::getInstance()->containerMake(TagElement::class, ['options' => [
98+
'tag' => 'div',
99+
'attributes' => ['class' => 'col text-center'],
100+
'text' => $this->renderMediaElement($media),
101+
]])
102+
);
103+
}
104+
105+
$field->addChild(
106+
App::getInstance()->containerMake(TagElement::class, ['options' => [
107+
'tag' => 'input',
108+
'type' => 'hidden',
109+
'name' => $this->name,
110+
'value' => $this->getValues(),
111+
]])
112+
);
113+
114+
return $field;
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*
120+
* @param Form $form form object
121+
*/
122+
public function preRender(Form $form)
123+
{
124+
if ($this->pre_rendered == true) {
125+
return;
126+
}
127+
$id = $this->getHtmlId();
128+
129+
$this->addJs("
130+
console.log('#{$id}','#{$form->getId()}')
131+
");
132+
if ($this->multiple) {
133+
$this->addJs("
134+
$('#{$id}','#{$form->getId()}').on('change', '.media-selector', function(){
135+
var selected = [];
136+
\$('.media-selector:checked','#{$form->getId()}').each(function(){
137+
selected.push( \$(this).val() );
138+
});
139+
\$('#{$id} input[type=\"hidden\"]','#{$form->getId()}').val( selected.join(',') );
140+
});
141+
");
142+
} else {
143+
$this->addJs("
144+
$('#{$id}','#{$form->getId()}').on('change', '.media-selector', function(){
145+
\$('#{$id} input[type=\"hidden\"]','#{$form->getId()}').val( \$(this).val() );
146+
\$('.media-selector','#{$form->getId()}').not(this).prop('checked', false);
147+
});
148+
");
149+
}
150+
151+
parent::preRender($form);
152+
}
153+
154+
protected function renderMediaElement(?MediaElement $media, ?string $labelText = null, int $maxLength = 12): string
155+
{
156+
$text = ($labelText ?? '<abbr title="' . $media?->getFilename() . '">' . substr("" . $media?->getFilename(), 0, $maxLength) . (strlen("".$media?->getFilename()) > $maxLength ? '...' : '') . '</abbr>');
157+
$label = '<label class="text-nowrap" for="media-selector-'.$media?->getId().'"><input type="checkbox" class="media-selector" id="media-selector-'.$media?->getId().'" value="' . $media?->getId() . '" />' . $text . '</label>';
158+
if (is_null($media) || $media->isDirectory()) {
159+
$uri = App::getInstance()->getEnvironment()->getRequest()->getUri();
160+
$parsed = parse_url($uri);
161+
parse_str($parsed['query'] ?? '', $queryParams);
162+
unset($queryParams['parent_id']);
163+
$uri = $parsed['scheme'] . '://' . $parsed['host'] . ($parsed['port'] ?? '') . $parsed['path'] . '?' . http_build_query($queryParams);
164+
165+
$label = '<label class="text-nowrap"><a class="inToolSidePanel" href="'.$uri.'&parent_id='.$media?->getId().'">' . $text . '</a></label>';
166+
}
167+
168+
return match (true) {
169+
is_null($media), $media?->isDirectory() => '<div class="media-element media-directory"><h2 style="height: 50px; margin: 0;">'.App::getInstance()->getHtmlRenderer()->getFAIcon('folder', 'regular').'</h2></div>',
170+
$media?->isImage() => '<div class="media-element media-image">' . $media->getThumb('50x50') . '</div>',
171+
default => '<div class="media-element media-file">' . $media->getMimeIcon() . '</div>',
172+
} . '<div>'.$label.'</div>';
173+
}
174+
175+
protected function getCollection(?int $parent_id): BaseCollection
176+
{
177+
$collection = MediaElement::getCollection();
178+
179+
if (is_numeric($parent_id)) {
180+
$collection->addCondition(['parent_id' => $parent_id]);
181+
} else {
182+
$collection->addCondition(['parent_id' => null]);
183+
}
184+
185+
$collection->addSelect('*')->addSelect('IF(mimetype =\'inode/directory\', 1, 0) AS is_dir');
186+
$collection->addOrder(['is_dir' => 'DESC'], 'start');
187+
188+
return $collection;
189+
}
190+
191+
}

js/src/admin/admin.init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
);
8888
});
8989

90-
$('a.inToolSidePanel[href]', $elem).click(function(evt){
90+
$(document).on('click', 'a.inToolSidePanel[href]', function(evt){
9191
const $btnElement = $(this);
9292

9393
if ($btnElement.attr('href') != '#') {

templates/frontend/commerce/giftcard_detail.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
<div class="row">
1313
<div class="col-md-10">
14-
1514
<h1 class="giftcard-title"><?php echo $object->getTitle();?></h1>
16-
<div class="giftcard-content"><?php echo $object->getContent();?></div>
15+
<div class="row">
16+
<?php if ($object->getMediaId()) : ?>
17+
<div class="giftcard-image col-2"><?= $object->getMedia()?->getThumb("400x300", class: 'img-fluid'); ?></div>
18+
<?php endif; ?>
19+
<div class="giftcard-content col"><?php echo $object->getContent();?></div>
20+
</div>
1721

1822
<?php if (($gallery = $object->getGallery()) && count($gallery)) : ?>
1923
<div class="page-gallery">

0 commit comments

Comments
 (0)