<?php
namespace VisualMedia\ComponentBundle\Element;
//use VisualMedia\LisaBundle\Controller\BaseController;
//use VisualMedia\BartBundle\Model\ValidationManager;
//use VisualMedia\BartBundle\Model\EntityMetaModel;
//use VisualMedia\BartBundle\Element\backend\DataTablesNet;
//use VisualMedia\BartBundle\Element\backend\FormComponent;
use VisualMedia\BartBundle\Controller\RootController;
/**
* Component short summary.
*
* Component description.
*
* @version 1.0
* @author berend
*/
class Element{
public $id = false;
public $type = "element";
public $title = "";
public $enabled = true;
public $data = array();
public $className = Element::class;
protected $displayLevel = Element::USER_PUBLIC;
protected $editLevel = Element::USER_ADMIN;
public $fields = array();
public $elements = array();
public $selectedIndex=0;
public $rendered = false;
public $twig_variant = false;
public $template;
public function __construct($data = array()){
//if argument is string: it is the id
if(is_string($data)){
$data = ["id" => $data];
}
foreach($data as $key => $val){
$this->$key = $val;
}
if(is_array($data)){
extract($data);
}
if(isset($id)){
$this->id = $id;
}
else{
$id = uniqid();
$this->id = $id;
}
if(!isset($title) || $title===false){
$from = $this->id;
if(isset($this->fieldname) && strlen($this->fieldname)){
$from = $this->fieldname;
}
if(isset($this->label) && strlen($this->fieldname)){
$from = $this->label;
}
$title = str_replace("_"," ",$from);
$title=ucfirst($title);
$this->title = $title;
}
if(!isset($type) || !$type){
$type = $this->getClassName();
}
$this->type = $type;
$this->title = $title;
$this->data = $data;
//$this->enabled = $enabled;
$this->className =$this->getFullClassName();
$this->initialize();
}
protected function setTitle($title = false){
if(!isset($title) || $title===false){
$from = $this->id;
if(isset($this->fieldname) && strlen($this->fieldname)){
$from = $this->fieldname;
}
if(isset($this->label) && strlen($this->fieldname)){
$from = $this->label;
}
$title = str_replace("_"," ",$from);
$title=ucfirst($title);
$this->title = $title;
}
return $title;
}
protected function initialize(){
}
public static function create($data){
$data = (array) $data;
$cls = get_called_class();
$object = new $cls();
$type = $object->type;
$object->loadData($data);
$object->type = $type;
if(isset($data['title'])){
$object->setTitle($data['title']);
}
return $object;
}
public function addField($id, $type = "element", $title = false, $data = array(), $enabled=true ): Element
{
if($title===false){
$title = str_replace("_"," ",$this->id);
$title=ucfirst($id);
}
$field = new Element($id, $type, $title,$data,$enabled);
//dd($field);
$this->fields[$id] = $field;
return $field;
}
public function addElement(Element $element): Element
{
$this->elements[] = $element;
return $element;
}
public function setTemplate(string $template): void
{
$this->template = $template;
}
public function getTemplate(): string
{
return $this->template;
}
public function setIndex($i)
{
$this->selectedIndex = $i;
foreach($this->data[$i] as $key => $val){
$this->$key = $val;
}
}
public function getFullClassName($object = false){
if($object===false){
$object = $this;
}
return get_class($object);
}
public function getClassName($object = false){
if($object===false){
$object = $this;
}
$className= get_class($object);
return substr($className,strrpos($className,"\\")+1);
}
public function getIcon($icon = false, $class=""){
if(!$icon){
$icon = $this->icon;
}
if(strlen($icon)>2 && substr($icon,0,2)=="fa"){
$parts = explode("-",$icon);
$icon = substr($icon,strlen($parts[0])+1);
if($class=="name"){
$icon = "{$parts[0]} fa-{$icon}";
}
else{
$icon = "<i class='{$parts[0]} fa-{$icon} {$class}'></i>";
}
}
return $icon;
}
public function getHtml(){
$this->preRender();
$this->rendered = true;
return $this->renderTwig();
//$reflection = new \ReflectionClass($this);
////$pad = $reflection->getFileName();
//$pad = str_replace("/Element/","/Resources/views/Element/", $reflection->getFileName());
//$pad = str_replace(".php",".html.twig",$pad);
//if(file_exists($pad)){
// $locator = "/Resources/views/";
// $pad = "@VisualMediaBart/".substr($pad,strpos($pad,$locator)+strlen($locator));
// //dd($pad);
// //dd($this);
// return RootController::renderTwig($this,$pad);
//}
//else{
// return "<div id='{$this->id}' class='element_Element'>not found:{$pad}</div>";
//}
}
public function findTemplate($replace = "/Resources/views/Element/"){
$reflection = new \ReflectionClass($this);
$pad = str_replace("/Element/",$replace, $reflection->getFileName());
$pad = str_replace(".php",".html.twig",$pad);
return $pad;
}
public function renderTwig($filename = false)
{
$locator = "/Resources/views/";
$extension = ".html.twig";
if($this->twig_variant){
$extension = "_".$this->twig_variant.$extension;
}
$reflection = new \ReflectionClass($this);
//$pad = $reflection->getFileName();
$pad = str_replace("/Element/","/Resources/views/Element/", $reflection->getFileName());
$pad = str_replace(".php",$extension,$pad);
$oldpad = $pad;
$pad_lokaal = str_replace("/src/Element/","/templates/Element/", $reflection->getFileName());
$pad_lokaal = str_replace(".php",$extension,$pad_lokaal);
if($this->template) {
return RootController::renderTwig($this, $this->template);
}
else if(isset($this->twig_template) && strlen($this->twig_template)>0){
//dd($this->twig_template);
$pad = $this->twig_template;
$locator = "public_html/templates/";
if(strpos($pad,$locator)>0){
$pad = substr($pad,strpos($pad,$locator) + strlen($locator));
}
if(file_exists($locator.$pad)){
return RootController::renderTwig($this, $pad);
}
else{
$pad = $oldpad;
}
//dd($pad);
}
elseif($filename){
$pad = str_replace($this->getClassName(),$filename,$pad);
}
//dd($pad_lokaal);
if(file_exists($pad_lokaal)){
$pad = "".substr($pad,strpos($pad,$locator)+strlen($locator));
//dd($pad);
//dd($this);
return RootController::renderTwig($this,$pad);
}
//dump($pad);
if(file_exists($pad)){
$namespace = 'VisualMediaBart';
$namespace = sprintf('VisualMedia%s', str_replace('Bundle', '', explode('\\', get_class($this))[1] ?? null));
$locator = "/Resources/views/";
$pad = "@{$namespace}/".substr($pad,strpos($pad,$locator)+strlen($locator));
return RootController::renderTwig($this,$pad);
}
else{
$this->rendered = false;
return "<div id='{$this->id}' class='element_Element'>not found:{$pad}</div>";
}
}
public function preRender(){
}
public static function mergeData($text, $data, $open = "[", $close = "]"){
foreach($data as $key => $val){
$text = str_replace("[".$key."]",$val,$text);
}
//preg_match_all("/\{[A-Z0-9_]+\}+/", $text, $matches);
//foreach($matches as $match_group) {
// foreach($match_group as $match) {
// $field = str_replace("}", "", $match);
// $field = str_replace("{", "", $field);
// $html = str_replace($match,$data[$field],$html);
// }
//}
return $text;
}
public function twig(){
return new \Twig\Markup($this->getHtml() , 'UTF-8');
}
public function loadData($data){
if($data){
//dump($data);
foreach($data as $key => $value){
//dump($key);
$this->$key = $value;
}
}
}
public function getData(){
$data = [];
foreach($this as $key => $val){
$data[$key] = $val;
}
return $data;
}
public static function fromArray($data){
if(!is_array($data)){
$data = (array) $data;
}
//dump($data);
if(isset($data['id'])){
$element = new $data['className']($data);
$element->loadData($data);
if($element->elements){
//dump($element->elements);
for($i=0;$i<count($element->elements);$i++){
$element->elements[$i] = Element::fromArray($element->elements[$i]);
}
}
return $element;
}
else if (is_array($data)){
for($i=0;$i<count($data);$i++){
$data[$i] = Element::fromArray($data[$i]);
}
return $data;
}
}
public function findElement($id){
if($this->id==$id){
return $this;
}
if(is_array($this->elements)){
foreach($this->elements as $child){
if($found = $child->findElement($id)){
return $found;
}
}
}
return false;
}
public function replaceElement($new, $id=false){
if(!$id){
$id = $new->id;
}
else if(is_object($id)){
$id = $id->id;
}
if(is_array($this->elements)){
foreach($this->elements as $key => $child){
if($child->id==$id){
$this->elements[$key] = $new;
return true;
}
}
}
if(is_array($this->elements)){
foreach($this->elements as $child){
if($child->replaceElement($new, $id)){
return true;
}
}
}
return false;
}
public function findBy($name, $value){
//dump($this->$name."!=".$value);
if(isset($this->$name) && $this->$name==$value){
return $this;
}
foreach($this->elements as $child){
if($found = $child->findBy($name,$value)){
return $found;
}
}
return false;
}
public function findElementOfType($type){
return $this->findBy("type",$type);
}
public function allow($task = 'display'){
return true;
}
/**************************** TODO: CLEAN ***********************************/
public function init($data){
$data = (array) $data;
$cls = get_called_class();
$object = new $cls();
$type = $object->type;
$object->loadData($data);
$object->type = $type;
return $object;
}
public const USER_PUBLIC = 0; //anonymous visitors
public const USER_USER = 1; //registered visitors
public const USER_ADMIN = 2; //client admin
public const USER_SUPER_ADMIN = 3; //developers / dappr
}
?>