refs #164 models update

This commit is contained in:
Cüneyt Şentürk 2018-01-10 18:21:12 +03:00
parent e65743c7c3
commit 87b327d9b8
11 changed files with 70 additions and 48 deletions

View File

@ -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;
@ -26,6 +28,26 @@ class Version117 extends Listener
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 = [
@ -77,23 +99,39 @@ class Version117 extends Listener
}
}
$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 => $item) {
if ($item->value) {
$path = explode('uploads/', $item->value);
foreach ($settings as $name => $items) {
foreach ($items as $item) {
if ($item->value) {
$path = explode('uploads/', $item->value);
$path = end($path);
$path = end($path);
if (!empty($item->company_id) && (strpos($path, $item->company_id . '/') === false)) {
$path = $item->company_id . '/' . $path;
}
if (!empty($item->company_id) && (strpos($path, $item->company_id . '/') === false)) {
$path = $item->company_id . '/' . $path;
}
if (!empty($path) && Storage::exists($path)) {
$media = MediaUploader::importPath(config('mediable.default_disk'), $path);
if (!empty($path) && Storage::exists($path)) {
$company = \App\Models\Company\Company::find($item->company_id);
$item->attachMedia($media, $name);
$media = \App\Models\Common\Media::where('filename', '=', pathinfo(basename($path), PATHINFO_FILENAME))->first();
if ($company && !$media) {
$media = MediaUploader::importPath(config('mediable.default_disk'), $path);
$company->attachMedia($media, $name);
$item->update(['value' => $media->id]);
} elseif ($media) {
$item->update(['value' => $media->id]);
} else {
$item->update(['value' => '']);
}
} else {
$item->update(['value' => '']);
}
}
}
}

View File

@ -88,11 +88,9 @@ class User extends Authenticatable
}
}
if (!empty($value)) {
if (!empty($value) && !$this->hasMedia('picture')) {
return $value;
}
if (!$this->hasMedia('picture')) {
} elseif (!$this->hasMedia('picture')) {
return false;
}

View File

@ -238,11 +238,9 @@ class Company extends Eloquent
*/
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;
}

View File

@ -137,11 +137,9 @@ class Bill extends Model
*/
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;
}

View File

@ -87,11 +87,9 @@ class BillPayment extends Model
*/
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;
}

View File

@ -100,11 +100,9 @@ class Payment extends Model
*/
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;
}

View File

@ -146,11 +146,9 @@ class Invoice extends Model
*/
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;
}

View File

@ -87,11 +87,9 @@ class InvoicePayment extends Model
*/
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;
}

View File

@ -106,11 +106,9 @@ class Revenue extends Model
*/
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;
}

View File

@ -120,11 +120,9 @@ class Item extends Model
*/
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;
}

View File

@ -10,6 +10,8 @@ class Setting extends Model
protected $table = 'settings';
public $timestamps = false;
/**
* Attributes that should be mass-assignable.
*