Skip to content

Commit

Permalink
extend methods and add svgconfigiconset
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimdoerr committed Nov 24, 2024
1 parent 2512df4 commit 856adca
Show file tree
Hide file tree
Showing 4 changed files with 1,469 additions and 40 deletions.
135 changes: 135 additions & 0 deletions lib/MFragment/Helper/BootstrapHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
namespace FriendsOfRedaxo\MFragment\Helper;

class BootstrapHelper
{
public static function getPaddingClasses(array $settings, float $mdFactor = 1, float $lgFactor = 1.25): array {
$class = [];
if (!empty($settings['padding'])) {
$class['padding-md'] = 'p-md-' . ceil($settings['padding'] * $mdFactor);
$class['padding-lg'] = 'p-lg-' . ceil($settings['padding'] * $lgFactor);
}
if (!empty($settings['padding-y'])) {
$class['padding-y-md'] = 'py-md-' . ceil($settings['padding-y'] * $mdFactor);
$class['padding-y-lg'] = 'py-lg-' . ceil($settings['padding-y'] * $lgFactor);
}
if (!empty($settings['padding-x'])) {
$class['padding-x-md'] = 'px-md-' . ceil($settings['padding-x'] * $mdFactor);
$class['padding-x-lg'] = 'px-lg-' . ceil($settings['padding-x'] * $lgFactor);
}
if (!empty($settings['padding-left'])) {
$class['padding-left-md'] = 'ps-md-' . ceil($settings['padding-left'] * $mdFactor);
$class['padding-left-lg'] = 'ps-lg-' . ceil($settings['padding-left'] * $lgFactor);
}
if (!empty($settings['padding-right'])) {
$class['padding-right-md'] = 'pe-md-' . ceil($settings['padding-right'] * $mdFactor);
$class['padding-right-lg'] = 'pe-lg-' . ceil($settings['padding-right'] * $lgFactor);
}
if (!empty($settings['padding-top'])) {
$class['padding-top-md'] = 'pt-md-' . ceil($settings['padding-top'] * $mdFactor);
$class['padding-top-lg'] = 'pt-lg-' . ceil($settings['padding-top'] * $lgFactor);
}
if (!empty($settings['padding-bottom'])) {
$class['padding-bottom-md'] = 'pb-md-' . ceil($settings['padding-bottom'] * $mdFactor);
$class['padding-bottom-lg'] = 'pb-lg-' . ceil($settings['padding-bottom'] * $lgFactor);
}
return $class;
}

public static function getMarginClasses(array $settings, float $mdFactor = 1, float $lgFactor = 1.25): array {
$class = [];
if (!empty($settings['margin'])) {
$class['margin-md'] = 'm-md-' . ceil($settings['margin'] * $mdFactor);
$class['margin-lg'] = 'm-lg-' . ceil($settings['margin'] * $lgFactor);
}
if (!empty($settings['margin-y'])) {
$class['margin-y-md'] = 'my-md-' . ceil($settings['margin-y'] * $mdFactor);
$class['margin-y-lg'] = 'my-lg-' . ceil($settings['margin-y'] * $lgFactor);
}
if (!empty($settings['margin-x'])) {
$class['margin-x-md'] = 'mx-md-' . ceil($settings['margin-x'] * $mdFactor);
$class['margin-x-lg'] = 'mx-lg-' . ceil($settings['margin-x'] * $lgFactor);
}
if (!empty($settings['margin-left'])) {
$class['margin-left-md'] = 'ms-md-' . ceil($settings['margin-left'] * $mdFactor);
$class['margin-left-lg'] = 'ms-lg-' . ceil($settings['margin-left'] * $lgFactor);
}
if (!empty($settings['margin-right'])) {
$class['margin-right-md'] = 'me-md-' . ceil($settings['margin-right'] * $mdFactor);
$class['margin-right-lg'] = 'me-lg-' . ceil($settings['margin-right'] * $lgFactor);
}
if (!empty($settings['margin-top'])) {
$class['margin-top-md'] = 'mt-md-' . ceil($settings['margin-top'] * $mdFactor);
$class['margin-top-lg'] = 'mt-lg-' . ceil($settings['margin-top'] * $lgFactor);
}
if (!empty($settings['margin-bottom'])) {
$class['margin-bottom-md'] = 'mb-md-' . ceil($settings['margin-bottom'] * $mdFactor);
$class['margin-bottom-lg'] = 'mb-lg-' . ceil($settings['margin-bottom'] * $lgFactor);
}
return $class;
}

public static function getColumnClasses(array $settings, float $mdFactor = 0.75, float $lgFactor = 1): array
{
$class = [
'default' => 'col-12'
];

if (!empty($settings['col'])) {
$mdValue = min(12, max(1, ceil($settings['col'] * $mdFactor)));
$lgValue = min(12, max(1, ceil($settings['col'] * $lgFactor)));

$class['col-md'] = 'col-md-' . $mdValue;
$class['col-lg'] = 'col-lg-' . $lgValue;
}

return array_filter($class);
}

public static function getPositionClasses(array $settings): array
{
$class = [];
if (!empty($settings['position'])) {
switch ($settings['position']) {
case 'left':
$class['position'] = 'me-auto';
break;
case 'center':
$class['position'] = 'm-auto';
break;
case 'right':
$class['position'] = 'ms-auto';
break;
}
}
return $class;
}

public static function getContainerClasses(array $settings): array
{
if (empty($settings['fitting'])) {
return ['default' => 'container-lg'];
}

$types = [
'smallBox' => 'container-narrow',
'box' => 'container-lg',
'fluid' => 'container-fluid',
'fluidBox' => 'container-fluid'
];

return [
'default' => $types[$settings['fitting']] ?? ''
];
}

public static function isContainerDataTxtFluid(array $settings): string {
if (!empty($settings['fitting'])) {
switch ($settings['fitting']) {
case 'fluidBox':
return 'false';
}
}
return 'true';
}
}
79 changes: 79 additions & 0 deletions lib/MFragment/Helper/MFragmentMFormInputsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace FriendsOfRedaxo\MFragment\Helper;

