function recursiveRender() { $template = $this->add('GiTemplate'); $template->loadTemplate('view/report/balancesheet'); $temp = $template->render(); /*Find [{}] String from template*/ // preg_match_all("/{([^:}]*):?([^}]*)}/", $temp,$match); // preg_match_all("/\[[^\]]*\]/", $temp,$match); preg_match_all('/( { ( (?: [^{}]* | (?1) )* ) } )/x', $temp, $match); /*Include EvalMath Class*/ $x = new \Webit\Util\EvalMath\EvalMath(); /*Get value from specific BalanceSheet , Group And Ledger save in array*/ $array_model = []; foreach ($match[2] as $m) { $value = explode(":", $m); switch ($value[0]) { case 'HEAD': $model = $this->add('xepan\\accounts\\Model_BalanceSheet')->addCondition('name', $value[1])->tryLoadAny(); $array_model[$value[0] . ':' . $value[1]] = $model->getBalance($this->from_date, $this->to_date); break; case 'GROUP': $model = $this->add('xepan\\accounts\\Model_Group')->addCondition('name', $value[1])->tryLoadAny(); $array_model[$value[0] . ':' . $value[1]] = $model->getBalance($this->from_date, $this->to_date); break; case 'LEDGER': $model = $this->add('xepan\\accounts\\Model_Ledger')->addCondition('name', $value[1])->tryLoadAny(); $array_model[$value[0] . ':' . $value[1]] = $model->getBalance($this->from_date, $this->to_date); break; } } /*Find Actual Value From Template String & Eval */ $result = str_replace($match[0], $array_model, $temp); preg_match_all("/\\[[^\\]]*\\]/", $result, $res); $r = []; foreach ($res[0] as $str) { $res = substr($str, 1, -1); if ($res) { $r[$str] = $x->evaluate($res); } } /*set Final Value From String */ foreach ($r as $key => $value) { $result = str_replace($key, $value, $result); } $this->setHTML($result); parent::recursiveRender(); }
/** * Return the value of a custom field * * @param string $key * * @return mixed */ public function getCustomField($key) { $var_name = "_customfield{$key}"; if (property_exists($this, $var_name)) { $customtype = CustomDatatype::getByKey($key); if ($customtype->getType() == CustomDatatype::CALCULATED_FIELD) { $result = null; $options = $customtype->getOptions(); if (!empty($options)) { $formula = array_pop($options)->getValue(); preg_match_all('/{([[:alnum:]]+)}/', $formula, $matches); $hasValues = false; for ($i = 0; $i < count($matches[0]); $i++) { $value = $this->getCustomField($matches[1][$i]); if ($value instanceof \thebuggenie\core\entities\CustomDatatypeOption) { $value = $value->getValue(); } if (is_numeric($value)) { $hasValues = true; } $value = floatval($value); $formula = str_replace($matches[0][$i], $value, $formula); } // Check to verify formula only includes numbers and allowed operators if ($hasValues && !preg_match('/[^0-9\\+-\\/*\\(\\)%]/', $formula)) { try { $m = new \Webit\Util\EvalMath\EvalMath(); $m->suppress_errors = true; $result = $m->evaluate($formula); if (!empty($m->last_error)) { $result = $m->last_error; } else { $result = round($result, 2); } } catch (\Exception $e) { $result = 'N/A'; } } } return $result; } elseif ($this->{$var_name} && $customtype->hasCustomOptions() && !$this->{$var_name} instanceof \thebuggenie\core\entities\CustomDatatypeOption) { $this->{$var_name} = tables\CustomFieldOptions::getTable()->selectById($this->{$var_name}); } elseif ($this->{$var_name} && $customtype->hasPredefinedOptions() && !$this->{$var_name} instanceof \thebuggenie\core\entities\common\Identifiable) { try { switch ($customtype->getType()) { case CustomDatatype::EDITIONS_CHOICE: $this->{$var_name} = tables\Editions::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::COMPONENTS_CHOICE: $this->{$var_name} = tables\Components::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::RELEASES_CHOICE: $this->{$var_name} = tables\Builds::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::MILESTONE_CHOICE: $this->{$var_name} = tables\Milestones::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::CLIENT_CHOICE: $this->{$var_name} = tables\Clients::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::USER_CHOICE: $this->{$var_name} = tables\Users::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::TEAM_CHOICE: $this->{$var_name} = tables\Teams::getTable()->selectById($this->{$var_name}); break; case CustomDatatype::STATUS_CHOICE: $this->{$var_name} = Status::getB2DBTable()->selectById($this->{$var_name}); break; } } catch (\Exception $e) { } } return $this->{$var_name}; } else { return null; } }