Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for forms and controllers + event listeners #23

Open
wants to merge 17 commits into
base: develop-v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/Controller/ContentElement/ColumnStartController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MadeYourDay\RockSolidColumns\Controller\ContentElement;

use Contao\BackendTemplate;
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Image\Studio\Studio;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\StringUtil;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Column start content element
*
* @author Martin Auswöger <[email protected]>
*
* @ContentElement("rs_column_start", category="rs_columns")
*/
class ColumnStartController extends AbstractContentElementController
{
private ScopeMatcher $scopeMatcher;
private Studio $studio;

public function __construct(ScopeMatcher $scopeMatcher, Studio $studio)
{
$this->scopeMatcher = $scopeMatcher;
$this->studio = $studio;
}

protected function getResponse(Template $template, ContentModel $model, Request $request): ?Response
{
if ($this->scopeMatcher->isBackendRequest($request)) {
$backendTemplate = new BackendTemplate('be_wildcard');
$backendTemplate->title = $template->headline;
return new Response($backendTemplate->parse());
}

$classes = array('rs-column');
$styles = array();
$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;

if (isset($GLOBALS['TL_RS_COLUMNS'][$parentKey]) && $GLOBALS['TL_RS_COLUMNS'][$parentKey]['active']) {

$GLOBALS['TL_RS_COLUMNS'][$parentKey]['active'] = false;
$GLOBALS['TL_RS_COLUMNS'][$parentKey]['count']++;

$count = $GLOBALS['TL_RS_COLUMNS'][$parentKey]['count'];
foreach ($GLOBALS['TL_RS_COLUMNS'][$parentKey]['config'] as $name => $media) {
$classes = array_merge($classes, $media[($count - 1) % count($media)]);
if ($count - 1 < count($media)) {
$classes[] = '-' . $name . '-first-row';
}
}

}
else {
trigger_error('Missing column wrapper start element before column start element ID ' . $model->id . '.', E_USER_WARNING);
}

if ($model->rs_column_color_inverted) {
$classes[] = '-color-inverted';
}

if ($model->rs_column_background) {

$backgroundColor = StringUtil::deserialize($model->rs_column_background_color);
if (is_array($backgroundColor) && $backgroundColor[0]) {
$styles[] = 'background-color: #' . $backgroundColor[0] . ';';
}

if (trim($model->rs_column_background_image)) {
$figure = $this->studio
->createFigureBuilder()
->fromUuid($model->rs_column_background_image ?: '')
->setSize($model->rs_column_background_image_size)
->enableLightbox(false)
->buildIfResourceExists()
;
if (null !== $figure) {
$styles[] = 'background-image: url(&quot;' . $figure->getImage()->getImageSrc(true) . '&quot;);';
}
}

if ($model->rs_column_background_size) {
$styles[] = 'background-size: ' . $model->rs_column_background_size . ';';
}

if ($model->rs_column_background_position) {
$styles[] = 'background-position: ' . $model->rs_column_background_position . ';';
}

if ($model->rs_column_background_repeat) {
$styles[] = 'background-repeat: ' . $model->rs_column_background_repeat . ';';
}

}

$template->class .= ' ' . implode(' ', $classes);
$template->style = implode(' ', $styles);

return new Response($template->parse());
}
}
54 changes: 54 additions & 0 deletions src/Controller/ContentElement/ColumnStopController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MadeYourDay\RockSolidColumns\Controller\ContentElement;

