Skip to content

Commit 14dd5ff

Browse files
committed
Merge branch 'images' into dev
2 parents c7fd1f9 + 87de4db commit 14dd5ff

6 files changed

Lines changed: 194 additions & 19 deletions

File tree

src/Controllers/CommentsController.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ protected function validation_common($request, $new_comment = true)
9191

9292
$common_data = [
9393
'request' => $request,
94+
'member' => Member::findOrFail(\Auth::user()->id),
9495
'a_content' => $a_content,
9596
'a_result_errors' => $a_result_errors
9697
];
@@ -113,9 +114,10 @@ public function store(Request $request)
113114
// Create comment
114115
DB::beginTransaction();
115116
$comment = new Models\Comment();
116-
117117
$ticket = Models\Ticket::findOrFail($request->get('ticket_id'));
118-
$member = Member::findOrFail(\Auth::user()->id);
118+
119+
$category_level = $member->levelInCategory($ticket->category_id);
120+
$permission_level = ($member->currentLevel() > 1 and $category_level > 1) ? $category_level : 1;
119121

120122
if ($ticket->hidden and $member->currentLevel() == 1){
121123
session()->flash('warning', trans('panichd::lang.you-are-not-permitted-to-access'));
@@ -148,17 +150,21 @@ public function store(Request $request)
148150
$comment->html = $a_content['html'];
149151
$comment->save();
150152

153+
// Create attachments from embedded images
154+
$this->embedded_images_to_attachments($permission_level, $ticket, $comment);
155+
151156
// Update parent ticket
152157
$ticket->updated_at = $comment->created_at;
153-
158+
154159
if ($member->currentLevel() > 1 and $member->canManageTicket($request->get('ticket_id')) and $comment->type == 'reply' and $request->has('add_to_intervention')){
155-
$ticket->intervention = $ticket->intervention.$a_content['content'];
156-
$ticket->intervention_html = $ticket->intervention_html.$a_content['html'];
160+
$ticket->intervention = $ticket->intervention.$comment->content;
161+
$ticket->intervention_html = $ticket->intervention_html.$comment->html;
157162
}
158163

159164
$ticket->save();
160165

161166
if (Setting::grab('ticket_attachments_feature')){
167+
// Attached files
162168
$a_result_errors = $this->saveAttachments($request, $a_result_errors, $ticket, $comment);
163169
}
164170

@@ -228,6 +234,9 @@ public function update(Request $request, $id)
228234

229235
$comment->save();
230236
$ticket = Models\Ticket::findOrFail($comment->ticket_id);
237+
$category_level = $member->levelInCategory($ticket->category_id);
238+
$permission_level = ($member->currentLevel() > 1 and $category_level > 1) ? $category_level : 1;
239+
231240
$ticket->touch();
232241

233242
if (Setting::grab('ticket_attachments_feature')){

src/Controllers/TicketsController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,9 @@ public function create_edit_data($ticket = false)
681681
*/
682682
protected function validation_common($request, $new_ticket = true)
683683
{
684-
$user = $this->member->find(auth()->user()->id);
685-
$category_level = $user->levelInCategory($request->category_id);
686-
$permission_level = ($user->currentLevel() > 1 and $category_level > 1) ? $category_level : 1;
684+
$member = $this->member->find(auth()->user()->id);
685+
$category_level = $member->levelInCategory($request->category_id);
686+
$permission_level = ($member->currentLevel() > 1 and $category_level > 1) ? $category_level : 1;
687687

688688
$a_content = $this->purifyHtml($request->get('content'));
689689
$common_data = [
@@ -698,9 +698,9 @@ protected function validation_common($request, $new_ticket = true)
698698
]);
699699

700700
if ($new_ticket){
701-
$allowed_categories = implode(",", $user->getNewTicketCategories()->keys()->toArray());
701+
$allowed_categories = implode(",", $member->getNewTicketCategories()->keys()->toArray());
702702
}else{
703-
$allowed_categories = implode(",", $user->getEditTicketCategories()->keys()->toArray());
703+
$allowed_categories = implode(",", $member->getEditTicketCategories()->keys()->toArray());
704704
}
705705

706706

@@ -907,6 +907,10 @@ public function store(Request $request)
907907
$ticket->save();
908908

909909
if (Setting::grab('ticket_attachments_feature')){
910+
// Create attachments from embedded images
911+
$this->embedded_images_to_attachments($permission_level, $ticket);
912+
913+
// Attached files
910914
$a_result_errors = $this->saveAttachments($request, $a_result_errors, $ticket);
911915
}
912916

@@ -1084,6 +1088,9 @@ public function update(Request $request, $id)
10841088
$ticket->save();
10851089

10861090
if (Setting::grab('ticket_attachments_feature')){
1091+
// Create attachments from embedded images
1092+
$this->embedded_images_to_attachments($permission_level, $ticket);
1093+
10871094
// 1 - update existing attachment fields
10881095
$a_result_errors = $this->updateAttachments($request, $a_result_errors, $ticket->attachments()->get());
10891096

src/Public/css/panichd.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ h1, h2, h3, h4, h5, h6 {
175175
margin-bottom: 15px;
176176
}
177177

178+
.summernote-text-wrapper .summernote_thumbnail_link {
179+
display: inline-block;
180+
border: 1px solid #eee;
181+
padding: 4px;
182+
border-radius: 5px;
183+
width: 60px;
184+
height: 60px;
185+
}
186+
178187
/*
179188
* Datatable styles
180189
*/

src/Traits/Attachments.php

Lines changed: 152 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,129 @@
1212

1313
trait 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

src/Views/shared/photoswipe_files.blade.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@
7575
<script>
7676
$(function(){
7777
// Modify link for each image in ticket to launch PhotoSwipe
78-
$('.pwsp_gallery_link').click(function(e){
78+
$('.pwsp_gallery_link, .summernote_thumbnail_link').click(function(e){
7979
var openpid = $(this).data('pwsp-pid');
80+
if (typeof openpid === 'undefined' || openpid == ""){
81+
var parts = $(this).prop('href').split('/');
82+
openpid = Number(parts[parts.length -1]);
83+
}
84+
8085
var openindex = 0;
8186
8287
for (var i = 0, len = pswpItems.length; i < len; i++) {

src/Views/tickets/partials/attachment_image.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="panel panel-default" style="display: inline-block; width: 70px; height: 70px; margin: 5px">
77
<div class="panel-body">
88
@if (\File::exists(storage_path('app'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$setting->grab('thumbnails_path').DIRECTORY_SEPARATOR).basename($attachment->file_path)))
9-
<img src="{{ URL::to('/').'/storage/'.$setting->grab('thumbnails_path').'/'.basename($attachment->file_path) }}" class="ximg-responsive">
9+
<img src="{{ URL::to('/').'/storage/'.$setting->grab('thumbnails_path').'/'.basename($attachment->file_path) }}">
1010
@else
1111
<i class="fa fa-file-image-o fa-2x" aria-hidden="true"></i>
1212
@endif

0 commit comments

Comments
 (0)