@@ -23,7 +23,7 @@ class CommentsController extends Controller
2323 public function __construct ()
2424 {
2525 $ this ->middleware ('PanicHD\PanicHD\Middleware\UserAccessMiddleware ' , ['only ' => ['store ' ]]);
26- $ this ->middleware ('PanicHD\PanicHD\Middleware\AgentAccessMiddleware ' , ['only ' => ['update ' , 'destroy ' ]]);
26+ $ this ->middleware ('PanicHD\PanicHD\Middleware\AgentAccessMiddleware ' , ['only ' => ['update ' , 'destroy ' ]]);
2727 }
2828
2929 /**
@@ -53,16 +53,16 @@ protected function validation_common($request, $new_comment = true)
5353 {
5454 $ a_content = $ this ->purifyHtml ($ request ->get ('content ' ));
5555 $ request ->merge (['content ' =>$ a_content ['content ' ]]);
56-
56+
5757 $ fields = [
5858 'content ' => 'required|min:6 '
5959 ];
6060 if ($ new_comment ) $ fields ['ticket_id ' ] = 'required|exists:panichd_tickets,id ' ;
61-
61+
6262 if ($ request ->exists ('attachments ' )){
6363 $ fields ['attachments ' ] = 'array ' ;
6464 }
65-
65+
6666 // Custom validation messages
6767 $ custom_messages = [
6868 'content.required ' => 'panichd::lang.validate-comment-required ' ,
@@ -76,18 +76,18 @@ protected function validation_common($request, $new_comment = true)
7676 $ custom_messages [$ field ] = $ trans ;
7777 }
7878 }
79-
79+
8080 // Form validation
8181 $ validator = Validator::make ($ request ->all (), $ fields , $ custom_messages );
8282 $ a_result_errors = [];
83-
83+
8484 if ($ validator ->fails ()) {
8585 $ a_result_errors = [
8686 'messages ' =>(array )$ validator ->errors ()->all (),
8787 'fields ' =>(array )$ validator ->errors ()->messages ()
8888 ];
8989 }
90-
90+
9191 $ common_data = [
9292 'request ' => $ request ,
9393 'member ' => \PanicHDMember::findOrFail (auth ()->user ()->id ),
@@ -96,8 +96,8 @@ protected function validation_common($request, $new_comment = true)
9696 ];
9797 return $ common_data ;
9898 }
99-
100-
99+
100+
101101 /**
102102 * Store a newly created resource in storage.
103103 *
@@ -109,29 +109,29 @@ public function store(Request $request)
109109 {
110110 $ common_data = $ this ->validation_common ($ request );
111111 extract ($ common_data );
112-
112+
113113 // Create comment
114114 DB ::beginTransaction ();
115115 $ comment = new Models \Comment ();
116116 $ ticket = Models \Ticket::findOrFail ($ request ->get ('ticket_id ' ));
117-
117+
118118 $ category_level = $ member ->levelInCategory ($ ticket ->category_id );
119119 $ permission_level = ($ member ->currentLevel () > 1 and $ category_level > 1 ) ? $ category_level : 1 ;
120-
120+
121121 if ($ ticket ->hidden and $ member ->currentLevel () == 1 ){
122122 session ()->flash ('warning ' , trans ('panichd::lang.you-are-not-permitted-to-access ' ));
123-
123+
124124 return response ()->json (array_merge (
125125 ['result ' => 'error ' ],
126126 ['redirect ' =>[route (Setting::grab ('main_route ' ).'.index ' )]]
127127 ));
128128 }
129-
129+
130130 $ create_list_comment = false ;
131131 if ($ member ->currentLevel () > 1 and $ member ->canManageTicket ($ request ->get ('ticket_id ' ))){
132132 // Filter response type
133133 $ comment ->type = in_array ($ request ->get ('response_type ' ), ['note ' ,'reply ' ]) ? $ request ->get ('response_type ' ) : 'note ' ;
134-
134+
135135 if ($ request ->has ('complete_ticket ' ) and $ member ->canCloseTicket ($ request ->get ('ticket_id ' ))){
136136 if ($ comment ->type == 'reply ' and $ ticket ->user_id == $ member ->id ){
137137 // comment for assigned agent only
@@ -143,61 +143,61 @@ public function store(Request $request)
143143 }
144144 }else
145145 $ comment ->type = 'reply ' ;
146-
146+
147147 // Close ticket + new status
148148 if ($ member ->canCloseTicket ($ request ->get ('ticket_id ' )) and $ request ->has ('complete_ticket ' )){
149- $ ticket ->completed_at = Carbon::now ();
150- $ ticket ->status_id = Status::where ('id ' ,$ request ->get ('status_id ' ))->count ()==1 ? $ request ->get ('status_id ' ) : Setting::grab ('default_close_status_id ' );
149+ $ ticket ->completed_at = Carbon::now ();
150+ $ ticket ->status_id = Status::where ('id ' ,$ request ->get ('status_id ' ))->count ()==1 ? $ request ->get ('status_id ' ) : Setting::grab ('default_close_status_id ' );
151151 }
152-
152+
153153 $ comment ->ticket_id = $ request ->get ('ticket_id ' );
154154 $ comment ->user_id = $ member ->id ;
155155 $ comment ->content = $ a_content ['content ' ];
156156 $ comment ->html = $ a_content ['html ' ];
157157 $ comment ->save ();
158-
158+
159159 // Create additional list comment if has('complete_ticket') but $member != auth()
160160 if ($ create_list_comment ){
161161 $ list_comment = new Models \Comment ;
162162 $ list_comment ->type = "complete " ;
163-
163+
164164 $ list_comment ->content = $ list_comment ->html = '' ;
165165 $ list_comment ->ticket_id = $ request ->get ('ticket_id ' );
166166 $ list_comment ->user_id = $ member ->id ;
167167 $ list_comment ->save ();
168168 }
169-
169+
170170 // Create attachments from embedded images
171171 $ this ->embedded_images_to_attachments ($ permission_level , $ ticket , $ comment );
172-
173- // Update parent ticket
172+
173+ // Update parent ticket
174174 $ ticket ->updated_at = $ comment ->created_at ;
175-
175+
176176 if ($ member ->currentLevel () > 1 and $ member ->canManageTicket ($ request ->get ('ticket_id ' )) and $ comment ->type == 'reply ' and $ request ->has ('add_to_intervention ' )){
177177 $ ticket ->intervention = $ ticket ->intervention .$ comment ->content ;
178178 $ ticket ->intervention_html = $ ticket ->intervention_html .$ comment ->html ;
179179 }
180-
180+
181181 $ ticket ->save ();
182-
182+
183183 if (Setting::grab ('ticket_attachments_feature ' )){
184184 // Attached files
185185 $ a_result_errors = $ this ->saveAttachments ($ request , $ a_result_errors , $ ticket , $ comment );
186186 }
187-
187+
188188 // If errors present
189189 if ($ a_result_errors ){
190190 return response ()->json (array_merge (
191191 ['result ' => 'error ' ],
192192 $ a_result_errors
193193 ));
194194 }
195-
195+
196196 DB ::commit ();
197197 event (new CommentCreated (Models \Comment::find ($ comment ->id ), $ request ));
198-
198+
199199 session ()->flash ('status ' , trans ('panichd::lang.comment-has-been-added-ok ' ));
200-
200+
201201 return response ()->json ([
202202 'result ' => 'ok ' ,
203203 'url ' => route (Setting::grab ('main_route ' ).'.show ' , $ ticket ->id )
@@ -240,55 +240,55 @@ public function update(Request $request, $id)
240240 {
241241 $ common_data = $ this ->validation_common ($ request , false );
242242 extract ($ common_data );
243-
243+
244244 // Update comment
245245 $ comment = Models \Comment::findOrFail ($ id );
246246 $ original_comment = clone $ comment ;
247247 $ ticket = Models \Ticket::findOrFail ($ comment ->ticket_id );
248-
248+
249249 DB ::beginTransaction ();
250250 $ comment ->content = $ a_content ['content ' ];
251251 $ comment ->html = $ a_content ['html ' ];
252-
252+
253253 $ comment ->save ();
254-
254+
255255 // Create attachments from embedded images
256256 $ category_level = $ member ->levelInCategory ($ ticket ->category_id );
257257 $ permission_level = ($ member ->currentLevel () > 1 and $ category_level > 1 ) ? $ category_level : 1 ;
258258 $ this ->embedded_images_to_attachments ($ permission_level , $ ticket , $ comment );
259-
259+
260260 $ ticket ->touch ();
261-
261+
262262 if (Setting::grab ('ticket_attachments_feature ' )){
263263 // 1 - update existing attachment fields
264264 $ a_result_errors = $ this ->updateAttachments ($ request , $ a_result_errors , $ comment ->attachments ()->get ());
265-
265+
266266 // 2 - add new attachments
267267 $ a_result_errors = $ this ->saveAttachments ($ request , $ a_result_errors , $ ticket , $ comment );
268-
268+
269269 if (!$ a_result_errors ){
270270 // 3 - destroy checked attachments
271271 if ($ request ->has ('delete_files ' )){
272272 $ destroy_error = $ this ->destroyAttachmentIds ($ request ->delete_files );
273-
273+
274274 if ($ destroy_error ) $ a_result_errors ['messages ' ][] = $ destroy_error ;
275- }
275+ }
276276 }
277277 }
278-
278+
279279 // If errors present
280280 if ($ a_result_errors ){
281281 return response ()->json (array_merge (
282282 ['result ' => 'error ' ],
283283 $ a_result_errors
284284 ));
285285 }
286-
286+
287287 DB ::commit ();
288288 event (new CommentUpdated ($ original_comment , $ comment ));
289-
289+
290290 session ()->flash ('status ' , trans ('panichd::lang.comment-has-been-updated ' ));
291-
291+
292292 return response ()->json ([
293293 'result ' => 'ok ' ,
294294 'url ' => route (Setting::grab ('main_route ' ).'.show ' , $ ticket ->id )
@@ -305,14 +305,14 @@ public function update(Request $request, $id)
305305 public function destroy ($ id )
306306 {
307307 $ comment =Models \Comment::findOrFail ($ id );
308-
308+
309309 $ error = $ comment ->delete ();
310-
310+
311311 if ($ error ){
312312 return redirect ()->back ()->with ('warning ' , trans ('panichd::lang.comment-destroy-error ' , ['error ' => $ error ]));
313313 }else {
314314 return back ()->with ('status ' , trans ('panichd::lang.comment-has-been-deleted ' ));
315315 }
316-
316+
317317 }
318318}
0 commit comments