deblan.tv/vendor/trinity/src/Trinity/Bundle/NewsletterBundle/Model/Model.php
2015-03-02 21:57:49 +01:00

143 lines
3.1 KiB
PHP

<?php
namespace Trinity\Bundle\NewsletterBundle\Model;
use Trinity\Bundle\NewsletterBundle\Model\om\BaseModel;
use Trinity\Bundle\NewsletterBundle\Configuration\ModelConfiguration;
class Model extends BaseModel
{
protected $blocks = array();
protected $configuration = null;
protected $object;
public function getConfiguration()
{
return $this->configuration;
}
protected function hasBlock($d)
{
return isset($this->blocks[is_object($d) ? $d->getName() : $d]);
}
public function setBlock(Block $block)
{
$block->setModelId($this->getId());
$this->blocks[$block->getName()] = $block;
return $this;
}
public function __construct($template = null)
{
if ($template) {
$this->setTemplate($template);
}
parent::__construct();
$this->configuration = new ModelConfiguration();
}
public function __toString()
{
return $this->name;
}
public function getTemplating()
{
return $this->templating;
}
public function setTemplating($templating)
{
$this->templating = $templating;
return $this;
}
public function save(\PropelPDO $con = null)
{
parent::save($con);
if (!empty($this->blocks)) {
foreach ($this->blocks as $block) {
$block->save($con);
}
} else {
foreach ($this->getConfiguration()->getBlocks() as $block_configuration) {
$this->getBlock($block_configuration->getName())->save($con);
}
}
return $this;
}
public function getBlock($name)
{
if (!$this->getId()) {
return $this->getNewBlock($name);
}
$qBlock = BlockQuery::create()->filterByName($name)->filterByModelId($this->getId())->findOne();
$block = $qBlock ? $qBlock : $this->getNewBlock($name, $this->getId());
if (!$this->hasBlock($block->getName())) {
$this->setBlock($block);
}
return $block;
}
public function getBlockTitle()
{
return $this->getBlock('title');
}
public function getBlockSubtitle()
{
return $this->getBlock('subtitle');
}
public function getBlockContent()
{
return $this->getBlock('content');
}
public function setBlockTitle(Block $block)
{
return $this->setBlock($block);
}
public function setBlockSubtitle(Block $block)
{
return $this->setBlock($block);
}
public function setBlockContent(Block $block)
{
return $this->setBlock($block);
}
protected function getNewBlock($name, $modelId = null)
{
$type = $this->getConfiguration()->getBlock($name)->getType();
$form = new $type();
$form_model = $form->getOption('data_class') ? $form->getOption('data_class') : 'Block';
$block = new $form_model();
$block->setname($name);
if (null !== $modelId) {
$block->setModelId($modelId);
}
return $block;
}
}