|
| 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\Base\Controllers\Admin\Commerce; |
| 15 | + |
| 16 | +use Degami\Basics\Exceptions\BasicException; |
| 17 | +use DI\DependencyException; |
| 18 | +use DI\NotFoundException; |
| 19 | +use App\Base\Abstracts\Controllers\AdminManageProductsPage; |
| 20 | +use App\Base\Models\GiftCard; |
| 21 | +use Degami\PHPFormsApi as FAPI; |
| 22 | +use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException; |
| 23 | +use App\Site\Models\MediaElement; |
| 24 | +use App\Base\Models\TaxClass; |
| 25 | +use App\Site\Models\MediaElement as Media; |
| 26 | + |
| 27 | +/** |
| 28 | + * "Gift Cards" Admin Page |
| 29 | + */ |
| 30 | +class GiftCards extends AdminManageProductsPage |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var string page title |
| 34 | + */ |
| 35 | + protected ?string $page_title = 'Gift Cards'; |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + * |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + public static function getObjectClass(): string |
| 43 | + { |
| 44 | + return GiftCard::class; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + * |
| 51 | + * @return array|null |
| 52 | + */ |
| 53 | + public static function getAdminPageLink() : array|null |
| 54 | + { |
| 55 | + return [ |
| 56 | + 'permission_name' => static::getAccessPermission(), |
| 57 | + 'route_name' => static::getPageRouteName(), |
| 58 | + 'icon' => 'gift', |
| 59 | + 'text' => 'Gift Cards', |
| 60 | + 'section' => 'commerce', |
| 61 | + 'order' => 70, |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + * |
| 68 | + * @param FAPI\Form $form |
| 69 | + * @param array &$form_state |
| 70 | + * @return FAPI\Form |
| 71 | + * @throws BasicException |
| 72 | + * @throws DependencyException |
| 73 | + * @throws NotFoundException |
| 74 | + * @throws PhpfastcacheSimpleCacheException |
| 75 | + */ |
| 76 | + public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\Form |
| 77 | + { |
| 78 | + $type = $this->getRequest()->query->get('action') ?? 'list'; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var GiftCard $product |
| 82 | + */ |
| 83 | + $product = $this->getObject(); |
| 84 | + |
| 85 | + $form->addField('action', [ |
| 86 | + 'type' => 'value', |
| 87 | + 'value' => $type, |
| 88 | + ]); |
| 89 | + |
| 90 | + switch ($type) { |
| 91 | + case 'edit': |
| 92 | + $this->addActionLink( |
| 93 | + 'media-btn', |
| 94 | + 'media-btn', |
| 95 | + $this->getHtmlRenderer()->getIcon('image') . ' ' . $this->getUtils()->translate('Media', locale: $this->getCurrentLocale()), |
| 96 | + $this->getUrl('crud.app.site.controllers.admin.json.downloadablemedia', ['id' => $this->getRequest()->query->get('product_id')]) . '?product_id=' . $this->getRequest()->query->get('product_id') . '&action=new', |
| 97 | + 'btn btn-sm btn-light inToolSidePanel' |
| 98 | + ); |
| 99 | + |
| 100 | + case 'new': |
| 101 | + |
| 102 | + $product_title = $product_content = $product_media = ''; |
| 103 | + $product_price = 0.0; |
| 104 | + if ($product->isLoaded()) { |
| 105 | + $product_title = $product->title; |
| 106 | + $product_content = $product->content; |
| 107 | + $product_media = $product->media_id; |
| 108 | + $product_price = $product->price; |
| 109 | + } |
| 110 | + |
| 111 | + $medias = ['' => '']; |
| 112 | + foreach (MediaElement::getCollection() as $media) { |
| 113 | + /** @var MediaElement $media */ |
| 114 | + if ($media->isDirectory()) { |
| 115 | + comtinue: |
| 116 | + } |
| 117 | + $medias[$media->getId()] = $media->getFilename(); |
| 118 | + } |
| 119 | + $tax_classes = ['' => '-- Select --']; |
| 120 | + foreach(TaxClass::getCollection() as $tax_class) { |
| 121 | + /** @var TaxClass $tax_class */ |
| 122 | + $tax_classes[$tax_class->getId()] = $tax_class->getClassName(); |
| 123 | + } |
| 124 | + |
| 125 | + $form->addField('title', [ |
| 126 | + 'type' => 'textfield', |
| 127 | + 'title' => 'Title', |
| 128 | + 'default_value' => $product_title, |
| 129 | + 'validate' => ['required'], |
| 130 | + ])->addField('content', [ |
| 131 | + 'type' => 'tinymce', |
| 132 | + 'title' => 'Content', |
| 133 | + 'tinymce_options' => DEFAULT_TINYMCE_OPTIONS, |
| 134 | + 'default_value' => $product_content, |
| 135 | + 'rows' => 20, |
| 136 | + ])->addField('media_id', [ |
| 137 | + 'type' => 'select', |
| 138 | + 'title' => 'Media', |
| 139 | + 'default_value' => $product_media, |
| 140 | + 'options' => $medias, |
| 141 | + 'validate' => ['required'], |
| 142 | + ])->addField('price', [ |
| 143 | + 'type' => 'number', |
| 144 | + 'title' => 'Price', |
| 145 | + 'default_value' => $product_price, |
| 146 | + 'min' => '0.00', |
| 147 | + 'max' => '1000000.00', |
| 148 | + 'step' => '0.01', |
| 149 | + 'validate' => ['required'], |
| 150 | + ])->addField('tax_class_id', [ |
| 151 | + 'type' => 'select', |
| 152 | + 'title' => 'Tax Class', |
| 153 | + 'default_value' => $product->getTaxClassId(), |
| 154 | + 'options' => $tax_classes, |
| 155 | + 'validate' => ['required'], |
| 156 | + ]); |
| 157 | + |
| 158 | + $this->addFrontendFormElements($form, $form_state); |
| 159 | + $this->addSeoFormElements($form, $form_state); |
| 160 | + |
| 161 | + $this->addSubmitButton($form); |
| 162 | + |
| 163 | + break; |
| 164 | + |
| 165 | + case 'delete': |
| 166 | + $this->fillConfirmationForm('Do you confirm the deletion of the selected element?', $form); |
| 167 | + break; |
| 168 | + |
| 169 | + case 'media_deassoc': |
| 170 | + $media = $this->containerCall([Media::class, 'load'], ['id' => $this->getRequest()->query->get('media_id')]); |
| 171 | + $form->addField('product_id', [ |
| 172 | + 'type' => 'hidden', |
| 173 | + 'default_value' => $product->id, |
| 174 | + ])->addField('media_id', [ |
| 175 | + 'type' => 'hidden', |
| 176 | + 'default_value' => $media->id, |
| 177 | + ])->addField('confirm', [ |
| 178 | + 'type' => 'markup', |
| 179 | + 'value' => 'Do you confirm the disassociation of the "' . $product->title . '" product from the media ID: ' . $media->id . '?', |
| 180 | + 'suffix' => '<br /><br />', |
| 181 | + ])->addMarkup('<a class="btn btn-danger btn-sm" href="' . $this->getUrl('crud.app.site.controllers.admin.json.mediadownloadableproducts', ['id' => $media->id]) . '?media_id=' . $media->id . '&action=downloadable_product_deassoc">Cancel</a>'); |
| 182 | + |
| 183 | + $this->addSubmitButton($form, true); |
| 184 | + break; |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + return $form; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * {@inheritdoc} |
| 193 | + * |
| 194 | + * @param FAPI\Form $form |
| 195 | + * @param array &$form_state |
| 196 | + * @return bool|string |
| 197 | + */ |
| 198 | + public function formValidate(FAPI\Form $form, &$form_state): bool|string |
| 199 | + { |
| 200 | + //$values = $form->values(); |
| 201 | + // @todo : check if page language is in page website languages? |
| 202 | + return true; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * {@inheritdoc} |
| 207 | + * |
| 208 | + * @param FAPI\Form $form |
| 209 | + * @param array &$form_state |
| 210 | + * @return mixed |
| 211 | + * @throws BasicException |
| 212 | + * @throws DependencyException |
| 213 | + * @throws NotFoundException |
| 214 | + */ |
| 215 | + public function formSubmitted(FAPI\Form $form, &$form_state): mixed |
| 216 | + { |
| 217 | + /** |
| 218 | + * @var GiftCard $product |
| 219 | + */ |
| 220 | + $product = $this->getObject(); |
| 221 | + |
| 222 | + $values = $form->values(); |
| 223 | + |
| 224 | + switch ($values['action']) { |
| 225 | + case 'new': |
| 226 | + |
| 227 | + // intentional fall trough |
| 228 | + // no break |
| 229 | + case 'edit': |
| 230 | + |
| 231 | + $product->setUrl($values['frontend']['url']); |
| 232 | + $product->setTitle($values['title']); |
| 233 | + $product->setLocale($values['frontend']['locale']); |
| 234 | + $product->setContent($values['content']); |
| 235 | + $product->setWebsiteId($values['frontend']['website_id']); |
| 236 | + $product->setMediaId($values['media_id']); |
| 237 | + $product->setPrice((float)$values['price']); |
| 238 | + $product->setTaxClassId($values['tax_class_id']); |
| 239 | + |
| 240 | + $this->setAdminActionLogData($product->getChangedData()); |
| 241 | + |
| 242 | + $product->persist(); |
| 243 | + |
| 244 | + $this->addSuccessFlashMessage($this->getUtils()->translate("Product Saved.")); |
| 245 | + break; |
| 246 | + case 'delete': |
| 247 | + $product->delete(); |
| 248 | + |
| 249 | + $this->setAdminActionLogData('Deleted product ' . $product->getId()); |
| 250 | + |
| 251 | + $this->addInfoFlashMessage($this->getUtils()->translate("Product Deleted.")); |
| 252 | + |
| 253 | + break; |
| 254 | + case 'media_deassoc': |
| 255 | + if ($values['media_id']) { |
| 256 | + $media = $this->containerCall([Media::class, 'load'], ['id' => $values['media_id']]); |
| 257 | + $product->removeMedia($media); |
| 258 | + } |
| 259 | + break; |
| 260 | + } |
| 261 | + |
| 262 | + return $this->refreshPage(); |
| 263 | + } |
| 264 | +} |
0 commit comments