PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Services / Parser / ShortcodeParser.php

ShortcodeParser.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Services/Parser/ShortcodeParser.php

202 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services\Parser;
4
5 use FluentSupport\App\Services\Helper;
6 use FluentSupport\Framework\Support\Arr;
7
8 class ShortcodeParser
9 {
10 public function parse($templateString, $data)
11 {
12 $result = [];
13 $isSingle = false;
14
15 if (!is_array($templateString)) {
16 $isSingle = true;
17 }
18
19 foreach ((array)$templateString as $key => $string) {
20 $result[$key] = $this->parseShortcode($string, $data);
21 }
22
23 if ($isSingle) {
24 return reset($result);
25 }
26
27 return $result;
28 }
29
30 public function parseShortcode($string, $data)
31 {
32 return preg_replace_callback('/({{|##)+(.*?)(}}|##)/', function ($matches) use ($data) {
33 return (string) $this->replace($matches, $data);
34 }, $string);
35 }
36
37 protected function replace($matches, $data)
38 {
39 if (empty($matches[2])) {
40 return apply_filters('fluentsupport/smartcode_fallback', $matches[0], $data);
41 }
42
43 $matches[2] = trim($matches[2]);
44
45 $matched = explode('.', $matches[2]);
46
47 if (count($matched) <= 1) {
48 return apply_filters('fluentsupport/smartcode_fallback', $matches[0], $data);
49 }
50
51 $dataKey = trim(array_shift($matched));
52
53 $valueKey = trim(implode('.', $matched));
54
55 if (!$valueKey) {
56 return apply_filters('fluentsupport/smartcode_fallback', $matches[0], $data);
57 }
58
59 $valueKeys = explode('|', $valueKey);
60
61 $valueKey = $valueKeys[0];
62 $defaultValue = '';
63 if (isset($valueKeys[1])) {
64 $defaultValue = trim($valueKeys[1]);
65 }
66
67 // Before the empty-data guard: with no agent (unassigned ticket, closed by
68 // a workflow) the signature is empty, not a literal {{agent.signature}}.
69 if ($dataKey == 'agent' && $valueKey == 'signature') {
70 return $this->getAgentSignature($data);
71 }
72
73 if(empty($data[$dataKey])) {
74 return $matches[0];
75 }
76
77 if ($dataKey == 'customer' || $dataKey == 'agent' || $dataKey == 'assigner') {
78 return $this->getPersonValue($data[$dataKey], $valueKey, $defaultValue);
79 }
80
81 if($dataKey == 'ticket') {
82 return $this->getTicketData($data['ticket'], $valueKey, $defaultValue);
83 }
84
85 if($dataKey == 'response') {
86 return $this->getResponseData($data['response'], $valueKey, $defaultValue);
87 }
88
89
90 if ($dataKey == 'business') {
91 $business = $data[$dataKey];
92 return $this->getBusinessValue($valueKey, $defaultValue, $business);
93 }
94
95 return apply_filters('fluentsupport/smartcode_fallback_callback_' . $dataKey, $matches[0], $valueKey, $defaultValue, $data);
96 }
97
98 /** Set while a signature renders: a signature naming itself, in any spelling, renders empty. */
99 private static $renderingSignature = false;
100
101 /**
102 * {{agent.signature}}: the agent's signature when enabled, otherwise empty.
103 * Its own smartcodes are filled from the same data.
104 */
105 protected function getAgentSignature($data)
106 {
107 if (self::$renderingSignature || empty($data['agent'])) {
108 return '';
109 }
110
111 $signature = \FluentSupport\App\Services\Tickets\ResponseService::getAppendedSignature($data['agent']);
112
113 if ($signature === '') {
114 return '';
115 }
116
117 self::$renderingSignature = true;
118 try {
119 return $this->parse($signature, $data);
120 } finally {
121 self::$renderingSignature = false;
122 }
123 }
124
125 protected function getTicketData($ticket, $valueKey, $defaultValue)
126 {
127 $valueKeys = explode('.', $valueKey);
128
129 if (count($valueKeys) == 1) {
130 if($valueKey == 'public_url') {
131 return Helper::getTicketViewSignedUrl($ticket);
132 }
133
134 if($valueKey == 'admin_url') {
135 return Helper::getTicketAdminUrl($ticket);
136 }
137
138 if($valueKey == 'public_id') {
139 return $ticket->display_ticket_number;
140 }
141
142 $accesors = ['id', 'title', 'content', 'priority', 'client_priority', 'status', 'created_at'];
143 if(in_array($valueKey, $accesors)) {
144 return $ticket->{$valueKey};
145 }
146 }
147
148 return $defaultValue;
149 }
150
151 protected function getResponseData($response, $valueKey, $defaultValue)
152 {
153 $valueKeys = explode('.', $valueKey);
154
155 if (count($valueKeys) == 1) {
156 $accesors = ['id', 'title', 'content'];
157
158 if($valueKey == 'full_content') {
159 // todo: We have to attach the attachments too.
160 // Maybe in the next version
161 $valueKey = 'content';
162 }
163
164 if(in_array($valueKey, $accesors)) {
165 return $response->{$valueKey};
166 }
167 }
168
169 return $defaultValue;
170 }
171
172 protected function getBusinessValue($valueKey, $defaultValue, $business)
173 {
174 $accessKeys = ['name', 'id', 'email'];
175
176 if(in_array($valueKey, $accessKeys)) {
177 return $business->{$valueKey};
178 }
179
180 return $defaultValue;
181 }
182
183 protected function getPersonValue($person, $valueKey, $defaultValue)
184 {
185 $valueKeys = explode('.', $valueKey);
186
187 if (count($valueKeys) == 1) {
188 $data = $person->toArray();
189 $extraAccessors = ['full_name', 'photo'];
190
191 if(in_array($valueKey, $extraAccessors)) {
192 return $person->{$valueKey};
193 }
194
195 $value = Arr::get($data, $valueKey);
196 return ($value) ? $value : $defaultValue;
197 }
198
199 return $defaultValue;
200 }
201 }
202