*/ public $params = array(); /** * Panel type * * @var string */ public $panel = ""; /** * @var string */ public $claseManejadora = null; /** * @var Smarty_Internal_Template */ public $template; /** * Initialize the PluginHandler object * * @param Smarty_Phrame $params * @param Smarty_Internal_Template $template */ public function __construct(&$params, &$template) { $this->stack = &$template->smarty->_cache["_tag_stack"]; $this->params = $params; $this->template = $template; $this->setGlobalAttrs(); $this->jsonData = &$this->stack[0][1]['datosJSON'][$this->claseManejadora]; $this->pathLength = count($this->componentPath); $this->attributes = [ "data-gvhCWSource" => $this->componentPath[count($this->componentPath)], "data-gvhClaseManejadora" => $this->claseManejadora, "data-gvhPanelOn" => $this->panel ]; $this->tplAssigns = [ self::PARAMS_ASSIGN => &$this->tplParameters, self::CONTENT_ASSIGN => &$this->content, self::ATTR_ASSIGN => &$this->attributes ]; } /** * Get an object or string with all attributes of component * * @param bool $toString * * @return Array|String */ public function getAttributes($toString=false) { if ($toString) { $attrStr = ""; foreach ($this->attributes as $attr => $attrValue) { $attrStr .= "$attr=\"$attrValue\" "; } return $attrStr; } else return $this->attributes; } /** * Get a component or component tag from components stack * * @param Integer $stack_position * @param Boolean $onlyTag * * @return Array|String */ public function getComponentFromStack($stackPos, $onlyTag=false) { if ($stackPos > 0 || $stackPos > $this->pathLength) { return $onlyTag ? $this->stack[$stackPos][0] : $this->stack[$stackPos]; } else return null; } /** * Set an attribute to component * * @param string $attr * @param string $attrValue */ public function setAttribute($attr, $attrValue) { $this->attributes += [$attr => $attrValue]; } /** * Set component content * * @param array $content */ public function setContent($content) { $this->content = $content; } /** * Get component content * * @return array */ public function getContent() { return $this->content; } public function getFromStack($position) { return isset($this->stack[$position]) ? $this->stack[$position][1] : null; } /** * Adds a parameter into tpl options * * @param string|array $keys * @param string|array|int|bool|null $value * @param string $keyName */ public function addParameter($keys, $value, $keyName=null, $assign=null) { if (!is_array($keys)) $keys = [$keys]; if ($assign && !isset($this->tplAssigns[$assign])) { throw new RuntimeException("The assign name does not exists"); } if ($assign) $section = &$this->tplAssigns[$assign]; else $section = &$this->tplParameters; foreach ($keys as $key) { if (!isset($section[$key])) $section[$key] = []; else $section = $section[$key]; } $key = $keyName ? $keyName : $keys[count($keys) - 1]; $section[$key] = $value; } /** * Adds the parameter from component parameters, if parameter is not set, * it will be assign the $noSetValue argument * * @param string|array $paramKey * @param string|array|int|bool|null $noSetValue */ public function addParameterFromParams($paramsKeys, $noSetValue=null, $keyName=null, $assign=null) { if (!is_array($paramsKeys)) $paramsKeys = [$paramsKeys]; $paramsSection = $this->params; foreach ($paramsKeys as $paramKey) { if (!isset($paramsSection[$paramKey])) { $paramsSection = null; break; } else $paramsSection = $paramsSection[$paramKey]; } $keyName = $keyName ? $keyName : $paramsKeys[count($paramsKeys)-1]; $value = $paramsSection ? $paramsSection : $noSetValue; if ($assign && isset($this->tplAssigns[$assign])) { $this->tplAssigns[$assign][$keyName] = $value; } else if (!$assign) $this->tplParameters[$keyName] = $value; else throw new RuntimeException("The name of assign does not exists"); } public function mergeParameters($newParamenters) { array_merge($this->params, $newParamenters); } /** * Get parameter from smarty template * * @param string $key * * @return (array|string) */ public function getParameter($keys) { if (!is_array($keys)) $keys = [$keys]; $section = $this->tplParameters; foreach ($keys as $key) { if (!isset($section[$key])) return null; else $section = $section[$key]; } return $section; } /** * Get all parameter from smarty template * * @return array */ public function getParameters() { return $this->tplParameters; } private function setGlobalAttrs() { foreach ($this->stack as $component) { array_push($this->componentPath, $component[0]); switch ($component[0]) { case "cwventana": $this->modal = $this->getAttrFromComponent($component, "modal"); break; case "cwpanel": $this->panel = $this->getAttrFromComponent($component, "id"); $this->claseManejadora = $this->getAttrFromComponent($component, "claseManejadora"); } } } private function getAttrFromComponent($component, $attr) { return isset($component[1]) && isset($component[1][$attr]) ? $component[1][$attr] : null; } public function setData($key, $value) { $this->setCustomData("data", $key, $value); } public function setDataPerform($key, $value) { $this->setCustomData("dataPerform", $key, $value); } public function setComponent($key, $value) { $this->setCustomData("component", $key, $value); } public function setDataRequired($key, $value) { $this->setCustomData("dataRequired", $key, $value); } public function setCustomData($customKeys, $key, $value) { if (is_array($customKeys)) { $section = &$this->jsonData[$this->panel]; foreach ($customKeys as $customKey) { if (!isset($section[$customKey])) $section[$customKey] = array(); $section = &$section[$customKey]; } $section[$key] = $value; } else { if (isset($this->jsonData[$this->panel][$customKeys])) { $this->jsonData[$this->panel][$customKeys][$key] = $value; } else { $this->jsonData[$this->panel][$customKeys] = array([$key => $value]); } } } public function addArrayToAssigns($name) { if (!isset($this->tplAssigns[$name])) { $this->tplAssigns += array($name => array()); } else throw new RuntimeException("The name of array already exists"); } /** * Assign arrays to smarty template, these are params with assigned tpl * parameters, attributes, attributes converted to string and the content * component. * * The arrays names are: params, content, attr, attrstr */ public function assign() { $assigns = $this->tplAssigns + ["attrstr" => $this->getAttributes(true)]; foreach ($assigns as $assign => $value) $this->template->assign($assign, $value); } public function getAssign($name) { return isset($this->tplAssigns[$name]) ? $this->tplAssigns[$name] : null; } /** * Unassing the assigned arrays with the method assign() * * These arrays are: params, content, attr, attrstr */ public function clear() { $assigns = array_keys($this->tplAssigns + ["attrstr"]); foreach($assigns as $assign) $this->template->clearAssign($assign); } }