76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace Trinity\Bundle\NewsletterBundle\Form\Type;
|
|||
|
|
|||
|
use Propel\PropelBundle\Form\BaseAbstractType;
|
|||
|
use Symfony\Component\Form\FormBuilderInterface;
|
|||
|
use \Trinity\Bundle\NewsletterBundle\Form\EventListener\AddBlockFieldsSubscriber;
|
|||
|
|
|||
|
class ModelType extends BaseAbstractType
|
|||
|
{
|
|||
|
protected $options = array(
|
|||
|
'data_class' => 'Trinity\Bundle\NewsletterBundle\Model\Model',
|
|||
|
'name' => 'model',
|
|||
|
);
|
|||
|
|
|||
|
/**
|
|||
|
* {@inheritdoc}
|
|||
|
*/
|
|||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
|||
|
{
|
|||
|
$builder->add('name');
|
|||
|
|
|||
|
$builder->add(
|
|||
|
'classKey',
|
|||
|
'choice',
|
|||
|
array(
|
|||
|
'choices' => self::getModels(),
|
|||
|
'required' => true,
|
|||
|
)
|
|||
|
);
|
|||
|
|
|||
|
$builder->add(
|
|||
|
'template',
|
|||
|
'choice',
|
|||
|
array(
|
|||
|
'choices' => $this->getTemplates($builder->getData()),
|
|||
|
'required' => true,
|
|||
|
)
|
|||
|
);
|
|||
|
|
|||
|
$builder->addEventSubscriber(new AddBlockFieldsSubscriber());
|
|||
|
}
|
|||
|
|
|||
|
public function getModels()
|
|||
|
{
|
|||
|
$models = array();
|
|||
|
|
|||
|
if (is_array($this->getOption('models'))) {
|
|||
|
foreach ($this->getOption('models') as $model => $settings) {
|
|||
|
$models[$model] = $settings['title'];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return $models;
|
|||
|
}
|
|||
|
|
|||
|
public function getTemplates(\Trinity\Bundle\NewsletterBundle\Model\Model $model = null)
|
|||
|
{
|
|||
|
$templates = array();
|
|||
|
|
|||
|
if (null !== $model) {
|
|||
|
$models = $this->getOption('models');
|
|||
|
|
|||
|
if (isset($models[$model->getClassKey()])) {
|
|||
|
$_templates = $models[$model->getClassKey()]['templates'];
|
|||
|
|
|||
|
foreach ($_templates as $_template) {
|
|||
|
$templates[$_template['template']] = $_template['title'];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return $templates;
|
|||
|
}
|
|||
|
}
|