Skip to content

Commit

Permalink
refs #164 models update
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Jan 10, 2018
1 parent e65743c commit 87b327d
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 48 deletions.
62 changes: 50 additions & 12 deletions app/Listeners/Updates/Version117.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Listeners\Updates;

use App\Events\UpdateFinished;
use App\Models\Auth\Role;
use App\Models\Auth\Permission;
use MediaUploader;
use Storage;
use Artisan;
Expand All @@ -26,6 +28,26 @@ public function handle(UpdateFinished $event)
return;
}

// Create permission
$permission = Permission::firstOrCreate([
'name' => 'delete-common-uploads',
'display_name' => 'Delete Common Uploads',
'description' => 'Delete Common Uploads',
]);

// Attach permission to roles
$roles = Role::all();

foreach ($roles as $role) {
$allowed = ['admin'];

if (!in_array($role->name, $allowed)) {
continue;
}

$role->attachPermission($permission);
}

$data = [];

$migrations = [
Expand Down Expand Up @@ -77,23 +99,39 @@ public function handle(UpdateFinished $event)
}
}

$settings['company_logo'] = \App\Models\Setting\Setting::where('key', '=', 'general.company_logo')->get();
$settings['invoice_logo'] = \App\Models\Setting\Setting::where('key', '=', 'general.invoice_logo')->get();
$settings['company_logo'] = \App\Models\Setting\Setting::where('key', '=', 'general.company_logo')->where('company_id', '<>', '0')->get();
$settings['invoice_logo'] = \App\Models\Setting\Setting::where('key', '=', 'general.invoice_logo')->where('company_id', '<>', '0')->get();

foreach ($settings as $name => $items) {
foreach ($items as $item) {
if ($item->value) {
$path = explode('uploads/', $item->value);

$path = end($path);

foreach ($settings as $name => $item) {
if ($item->value) {
$path = explode('uploads/', $item->value);
if (!empty($item->company_id) && (strpos($path, $item->company_id . '/') === false)) {
$path = $item->company_id . '/' . $path;
}

$path = end($path);
if (!empty($path) && Storage::exists($path)) {
$company = \App\Models\Company\Company::find($item->company_id);

if (!empty($item->company_id) && (strpos($path, $item->company_id . '/') === false)) {
$path = $item->company_id . '/' . $path;
}
$media = \App\Models\Common\Media::where('filename', '=', pathinfo(basename($path), PATHINFO_FILENAME))->first();

if (!empty($path) && Storage::exists($path)) {
$media = MediaUploader::importPath(config('mediable.default_disk'), $path);
if ($company && !$media) {
$media = MediaUploader::importPath(config('mediable.default_disk'), $path);

$item->attachMedia($media, $name);
$company->attachMedia($media, $name);

$item->update(['value' => $media->id]);
} elseif ($media) {
$item->update(['value' => $media->id]);
} else {
$item->update(['value' => '']);
}
} else {
$item->update(['value' => '']);
}
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions app/Models/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ public function getPictureAttribute($value)
}
}

if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('picture')) {
return $value;
}

if (!$this->hasMedia('picture')) {
} elseif (!$this->hasMedia('picture')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Company/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,9 @@ public function emailSortable($query, $direction)
*/
public function getCompanyLogoAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('company_logo')) {
return $value;
}

if (!$this->hasMedia('company_logo')) {
} elseif (!$this->hasMedia('company_logo')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Expense/Bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,9 @@ public function setCurrencyRateAttribute($value)
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
}

if (!$this->hasMedia('attachment')) {
} elseif (!$this->hasMedia('attachment')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Expense/BillPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ public function scopePaid($query)
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
}

if (!$this->hasMedia('attachment')) {
} elseif (!$this->hasMedia('attachment')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Expense/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ public static function scopeLatest($query)
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
}

if (!$this->hasMedia('attachment')) {
} elseif (!$this->hasMedia('attachment')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Income/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ public function setCurrencyRateAttribute($value)
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
}

if (!$this->hasMedia('attachment')) {
} elseif (!$this->hasMedia('attachment')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Income/InvoicePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ public function scopePaid($query)
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
}

if (!$this->hasMedia('attachment')) {
} elseif (!$this->hasMedia('attachment')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Income/Revenue.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ public function scopeLatest($query)
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
}

if (!$this->hasMedia('attachment')) {
} elseif (!$this->hasMedia('attachment')) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ public function categorySortable($query, $direction)
*/
public function getPictureAttribute($value)
{
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('picture')) {
return $value;
}

if (!$this->hasMedia('picture')) {
} elseif (!$this->hasMedia('picture')) {
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Setting/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Setting extends Model

protected $table = 'settings';

public $timestamps = false;

/**
* Attributes that should be mass-assignable.
*
Expand Down

0 comments on commit 87b327d

Please sign in to comment.