lib/composer/BartBundle/src/VisualMedia/ComponentBundle/Element/Element.php line 38

Open in your IDE?
  1. <?php
  2. namespace VisualMedia\ComponentBundle\Element;
  3. //use VisualMedia\LisaBundle\Controller\BaseController;
  4. //use VisualMedia\BartBundle\Model\ValidationManager;
  5. //use VisualMedia\BartBundle\Model\EntityMetaModel;
  6. //use VisualMedia\BartBundle\Element\backend\DataTablesNet;
  7. //use VisualMedia\BartBundle\Element\backend\FormComponent;
  8. use VisualMedia\BartBundle\Controller\RootController;
  9. /**
  10.  * Component short summary.
  11.  *
  12.  * Component description.
  13.  *
  14.  * @version 1.0
  15.  * @author berend
  16.  */
  17. class Element{
  18.     public $id false;
  19.     public $type "element";
  20.     public $title "";
  21.     public $enabled true;
  22.     public $data = array();
  23.     public $className Element::class;
  24.     protected $displayLevel Element::USER_PUBLIC;
  25.     protected $editLevel Element::USER_ADMIN;
  26.     public $fields = array();
  27.     public $elements = array();
  28.     public $selectedIndex=0;
  29.     public $rendered false;
  30.     public $twig_variant false;
  31.     public $template;
  32.     public function __construct($data = array()){
  33.         //if argument is string: it is the id
  34.         if(is_string($data)){
  35.             $data = ["id" => $data];
  36.         }
  37.         foreach($data as $key => $val){
  38.             $this->$key $val;
  39.         }
  40.         if(is_array($data)){
  41.             extract($data);
  42.         }
  43.         if(isset($id)){
  44.             $this->id $id;
  45.         }
  46.         else{
  47.             $id uniqid();
  48.             $this->id $id;
  49.         }
  50.         if(!isset($title) || $title===false){
  51.             $from $this->id;
  52.             if(isset($this->fieldname) && strlen($this->fieldname)){
  53.                 $from $this->fieldname;
  54.             }
  55.             if(isset($this->label) && strlen($this->fieldname)){
  56.                 $from $this->label;
  57.             }
  58.             $title str_replace("_"," ",$from);
  59.             $title=ucfirst($title);
  60.             $this->title $title;
  61.         }
  62.         if(!isset($type) || !$type){
  63.             $type $this->getClassName();
  64.         }
  65.         $this->type $type;
  66.         $this->title $title;
  67.         $this->data $data;
  68.         //$this->enabled = $enabled;
  69.         $this->className =$this->getFullClassName();
  70.         $this->initialize();
  71.     }
  72.     protected function setTitle($title false){
  73.         if(!isset($title) || $title===false){
  74.             $from $this->id;
  75.             if(isset($this->fieldname) && strlen($this->fieldname)){
  76.                 $from $this->fieldname;
  77.             }
  78.             if(isset($this->label) && strlen($this->fieldname)){
  79.                 $from $this->label;
  80.             }
  81.             $title str_replace("_"," ",$from);
  82.             $title=ucfirst($title);
  83.             $this->title $title;
  84.         }
  85.         return $title;
  86.     }
  87.     protected function initialize(){
  88.     }
  89.     public static function create($data){
  90.         $data = (array) $data;
  91.         $cls get_called_class();
  92.         $object = new $cls();
  93.         $type $object->type;
  94.         $object->loadData($data);
  95.         $object->type $type;
  96.         if(isset($data['title'])){
  97.             $object->setTitle($data['title']);
  98.         }
  99.         return $object;
  100.     }
  101.     public function addField($id$type "element"$title false$data = array(), $enabled=true ): Element
  102.     {
  103.         if($title===false){
  104.             $title str_replace("_"," ",$this->id);
  105.             $title=ucfirst($id);
  106.         }
  107.         $field = new Element($id$type$title,$data,$enabled);
  108.         //dd($field);
  109.         $this->fields[$id] = $field;
  110.         return $field;
  111.     }
  112.     public function addElement(Element $element): Element
  113.     {
  114.         $this->elements[] = $element;
  115.         return $element;
  116.     }
  117.     public function setTemplate(string $template): void
  118.     {
  119.         $this->template $template;
  120.     }
  121.     public function getTemplate(): string
  122.     {
  123.         return $this->template;
  124.     }
  125.     public function setIndex($i)
  126.     {
  127.         $this->selectedIndex $i;
  128.         foreach($this->data[$i] as $key => $val){
  129.             $this->$key $val;
  130.         }
  131.     }
  132.     public function getFullClassName($object false){
  133.         if($object===false){
  134.             $object $this;
  135.         }
  136.         return get_class($object);
  137.     }
  138.     public function getClassName($object false){
  139.         if($object===false){
  140.             $object $this;
  141.         }
  142.         $classNameget_class($object);
  143.         return substr($className,strrpos($className,"\\")+1);
  144.     }
  145.     public function getIcon($icon false$class=""){
  146.         if(!$icon){
  147.             $icon $this->icon;
  148.         }
  149.         if(strlen($icon)>&& substr($icon,0,2)=="fa"){
  150.             $parts explode("-",$icon);
  151.             $icon substr($icon,strlen($parts[0])+1);
  152.             if($class=="name"){
  153.                 $icon "{$parts[0]} fa-{$icon}";
  154.             }
  155.             else{
  156.                 $icon "<i class='{$parts[0]} fa-{$icon} {$class}'></i>";
  157.             }
  158.         }
  159.         return $icon;
  160.     }
  161.     public function getHtml(){
  162.         $this->preRender();
  163.         $this->rendered true;
  164.         return $this->renderTwig();
  165.         //$reflection = new \ReflectionClass($this);
  166.         ////$pad = $reflection->getFileName();
  167.         //$pad = str_replace("/Element/","/Resources/views/Element/", $reflection->getFileName());
  168.         //$pad = str_replace(".php",".html.twig",$pad);
  169.         //if(file_exists($pad)){
  170.         //    $locator = "/Resources/views/";
  171.         //    $pad = "@VisualMediaBart/".substr($pad,strpos($pad,$locator)+strlen($locator));
  172.         //    //dd($pad);
  173.         //    //dd($this);
  174.         //    return RootController::renderTwig($this,$pad);
  175.         //}
  176.         //else{
  177.         //    return "<div id='{$this->id}' class='element_Element'>not found:{$pad}</div>";
  178.         //}
  179.     }
  180.     public function findTemplate($replace "/Resources/views/Element/"){
  181.         $reflection = new \ReflectionClass($this);
  182.         $pad str_replace("/Element/",$replace$reflection->getFileName());
  183.         $pad str_replace(".php",".html.twig",$pad);
  184.         return $pad;
  185.     }
  186.     public function renderTwig($filename false)
  187.     {
  188.         $locator "/Resources/views/";
  189.         $extension ".html.twig";
  190.         if($this->twig_variant){
  191.             $extension "_".$this->twig_variant.$extension;
  192.         }
  193.         $reflection = new \ReflectionClass($this);
  194.         //$pad = $reflection->getFileName();
  195.         $pad str_replace("/Element/","/Resources/views/Element/"$reflection->getFileName());
  196.         $pad str_replace(".php",$extension,$pad);
  197.         $oldpad $pad;
  198.         $pad_lokaal str_replace("/src/Element/","/templates/Element/"$reflection->getFileName());
  199.         $pad_lokaal str_replace(".php",$extension,$pad_lokaal);
  200.         if($this->template) {
  201.             return RootController::renderTwig($this$this->template);
  202.         }
  203.         else if(isset($this->twig_template) && strlen($this->twig_template)>0){
  204.             //dd($this->twig_template);
  205.             $pad $this->twig_template;
  206.             $locator "public_html/templates/";
  207.             if(strpos($pad,$locator)>0){
  208.                 $pad substr($pad,strpos($pad,$locator) + strlen($locator));
  209.             }
  210.             if(file_exists($locator.$pad)){
  211.                 return RootController::renderTwig($this$pad);
  212.             }
  213.             else{
  214.                 $pad $oldpad;
  215.             }
  216.             
  217.             //dd($pad);
  218.         }
  219.         elseif($filename){
  220.             $pad str_replace($this->getClassName(),$filename,$pad);
  221.         }
  222.         //dd($pad_lokaal);
  223.         if(file_exists($pad_lokaal)){
  224.             $pad "".substr($pad,strpos($pad,$locator)+strlen($locator));
  225.             //dd($pad);
  226.             //dd($this);
  227.             return RootController::renderTwig($this,$pad);
  228.         }
  229.         //dump($pad);
  230.         if(file_exists($pad)){
  231.             $namespace 'VisualMediaBart';
  232.             $namespace sprintf('VisualMedia%s'str_replace('Bundle'''explode('\\'get_class($this))[1] ?? null));
  233.             $locator "/Resources/views/";
  234.             $pad "@{$namespace}/".substr($pad,strpos($pad,$locator)+strlen($locator));
  235.             return RootController::renderTwig($this,$pad);
  236.         }
  237.         else{
  238.             $this->rendered false;
  239.             return "<div id='{$this->id}' class='element_Element'>not found:{$pad}</div>";
  240.         }
  241.     }
  242.     public function preRender(){
  243.     }
  244.     public static function mergeData($text$data$open "["$close "]"){
  245.         foreach($data as $key => $val){
  246.             $text str_replace("[".$key."]",$val,$text);
  247.         }
  248.         //preg_match_all("/\{[A-Z0-9_]+\}+/", $text, $matches);
  249.         //foreach($matches as $match_group) {
  250.         //    foreach($match_group as $match) {
  251.         //        $field = str_replace("}", "", $match);
  252.         //        $field = str_replace("{", "", $field);
  253.         //        $html = str_replace($match,$data[$field],$html);
  254.         //    }
  255.         //}
  256.         return $text;
  257.     }
  258.     public function twig(){
  259.         return new \Twig\Markup($this->getHtml() , 'UTF-8');
  260.     }
  261.     public function loadData($data){
  262.         if($data){
  263.             //dump($data);
  264.             foreach($data as $key => $value){
  265.                 //dump($key);
  266.                 $this->$key $value;
  267.             }
  268.         }
  269.     }
  270.     public function getData(){
  271.         $data = [];
  272.         foreach($this as $key => $val){
  273.             $data[$key] = $val;
  274.         }
  275.         return $data;
  276.     }
  277.     public static function fromArray($data){
  278.         if(!is_array($data)){
  279.             $data = (array) $data;
  280.         }
  281.         //dump($data);
  282.         if(isset($data['id'])){
  283.             $element = new $data['className']($data);
  284.             $element->loadData($data);
  285.             if($element->elements){
  286.                 //dump($element->elements);
  287.                 for($i=0;$i<count($element->elements);$i++){
  288.                     $element->elements[$i] = Element::fromArray($element->elements[$i]);
  289.                 }
  290.             }
  291.             return $element;
  292.         }
  293.         else if (is_array($data)){
  294.             for($i=0;$i<count($data);$i++){
  295.                 $data[$i] = Element::fromArray($data[$i]);
  296.             }
  297.             return $data;
  298.         }
  299.     }
  300.     public function findElement($id){
  301.         if($this->id==$id){
  302.             return $this;
  303.         }
  304.         if(is_array($this->elements)){
  305.             foreach($this->elements as $child){
  306.                 if($found $child->findElement($id)){
  307.                     return $found;
  308.                 }
  309.             }
  310.         }
  311.         return false;
  312.     }
  313.     public function replaceElement($new$id=false){
  314.         if(!$id){
  315.             $id $new->id;
  316.         }
  317.         else if(is_object($id)){
  318.             $id $id->id;
  319.         }
  320.         if(is_array($this->elements)){
  321.             foreach($this->elements as $key => $child){
  322.                 if($child->id==$id){
  323.                     $this->elements[$key] = $new;
  324.                     return true;
  325.                 }
  326.             }
  327.         }
  328.         if(is_array($this->elements)){
  329.             foreach($this->elements as $child){
  330.                 if($child->replaceElement($new$id)){
  331.                     return true;
  332.                 }
  333.             }
  334.         }
  335.         return false;
  336.     }
  337.     public function findBy($name$value){
  338.         //dump($this->$name."!=".$value);
  339.         if(isset($this->$name) && $this->$name==$value){
  340.             return $this;
  341.         }
  342.         foreach($this->elements as $child){
  343.             if($found $child->findBy($name,$value)){
  344.                 return $found;
  345.             }
  346.         }
  347.         return false;
  348.     }
  349.     public function findElementOfType($type){
  350.         return $this->findBy("type",$type);
  351.     }
  352.     public function allow($task 'display'){
  353.         return true;
  354.     }
  355.     /**************************** TODO: CLEAN ***********************************/
  356.     public function init($data){
  357.         $data = (array) $data;
  358.         $cls get_called_class();
  359.         $object = new $cls();
  360.         $type $object->type;
  361.         $object->loadData($data);
  362.         $object->type $type;
  363.         return $object;
  364.     }
  365.     public const USER_PUBLIC 0;        //anonymous visitors
  366.     public const USER_USER 1;            //registered visitors
  367.     public const USER_ADMIN 2;            //client admin
  368.     public const USER_SUPER_ADMIN 3;    //developers / dappr
  369. }
  370. ?>