From 9cc12bdb01396ed3eff93a2f6353e109000e9645 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Thu, 2 Jul 2015 11:24:58 -0500 Subject: [PATCH] variable: Rework the variable replacement system getVar() is now called first, then object properties and array properties are considered, and then getTag() is considered if none of the other options work. This allows for more predictable results using the variable replacement system, and allows ::getVar() to override every other means to use variables in email templates. --- include/class.dept.php | 9 ++++- include/class.format.php | 3 -- include/class.organization.php | 3 -- include/class.team.php | 15 +++++---- include/class.thread.php | 5 --- include/class.ticket.php | 4 --- include/class.user.php | 3 -- include/class.variable.php | 61 +++++++++++++++++++--------------- 8 files changed, 50 insertions(+), 53 deletions(-) diff --git a/include/class.dept.php b/include/class.dept.php index 8131be44f..d06bef503 100644 --- a/include/class.dept.php +++ b/include/class.dept.php @@ -87,6 +87,13 @@ implements TemplateVariable { ); } + function getVar($tag) { + switch ($tag) { + case 'members': + return new UserList($this->getMembers()->all()); + } + } + function getId() { return $this->id; } @@ -187,7 +194,7 @@ implements TemplateVariable { $this->_members = $members; } - return new UserList($this->_members->all()); + return $this->_members; } function getAvailableMembers() { diff --git a/include/class.format.php b/include/class.format.php index 903256a6f..85b1fd9ae 100644 --- a/include/class.format.php +++ b/include/class.format.php @@ -893,9 +893,6 @@ implements TemplateVariable { function getVar($what) { global $cfg; - if (method_exists($this, 'get' . ucfirst($what))) - return call_user_func(array($this, 'get'.ucfirst($what))); - // TODO: Rebase date format so that locale is discovered HERE. switch ($what) { diff --git a/include/class.organization.php b/include/class.organization.php index 6d3ceaeb1..e1514f19d 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -293,9 +293,6 @@ implements TemplateVariable { } function getVar($tag) { - if($tag && is_callable(array($this, 'get'.ucfirst($tag)))) - return call_user_func(array($this, 'get'.ucfirst($tag))); - $tag = mb_strtolower($tag); foreach ($this->getDynamicData() as $e) if ($a = $e->getAnswer($tag)) diff --git a/include/class.team.php b/include/class.team.php index 0d5e7013f..1165ecc8a 100644 --- a/include/class.team.php +++ b/include/class.team.php @@ -26,7 +26,6 @@ implements TemplateVariable { 'constraint' => array('lead_id' => 'Staff.staff_id'), ), 'members' => array( - 'null' => true, 'list' => true, 'reverse' => 'TeamMember.team', ), @@ -58,6 +57,13 @@ implements TemplateVariable { ); } + function getVar($tag) { + switch ($tag) { + case 'members': + return new UserList($this->getMembers()->all()); + } + } + function getId() { return $this->team_id; } @@ -74,14 +80,12 @@ implements TemplateVariable { } function getMembers() { - if (!isset($this->_members)) { $this->_members = array(); foreach ($this->members as $m) $this->_members[] = $m->staff; } - - return new UserList($this->_members); + return $this->_members; } function hasMember($staff) { @@ -148,9 +152,6 @@ implements TemplateVariable { $errors['name']=__('Team name already exists'); } - if ($errors) - return false; - // Reset team lead if they're getting removed if (isset($this->lead_id) && $this->lead_id == $vars['lead_id'] diff --git a/include/class.thread.php b/include/class.thread.php index 7e84d5baa..4d45e5f95 100644 --- a/include/class.thread.php +++ b/include/class.thread.php @@ -1049,9 +1049,6 @@ implements TemplateVariable { } function getVar($tag) { - if ($tag && is_callable(array($this, 'get'.ucfirst($tag)))) - return call_user_func(array($this, 'get'.ucfirst($tag))); - switch(strtolower($tag)) { case 'create_date': return new FormattedDate($this->getCreateDate()); @@ -1060,8 +1057,6 @@ implements TemplateVariable { case 'files': throw new OOBContent(OOBContent::FILES, $this->attachments->all()); } - - return false; } static function getVarScope() { diff --git a/include/class.ticket.php b/include/class.ticket.php index bcacc0dd3..b19fc6e9f 100644 --- a/include/class.ticket.php +++ b/include/class.ticket.php @@ -1663,9 +1663,6 @@ implements RestrictedAccess, Threadable { function getVar($tag) { global $cfg; - if ($tag && is_callable(array($this, 'get'.ucfirst($tag)))) - return call_user_func(array($this, 'get'.ucfirst($tag))); - switch(mb_strtolower($tag)) { case 'phone': case 'phone_number': @@ -1703,7 +1700,6 @@ implements RestrictedAccess, Threadable { // answer is coerced into text return $this->_answers[$tag]; } - return false; } static function getVarScope() { diff --git a/include/class.user.php b/include/class.user.php index ad3302a2c..d9a732861 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -360,9 +360,6 @@ implements TemplateVariable { } function getVar($tag) { - if($tag && is_callable(array($this, 'get'.ucfirst($tag)))) - return call_user_func(array($this, 'get'.ucfirst($tag))); - $tag = mb_strtolower($tag); foreach ($this->getDynamicData() as $e) if ($a = $e->getAnswer($tag)) diff --git a/include/class.variable.php b/include/class.variable.php index 869c87f9b..0efd70d71 100644 --- a/include/class.variable.php +++ b/include/class.variable.php @@ -59,37 +59,44 @@ class VariableReplacer { function getVar($obj, $var) { - if(!$obj) return ""; + if (!$obj) + return ""; - if (!$var) { - if (method_exists($obj, 'asVar')) - return call_user_func(array($obj, 'asVar'), $this); - elseif (method_exists($obj, '__toString')) - return (string) $obj; + // Order or resolving %{... .tag.remainder} + // 1. $obj[$tag] + // 2. $obj->tag + // 3. $obj->getVar(tag) + // 4. $obj->getTag() + @list($tag, $remainder) = explode('.', $var ?: '', 2); + $tag = mb_strtolower($tag); + $rv = null; + + if (!is_object($obj)) { + if ($tag && is_array($obj) && array_key_exists($tag, $obj)) + $rv = $obj[$tag]; + else + // Not able to continue the lookup + return ''; } - - list($v, $part) = explode('.', $var, 2); - if ($v && is_callable(array($obj, 'get'.ucfirst($v)))) { - $rv = call_user_func(array($obj, 'get'.ucfirst($v))); - if(!$rv || !is_object($rv)) - return $rv; - - return $this->getVar($rv, $part); + else { + if (!$var) { + if (method_exists($obj, 'asVar')) + return call_user_func(array($obj, 'asVar'), $this); + elseif (method_exists($obj, '__toString')) + return (string) $obj; + } + if (method_exists($obj, 'getVar')) { + $rv = $obj->getVar($tag, $this); + } + if (!isset($rv) && property_exists($obj, $tag)) { + $rv = $obj->{$tag}; + } + if (!isset($rv) && is_callable(array($obj, 'get'.ucfirst($tag)))) { + $rv = call_user_func(array($obj, 'get'.ucfirst($tag))); + } } - if (is_array($obj) && isset($obj[$v])) - return $obj[$v]; - - if (!$var || !method_exists($obj, 'getVar')) - return ""; - - list($tag, $remainder) = explode('.', $var, 2); - if(($rv = call_user_func(array($obj, 'getVar'), $tag, $this))===false) - return ""; - - if(!is_object($rv)) - return $rv; - + // Recurse with $rv return $this->getVar($rv, $remainder); } -- GitLab