Attendize/app/Models/Organiser.php

151 lines
3.9 KiB
PHP
Raw Normal View History

2016-03-05 00:18:10 +00:00
<?php
namespace App\Models;
2016-02-29 15:59:36 +00:00
use Illuminate\Http\UploadedFile;
2016-02-29 15:59:36 +00:00
use Str;
use Image;
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
class Organiser extends MyBaseModel
{
2016-03-14 16:37:38 +00:00
/**
* The validation rules for the model.
*
* @var array $rules
*/
2016-03-05 00:18:10 +00:00
protected $rules = [
'name' => ['required'],
'email' => ['required', 'email'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
];
2016-03-14 16:37:38 +00:00
/**
* The validation error messages for the model.
*
* @var array $messages
*/
2016-03-05 00:18:10 +00:00
protected $messages = [
'name.required' => 'You must at least give a name for the event organiser.',
'organiser_logo.max' => 'Please upload an image smaller than 10Mb',
'organiser_logo.size' => 'Please upload an image smaller than 10Mb',
'organiser_logo.mimes' => 'Please select a valid image type (jpeg, jpg, png)',
];
/**
* The account associated with the organiser
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function account()
{
return $this->belongsTo(\App\Models\Account::class);
}
2016-03-14 16:37:38 +00:00
/**
* The events associated with the organizer.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-03-05 00:18:10 +00:00
public function events()
{
return $this->hasMany(\App\Models\Event::class);
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
2016-03-14 16:37:38 +00:00
/**
* The attendees associated with the organizer.
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
2016-03-05 00:18:10 +00:00
public function attendees()
{
return $this->hasManyThrough(\App\Models\Attendee::class, \App\Models\Event::class);
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
/**
* Get the orders related to an organiser
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function orders()
{
return $this->hasManyThrough(\App\Models\Order::class, \App\Models\Event::class);
}
2016-03-14 16:37:38 +00:00
/**
* Get the full logo path of the organizer.
*
* @return mixed|string
*/
2016-03-05 00:18:10 +00:00
public function getFullLogoPathAttribute()
{
2016-09-06 20:39:27 +00:00
if ($this->logo_path && (file_exists(config('attendize.cdn_url_user_assets') . '/' . $this->logo_path) || file_exists(public_path($this->logo_path)))) {
return config('attendize.cdn_url_user_assets') . '/' . $this->logo_path;
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
return config('attendize.fallback_organiser_logo_url');
2016-02-29 15:59:36 +00:00
}
2016-03-14 16:37:38 +00:00
/**
* Get the url of the organizer.
*
* @return string
*/
2016-03-05 00:18:10 +00:00
public function getOrganiserUrlAttribute()
{
2016-02-29 15:59:36 +00:00
return route('showOrganiserHome', [
2016-03-05 00:18:10 +00:00
'organiser_id' => $this->id,
'organiser_slug' => Str::slug($this->oraganiser_name),
2016-02-29 15:59:36 +00:00
]);
}
2016-03-14 16:37:38 +00:00
/**
* Get the sales volume of the organizer.
*
* @return mixed|number
*/
2016-03-05 00:18:10 +00:00
public function getOrganiserSalesVolumeAttribute()
{
2016-02-29 15:59:36 +00:00
return $this->events->sum('sales_volume');
}
2016-03-14 16:37:38 +00:00
/**
* TODO:implement DailyStats method
*/
2016-03-05 00:18:10 +00:00
public function getDailyStats()
{
2016-02-29 15:59:36 +00:00
}
/**
* Set a new Logo for the Organiser
*
* @param \Illuminate\Http\UploadedFile $file
*/
public function setLogo(UploadedFile $file)
{
$filename = str_slug($this->name).'-logo-'.$this->id.'.'.strtolower($file->getClientOriginalExtension());
// Image Directory
$imageDirectory = public_path() . '/' . config('attendize.organiser_images_path');
// Paths
$relativePath = config('attendize.organiser_images_path').'/'.$filename;
$absolutePath = public_path($relativePath);
$file->move($imageDirectory, $filename);
$img = Image::make($absolutePath);
$img->resize(250, 250, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->save($absolutePath);
if (file_exists($absolutePath)) {
$this->logo_path = $relativePath;
}
}
2016-02-29 15:59:36 +00:00
}