<?php
 namespace Engine\Form\Button; abstract class GeneralAbstract { protected $class = "general"; protected $label = ""; protected $id = ""; protected $tooltip = ""; protected $dontRender = false; protected $disabled = false; protected $settings = []; protected $name=''; protected $style = [ 'width' => null, 'height' => null, 'CSSClass' => null, 'special' => false, ]; protected $defaultStyle = []; protected $computedStyle = []; protected $parent = null; static public function create($label=null) { return new static($label); } public function __construct($label=null, $name='') { $this->style['CSSClass'] = "button-{$this->class}"; if ($label){ $this->setLabel($label); } if ($name) { $this->setName($name); } } public function isContainer() { return false; } public function getComputedStyle() { return $this->computedStyle; } public function getStyles() { return $this->style; } public function getStyle($key) { return $this->style[$key]; } public function getWidth() { return $this->getStyle('width'); } public function setWidth($width) { return $this->setStyle('width', $width); } public function setParent(\Engine\Form\Container\GeneralAbstract $parent) { return $this->parent = $parent; } public function setName($name) { $this->name=$name; return $this; } public function setStyle($key, $value=true) { $this->style[$key] = $value; return $this; } public function getLabel() { return $this->label; } public function setLabel($label) { $this->label = $label; return $this; } public function getTooltip() { return $this->tooltip; } public function setTooltip($tooltip) { $this->tooltip = $tooltip; return $this; } public function getId() { return $this->elementId(); } public function setId($id) { $this->id = $id; return $this; } public function elementId() { $id = $this->id; if(!$id){ $id = \Engine\Form\Container\GeneralStaticMethods::generateUniqueID("button"); $this->id = $id; } return $id; } public function setInvisible($dontRender=true) { $this->dontRender = $dontRender; return $this; } public function getInvisible() { return $this->dontRender; } public function addEvent($event, $jsCallback) { if (!isset($this->settings['events'][$event])) { $this->settings['events'][$event] = []; } $this->settings['events'][$event][] = $jsCallback; return $this; } public function onClick($jsCallback) { return $this->addEvent('onClick', $jsCallback); } public function getTemplatePath($templateClass = null) { if($templateClass==null){ $templateClass = $this->class; } $templateFile = ""; if (is_file(APP_REAL_PATH."/_templates/application/formField/buttons/{$templateClass}.tpl")) { $templateFile = APP_REAL_PATH."/_templates/application/formField/buttons/{$templateClass}.tpl"; } elseif (is_file(APP_REAL_PATH."/_templates/engine/formField/buttons/{$templateClass}.tpl")) { $templateFile = APP_REAL_PATH."/_templates/engine/formField/buttons/{$templateClass}.tpl"; } if (!$templateFile) { throw new \Exception("Cannot find template for button element '{$templateClass}'"); } return $templateFile; } public function getTemplateParams($templateClass = null, $parentElement = null) { $id = $this->elementId(); $style = $this->style; $this->computedStyle = $style; $_LANG = \App\Loader_Lang::getInstance()->getAll(); $translation = $this->label; switch($this->label){ case 'close': case 'cancel': case 'save': $translation = $_LANG[$this->label]; break; case 'insert': $translation = $_LANG['add']; break; case 'update': $translation = $_LANG['edit']; break; } return [ 'class' => $this->class, 'name' => $this->name, 'label' => $translation, 'id' => $id, 'settings' => $this->settings, 'disabled' => $this->disabled, 'style' => $style, 'parent' => $this->parent, 'methods' => $this, 'element' => $this, ]; } public function render($templateClass = null, $parentElement = null) { if($this->dontRender){ return ''; } if($templateClass==null){ $templateClass = $this->class; } $smarty = \Engine\Smarty\SmartyMultiton::getInstance(); $oldElement = $smarty->getTemplateVars('button'); $smarty->assign('button', $this->getTemplateParams($templateClass, $parentElement)); $return = $smarty->fetch($this->getTemplatePath($templateClass)); $smarty->clearAssign('button'); if($oldElement){ $smarty->assign('button', $oldElement); } return $return; } } 