1212
1313trait Attachments
1414{
15+ /**
16+ * Generate attachment from any embbeded image within $html that exceeds any of the max dimensions (hardcoded 380 x 300 max)
17+ *
18+ * @Param $html string
19+ *
20+ * @return string
21+ */
22+ protected function createAttachmentsFrom ($ html , $ ticket , $ comment = false , $ count = 0 )
23+ {
24+ $ dom = new \DomDocument ();
25+
26+ $ dom ->loadHtml ( mb_convert_encoding ($ html , 'HTML-ENTITIES ' , "UTF-8 " ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
27+
28+ $ images = $ dom ->getElementsByTagName ('img ' );
29+
30+ $ i = 0 ;
31+ foreach ($ images as $ k => $ node ){
32+ $ i ++;
33+
34+ if (in_array ($ node ->getAttribute ('class ' ), ['summernote_embedded_image ' , 'summernote_thumbnail_image ' ])){
35+ // Image has been processed before. Don't need to do anything with it
36+
37+ }else {
38+ // Read image dimensions
39+ $ data = $ node ->getAttribute ('src ' );
40+ list ($ type , $ data ) = explode ('; ' , $ data );
41+ list (, $ data ) = explode (', ' , $ data );
42+ $ img = Image::make ($ data );
43+
44+ if ($ img ->width () < 380 and $ img ->height () < 300 ){
45+ // Image is small and will not be processed again
46+ $ node ->setAttribute ('class ' , 'summernote_embedded_image ' );
47+ }else {
48+ // Create filename
49+ $ original_filename = 'embedded_image_ ' .($ i +$ count ).'.png ' ;
50+
51+ $ file_name = $ this ->makeFilename ($ original_filename .date ('YmdHis ' , time ()), $ ticket ->id .'_embedded ' , '' );
52+
53+ // Attach real file to storage folder
54+ $ file_path = storage_path (Setting::grab ('attachments_path ' )) .DIRECTORY_SEPARATOR . $ file_name ;
55+ file_put_contents ($ file_path , base64_decode ($ data ));
56+
57+ // Create new Attachment
58+ $ attachment = new Attachment ();
59+ $ attachment ->ticket_id = $ ticket ->id ;
60+ if ($ comment ){
61+ $ attachment ->comment_id = $ comment ->id ;
62+ $ attachment ->uploaded_by_id = $ comment ->user_id ;
63+ }else {
64+ $ attachment ->uploaded_by_id = $ ticket ->user_id ;
65+ }
66+ $ attachment ->original_filename = $ attachment ->new_filename = $ original_filename ;
67+
68+ $ attachment ->bytes = filesize ($ file_path );
69+ $ attachment ->mimetype = 'image/png ' ;
70+ $ attachment ->image_sizes = $ img ->width ()."x " .$ img ->height ();
71+ $ attachment ->file_path = $ file_path ;
72+ $ attachment ->original_attachment = $ file_name ;
73+
74+ // Make thumbnail
75+ $ this ->makeThumbnailFromImage ($ img , $ file_name );
76+
77+ // Save attachment instance
78+ $ attachment ->save ();
79+
80+ // Insert image thumbnail
81+ $ child_img = $ dom ->createElement ('img ' );
82+ $ child_img ->setAttribute ('src ' , \URL ::to ('/ ' ).'/storage/ ' .Setting::grab ('thumbnails_path ' ).'/ ' .basename ($ attachment ->file_path ));
83+ $ child_img ->setAttribute ('class ' , 'summernote_thumbnail_image ' );
84+
85+ // Create link
86+ $ link = $ dom ->createElement ('a ' );
87+ $ link ->setAttribute ('href ' , \URL ::route (Setting::grab ('main_route ' ).'.view-attachment ' , [$ attachment ->id ]));
88+ $ link ->setAttribute ('class ' , 'summernote_thumbnail_link tooltip-show ' );
89+ $ link ->setAttribute ('title ' , $ original_filename );
90+
91+ // Append thumbnail in link
92+ $ link ->appendChild ($ child_img );
93+
94+ // replace original image with thumbnail link
95+ $ node ->parentNode ->replaceChild ($ link , $ node );
96+ }
97+ }
98+ }
99+
100+ return [
101+ 'html ' => $ dom ->saveHTML (),
102+ 'count ' => $ i
103+ ];
104+ }
105+
106+ /**
107+ * Filter ticket or comment fields and create attachments from big embedded images
108+ *
109+ * @param $ticket instance of PanicHD\PanicHD\Models\Ticket
110+ * @param $comment instance of PanicHD\PanicHD\Models\Comment
111+ */
112+ protected function embedded_images_to_attachments ($ permission_level , &$ ticket , &$ comment = false )
113+ {
114+ $ field = $ comment ? $ comment ->html : $ ticket ->html ;
115+
116+ // Move emmbedded images in description to attachments
117+ $ output = $ this ->createAttachmentsFrom ($ field , $ ticket , $ comment );
118+ if ($ output ['count ' ] > 0 )
119+ $ field = $ output ['html ' ];
120+
121+ // Move emmbedded images in intervention to attachments
122+ if (!$ comment and $ permission_level > 1 and $ ticket ->intervention_html != "" ) {
123+ $ output = $ this ->createAttachmentsFrom ($ ticket ->intervention_html , $ ticket , $ comment , $ output ['count ' ]);
124+ if ($ output ['count ' ] > 0 )
125+ $ ticket ->intervention_html = $ output ['html ' ];
126+ }
127+
128+ if ($ comment ){
129+ $ comment ->html = $ field ;
130+ $ comment ->save ();
131+ }else {
132+ $ ticket ->html = $ field ;
133+ $ ticket ->save ();
134+ }
135+ }
136+
137+
15138 /**
16139 * Saves form attached files in name="attachments[]"
17140 *
@@ -235,14 +358,26 @@ protected function updateAttachments($request, $a_result_errors, $attachments)
235358 $ field = 'attachment_ ' .$ att ->id .'_image_crop ' ;
236359 if ($ request ->has ($ field )){
237360 $ a_fields ['image_crop ' ] = $ request ->input ($ field );
361+ $ original_filename = basename ($ att ->file_path );
238362 }
239363
240364 if ($ a_fields ) $ this ->updateSingleAttachment ($ att , $ a_fields , $ a_single_errors );
241365
242366 if ($ a_single_errors ){
243367 $ a_errors ['attachment_block_ ' .$ index ] = implode ('. ' , $ a_single_errors );
244368 }else {
369+ // Save attachment
245370 $ att ->save ();
371+
372+ if ($ a_fields and isset ($ a_fields ['image_crop ' ])){
373+ // Update related Ticket or comment Html field
374+ $ model = $ att ->comment_id == "" ? $ att ->ticket : $ att ->comment ;
375+ $ model ->html = str_replace ($ original_filename , basename ($ att ->file_path ), $ model ->html );
376+ if ($ att ->comment_id == "" ){
377+ $ model ->intervention_html = str_replace ($ original_filename , basename ($ att ->file_path ), $ model ->intervention_html );
378+ }
379+ $ model ->save ();
380+ }
246381 }
247382 $ index ++;
248383 }
@@ -297,6 +432,7 @@ protected function updateSingleAttachment(&$att, $a_fields, &$a_single_errors)
297432
298433 // Resize and save image
299434 $ img ->crop (intval ($ coords [2 ]-$ coords [0 ]), intval ($ coords [3 ]-$ coords [1 ]), intval ($ coords [0 ]), intval ($ coords [1 ]))->save ($ new_file_path );
435+ $ att ->image_sizes = $ img ->width ()."x " .$ img ->height ();
300436
301437 // Create new thumbnail
302438 $ this ->makeThumbnailFromImage ($ img , $ new_filename );
@@ -312,7 +448,6 @@ protected function updateSingleAttachment(&$att, $a_fields, &$a_single_errors)
312448 $ this ->deleteThumbnail (basename ($ att ->file_path ));
313449
314450 // Updated fields
315- $ att ->image_sizes = $ img ->width ()."x " .$ img ->height ();
316451 $ att ->file_path = $ new_file_path ;
317452 }
318453 }
@@ -353,21 +488,31 @@ protected function destroyAttachmentsLoop($attachments)
353488 /**
354489 * Destroy a single attachment files and model instance
355490 */
356- protected function destroyAttachedElement ($ attachment )
491+ protected function destroyAttachedElement ($ att )
357492 {
358493 // Delete attachment file
359- $ error = $ this ->deleteAttachmentFile ($ attachment ->file_path , $ attachment ->original_filename );
494+ $ error = $ this ->deleteAttachmentFile ($ att ->file_path , $ att ->original_filename );
360495 if ($ error ) return $ error ;
361496
362497 // Delete original file (if exists)
363- if ($ attachment ->original_attachment != basename ($ attachment ->file_path )){
364- $ original_path = pathinfo ($ attachment ->file_path , PATHINFO_DIRNAME ).DIRECTORY_SEPARATOR .$ attachment ->original_attachment ;
365- $ error = $ this ->deleteAttachmentFile ($ original_path , $ attachment ->original_filename );
498+ if ($ att ->original_attachment != basename ($ att ->file_path )){
499+ $ original_path = pathinfo ($ att ->file_path , PATHINFO_DIRNAME ).DIRECTORY_SEPARATOR .$ att ->original_attachment ;
500+ $ error = $ this ->deleteAttachmentFile ($ original_path , $ att ->original_filename );
366501 if ($ error ) return $ error ;
367502 }
368503
369504 // Delete thumbnail
370- $ this ->deleteThumbnail (basename ($ attachment ->file_path ));
505+ $ this ->deleteThumbnail (basename ($ att ->file_path ));
506+
507+ // Remove thumbnail link from any html field
508+ $ model = $ att ->comment_id == "" ? $ att ->ticket : $ att ->comment ;
509+ $ model ->html = preg_replace ('/<a[^>]*summernote_thumbnail_link[^>]*?><img[^>]* ' .basename ($ att ->file_path ).'[^>]*><\/a>/ ' , '' , $ model ->html );
510+
511+ if ($ att ->comment_id == "" ){
512+ $ model ->intervention_html = preg_replace ('/<a[^>]*summernote_thumbnail_link[^>]*?><img[^>]* ' .basename ($ att ->file_path ).'[^>]*><\/a>/ ' , '' , $ model ->intervention_html );
513+ }
514+ $ model ->save ();
515+
371516 return false ;
372517 }
373518
0 commit comments