PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.1.2
Fluent Support – Helpdesk & Customer Support Ticket System v2.1.2
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 1.5.5 All 68 releases
fluent-support / app / Services / Parser / ShortcodeParser.php

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

165 lines 4.5 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 if(empty($data[$dataKey])) {
68 return $matches[0];
69 }
70
71 if ($dataKey == 'customer' || $dataKey == 'agent' || $dataKey == 'assigner') {
72 return $this->getPersonValue($data[$dataKey], $valueKey, $defaultValue);
73 }
74
75 if($dataKey == 'ticket') {
76 return $this->getTicketData($data['ticket'], $valueKey, $defaultValue);
77 }
78
79 if($dataKey == 'response') {
80 return $this->getResponseData($data['response'], $valueKey, $defaultValue);
81 }
82
83
84 if ($dataKey == 'business') {
85 $business = $data[$dataKey];
86 return $this->getBusinessValue($valueKey, $defaultValue, $business);
87 }
88
89 return apply_filters('fluentsupport/smartcode_fallback_callback_' . $dataKey, $matches[0], $valueKey, $defaultValue, $data);
90 }
91
92 protected function getTicketData($ticket, $valueKey, $defaultValue)
93 {
94 $valueKeys = explode('.', $valueKey);
95
96 if (count($valueKeys) == 1) {
97 if($valueKey == 'public_url') {
98 return Helper::getTicketViewSignedUrl($ticket);
99 }
100
101 if($valueKey == 'admin_url') {
102 return Helper::getTicketAdminUrl($ticket);
103 }
104
105 $accesors = ['id', 'title', 'content', 'priority', 'client_priority', 'status', 'created_at'];
106 if(in_array($valueKey, $accesors)) {
107 return $ticket->{$valueKey};
108 }
109 }
110
111 return $defaultValue;
112 }
113
114 protected function getResponseData($response, $valueKey, $defaultValue)
115 {
116 $valueKeys = explode('.', $valueKey);
117
118 if (count($valueKeys) == 1) {
119 $accesors = ['id', 'title', 'content'];
120
121 if($valueKey == 'full_content') {
122 // todo: We have to attach the attachments too.
123 // Maybe in the next version
124 $valueKey = 'content';
125 }
126
127 if(in_array($valueKey, $accesors)) {
128 return $response->{$valueKey};
129 }
130 }
131
132 return $defaultValue;
133 }
134
135 protected function getBusinessValue($valueKey, $defaultValue, $business)
136 {
137 $accessKeys = ['name', 'id', 'email'];
138
139 if(in_array($valueKey, $accessKeys)) {
140 return $business->{$valueKey};
141 }
142
143 return $defaultValue;
144 }
145
146 protected function getPersonValue($person, $valueKey, $defaultValue)
147 {
148 $valueKeys = explode('.', $valueKey);
149
150 if (count($valueKeys) == 1) {
151 $data = $person->toArray();
152 $extraAccessors = ['full_name', 'photo'];
153
154 if(in_array($valueKey, $extraAccessors)) {
155 return $person->{$valueKey};
156 }
157
158 $value = Arr::get($data, $valueKey);
159 return ($value) ? $value : $defaultValue;
160 }
161
162 return $defaultValue;
163 }
164 }
165