Merge pull request #174 from cuneytsenturk/master

Attachment manager
This commit is contained in:
Cüneyt Şentürk
2018-01-09 02:02:38 +03:00
committed by GitHub
57 changed files with 1398 additions and 167 deletions

View File

@ -7,11 +7,14 @@ use App\Http\Requests\Auth\User as Request;
use Illuminate\Http\Request as ARequest;
use App\Models\Auth\User;
use App\Models\Auth\Role;
use App\Traits\Uploads;
use Auth;
class Users extends Controller
{
use Uploads;
/**
* Display a listing of the resource.
*
@ -34,11 +37,12 @@ class Users extends Controller
*/
public function create()
{
$roles = Role::all()->reject(function($r) {
$roles = Role::all()->reject(function ($r) {
return $r->hasPermission('read-customer-panel');
});
$companies = Auth::user()->companies()->get()->sortBy('name');
foreach ($companies as $company) {
$company->setSettings();
}
@ -55,15 +59,16 @@ class Users extends Controller
*/
public function store(Request $request)
{
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('uploads/users');
}
// Create user
$user = User::create($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'users');
$user->attachMedia($media, 'picture');
}
// Attach roles
$user->roles()->attach($request['roles']);
@ -88,17 +93,18 @@ class Users extends Controller
{
if ($user->customer) {
// Show only roles with customer permission
$roles = Role::all()->reject(function($r) {
$roles = Role::all()->reject(function ($r) {
return !$r->hasPermission('read-customer-panel');
});
} else {
// Don't show roles with customer permission
$roles = Role::all()->reject(function($r) {
$roles = Role::all()->reject(function ($r) {
return $r->hasPermission('read-customer-panel');
});
}
$companies = Auth::user()->companies()->get()->sortBy('name');
foreach ($companies as $company) {
$company->setSettings();
}
@ -116,12 +122,6 @@ class Users extends Controller
*/
public function update(User $user, Request $request)
{
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('users');
}
// Do not reset password if not entered/changed
if (empty($request['password'])) {
unset($request['password']);
@ -131,6 +131,13 @@ class Users extends Controller
// Update user
$user->update($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'users');
$user->attachMedia($media, 'picture');
}
// Sync roles
$user->roles()->sync($request['roles']);

View File

@ -3,7 +3,9 @@
namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;
use App\Models\Common\Media;
use Storage;
use File;
class Uploads extends Controller
{
@ -14,10 +16,12 @@ class Uploads extends Controller
* @param $file
* @return boolean|Response
*/
public function get($folder, $file)
public function get($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($folder, $file)) {
if (!$path = $this->getPath($media)) {
return false;
}
@ -31,16 +35,45 @@ class Uploads extends Controller
* @param $file
* @return boolean|Response
*/
public function download($folder, $file)
public function download($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($folder, $file)) {
if (!$path = $this->getPath($media)) {
return false;
}
return response()->download($path);
}
/**
* Destroy the specified resource.
*
* @param $folder
* @param $file
* @return callable
*/
public function destroy($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($media)) {
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
flash($message)->warning();
return back();
}
$media->delete(); //will not delete files
File::delete($path);
return back();
}
/**
* Get the full path of resource.
*
@ -48,14 +81,13 @@ class Uploads extends Controller
* @param $file
* @return boolean|string
*/
protected function getPath($folder, $file)
protected function getPath($media)
{
// Add company id
if ($folder != 'users') {
$folder = session('company_id') . '/' . $folder;
}
$path = $media->basename;
$path = $folder . '/' . $file;
if (!empty($media->directory)) {
$path = $media->directory . '/' . $media->basename;
}
if (!Storage::exists($path)) {
return false;

View File

@ -59,9 +59,14 @@ class Companies extends Controller
setting()->set('general.company_email', $request->get('company_email'));
setting()->set('general.company_address', $request->get('company_address'));
$logo_path = $this->getUploadedFilePath($request->file('company_logo'), 'settings', $company->id);
if ($logo_path) {
setting()->set('general.company_logo', $logo_path);
if ($request->file('company_logo')) {
$company_logo = $this->getMedia($request->file('company_logo'), 'settings', $company->id);
if ($company_logo) {
$company->attachMedia($company_logo, 'company_logo');
setting()->set('general.company_logo', $company_logo->id);
}
}
setting()->set('general.default_currency', $request->get('default_currency'));
@ -135,9 +140,14 @@ class Companies extends Controller
setting()->set('general.company_email', $request->get('company_email'));
setting()->set('general.company_address', $request->get('company_address'));
$logo_path = $this->getUploadedFilePath($request->file('company_logo'), 'settings', $company->id);
if ($logo_path) {
setting()->set('general.company_logo', $logo_path);
if ($request->file('company_logo')) {
$company_logo = $this->getMedia($request->file('company_logo'), 'settings', $company->id);
if ($company_logo) {
$company->attachMedia($company_logo, 'company_logo');
setting()->set('general.company_logo', $company_logo->id);
}
}
setting()->set('general.default_payment_method', 'offlinepayment.cash.1');

View File

@ -5,9 +5,11 @@ namespace App\Http\Controllers\Customers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Profile as Request;
use App\Models\Auth\User;
use App\Traits\Uploads;
class Profile extends Controller
{
use Uploads;
public function index()
{
@ -42,12 +44,6 @@ class Profile extends Controller
{
$user = auth()->user();
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('users');
}
// Do not reset password if not entered/changed
if (empty($request['password'])) {
unset($request['password']);
@ -57,6 +53,13 @@ class Profile extends Controller
// Update user
$user->update($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'users');
$user->attachMedia($media, 'picture');
}
// Update customer
$user->customer->update($request->input());

View File

@ -128,16 +128,17 @@ class Bills extends Controller
$request['amount'] = 0;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$bill = Bill::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'bills');
$bill->attachMedia($media, 'attachment');
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -357,13 +358,6 @@ class Bills extends Controller
$request['amount'] = 0;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -460,6 +454,13 @@ class Bills extends Controller
$bill->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'bills');
$bill->attachMedia($media, 'attachment');
}
// Added bill total total
$bill_total = [
'company_id' => $request['company_id'],
@ -594,13 +595,6 @@ class Bills extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'revenues');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$bill = Bill::find($request['bill_id']);
$total_amount = $bill->amount;
@ -639,6 +633,13 @@ class Bills extends Controller
$bill_payment = BillPayment::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'bills');
$bill_payment->attachMedia($media, 'attachment');
}
$request['status_code'] = $bill->bill_status_code;
$request['notify'] = 0;

View File

@ -77,13 +77,14 @@ class Payments extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'payments');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$payment = Payment::create($request->input());
Payment::create($request->input());
// Upload attachment
$media = $this->getMedia($request->file('attachment'), 'payments');
if ($media) {
$payment->attachMedia($media, 'attachment');
}
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
@ -175,14 +176,15 @@ class Payments extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'payments');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$payment->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'payments');
$payment->attachMedia($media, 'attachment');
}
$message = trans('messages.success.updated', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();

View File

@ -20,6 +20,7 @@ use App\Models\Item\Item;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
use App\Models\Common\Media;
use App\Notifications\Income\Invoice as Notification;
use App\Notifications\Item\Item as ItemNotification;
use App\Traits\Currencies;
@ -137,16 +138,17 @@ class Invoices extends Controller
$request['amount'] = 0;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$invoice = Invoice::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice->attachMedia($media, 'attachment');
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -349,13 +351,6 @@ class Invoices extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -418,6 +413,13 @@ class Invoices extends Controller
$invoice->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice->attachMedia($media, 'attachment');
}
// Delete previous invoice totals
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
@ -624,13 +626,6 @@ class Invoices extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$invoice = Invoice::find($request['invoice_id']);
$total_amount = $invoice->amount;
@ -669,6 +664,13 @@ class Invoices extends Controller
$invoice_payment = InvoicePayment::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice_payment->attachMedia($media, 'attachment');
}
$request['status_code'] = $invoice->invoice_status_code;
$request['notify'] = 0;
@ -783,18 +785,26 @@ class Invoices extends Controller
{
$logo = '';
$media_id = setting('general.company_logo');
if (setting('general.invoice_logo')) {
$file = session('company_id') . '/' . setting('general.invoice_logo');
} else {
$file = session('company_id') . '/' . setting('general.company_logo');
$media_id = setting('general.invoice_logo');
}
$path = Storage::path($file);
$media = Media::find($media_id);
if (empty($media)) {
return $logo;
}
$path = Storage::path($media->getDiskPath());
if (!is_file($path)) {
return $logo;
}
$image = Image::make($path)->encode()->getEncoded();
if (empty($image)) {
return $logo;
}

View File

@ -79,13 +79,14 @@ class Revenues extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'revenues');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$revenue = Revenue::create($request->input());
Revenue::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'revenues');
$revenue->attachMedia($media, 'attachment');
}
$message = trans('messages.success.added', ['type' => trans_choice('general.revenues', 1)]);
@ -177,14 +178,15 @@ class Revenues extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'revenues');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$revenue->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'revenues');
$revenue->attachMedia($media, 'attachment');
}
$message = trans('messages.success.updated', ['type' => trans_choice('general.revenues', 1)]);
flash($message)->success();

