1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar; |
7
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; // to remove |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
class Teacher extends Model |
12
|
|
|
{ |
13
|
|
|
/***************************************************************************/ |
14
|
|
|
/** |
15
|
|
|
* The table associated with the model. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $table = 'teachers'; |
20
|
|
|
|
21
|
|
|
/***************************************************************************/ |
22
|
|
|
|
23
|
|
|
protected $fillable = [ |
24
|
|
|
'name', 'bio', 'country_id', 'year_starting_practice', 'year_starting_teach', 'significant_teachers', 'profile_picture', 'website', 'facebook', 'created_by', 'slug', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/***************************************************************************/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the user that owns the event. eg. $teacher->user. |
31
|
|
|
*/ |
32
|
|
|
public function user() |
33
|
|
|
{ |
34
|
|
|
return $this->belongsTo('\Illuminate\Foundation\Auth\User', 'created_by'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/***************************************************************************/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the events for the teacher. |
41
|
|
|
*/ |
42
|
3 |
|
public function events() |
43
|
|
|
{ |
44
|
3 |
|
return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Event', 'event_has_teachers', 'teacher_id', 'event_id'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/***************************************************************************/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the events where this teacher is going to teach to. |
51
|
|
|
* |
52
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Teacher $teacher |
53
|
|
|
* @return \Illuminate\Http\Response |
54
|
|
|
*/ |
55
|
3 |
|
public static function eventsByTeacher($teacher, $lastestEventsRepetitionsQuery) |
56
|
|
|
{ |
57
|
3 |
|
$ret = $teacher->events() |
58
|
3 |
|
->select('events.title', 'events.category_id', 'events.slug', 'event_venues.name AS venue_name', 'countries.name AS country', 'event_venues.city AS city', 'events.sc_teachers_names', 'event_repetitions.start_repeat', 'event_repetitions.end_repeat') |
59
|
3 |
|
->join('event_venues', 'event_venues.id', '=', 'events.venue_id') |
60
|
3 |
|
->join('countries', 'countries.id', '=', 'event_venues.country_id') |
61
|
|
|
->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
62
|
3 |
|
$join->on('events.id', '=', 'event_repetitions.event_id'); |
63
|
3 |
|
}) |
64
|
3 |
|
->orderBy('event_repetitions.start_repeat', 'asc') |
65
|
3 |
|
->get(); |
66
|
|
|
|
67
|
3 |
|
return $ret; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/***************************************************************************/ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Save the record on DB. |
74
|
|
|
* |
75
|
|
|
* @param \Illuminate\Http\Request $request |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
3 |
|
public function preSave(Request $request) |
79
|
|
|
{ |
80
|
3 |
|
$this->name = $request->get('name'); |
81
|
|
|
//$this->bio = $request->get('bio'); |
82
|
3 |
|
$this->bio = clean($request->get('bio')); |
83
|
3 |
|
$this->country_id = $request->get('country_id'); |
84
|
3 |
|
$this->year_starting_practice = $request->get('year_starting_practice'); |
85
|
3 |
|
$this->year_starting_teach = $request->get('year_starting_teach'); |
86
|
3 |
|
$this->significant_teachers = $request->get('significant_teachers'); |
87
|
|
|
|
88
|
|
|
// Teacher profile picture upload |
89
|
3 |
|
if ($request->file('profile_picture')) { |
90
|
|
|
$imageFile = $request->file('profile_picture'); |
91
|
|
|
$imageName = $imageFile->hashName(); |
92
|
|
|
$imageSubdir = 'teachers_profile'; |
93
|
|
|
$imageWidth = 968; |
94
|
|
|
$thumbWidth = 300; |
95
|
|
|
|
96
|
|
|
LaravelEventsCalendar::uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth); |
|
|
|
|
97
|
|
|
$this->profile_picture = $imageName; |
98
|
|
|
} else { |
99
|
3 |
|
$this->profile_picture = $request->profile_picture; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
$this->website = $request->get('website'); |
103
|
3 |
|
$this->facebook = $request->get('facebook'); |
104
|
|
|
|
105
|
|
|
//$this->created_by = Auth::id(); |
106
|
3 |
|
$this->created_by = $request->get('created_by'); |
107
|
|
|
|
108
|
3 |
|
if (! $this->slug) { |
109
|
2 |
|
$this->slug = Str::slug($this->name, '-').'-'.rand(10000, 100000); |
110
|
|
|
} |
111
|
3 |
|
} |
112
|
|
|
|
113
|
|
|
/***************************************************************************/ |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|