Skip to content

Commit

Permalink
Merge pull request #7 from leanormandon/main
Browse files Browse the repository at this point in the history
Fixs Header HighLights
  • Loading branch information
zawaze authored Jul 15, 2024
2 parents 4fde762 + 13beb8d commit 290529c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 94 deletions.
3 changes: 2 additions & 1 deletion Config/TheliaMain.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CREATE TABLE `header_highlights_i18n`
`title` VARCHAR(255),
`call_to_action` VARCHAR(255),
`url` VARCHAR(255),
`catchphrase` VARCHAR(255),
PRIMARY KEY (`id`,`locale`),
CONSTRAINT `header_highlights_i18n_fk_2edfcb`
FOREIGN KEY (`id`)
Expand All @@ -85,7 +86,7 @@ CREATE TABLE `header_highlights_image_i18n`
`chapo` TEXT,
`postscriptum` TEXT,
PRIMARY KEY (`id`,`locale`),
CONSTRAINT `header_highlights_image_i18n_fk_7068c5`
CONSTRAINT `header_highlights_image_i18n_fk_7068c6`
FOREIGN KEY (`id`)
REFERENCES `header_highlights_image` (`id`)
ON DELETE CASCADE
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.1.4</version>
<version>1.1.5</version>
<authors>
<author>
<name>Loïc MO</name>
Expand Down
3 changes: 2 additions & 1 deletion Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
<column name="title" size="255" type="VARCHAR" />
<column name="image_block" size="255" type="VARCHAR" />
<column name="call_to_action" size="255" type="VARCHAR" />
<column name="catchphrase" size="255" type="VARCHAR" />
<column name="display_type" size="255" type="VARCHAR" />
<column name="url" size="255" type="VARCHAR" />
<behavior name="timestampable" />
<foreign-key foreignTable="category" name="fk_header_highlights_category" onDelete="CASCADE" onUpdate="RESTRICT">
<reference foreign="id" local="category_id" />
</foreign-key>
<behavior name="i18n">
<parameter name="i18n_columns" value="title,call_to_action, url" />
<parameter name="i18n_columns" value="title,call_to_action, url,catchphrase" />
</behavior>
</table>
<table name="header_highlights_image">
Expand Down
1 change: 1 addition & 0 deletions Config/update/1.1.5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `header_highlights_i18n` ADD `catchphrase` VARCHAR(255);
86 changes: 39 additions & 47 deletions Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use HeaderHighlights\Model\HeaderHighlightsImage;
use HeaderHighlights\Model\HeaderHighlightsImageQuery;
use Exception;
use HeaderHighlights\Model\HeaderHighlightsQuery;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand All @@ -19,7 +20,6 @@
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Event\File\FileCreateOrUpdateEvent;
use Thelia\Core\Event\File\FileDeleteEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Form\TheliaFormFactory;
use Thelia\Core\Template\ParserContext;
Expand Down Expand Up @@ -48,32 +48,10 @@ public function updateImage(
try {
$formData = $this->validateForm($form)->getData();

$images = HeaderHighlightsImageQuery::create()
->useHeaderHighlightsQuery()
->filterByDisplayType($displayType)
->endUse()
->find();

$locale = $this->getCurrentEditionLocale();

/** @var HeaderHighlightsImage $carousel */
foreach ($images as $image) {
$this->headerHighlights($eventDispatcher, $requestStack, $image, $locale, $formData);
}

// If some images are missing, recreate them
for ($idx = 1; $idx <= HeaderHighlights::IMAGE_COUNT; $idx++) {
if (0 === HeaderHighlightsImageQuery::create()
->useHeaderHighlightsQuery()
->filterByDisplayType($displayType)
->filterByImageBlock($idx)
->endUse()
->count()
) {
$emptyHeaderHighLight = (new HeaderHighlightsModel())->createEmptyHeaderHighlights($idx, $displayType);
$emptyHeaderHighLight->save();
(new HeaderHighlightsImage())->createEmptyImage($emptyHeaderHighLight->getId())->save();
}
for ($i = 1; $i <=3; $i++) {
$this->saveHeaderHighlight($eventDispatcher, $formData, $locale, $i,$displayType);
}

return $this->generateSuccessRedirect($form);
Expand All @@ -95,24 +73,30 @@ public function updateImage(
/**
* @throws PropelException
*/
private function headerHighlights(
EventDispatcherInterface $eventDispatcher,
RequestStack $requestStack,
HeaderHighlightsImage $headerHighlightsImage,
private function saveHeaderHighlight( EventDispatcherInterface $eventDispatcher,
$formData,
$locale,
$formData
)
{
$id = $headerHighlightsImage->getId();
$idx,
$displayType
) {

$headerHighlights = HeaderHighlightsQuery::create()
->filterByDisplayType($displayType)
->filterByImageBlock($idx)
->findOne();
$headerHighlightsImage = HeaderHighlightsImageQuery::create()
->filterByHeaderHighlightsId($headerHighlights->getId())
->findOneOrCreate();

/** @var UploadedFile $fileBeingUploaded */
$fileBeingUploaded = $formData['image' . $id];
$fileBeingUploaded = $formData['image' . $idx];


if (null !== $fileBeingUploaded) {
$fileCreateOrUpdateEvent = new FileCreateOrUpdateEvent(1);
$fileCreateOrUpdateEvent->setModel($headerHighlightsImage);
$fileCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);

$fileCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);

if (empty($headerHighlightsImage->getFile())){
$eventNameImage = TheliaEvents::IMAGE_SAVE;
Expand All @@ -122,10 +106,18 @@ private function headerHighlights(
$fileCreateOrUpdateEvent->setOldModel($headerHighlightsImage);
$eventNameImage = TheliaEvents::IMAGE_UPDATE;
}
$eventDispatcher->dispatch(
$fileCreateOrUpdateEvent,
$eventNameImage
);
try {
$eventDispatcher->dispatch(
$fileCreateOrUpdateEvent,
$eventNameImage
);
} catch (\Exception $e) {
// On IMAGE_UPDATE, if image file has been deleted it will throw an error
$eventDispatcher->dispatch(
$fileCreateOrUpdateEvent,
TheliaEvents::IMAGE_SAVE
);
}

$langs = LangQuery::create()->find();

Expand All @@ -137,19 +129,19 @@ private function headerHighlights(
$headerHighlights = $headerHighlightsImage->getHeaderHighlights();

$headerHighlights
->setImageBlock($formData['image_block' . $id])
->setCategoryId($formData['category' . $id])
->setImageBlock($formData['image_block' . $idx])
->setCategoryId($formData['category' . $idx])
->setLocale($locale)
->setCallToAction($formData['call_to_action' . $id])
->setUrl($formData['url' . $id])
->setTitle($formData['title' . $id])
->setDisplayType($formData['display_type' . $id])
->setCallToAction($formData['call_to_action' . $idx])
->setUrl($formData['url' . $idx])
->setTitle($formData['title' . $idx])
->setDisplayType($formData['display_type' . $idx])
->setCatchphrase($formData['catchphrase' . $idx])
->save();

$headerHighlightsImage
->setLocale($locale)
->setTitle($formData['title' . $id])
->setDescription($formData['catchphrase' . $id])
->setTitle($formData['title' . $idx])
->save();

$this->emptyImageCache($headerHighlightsImage->getId());
Expand Down
12 changes: 2 additions & 10 deletions Form/AbstractHeaderHighlightsImageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,9 @@ protected function buildForm(): void

$categoriesArray[$categoryTitle] = $category->getId();
}

$images = HeaderHighlightsImageQuery::create()
->useHeaderHighlightsQuery()
->filterByDisplayType($this->getDisplayType())
->endUse()
->orderByPosition()
->find();

/** @var HeaderHighlightsImage $carousel */
foreach ($images as $image) {
$id = $image->getId();
for ($i = 1; $i <=3; $i++) {
$id = $i;

$this->formBuilder
->add(
Expand Down
40 changes: 19 additions & 21 deletions Loop/HeaderHighlightsLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,33 @@ public function parseResults(LoopResult $loopResult): LoopResult
$fileUrl = $originalFileUrl = null;

$headerHighlightsImage = $headerHighlights->getHeaderHighlightsImages()->getFirst();
if (!$this->getUseTheliaLibrary() && !empty($headerHighlightsImage->getFile())) {
$imgSourcePath = $headerHighlightsImage->getUploadDir() . DS . $headerHighlightsImage->getFile();

$event = new ImageEvent();
$event->setSourceFilepath($imgSourcePath)
->setCacheSubdirectory('carousel');
try {
if (!!$headerHighlightsImage && !$this->getUseTheliaLibrary() && !empty($headerHighlightsImage->getFile())) {
$imgSourcePath = $headerHighlightsImage->getUploadDir() . DS . $headerHighlightsImage->getFile();

// Dispatch image processing event
$this->dispatcher->dispatch($event, TheliaEvents::IMAGE_PROCESS);
$event = new ImageEvent();
$event->setSourceFilepath($imgSourcePath)
->setCacheSubdirectory('carousel');

// Dispatch image processing event
$this->dispatcher->dispatch($event, TheliaEvents::IMAGE_PROCESS);

$fileUrl = $event->getFileUrl();
$originalFileUrl = $event->getOriginalFileUrl();
}

} catch (\Exception $e) {

$fileUrl = $event->getFileUrl();
$originalFileUrl = $event->getOriginalFileUrl();
}

$loopResultRow
->set('ID', $headerHighlights->getId())
->set('IMAGE_ID', $headerHighlightsImage?->getId())
->set('TITLE', $headerHighlights->getVirtualColumn("i18n_TITLE"))
->set('CATEGORY', $headerHighlights->getCategoryId())
->set('CTA', $headerHighlights->getVirtualColumn("i18n_CALL_TO_ACTION"))
->set('CATCHPHRASE', $headerHighlights->getVirtualColumn("header_highlights_image_i18n_DESCRIPTION"))
->set('CATCHPHRASE', $headerHighlights->getVirtualColumn("i18n_CATCHPHRASE"))
->set('URL', $headerHighlights->getVirtualColumn("i18n_URL"))
->set('IMAGE_URL', $fileUrl)
->set('ORIGINAL_IMAGE_URL', $originalFileUrl)
Expand Down Expand Up @@ -112,19 +119,10 @@ public function buildModelCriteria(): ModelCriteria
[
'TITLE',
'CALL_TO_ACTION',
'URL'
'URL',
"CATCHPHRASE"
]
);
$this->configureI18nProcessing(
$query,
[
'TITLE',
'CHAPO',
'DESCRIPTION',
'POSTSCRIPTUM'
],
HeaderHighlightsImageTableMap::getTableMap()->getName()
);

$query
->filterByDisplayType($this->getDisplayType())
Expand Down
54 changes: 41 additions & 13 deletions templates/backOffice/default/module_configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,78 @@
<div class="alert alert-danger">{$form_error_message}</div>
{/if}

{$images = []}
{loop type="header_highlights_loop" name="header_highlights_loop" lang=$edit_language_id display_type=$screen}
{$idx = $ID}
{$images[$IMAGE_BLOCK] = [
'ORIGINAL_IMAGE_URL' => $ORIGINAL_IMAGE_URL,
'IMAGE_URL' => $IMAGE_URL,
'CATCHPHRASE' => $CATCHPHRASE,
'IMAGE_BLOCK' => $IMAGE_BLOCK,
'TITLE' => $TITLE,
'CATEGORY' => $CATEGORY,
'CTA' => $CTA,
'URL' => $URL,
'ID' => $ID
]}
{/loop}
{* {$images|@var_dump}*}
{for $position=1 to 3}
{if !$images[$position]}
{$images[$position] = [
'ORIGINAL_IMAGE_URL' => '',
'IMAGE_URL' => '',
'CATCHPHRASE' => '',
'IMAGE_BLOCK' => $position,
'TITLE' => '',
'CATEGORY' => '',
'CTA' => '',
'URL' => '',
'ID' => ''
]}
{/if}
{$idx = $position}

{render_form_field field="display_type`$idx`" value=$screen}

<div class="well well-sm">
<div class="row">
<div class="col-md-4">
{render_form_field field="title`$idx`" value=$TITLE}
{render_form_field field="category`$idx`" value=$CATEGORY}
{render_form_field field="call_to_action`$idx`" value=$CTA}
{render_form_field field="title`$idx`" value=$images[$idx]['TITLE']}
{render_form_field field="category`$idx`" value=$images[$idx]['CATEGORY']}
{render_form_field field="call_to_action`$idx`" value=$images[$idx]['CTA']}

<div class="mb-2 header-highlights-image">
{if $ORIGINAL_IMAGE_URL}
<a href="{$ORIGINAL_IMAGE_URL}" target="_blank">
<img src="{$IMAGE_URL}" alt="header-highlights-image-{$ID}">
{if $images[$idx]['ORIGINAL_IMAGE_URL']}
<a href="{$images[$idx]['ORIGINAL_IMAGE_URL']}" target="_blank">
<img src="{$images[$idx]['IMAGE_URL']}" alt="header-highlights-image-{$images[$idx]['ID']}">
</a>
{else}
<div class="alert alert-warning">{intl l="No image uploaded" d='headerhighlights.bo.default'}</div>
{/if}
</div>
{render_form_field field="image`$idx`" value=$URL}
{render_form_field field="image`$idx`" value=$images[$idx]['URL']}
</div>
<div class="col-md-4">
{render_form_field field="url`$idx`" value=$URL}
{render_form_field field="catchphrase`$idx`" value=$CATCHPHRASE}
{render_form_field field="url`$idx`" value=$images[$idx]['URL']}
{render_form_field field="catchphrase`$idx`" value=$images[$idx]['CATCHPHRASE']}
</div>
<div class="col-md-4">
<div class="form-group text-center">
{render_form_field field="image_block`$idx`" value=$IMAGE_BLOCK}
{render_form_field field="image_block`$idx`" value=$images[$idx]['IMAGE_BLOCK']}

<label class="control-label text-center" style="display: block">
{intl l="Image position" d='headerhighlights.bo.default'}
</label>
<div class="grid-{$screen}">
{for $img=1 to $imageCount}
<div class="grid-item grid-item-{$screen} {if $IMAGE_BLOCK == $img}selected{/if}">{$img}</div>
<div class="grid-item grid-item-{$screen} {if $images[$idx]['IMAGE_BLOCK'] == $img}selected{/if}">{$img}</div>
{/for}
</div>
</div>
</div>
</div>
</div>
{/loop}
{/for}
</form>
{/form}
</div>
Expand Down

0 comments on commit 290529c

Please sign in to comment.