View File

@ -111,9 +111,6 @@ class Updates extends Controller
// Clear cache after update
Artisan::call('cache:clear');
// Update database
Artisan::call('migrate', ['--force' => true]);
event(new UpdateFinished($alias, $old, $new));
flash(trans('updates.success'))->success();

View File

@ -53,13 +53,14 @@ class Items extends Controller
*/
public function store(Request $request)
{
// Upload picture
$picture_path = $this->getUploadedFilePath($request->file('picture'), 'items');
if ($picture_path) {
$request['picture'] = $picture_path;
}
$item = Item::create($request->input());
Item::create($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'items');
$item->attachMedia($media, 'picture');
}
$message = trans('messages.success.added', ['type' => trans_choice('general.items', 1)]);
@ -137,14 +138,15 @@ class Items extends Controller
*/
public function update(Item $item, Request $request)
{
// Upload picture
$picture_path = $this->getUploadedFilePath($request->file('picture'), 'items');
if ($picture_path) {
$request['picture'] = $picture_path;
}
$item->update($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'items');
$item->attachMedia($media, 'picture');
}
$message = trans('messages.success.updated', ['type' => trans_choice('general.items', 1)]);
flash($message)->success();

View File

@ -8,6 +8,7 @@ use App\Models\Banking\Account;
use App\Models\Company\Company;
use App\Models\Setting\Currency;
use App\Models\Setting\Setting;
use App\Models\Common\Media;
use App\Models\Setting\Tax;
use App\Traits\DateTime;
use App\Traits\Uploads;
@ -26,11 +27,20 @@ class Settings extends Controller
public function edit()
{
/*$setting = Setting::all()->pluck('value', 'key');*/
$setting = Setting::all()->map(function($s) {
$setting = Setting::all()->map(function ($s) {
$s->key = str_replace('general.', '', $s->key);
return $s;
})->pluck('value', 'key');
$company_logo = $setting->pull('company_logo');
$setting['company_logo'] = Media::find($company_logo);
$invoice_logo = $setting->pull('invoice_logo');
$setting['invoice_logo'] = Media::find($invoice_logo);
$timezones = $this->getTimezones();
$accounts = Account::enabled()->pluck('name', 'id');
@ -88,7 +98,13 @@ class Settings extends Controller
{
$fields = $request->all();
$company_id = $request->get('company_id');
if (empty($company_id)) {
$company_id = session('company_id');
}
$company = Company::find($company_id);
$skip_keys = ['company_id', '_method', '_token'];
$file_keys = ['company_logo', 'invoice_logo'];
@ -102,7 +118,14 @@ class Settings extends Controller
// Process file uploads
if (in_array($key, $file_keys)) {
$value = $this->getUploadedFilePath($request->file($key), 'settings');
// Upload attachment
if ($request->file($key)) {
$media = $this->getMedia($request->file($key), 'settings');
$company->attachMedia($media, $key);
$value = $media->id;
}
// Prevent reset
if (empty($value)) {
@ -133,5 +156,4 @@ class Settings extends Controller
return redirect('settings/settings');
}
}