From a9d446e27e7670d03cac49d0e730d73e89af00b2 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Mon, 20 Apr 2015 16:25:41 -0500 Subject: [PATCH] templates: Fix up custom list selections in templates --- include/class.dynamic_forms.php | 18 ++++++++++++---- include/class.list.php | 2 +- include/class.ticket.php | 4 ++-- include/class.variable.php | 38 +++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 10419a209..a60cf4fd8 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -1265,13 +1265,14 @@ class DynamicFormEntryAnswer extends VerySimpleModel { } function asVar() { - return (is_object($this->getValue())) - ? $this->getValue() : $this->toString(); + return $this->getField()->asVar( + $this->get('value'), $this->get('value_id') + ); } function getVar($tag) { - if (is_object($this->getValue()) && method_exists($this->getValue(), 'getVar')) - return $this->getValue()->getVar($tag); + if (is_object($var = $this->asVar()) && method_exists($var, 'getVar')) + return $var->getVar($tag); } function __toString() { @@ -1372,6 +1373,15 @@ class SelectionField extends FormField { return $value; } + function asVar($value, $id=false) { + $values = $this->to_php($value, $id); + if (is_array($values)) { + return new PlaceholderList($this->getList()->getAllItems() + ->filter(array('id__in' => array_keys($values))) + ); + } + } + function hasSubFields() { return $this->getList()->getForm(); } diff --git a/include/class.list.php b/include/class.list.php index 5a1092ea1..950efdb6b 100644 --- a/include/class.list.php +++ b/include/class.list.php @@ -737,7 +737,7 @@ class DynamicListItem extends VerySimpleModel implements CustomListItem { $name = mb_strtolower($name); foreach ($this->getConfigurationForm()->getFields() as $field) { if (mb_strtolower($field->get('name')) == $name) - return $config[$field->get('id')]; + return $field->asVar($config[$field->get('id')]); } } diff --git a/include/class.ticket.php b/include/class.ticket.php index 98f299764..dc2d831e1 100644 --- a/include/class.ticket.php +++ b/include/class.ticket.php @@ -294,8 +294,8 @@ implements RestrictedAccess, Threadable, TemplateVariable { if (!$this->_answers) { foreach (DynamicFormEntry::forTicket($this->getId(), true) as $form) { foreach ($form->getAnswers() as $answer) { - $tag = mb_strtolower($answer->getField()->get('name')) - ?: 'field.' . $answer->getField()->get('id'); + $tag = mb_strtolower($answer->field->name) + ?: 'field.' . $answer->field->id; $this->_answers[$tag] = $answer; } } diff --git a/include/class.variable.php b/include/class.variable.php index 051f9f6a5..55fac7a2c 100644 --- a/include/class.variable.php +++ b/include/class.variable.php @@ -212,6 +212,44 @@ class VariableReplacer { } } +class PlaceholderList +/* implements TemplateVariable */ { + var $items; + + function __construct($items) { + $this->items = $items; + } + + function asVar() { + $items = array(); + foreach ($this->items as $I) { + if (method_exists($I, 'asVar')) { + $items[] = $I->asVar(); + } + else { + $items[] = (string) $I; + } + } + return implode(',', $items); + } + + function getVar($tag) { + $items = array(); + foreach ($this->items as $I) { + if (is_object($I) && method_exists($I, 'get'.ucfirst($tag))) { + $items[] = call_user_func(array($I, 'get'.ucfirst($tag))); + } + elseif (method_exists($I, 'getVar')) { + $items[] = $I->getVar($tag); + } + } + if (count($items) == 1) { + return $items[0]; + } + return new static($items); + } +} + interface TemplateVariable { // function asVar(); — not absolutely required // function getVar($name); — not absolutely required -- GitLab