use FriendsOfRedaxo\MFragment\Utils\SVGConfigIconSet;

class MFragmentMFormInputsHelper
{
/**
* @param string $iconType [DoubleContainerTextIcon]
* @param array $options [0 => 'None', 1 => 'Small', 2 => 'Medium', 3 => 'Large' ... ]
*/
public static function getIconPaddingOptions(string $iconType, array $options = [], $iconLabelSuffix = '', $iconLabelPrefix = ''): array
{
$itemPaddingOptions = [];
foreach ($options as $i => $label) {
$getMethod = 'get' . $iconType;
if (method_exists(SVGConfigIconSet::class, $getMethod)) {
switch($iconType) {
case 'DoubleContainerTextIcon':
$itemPaddingOptions[$i] = ['svgIconSet' => SVGConfigIconSet::$getMethod($iconLabelPrefix . $label . $iconLabelSuffix, ['top' => 200, 'containerHeight' => (SVGConfigIconSet::config['container']['height'] + floatval($i * 350) - 500)], false, ['containerHeight' => (SVGConfigIconSet::config['container']['height'] - 250)], null, false, ['containerHeight' => (SVGConfigIconSet::config['container']['height'] - 250)], floatval($i * 180) + 1300), 'label' => $label];
break;
case 'DoubleContainerTextLayoutImgIcon':
$topTextPosition = floatval($i * 180) + 1300;
if ($i == 0) $topTextPosition = 2600;
$itemPaddingOptions[$i] = ['svgIconSet' => SVGConfigIconSet::$getMethod($iconLabelPrefix . $label . $iconLabelSuffix, 'left', ['top' => 200, 'containerHeight' => (SVGConfigIconSet::config['container']['height'] + floatval($i * 350) - 500)], null, null, ['containerHeight' => (SVGConfigIconSet::config['container']['height'] - 250)], null, null, $topTextPosition), 'label' => $label];
break;
}
}
}
return $itemPaddingOptions;
}

/**
* @param string $iconType [DoubleContainerTextIcon|DoubleContainerTextLayoutImgIcon]
* @param array $options [0 => 'None', 1 => 'Small', 2 => 'Medium', 3 => 'Large' ... ]
*/
public static function getIconMarginOptions(string $iconType, array $options = [], $iconLabelSuffix = '', $iconLabelPrefix = ''): array
{
$itemMarginOptions = [];
foreach ($options as $i => $label) {
$getMethod = 'get' . $iconType;
if (method_exists(SVGConfigIconSet::class, $getMethod)) {
switch($iconType) {
case 'DoubleContainerTextIcon':
$itemMarginOptions[$i] = ['svgIconSet' => SVGConfigIconSet::$getMethod($iconLabelPrefix . $label . $iconLabelSuffix, ['top' => 200], false, null, ['top' => intval($i + 1) * 270], false, null, floatval($i * 90) + 1380), 'label' => $label];
break;
case 'DoubleContainerTextLayoutImgIcon':
case 'DoubleContainerMasonryIcon':
$itemMarginOptions[$i] = ['svgIconSet' => SVGConfigIconSet::$getMethod($iconLabelPrefix . $label . $iconLabelSuffix, 'left', ['top' => 200], null, null, ['top' => intval($i+1) * 270], null, null, floatval($i * 90) + 1380), 'label' => $label];
break;
}
}
}
return $itemMarginOptions;
}

/**
* @param string $containerType [ContainerTextFluidIcon|ContainerTextLayoutImgIcon]
* @param array $options ['smallBox' => 'Small', 'box' => 'Box', 'fluid' => 'Fluid', 'fluidBox' => 'Fluid Box']
*/
public static function getContainerTypeOptions(string $containerType, array $options = [], $iconLabelSuffix = '', $iconLabelPrefix = ''): array
{
$containerOptions = [];
foreach ($options as $i => $label) {
$getMethod = 'get' . $containerType;
if (method_exists(SVGConfigIconSet::class, $getMethod)) {
switch ($containerType) {
case 'ContainerTextFluidIcon':
case 'ContainerTextLayoutImgIcon':
case 'DoubleContainerMasonryIcon':
$containerOptions[$i] = ['svgIconSet' => SVGConfigIconSet::$getMethod($iconLabelPrefix . $label . $iconLabelSuffix, 'container' . ucfirst($i), null, false), 'label' => $label];
break;
}
}
}
return $containerOptions;
}
}
53 changes: 13 additions & 40 deletions lib/MFragment/MFragmentElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ abstract class MFragmentElements
/** @var MFragmentItem[] */
public array $items = [];

