Skip to content

Commit

Permalink
Merge pull request #11 from sumocoders/add-template-to-the-widgets
Browse files Browse the repository at this point in the history
Add template to the widgets
  • Loading branch information
carakas authored Feb 16, 2017
2 parents aafdad8 + 76a5736 commit 9930a87
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/Backend/Modules/Partners/Domain/Widget/Widget.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
final class Widget
{
const DEFAULT_TEMPLATE = 'Slideshow.html.twig';

/**
* @var int
*
Expand Down Expand Up @@ -52,12 +54,21 @@ final class Widget
*/
private $partners;

/**
* @var string
*
* @ORM\Column(type="string", length=255, options={"default" = "Slideshow.html.twig"})
*/
private $template = 'Slideshow.html.twig';

/**
* @param string $title
* @param string $template
*/
public function __construct($title)
public function __construct($title, $template)
{
$this->title = $title;
$this->template = $template;
$this->partners = new ArrayCollection();
}

Expand Down Expand Up @@ -94,6 +105,7 @@ public function updateWidget()
$data['id'] = $this->id;
$data['edit_url'] = $editUrl;
$data['extra_label'] = $this->title;
$data['custom_template'] = $this->template;

Model::updateExtra($this->widgetId, 'data', $data);
}
Expand All @@ -114,6 +126,14 @@ public function getTitle()
return $this->title;
}

/**
* @return string
*/
public function getTemplate()
{
return $this->template;
}

/**
* @param WidgetDataTransferObject $widgetDataTransferObject
*
Expand All @@ -125,6 +145,7 @@ public static function fromDataTransferObject(WidgetDataTransferObject $widgetDa
$widget = $widgetDataTransferObject->getWidgetEntity();

$widget->title = $widgetDataTransferObject->title;
$widget->template = $widgetDataTransferObject->template;
$widget->partners = $widgetDataTransferObject->partners->map(
function (PartnerDataTransferObject $partnerDataTransferObject) use ($widget) {
return Partner::fromDataTransferObject($partnerDataTransferObject, $widget);
Expand All @@ -134,7 +155,7 @@ function (PartnerDataTransferObject $partnerDataTransferObject) use ($widget) {
return $widget;
}

$widget = new self($widgetDataTransferObject->title);
$widget = new self($widgetDataTransferObject->title, $widgetDataTransferObject->template);

$widget->partners = $widgetDataTransferObject->partners->map(
function (PartnerDataTransferObject $partnerDataTransferObject) use ($widget) {
Expand Down
8 changes: 8 additions & 0 deletions src/Backend/Modules/Partners/Domain/Widget/WidgetDataTransferObject.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class WidgetDataTransferObject
*/
public $partners;

/**
* @var string
*
* @Assert\NotBlank(message="err.FieldIsRequired")
*/
public $template = Widget::DEFAULT_TEMPLATE;

/**
* @param Widget|null $widget
*/
Expand All @@ -41,6 +48,7 @@ public function __construct(Widget $widget = null)
}

$this->title = $widget->getTitle();
$this->template = $widget->getTemplate();
$this->partners = $widget->getPartners()->map(
function (Partner $partner) {
return new PartnerDataTransferObject($partner);
Expand Down
55 changes: 55 additions & 0 deletions src/Backend/Modules/Partners/Domain/Widget/WidgetType.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Backend\Modules\Partners\Domain\Widget;

use Backend\Modules\Partners\Domain\Partner\PartnerType;
use Common\ModulesSettings;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -12,6 +15,17 @@

class WidgetType extends AbstractType
{
/** @var string */
private $theme;

/**
* @param ModulesSettings $modulesSettings
*/
public function __construct(ModulesSettings $modulesSettings)
{
$this->theme = $modulesSettings->get('Core', 'theme', 'core');
}

/**
* @param FormBuilderInterface $builder
* @param array $options
Expand All @@ -38,6 +52,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
]
]
);

$templates = $this->getPossibleTemplates();
// if we have multiple templates, add a dropdown to select them
if (count($templates) > 1) {
$builder->add(
'template',
ChoiceType::class,
[
'required' => true,
'label' => 'lbl.Template',
'choices' => $templates,
'choice_translation_domain' => false,
]
);
}
}

/**
Expand All @@ -52,4 +81,30 @@ public function configureOptions(OptionsResolver $resolver)
]
);
}

/**
* Get templates.
*
* @return array
*/
private function getPossibleTemplates()
{
$templates = array();
$finder = new Finder();
$finder->name('*.html.twig');
$finder->in(FRONTEND_MODULES_PATH . '/Partners/Layout/Widgets');
// if there is a custom theme we should include the templates there also
$theme = $this->theme;
if ($theme != 'core') {
$path = FRONTEND_PATH . '/Themes/' . $theme . '/Modules/Partners/Layout/Widgets';
if (is_dir($path)) {
$finder->in($path);
}
}
foreach ($finder->files() as $file) {
$templates[] = $file->getBasename();
}

return array_combine($templates, $templates);
}
}
16 changes: 16 additions & 0 deletions src/Backend/Modules/Partners/Layout/Templates/FormLayout.html.twig
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@
{% endif %}
</div>

{% if form.template is defined %}
<div class="panel panel-default">
<div class="panel-heading">
{{ form_label(form.template) }}
</div>
<div class="panel-body">
{{ form_widget(form.template) }}
</div>
{% if form.template.vars.errors|length %}
<div class="panel-footer">
{{ form_errors(form.template) }}
</div>
{% endif %}
</div>
{% endif %}

{{ form_row(form.partners) }}
</div>
{% endblock %}
Expand Down
6 changes: 6 additions & 0 deletions src/Backend/Modules/Partners/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ services:
- "@partners.repository.widget"
tags:
- { name: command_handler, handles: Backend\Modules\Partners\Domain\Widget\Command\DeleteWidget }
partners.widget.form_type:
class: Backend\Modules\Partners\Domain\Widget\WidgetType
arguments:
- "@fork.settings"
tags:
- { name:form.type }
1 change: 0 additions & 1 deletion src/Frontend/Modules/Partners/Widgets/Slideshow.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Slideshow extends FrontendBaseWidget
public function execute()
{
parent::execute();
$this->loadTemplate();
$this->tpl->assign(
'partners',
$this->get('partners.repository.widget')->find($this->data['id'])->getPartners()
Expand Down

0 comments on commit 9930a87

Please sign in to comment.