diff --git a/app/Http/Controllers/HelpDeskController.php b/app/Http/Controllers/HelpDeskController.php index 25eb533d..b1d05fca 100644 --- a/app/Http/Controllers/HelpDeskController.php +++ b/app/Http/Controllers/HelpDeskController.php @@ -9,6 +9,7 @@ use App\Http\Requests\HelpTicketRequest; use App\Models\HelpTicket; use App\Models\HelpTicketComment; use App\Models\HelpTopic; +use Illuminate\Support\Facades\Log; class HelpDeskController extends Controller { @@ -33,16 +34,22 @@ class HelpDeskController extends Controller } public function store(HelpTicketRequest $request){ +// +// try{ - $ticekt = HelpTicket::create([ - 'name' => $request->get('name'), - 'email' => $request->get('email'), - 'text' => $request->get('text'), - 'phone' => $request->get('phone'), - 'subject' => $request->get('subject'), - 'ticket_category_id' => $request->get('topic') - ]); - + $ticekt = HelpTicket::create([ + 'name' => $request->get('name'), + 'email' => $request->get('email'), + 'text' => $request->get('text'), + 'phone' => $request->get('phone'), + 'subject' => $request->get('subject'), + 'ticket_category_id' => $request->get('topic'), + 'attachment' => $request->get('attachment') + ]); +// } +// catch (\Exception $exception){ +// Log::error($exception); +// } //todo fire event notify admin by mail, attachment return redirect()->route('help.show',['code' => $ticekt->code]); @@ -64,6 +71,10 @@ class HelpDeskController extends Controller $ticket->update(['status' => 'pending']) ; + if($request->has('attachment')){ + + } + //todo notify, attachment return redirect()->route('help.show',['code' => $code]); diff --git a/app/Http/Requests/HelpTicketCommentRequest.php b/app/Http/Requests/HelpTicketCommentRequest.php index f8451e94..bd9a256b 100644 --- a/app/Http/Requests/HelpTicketCommentRequest.php +++ b/app/Http/Requests/HelpTicketCommentRequest.php @@ -26,7 +26,8 @@ class HelpTicketCommentRequest extends FormRequest public function rules() { return [ - 'text' => 'required' + 'text' => 'required', + 'attachment' => 'max:1024|mimes:pdf,jpg,png,jpeg,txt' ]; } diff --git a/app/Http/Requests/HelpTicketRequest.php b/app/Http/Requests/HelpTicketRequest.php index a3371986..9fc46059 100644 --- a/app/Http/Requests/HelpTicketRequest.php +++ b/app/Http/Requests/HelpTicketRequest.php @@ -29,6 +29,7 @@ class HelpTicketRequest extends FormRequest 'email' => 'required|email', 'topic' => 'required', 'text' => 'required', + 'attachment' => 'image|max:1024' ]; } diff --git a/app/Models/HelpTicket.php b/app/Models/HelpTicket.php index 276a4ff3..6b97c310 100644 --- a/app/Models/HelpTicket.php +++ b/app/Models/HelpTicket.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Backpack\CRUD\CrudTrait; +use Illuminate\Support\Str; class HelpTicket extends Model { @@ -59,6 +60,36 @@ class HelpTicket extends Model | MUTATORS |-------------------------------------------------------------------------- */ + public function setAttachmentAttribute($value){ + $attribute_name = "attachment"; + $disk = config('filesystems.default'); // or use your own disk, defined in config/filesystems.php + $destination_path = "help"; // path relative to the disk above + + // if the image was erased + if ($value==null) { + // delete the image from disk + \Storage::disk($disk)->delete($this->{$attribute_name}); + + // set null in the database column + $this->attributes[$attribute_name] = null; + } + + // if a base64 was sent, store it in the db + if (starts_with($value, 'data:image')) + { + // 0. Make the image + $image = \Image::make($value)->encode('jpg', 90); + // 1. Generate a filename. + $filename = md5($value.time()).'.jpg'; + // 2. Store the image on disk. + \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream()); + // 3. Save the public path to the database + // but first, remove "public/" from the path, since we're pointing to it from the root folder + // that way, what gets saved in the database is the user-accesible URL + $public_destination_path = Str::replaceFirst('public/', '', $destination_path); + $this->attributes[$attribute_name] = $public_destination_path.'/'.$filename; + } + } /** * Boot all of the bootable traits on the model. */ @@ -69,5 +100,16 @@ class HelpTicket extends Model static::creating(function ($ticket) { $ticket->code = strtoupper(str_random(5)) . date('jn'); }); + + static::deleting(function($obj) { + $disk = config('filesystems.default'); + \Storage::disk($disk)->delete($obj->seats_image); + + if (count((array)$obj->images)) { + foreach ($obj->images as $file_path) { + \Storage::disk('uploads')->delete($file_path); + } + } + }); } } diff --git a/app/Models/HelpTicketComment.php b/app/Models/HelpTicketComment.php index 0bd3a625..aeba750b 100644 --- a/app/Models/HelpTicketComment.php +++ b/app/Models/HelpTicketComment.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Backpack\CRUD\CrudTrait; +use Illuminate\Support\Str; class HelpTicketComment extends Model { @@ -55,4 +56,54 @@ class HelpTicketComment extends Model | MUTATORS |-------------------------------------------------------------------------- */ + //todo use trait for image upload + public function setAttachmentAttribute($value){ + $attribute_name = "attachment"; + $disk = config('filesystems.default'); // or use your own disk, defined in config/filesystems.php + $destination_path = "help"; // path relative to the disk above + + // if the image was erased + if ($value==null) { + // delete the image from disk + \Storage::disk($disk)->delete($this->{$attribute_name}); + + // set null in the database column + $this->attributes[$attribute_name] = null; + } + + // if a base64 was sent, store it in the db + if (starts_with($value, 'data:image')) + { + // 0. Make the image + $image = \Image::make($value)->encode('jpg', 90); + // 1. Generate a filename. + $filename = md5($value.time()).'.jpg'; + // 2. Store the image on disk. + \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream()); + // 3. Save the public path to the database + // but first, remove "public/" from the path, since we're pointing to it from the root folder + // that way, what gets saved in the database is the user-accesible URL + $public_destination_path = Str::replaceFirst('public/', '', $destination_path); + $this->attributes[$attribute_name] = $public_destination_path.'/'.$filename; + } + } + + /** + * Boot all of the bootable traits on the model. + */ + public static function boot() + { + parent::boot(); + + static::deleting(function($obj) { + $disk = config('filesystems.default'); + \Storage::disk($disk)->delete($obj->seats_image); + + if (count((array)$obj->images)) { + foreach ($obj->images as $file_path) { + \Storage::disk('uploads')->delete($file_path); + } + } + }); + } } diff --git a/resources/views/desktop/Pages/HelpDeskCreateForm.blade.php b/resources/views/desktop/Pages/HelpDeskCreateForm.blade.php index 03c510b6..9ccdf45f 100644 --- a/resources/views/desktop/Pages/HelpDeskCreateForm.blade.php +++ b/resources/views/desktop/Pages/HelpDeskCreateForm.blade.php @@ -33,6 +33,10 @@ 'rows' => 5 )) !!} +