From b8ce67b9b275b93237a94e5fa17802c65578652d Mon Sep 17 00:00:00 2001 From: Bruno17 Date: Fri, 5 Mar 2021 17:19:29 +0100 Subject: [PATCH 1/4] properties for @CHUNK and @SNIPPET binding --- core/src/Revolution/modTemplateVar.php | 30 +++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/core/src/Revolution/modTemplateVar.php b/core/src/Revolution/modTemplateVar.php index 595c00bca97..c7b99b7acf5 100644 --- a/core/src/Revolution/modTemplateVar.php +++ b/core/src/Revolution/modTemplateVar.php @@ -52,6 +52,7 @@ class modTemplateVar extends modElement public $bindings = [ 'FILE', 'CHUNK', + 'SNIPPET', 'DOCUMENT', 'RESOURCE', 'SELECT', @@ -833,11 +834,11 @@ public function getBindingDataFromValue($value) $cmd = false; $param = ''; if (substr($nvalue, 0, 1) == '@') { - list($cmd, $param) = $this->parseBinding($nvalue); + list($cmd, $param, $properties) = $this->parseBinding($nvalue); $cmd = trim($cmd); } - return ['cmd' => $cmd, 'param' => $param]; + return ['cmd' => $cmd, 'param' => $param, 'properties' => $properties]; } /** @@ -870,6 +871,7 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true } $cmd = $bdata['cmd']; $param = !empty($bdata['param']) ? $bdata['param'] : null; + $properties = !empty($bdata['properties']) ? $bdata['properties'] : []; switch ($cmd) { case 'FILE': if ($preProcess) { @@ -879,9 +881,15 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true case 'CHUNK': /* retrieve a chunk and process it's content */ if ($preProcess) { - $output = $this->xpdo->getChunk($param); + $output = $this->xpdo->getChunk($param, $properties); } break; + + case 'SNIPPET': + if ($preProcess) { + $output = $this->xpdo->runSnippet($param, $properties); + } + break; case 'RESOURCE': case 'DOCUMENT': /* retrieve a document and process it's content */ @@ -986,11 +994,27 @@ public function parseBinding($binding_string) $match = []; $binding_string = trim($binding_string); $regexp = '/@(' . implode('|', $this->bindings) . ')\s*(.*)/is'; /* Split binding on whitespace */ + if (preg_match($regexp, $binding_string, $match)) { /* We can't return the match array directly because the first element is the whole string */ + + $regexp2 = '/(\S+)\s+(.+)/is'; /* Split binding on second whitespace to get properties */ + + $properties = []; + if (preg_match($regexp2, $match[2] , $match2)) { + if (isset($match2[2])) { + $props = json_decode($match2[2],true); + if (is_array($props)){ + $properties = $props; + $match[2] = $match2[1]; + } + } + } + $binding_array = [ strtoupper($match[1]), trim($match[2]), + $properties ]; /* Make command uppercase */ return $binding_array; From 32a9449d164195f9fd1fc97c013582817fc235af Mon Sep 17 00:00:00 2001 From: Bruno17 Date: Sat, 6 Mar 2021 08:50:20 +0100 Subject: [PATCH 2/4] fix undefined properties --- core/src/Revolution/modTemplateVar.php | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/Revolution/modTemplateVar.php b/core/src/Revolution/modTemplateVar.php index c7b99b7acf5..12c1e1dad0d 100644 --- a/core/src/Revolution/modTemplateVar.php +++ b/core/src/Revolution/modTemplateVar.php @@ -833,6 +833,7 @@ public function getBindingDataFromValue($value) $nvalue = trim($value); $cmd = false; $param = ''; + $properties = []; if (substr($nvalue, 0, 1) == '@') { list($cmd, $param, $properties) = $this->parseBinding($nvalue); $cmd = trim($cmd); From fe3983d22319b220e8de55265c13b9a5ea09d435 Mon Sep 17 00:00:00 2001 From: Bruno17 Date: Sat, 6 Mar 2021 08:56:20 +0100 Subject: [PATCH 3/4] initialize match2 --- core/src/Revolution/modTemplateVar.php | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/Revolution/modTemplateVar.php b/core/src/Revolution/modTemplateVar.php index 12c1e1dad0d..40a174d6d81 100644 --- a/core/src/Revolution/modTemplateVar.php +++ b/core/src/Revolution/modTemplateVar.php @@ -993,6 +993,7 @@ public function processBindings($value = '', $resourceId = 0, $preProcess = true public function parseBinding($binding_string) { $match = []; + $match2 = []; $binding_string = trim($binding_string); $regexp = '/@(' . implode('|', $this->bindings) . ')\s*(.*)/is'; /* Split binding on whitespace */ From 230739394174724d13adacb68bc549467d5a256d Mon Sep 17 00:00:00 2001 From: Bruno17 Date: Sat, 6 Mar 2021 13:31:41 +0100 Subject: [PATCH 4/4] check for valid JSON --- core/src/Revolution/modTemplateVar.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/Revolution/modTemplateVar.php b/core/src/Revolution/modTemplateVar.php index 40a174d6d81..f3b1978772b 100644 --- a/core/src/Revolution/modTemplateVar.php +++ b/core/src/Revolution/modTemplateVar.php @@ -1006,9 +1006,12 @@ public function parseBinding($binding_string) if (preg_match($regexp2, $match[2] , $match2)) { if (isset($match2[2])) { $props = json_decode($match2[2],true); - if (is_array($props)){ + $valid = json_last_error() === JSON_ERROR_NONE; + if ($valid && is_array($props)){ $properties = $props; $match[2] = $match2[1]; + } else { + $this->xpdo->log(modX::LOG_LEVEL_ERROR, 'modTemplateVar::parseBinding - third parameter is invalid JSON :' . $binding_string); } } }