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

Enh/example ref #361

Draft
wants to merge 2 commits into
base: enh/26-template-add-spaces-users-element-type
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions modules/template/components/TemplateElementValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace humhub\modules\custom_pages\modules\template\components;

use humhub\modules\custom_pages\modules\template\elements\AbstractElement;

/**
* Standard Object which is provided as a variable via Twig.
* This can be extended for more complex variables.
*/
class TemplateElementValue
{
private AbstractElement $element;

public $value = '';

public function __construct(AbstractElement $element, string $value)
{
$this->element = $element;
}

public function __toString()
{
return $this->value;
}

}
43 changes: 43 additions & 0 deletions modules/template/elements/AbstractElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace humhub\modules\custom_pages\modules\template\elements;

use humhub\modules\custom_pages\modules\template\components\TemplateElementValue;
use humhub\modules\custom_pages\modules\template\models\TemplateElement;
use humhub\modules\custom_pages\modules\template\models\TemplateInstance;
use Yii;
use yii\base\BaseObject;

abstract class AbstractElement extends BaseObject
{
private TemplateElement $element;

public function __construct(TemplateElement $element)
{
if ($element->content_type !== get_class($this)) {
throw new InvalidArgumentException("Invalid element given!");
}
}

abstract public static function getElementTypeTitle(): string;

abstract public static function getElementTypeDescription(): string;

abstract public function getTemplateValue(?TemplateInstance $templateInstance): mixed;

public static function create(TemplateElement $elementModel)
{
return Yii::createObject($elementModel->content_type);
}

public function getTemplateName()
{
return $this->element->name;
}

public function isDynamic(): bool
{
return false;
}

}
60 changes: 60 additions & 0 deletions modules/template/elements/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace humhub\modules\custom_pages\modules\template\elements;

use humhub\modules\custom_pages\modules\template\components\TemplateElementValue;
use humhub\modules\custom_pages\modules\template\models\ImageContentDefinition;
use humhub\modules\custom_pages\modules\template\models\TemplateInstance;
use humhub\modules\custom_pages\modules\template\models\TextContent;

class Text extends AbstractElement
{
public static function getElementTypeTitle(): string
{
return "Text element";
}

public static function getElementTypeDescription(): string
{
return "Use this for plain text elements.";
}

public function getTemplateValue(?TemplateInstance $templateInstance): string
{
// Switch between default and instance content
if ($templateInstance && $this->getDataRecord($templateInstance)->hasValues()) {
$value = $this->getDataRecord($templateInstance)->content;
} else {
$value = $this->getDataRecord(null)->content;
}

return new TemplateElementValue($this, $value);
}


/**
* @param TemplateInstance|null $templateInstance - Set null for default data
* @return TextContent
*/
private function getDataRecord(?TemplateInstance $templateInstance): TextContent
{
$data = TextContent::findOne(['template_instance_id' => $templateInstance->id]);
if ($data === null) {
$definition = new TextContent();
$definition->template_instance_id = $templateInstance->id;
}
}

private function getDefinitionRecord(Template $template): ?ImageContentDefinition
{
$definition = ImageContentDefinition::findOne(['template_id' => $template->id]);
if ($definition === null) {
$definition = new ImageContentDefinition();
$definition->template_id = $template->id;
}
return $definition;
}


// More "service" method which are not neccesary be done in the model
}
51 changes: 51 additions & 0 deletions modules/template/services/TemplateInstanceRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace humhub\modules\custom_pages\modules\template\services;

use humhub\modules\custom_pages\lib\templates\TemplateEngineFactory;
use humhub\modules\custom_pages\modules\template\models\TemplateInstance;
use Yii;
use yii\caching\DummyCache;

class TemplateInstanceRendererService
{
public const CACHE_PREFIX = 'cp-';

private $templateInstance;

private TemplateService $templateService;

public function __construct(TemplateInstance $templateInstance)
{
$this->templateInstance = $templateInstance;
$this->templateService = new TemplateService($this->templateInstance->template);
}

public function render()
{
$cache = ($this->isCachable()) ? Yii::$app->cache : new DummyCache();

return $cache->getOrSet(static::CACHE_PREFIX . $this->templateInstance->id, function () {
luke- marked this conversation as resolved.
Show resolved Hide resolved
$engine = TemplateEngineFactory::create("twig");

$twigVariables = [];
foreach ($this->templateService->getElements() as $element) {
$twigVariables[$element->getTemplateName()] = $element->getTemplateValue($this->templateInstance);
}

return $engine->render($this->templateInstance->template->source, $twigVariables);
});
}

private function isCachable(): bool
{
foreach ($this->templateService->getElements() as $element) {
if ($element->isDynamic()) {
luke- marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

return true;
}

}
36 changes: 36 additions & 0 deletions modules/template/services/TemplateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace humhub\modules\custom_pages\modules\template\services;

use humhub\modules\custom_pages\modules\template\elements\AbstractElement;
use humhub\modules\custom_pages\modules\template\models\Template;

class TemplateService
{
private Template $template;

/**
* @var AbstractElement[]
*/
private array $elements;

public function __construct(Template $template)
{
$this->template = $template;
}

private function loadElements()
{
foreach ($this->template->elements as $elementModel) {
$this->elements[] = AbstractElement::create($elementModel);
}
}

/**
* @return AbstractElement[]
*/
public function getElements(): array
{
return $this->elements;
}
}