use Contao\BackendTemplate;
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Column stop content element
*
* @author Martin Auswöger <[email protected]>
*
* @ContentElement("rs_column_stop", category="rs_columns")
*/
class ColumnStopController extends AbstractContentElementController
{
private ScopeMatcher $scopeMatcher;

public function __construct(ScopeMatcher $scopeMatcher)
{
$this->scopeMatcher = $scopeMatcher;
}

protected function getResponse(Template $template, ContentModel $model, Request $request): ?Response
{
if ($this->scopeMatcher->isBackendRequest($request)) {
$backendTemplate = new BackendTemplate('be_wildcard');
return new Response($backendTemplate->parse());
}

$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;

if (isset($GLOBALS['TL_RS_COLUMNS'][$parentKey]) && !$GLOBALS['TL_RS_COLUMNS'][$parentKey]['active']) {
$GLOBALS['TL_RS_COLUMNS'][$parentKey]['active'] = true;
}
else {
trigger_error('Missing column start element before column stop element ID ' . $model->id . '.', E_USER_WARNING);
}

return new Response($template->parse());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,42 @@
* file that was distributed with this source code.
*/

namespace MadeYourDay\RockSolidColumns\Element;
namespace MadeYourDay\RockSolidColumns\Controller\ContentElement;

use Contao\BackendTemplate;
use Contao\ContentElement;
use Contao\FrontendTemplate;
use Contao\System;
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Columns start content element
*
* @author Martin Auswöger <[email protected]>
*
* @ContentElement("rs_columns_start", category="rs_columns")
*/
class ColumnsStart extends ContentElement
class ColumnsStartController extends AbstractContentElementController
{
/**
* @var string Template
*/
protected $strTemplate = 'ce_rs_columns_start';
private ScopeMatcher $scopeMatcher;

/**
* Parse the template
*
* @return string Parsed element
*/
public function generate()
public function __construct(ScopeMatcher $scopeMatcher)
{
$this->scopeMatcher = $scopeMatcher;
}

protected function getResponse(Template $template, ContentModel $model, Request $request): ?Response
{
if (System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest(System::getContainer()->get('request_stack')->getCurrentRequest() ?? Request::create(''))) {
return parent::generate();
if ($this->scopeMatcher->isBackendRequest($request)) {
$backendTemplate = new BackendTemplate('be_wildcard');
$backendTemplate->title = $template->headline;
return new Response($backendTemplate->parse());
}

$parentKey = ($this->arrData['ptable'] ?: 'tl_article') . '__' . $this->arrData['pid'];
$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;

$htmlPrefix = '';

Expand Down Expand Up @@ -70,24 +74,21 @@ public function generate()
$GLOBALS['TL_RS_COLUMNS'][$parentKey] = array(
'active' => true,
'count' => 0,
'config' => static::getColumnsConfiguration($this->arrData),
'config' => static::getColumnsConfiguration($model->row()),
);

if (!is_array($this->cssID)) {
$this->cssID = array('', '');
}
$this->arrData['cssID'][1] .= ' ' . static::getWrapperClassName($this->arrData);
$template->class .= ' ' . static::getWrapperClassName($model->row());

return $htmlPrefix . parent::generate();
return new Response($htmlPrefix . $template->parse());
}

/**
* Generate the columns configuration
*
* @param array $data Data array
* @return array Columns configuration
* @return array Columns configuration
*/
public static function getColumnsConfiguration(array $data)
public static function getColumnsConfiguration(array $data): array
rabauss marked this conversation as resolved.
Show resolved Hide resolved
{
$config = array();
$lastColumns = null;
Expand All @@ -99,27 +100,25 @@ public static function getColumnsConfiguration(array $data)

foreach (array('xlarge', 'large', 'medium', 'small', 'xsmall') as $media) {

$columns = isset($data['rs_columns_' . $media])
? $data['rs_columns_' . $media]
: null;
$columns = $data['rs_columns_'.$media] ?? null;
if (!$columns) {
$columns = $lastColumns ?: '2';
}
$lastColumns = $columns;

$columns = array_map(function($value) {
$columns = array_map(static function($value) {
return (int)$value ?: 1;
}, explode('-', $columns));

if (count($columns) === 1 && $columns[0] > 1) {
$columns = array_fill(0, (int)$columns[0], '1');
}

$columnsTotal = array_reduce($columns, function($a, $b) {
$columnsTotal = array_reduce($columns, static function($a, $b) {
return $a + $b;
});
$classes = array();
foreach ($columns as $key => $column) {
foreach ($columns as $column) {
$classes[] = array('-' . $media . '-col-' . $columnsTotal . '-' . $column);
}
$classes[0][] = '-' . $media . '-first';
Expand All @@ -135,9 +134,9 @@ public static function getColumnsConfiguration(array $data)
* Generate the wrapper class name
*
* @param array $data Data array
* @return string Wrapper class name
* @return string Wrapper class name
*/
public static function getWrapperClassName(array $data)
public static function getWrapperClassName(array $data): string
rabauss marked this conversation as resolved.
Show resolved Hide resolved
{
$classes = array('rs-columns');

Expand All @@ -155,22 +154,4 @@ public static function getWrapperClassName(array $data)

return implode(' ', $classes);
}

/**
* Compile the content element
*
* @return void
*/
public function compile()
{
if (System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest(System::getContainer()->get('request_stack')->getCurrentRequest() ?? Request::create(''))) {
$this->strTemplate = 'be_wildcard';
$this->Template = new BackendTemplate($this->strTemplate);
$this->Template->title = $this->headline;
}
else {
$this->Template = new FrontendTemplate($this->strTemplate);
$this->Template->setData($this->arrData);
}
}
}
67 changes: 67 additions & 0 deletions src/Controller/ContentElement/ColumnsStopController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MadeYourDay\RockSolidColumns\Controller\ContentElement;

use Contao\BackendTemplate;
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\ServiceAnnotation\ContentElement;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Columns stop content element
*
* @author Martin Auswöger <[email protected]>
*
* @ContentElement("rs_columns_stop", category="rs_columns")
*/
class ColumnsStopController extends AbstractContentElementController
{
private ScopeMatcher $scopeMatcher;

public function __construct(ScopeMatcher $scopeMatcher)
{
$this->scopeMatcher = $scopeMatcher;
}

protected function getResponse(Template $template, ContentModel $model, Request $request): ?Response
{
if ($this->scopeMatcher->isBackendRequest($request)) {
$backendTemplate = new BackendTemplate('be_wildcard');
return new Response($backendTemplate->parse());
}

$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;

if (isset($GLOBALS['TL_RS_COLUMNS'][$parentKey])) {
if (!$GLOBALS['TL_RS_COLUMNS'][$parentKey]['active']) {
trigger_error('Missing column stop element before column wrapper stop element ID ' . $model->id . '.', E_USER_WARNING);
}
unset($GLOBALS['TL_RS_COLUMNS'][$parentKey]);
}
else {
trigger_error('Missing column wrapper start element before column wrapper stop element ID ' . $model->id . '.', E_USER_WARNING);
}

$htmlSuffix = '';

if (!empty($GLOBALS['TL_RS_COLUMNS_STACK'][$parentKey])) {
$GLOBALS['TL_RS_COLUMNS'][$parentKey] = array_pop($GLOBALS['TL_RS_COLUMNS_STACK'][$parentKey]);
if ($GLOBALS['TL_RS_COLUMNS'][$parentKey]['active']) {
$htmlSuffix .= '</div>';
}
}

return new Response($template->parse() . $htmlSuffix);
}

}
Loading