| Current Path : /var/www/v3.cesa.co.za/src/ContentBundle/Services/ |
| Current File : /var/www/v3.cesa.co.za/src/ContentBundle/Services/FormItemListManager.php |
<?php
namespace App\ContentBundle\Services;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Monolog\Logger;
use App\ContentBundle\Entity\FormItem;
use App\ContentBundle\Entity\FormItemList;
/**
* Form Item List manager
*
* @author Otto Saayman <otto@igotafrica.com>
* @package ContentBundle
* @subpackage Form Item List
* @version 0.0.1
*/
final class FormItemListManager {
/**
* Service Container
* @var object
*/
private $container = null;
/**
* Entity manager
* @var object
*/
private $em;
/**
* Class construct
*
* @param ContainerInterface $container
* @param Logger $logger
* @return void
*/
public function __construct(
ContainerInterface $container) {
$this->setContainer($container);
$this->setEm($container->get('doctrine')->getManager('default'));
return;
}
/**
* Get Container
*
* @return ContainerInterface $container
*/
public function getContainer() {
return $this->container;
}
/**
* Set Container
*
* @param ContainerInterface $container
* @return void
*/
public function setContainer($container) {
$this->container = $container;
}
/**
* Get Entity Manager
*
* @return string
*/
public function getEm() {
return $this->em;
}
/**
* Set Container
*
* @param string $em
* @return void
*/
public function setEm($em) {
$this->em = $em;
}
/**
* save a form list item
*
* @param FormItemList $formItemList
*/
public function save(&$formItemList) {
$this->getEm()->persist($formItemList);
$this->getEm()->flush();
}
/**
* Get a form list item
*
* @param string $id
* @return FormItemList
*/
public function find($id) {
return $this->getEm()->getRepository(FormItemList::class)->find($id);
}
/**
* Get a form list item by params
*
* @param array $params
* @return FormItemList
*/
public function findOneBy($params) {
return $this->getEm()->getRepository(FormItemList::class)->findOneBy($params);
}
/**
* Save form item form object
*
* @param array $conf
* @param array $orderBy
* @return array
*/
public function findBy($conf = array(), $orderBy = array()) {
return $this->getEm()->getRepository(FormItemList::class)->findBy($conf, $orderBy);
}
/**
* Get the widget name for a list item
*
* @param string $formItemList
* @param FormItem $formItem
* @return void
*/
public function saveFromList($formItemList, FormItem $formItem) {
if (is_array($formItemList)) {
$options = $formItemList;
} else {
$options = explode("\n", $formItemList);
}
$notDelete = array();
$count = 0;
foreach ($options as $option) {
if (strlen($option) > 0) {
$formItemList = $this->findOneBy(array('form_item' => $formItem, 'list_item' => $option));
if (!is_object($formItemList)) {
$formItemList = new FormItemList();
}
$formItemList->setFormItem($formItem);
$formItemList->setListItem($option);
$formItemList->setOrderBy($count);
$this->save($formItemList);
$notDelete[] = $formItemList->getId();
$count++;
}
} //foreach
$this->getContainer()->get('form_item.manager')->getEm()->refresh($formItem);
$itemLists = $formItem->getFormItemList();
foreach ($itemLists as $itemList) {
if (!in_array($itemList->getId(), $notDelete)) {
$this->getEm()->remove($itemList);
$this->getEm()->flush();
}
} //foreach
}
}