public function addTagElement(string $tag, array|string $content, array $attributes = []): self
public function addTagElement(string $tag, MFragment|array|string $content, array $attributes = []): self
{
$this->items[] = new MFragmentItem($tag, false, $content, $this->getConfig($attributes), $this->getAttributes($attributes));
return $this;
}

public function addFragmentElement(string $element, array|string $content, array $config = []): self
public function addFragmentElement(string $element, MFragment|array|string|null $content, array $config = []): self
{
if ($content === null) return $this;
$this->items[] = new MFragmentItem(false, $element, $content, $this->getConfig($config), $this->getAttributes($config));
return $this;
}
Expand All @@ -44,35 +45,15 @@ private function getConfig(array $arr): array
return $arr;
}

public function addSection(MFragment|array|string $content, array|bool $sectionConfig = [], array|bool $containerConfig = []): self
public function addSection(MFragment|array|string $content, array $config = []): self
{
$config = [];
if (is_array($sectionConfig)) {
$config['showSection'] = true;
$config['section'] = $sectionConfig;
} else {
$config['showSection'] = $sectionConfig;
}
if (is_array($containerConfig)) {
$config['showContainer'] = true;
$config['container'] = $containerConfig;
} else {
$config['showContainer'] = $containerConfig;
}
if ($content instanceof MFragment) {
$content = $content->items;
}
$this->addFragmentElement("bootstrap/section", $content, $config);
return $this;
}

/**
* Add a single column
*
* @param mixed $content Content of the column
* @param array $config Configuration for the column
* @return $this
*/
public function addColumn($content, array $config = []): self
{
$defaultConfig = [
Expand Down Expand Up @@ -122,35 +103,27 @@ public function addColumns(MFragment $columns, array $config = []): self

public function addImages(array $images, array $config = []): self
{
$this->addFragmentElement('bootstrap/images', [
'images' => $images,
'config' => $config
]);

return $this;
return $this->addFragmentElement('bootstrap/images', $images, $config);
}

public function addCard(MFragment|array|string $header = null, MFragment|array|string $body = null, MFragment|array|string $footer = null, array $config = [], $headerConfig = [], array $bodyConfig = [], array $footerConfig = []): self
public function addCard(MFragment|array|string $header = null, MFragment|array|string $body = null, MFragment|array|string $footer = null, array $config = []): self
{
$this->addFragmentElement(
"bootstrap/card",
[
'header' => array_merge(['content' => $header], $headerConfig),
'body' => array_merge(['content' => $body], $bodyConfig),
'footer' => array_merge(['content' => $footer], $footerConfig),
'header' => ['content' => $header],
'body' => ['content' => $body],
'footer' => ['content' => $footer],
],
[
'card' => $config,
'header' => $headerConfig,
'body' => $bodyConfig,
'footer' => $footerConfig,
]
$config
);
return $this;
}

public function addAccordion(array $items, array $config = []): self
public function addAccordion(?array $items, array $config = []): self
{
if (!is_array($items)) return $this;

$processedItems = [];
foreach ($items as $item) {
$processedItems[] = [
Expand Down
Loading

0 comments on commit 856adca

Please sign in to comment.