PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
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 trunk, at app/Services/Parser/ShortcodeParser.php

169 lines 4.6 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 if($valueKey == 'public_id') {
106 return $ticket->display_ticket_number;
107 }
108
109 $accesors = ['id', 'title', 'content', 'priority', 'client_priority', 'status', 'created_at'];
110 if(in_array($valueKey, $accesors)) {
111 return $ticket->{$valueKey};
112 }
113 }
114
115 return $defaultValue;
116 }
117
118 protected function getResponseData($response, $valueKey, $defaultValue)
119 {
120 $valueKeys = explode('.', $valueKey);
121
122 if (count($valueKeys) == 1) {
123 $accesors = ['id', 'title', 'content'];
124
125 if($valueKey == 'full_content') {
126 // todo: We have to attach the attachments too.
127 // Maybe in the next version
128 $valueKey = 'content';
129 }
130
131 if(in_array($valueKey, $accesors)) {
132 return $response->{$valueKey};
133 }
134 }
135
136 return $defaultValue;
137 }
138
139 protected function getBusinessValue($valueKey, $defaultValue, $business)
140 {
141 $accessKeys = ['name', 'id', 'email'];
142
143 if(in_array($valueKey, $accessKeys)) {
144 return $business->{$valueKey};
145 }
146
147 return $defaultValue;
148 }
149
150 protected function getPersonValue($person, $valueKey, $defaultValue)
151 {
152 $valueKeys = explode('.', $valueKey);
153
154 if (count($valueKeys) == 1) {
155 $data = $person->toArray();
156 $extraAccessors = ['full_name', 'photo'];
157
158 if(in_array($valueKey, $extraAccessors)) {
159 return $person->{$valueKey};
160 }
161
162 $value = Arr::get($data, $valueKey);
163 return ($value) ? $value : $defaultValue;
164 }
165
166 return $defaultValue;
167 }
168 }
169