<?php
namespace VisualMedia\ComponentBundle\Element\widgets;
use VisualMedia\ComponentBundle\Element\Component;
use VisualMedia\PageBundle\Entity\BasePage;
use VisualMedia\BartBundle\Element\backend\layout\ButtonElement;
use VisualMedia\ComponentBundle\Element\widgets\page\RowComponent;
use VisualMedia\ComponentBundle\Element\widgets\page\ColumnComponent;
use VisualMedia\BartBundle\Entity\RootEntity;
use VisualMedia\ComponentBundle\Element\Element;
use VisualMedia\ComponentBundle\Model\ComponentEntityModel;
use VisualMedia\ComponentBundle\Element\WidgetHolder;
use VisualMedia\ComponentBundle\Element\WidgetComponent;
use VisualMedia\ComponentBundle\Element\widgets\page\GlobalComponent;
use VisualMedia\BartBundle\Controller\RootController;
use VisualMedia\ComponentBundle\Element\components\ui\FlashMessageElement;
use VisualMedia\ComponentBundle\Element\components\backend\AdminToolbarElement;
class ComponentsComponent extends Component{
public $entity = false;
protected $components = array();
protected static $widgets = false;
public $componentType = "group";
public $added = [];
public $componentField = "components";
public $message = false;
public $admintoolbar = false;
public $modus = "client";
public $hasChanges = false;
public static $page = false;
public function initialize(){
if(RootController::isPermitted('ROLE_SUPER_ADMIN')){
$this->modus = RootController::getSession()->get("edit_components_modus") ?? "super_admin";
}
}
public function setPage($entity){
if(get_class($entity)== BasePage::class){
static::$page = $entity;
}
else{
static::$page = false;
}
}
public static function createComponents($entity, $componentField = false){
//$entity->components = str_replace("BartPageBundle","ComponentBundle",$entity->components);
$component = new ComponentsComponent();
$component->setPage($entity);
if($componentField) $component->componentField = $componentField;
if(strpos(get_class($entity),"BasePage")>0){
$component->componentType = "page";
}
$component->id = "components_".$entity->id;
$component->object = $entity;
$component->entity = $entity;
$components = [];
if($entity->{$component->componentField}){
$components = json_decode($entity->{$component->componentField},true);
$components = Element::fromArray($components);
if(!is_array($components)) $components = [$components];
//dd($components);
$component->elements = $components;
}
else{
//$component->fillDummy();
//$entity->{$component->componentField} = json_encode($component->elements);
//$em = RootEntity::$doctrine->getManager();
//$em->persist($entity);
//$em->flush();
}
//dd($components);
return $component;
}
public function loadEntity($entity = false){
if($entity) $this->entity = $entity;
if($this->entity->{$this->componentField}){
$components = json_decode($this->entity->{$this->componentField},true);
$components = Element::fromArray($components);
if(!is_array($components)) $components = [$components];
$this->elements = $components;
}
}
public function editElement($id){
$element = $this->getElement($id);
dd($element);
}
public function getElement($id){
if(count($this->components)==0){
$this->explodeComponents();
}
if(isset($this->components[$id])){
return $this->components[$id];
}
return false;
}
public function deleteElement($id){
foreach($this->elements as $rowkey => $row){
if($row->id == $id){
array_splice($this->elements,$rowkey,1);
return;
}
else{
foreach($row->elements as $colkey => $col){
if($col->id == $id){
array_splice($row->elements,$colkey,1);
return;
}
else{
foreach($col->elements as $compkey => $comp){
if($comp->id==$id){
array_splice($col->elements,$compkey,1);
return;
}
else{
foreach($comp->elements as $compkey1 => $comp1){
if($comp1->id==$id){
array_splice($comp->elements,$compkey1,1);
return;
}
else{
}
}
}
}
}
}
}
}
}
public static function getWidgets(){
if(!static::$widgets){
static::$widgets = ComponentEntityModel::getAllComponents(false);
}
return static::$widgets;
}
public function setComponents($components){
//dd($components);
$elements = $this->getEmptyComponents();
$this->elements = [];
foreach($components as $rowItem){
if($rowItem->id != "new"){
//dd($this->components);
$row = $this->components[$rowItem->id];
foreach($rowItem->elements as $colItem){
if($colItem->id!="new"){
$col = $this->components[$colItem->id];
foreach($colItem->elements as $compItem){
if($compItem->id!="new"){
$comp = $this->components[$compItem->id];
$col->elements[] = $comp;
}
else{
$this->addNew($compItem, $col);
}
}
$row->elements[] = $col;
}
else{
$this->addNew($colItem, $row);
}
}
$this->elements[] = $row;
}
else{
$this->addNew($rowItem, $this);
}
}
return $this->elements;
}
public function addComponents($components, $elements = false, $item = false){
$added = [];
$root = false;
if(!$item){
$item = $this;
$root = true;
}
if(!$elements){
$elements = $this->getEmptyComponents();
$this->elements = [];
}
foreach($components as $comp){
if($comp->id!="new"){
//dd($components,$this->components);
if(!isset($this->components[$comp->id])){
//dd($comp->id, $comp);
}
$component = $this->components[$comp->id];
$this->addComponents($comp->elements,$elements,$component);
$item->elements[] = $component;
}
else{
$adding = $this->addNew($comp,$item);
$added[] = $adding->id;
}
}
if($root){
return $this->elements;
}
}
public function addNew($element, $parent = false){
$widget = static::getWidgets()[$element->type];
//dd($widget);
$cls = $widget->classname;
$el = new $cls();
if($parent){
if($element->type=="Group"){
if(isset($parent->behaviour)){
switch($parent->behaviour){
case "row":
$el->behaviour = "col";
break;
case "col":
$el->behaviour = "component";
break;
default:
$el->behaviour = "row";
}
}
else{
$el->behaviour = "container";
}
}
$parent->elements[] = $el;
}
$this->fillDefault($el, $element);
$this->added[] = $el->id;
return $el;
}
public function fillDefault($element, $data = false){
switch($element->type){
case "ContainerComponent":
$child = new RowComponent();
$this->fillDefault($child);
$element->elements[] = $child;
break;
case "RowComponent":
if($data && isset($data->data) && strpos($data->data,"_")>0){
$cols = explode("_",$data->data);
foreach($cols as $width){
$child = new ColumnComponent();
$child->setWidth($width);
$this->fillDefault($child);
$element->elements[] = $child;
}
}
else{
$child = new ColumnComponent();
$this->fillDefault($child);
$element->elements[] = $child;
}
break;
}
}
public function explodeComponents($element = false){
$root = false;
if($element===false){
$root = true;
$element = $this;
$this->components = [];
}
else{
$this->components[$element->id] = $element;
}
foreach($element->elements as $component){
$this->explodeComponents($component);
}
return $this->components;
}
public function getEmptyComponents(){
$comps = $this->explodeComponents();
$result = [];
foreach($comps as $id => $comp){
$comp->elements = [];
$result[$id] = $comp;
}
return $result;
}
public function fillDummy(){
//for($i = 0;$i<1;$i++){
// $row = new RowComponent();
// for($j=0;$j<1;$j++){
// $column = new ColumnComponent();
// //$element = new Component();
// //$column->addElement($element);
// $row->addElement($column);
// }
// $this->addElement($row);
//}
}
public function manageHtml(){
//$this->getBuffer();
return $this->manageTwig();
}
public static function addToForm(&$form, &$entity, $componentsComponent = false){
$form->addFormField("tab_components","Componenten",'tab');
if($componentsComponent){
$components = $componentsComponent;
}
else{
$components = ComponentsComponent::createComponents($entity);
}
//$children = $components->getComponents();
$items = ComponentEntityModel::getAllComponents();
$holder = new WidgetHolder();
//dd($this->getModus());
//$holder->modus = $this->getModus();
$holder->addWidgets($items);
$components->widgetHolder = $holder;
$html = $components->manageTwig();
$form->addFormField("components_row","Componenten","htmlrow", ["value" => $html]);
}
public function process(){
if(isset($_POST['components_action'])){
$action = $_POST['components_action'];
if($action=="drop"){
$components = json_decode($_POST['components']);
$this->addComponents($components);
//dd($this);
}
else if($action=="delete"){
$this->deleteElement($_POST['component']);
}
else if($action=="save_data"){
$component = $this->findElement($_POST['components_id']);
if($component->type=="GlobalComponent" && isset($_POST['global_id']) && $_POST['global_id']=="dissolve"){
$data = GlobalComponent::getContents($component->global_id);
$data->id = uniqid();
$this->replaceElement($data,$component);
}
else{
$ignore = ["components_"];
foreach($_POST as $key => $value){
$ignoreMe = false;
foreach($ignore as $not){
if(substr($key,0,strlen($not))===$not)$ignoreMe = true;
}
if(!$ignoreMe && $key){
//dump($key, $value, $component, $_POST);
$component->$key = $value;
}
}
}
$edited = $this->findElement($_POST['components_id']);
}
else if($action=="edit_element"){
$component = $this->findElement($_POST['id']);
if($component && isset($_POST['field']) && isset($_POST['value'])){
$component->{$_POST['field']} = $_POST['value'];
}
//$this->replaceElement($component);
$edited = $this->findElement($_POST['id']);
//dd($edited);
}
else if($action=="config_component"){
$component = $this->findElement($_POST['components_id']);
//dd($_POST, $component);
if($_POST['template_name']){
//create as static template
$template = GlobalComponent::findGlobalComponentByName($_POST['template_name'],true);
$template->setComponent($component);
//dd($template->getComponent());
$template->saveToDatabase();
$this->replaceElement($template,$component);
//dd($template);
}
else if(substr($_POST['className'],1)!=$component->className){
$component = $component->changeType($_POST['className']);
$this->replaceElement($component);
}
}
$this->entity->components = json_encode($this->elements);
//dd($this->entity);
$this->setBuffer();
}
}
public function manageTwig(){
$this->admintoolbar = new AdminToolbarElement(["componentsComponent" => $this]);
$this->getBuffer();
//dd($this->admintoolbar);
//dd($this);
return parent::manageTwig();
}
public function setModus(){
}
public function getBuffer(){
//return;
$buffer = ComponentBuffer::get($this->entity);
if($buffer){
if($this->entity->components !== $buffer->componentString || $this->hasChanges){
$this->hasChanges = true;
if($this->admintoolbar){
$this->admintoolbar->addButton("?action=Origineel herstellen","Origineel herstellen",'','a',["secondaryClass" => "btn-delete"]);
$this->admintoolbar->message = "Er zijn onopgeslagen wijzigingen";
$this->admintoolbar->classes[] = "alert alert-danger";
}
$this->entity->components = $buffer->componentString;
$this->loadEntity();
}
}
else{
ComponentBuffer::set($this->entity);
}
}
public function setBuffer(){
ComponentBuffer::set($this->entity);
}
public function saveBuffer($manager = null){
if(isset($this->entity) && isset($this->entity->id) && $this->entity->id>0){
if(!$manager){
$manager = RootEntity::$doctrine->getManagerForClass(get_class($this->entity));
}
$this->getBuffer();
$manager->persist($this->entity);
$this->setBuffer();
$manager->flush();
}
}
public static function processPost(&$entity, $componentsComponent = false, $manager = false){
//dd(":p");
if(isset($_POST['components_action'])){
$action = $_POST['components_action'];
if($componentsComponent){
$pc = $componentsComponent;
}
else{
$pc = static::createComponents($entity);
}
if($action=="drop"){
$components = json_decode($_POST['components']);
//dd($components);
$pc->addComponents($components);
}
else if($action=="delete"){
$pc->deleteElement($_POST['component']);
}
else if($action=="save_data"){
$component = $pc->findElement($_POST['components_id']);
if($component->type=="GlobalComponent" && isset($_POST['global_id']) && $_POST['global_id']=="dissolve"){
$data = GlobalComponent::getContents($component->global_id);
$data->id = uniqid();
$pc->replaceElement($data,$component);
}
else{
$ignore = ["components_"];
foreach($_POST as $key => $value){
$ignoreMe = false;
foreach($ignore as $not){
if(substr($key,0,strlen($not))===$not)$ignoreMe = true;
}
if(!$ignoreMe && $key){
//dump($key, $value, $component, $_POST);
$component->$key = $value;
}
}
}
$edited = $pc->findElement($_POST['components_id']);
}
else if($action=="config_component"){
$component = $pc->findElement($_POST['components_id']);
if($_POST['template_name']){
//create as static template
$template = GlobalComponent::findGlobalComponentByName($_POST['template_name'],true);
$template->setComponent($component);
$template->saveToDatabase();
$pc->replaceElement($template,$component);
}
else if(substr($_POST['className'],1)!=$component->className){
$component = $component->changeType($_POST['className']);
$pc->replaceElement($component);
}
}
//dd($entity);
$entity->components = json_encode($pc->elements);
if(!$manager){
$manager = RootEntity::$doctrine->getManager();
}
$manager->persist($entity);
$manager->getUnitOfWork()->scheduleForUpdate($entity);
$manager->flush();
return true;
}
return false;
}
}
class ComponentBuffer{
public $entityName;
public $entityId;
public $componentString;
public function __construct($entity, $componentField = "components"){
$this->entityName = get_class($entity);
$this->entityId = $entity->id;
$this->componentString = $entity->{$componentField};
}
public static function set($entity){
$item = new ComponentBuffer($entity);
RootController::$requestStack->getSession()->set("edit_components", json_encode($item));
return $item;
}
public static function get($entity){
$item = new ComponentBuffer($entity);
$buffer = RootController::$requestStack->getSession()->get("edit_components");
$buffer = json_decode($buffer,true);
if(isset($buffer['entityId']) && $buffer['entityId']==$entity->id && isset($buffer['entityName']) && $buffer['entityName']===get_class($entity)){
$item->componentString = $buffer['componentString'];
return $item;
}
//dump($buffer);
return false;
}
}
?>