|
| 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 | +} |
0 commit comments