1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Models\Business; |
7
|
|
|
use Timegridio\Concierge\Models\Contact; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The Addressbook class acts as a simplified Contact repository with most |
11
|
|
|
* common read/write functions. |
12
|
|
|
*/ |
13
|
|
|
class Addressbook |
14
|
|
|
{ |
15
|
|
|
private $business; |
16
|
|
|
|
17
|
|
|
public function __construct($business) |
18
|
|
|
{ |
19
|
|
|
$this->business = $business; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function listing($limit) |
23
|
|
|
{ |
24
|
|
|
return $this->business->contacts()->orderBy('lastname', 'ASC')->simplePaginate($limit); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function find(Contact $contact) |
28
|
|
|
{ |
29
|
|
|
return $this->business->contacts()->find($contact->id); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function register($data) |
33
|
|
|
{ |
34
|
|
|
$contact = $this->reuseExisting($data['email']); |
35
|
|
|
|
36
|
|
|
if ($contact) { |
37
|
|
|
return $contact; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->sanitizeDate($data['birthdate']); |
41
|
|
|
|
42
|
|
|
$contact = Contact::create($data); |
43
|
|
|
|
44
|
|
|
$this->business->contacts()->attach($contact); |
45
|
|
|
$this->business->save(); |
46
|
|
|
|
47
|
|
|
if (array_key_exists('notes', $data)) { |
48
|
|
|
$this->updateNotes($contact, $data['notes']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $contact; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function update(Contact $contact, $data = [], $notes = null) |
55
|
|
|
{ |
56
|
|
|
$birthdate = array_get($data, 'birthdate'); |
57
|
|
|
$this->sanitizeDate($birthdate); |
58
|
|
|
|
59
|
|
|
$contact->firstname = array_get($data, 'firstname'); |
|
|
|
|
60
|
|
|
$contact->lastname = array_get($data, 'lastname'); |
|
|
|
|
61
|
|
|
$contact->email = array_get($data, 'email'); |
|
|
|
|
62
|
|
|
$contact->nin = array_get($data, 'nin'); |
|
|
|
|
63
|
|
|
$contact->gender = array_get($data, 'gender'); |
|
|
|
|
64
|
|
|
$contact->birthdate = $birthdate; |
|
|
|
|
65
|
|
|
$contact->mobile = array_get($data, 'mobile'); |
|
|
|
|
66
|
|
|
$contact->mobile_country = array_get($data, 'mobile_country'); |
|
|
|
|
67
|
|
|
$contact->postal_address = array_get($data, 'postal_address'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$contact->save(); |
70
|
|
|
|
71
|
|
|
$this->updateNotes($contact, $notes); |
72
|
|
|
|
73
|
|
|
return $contact; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function reuseExisting($email) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (trim($email) == '') { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->business->contacts()->where('email', '=', $email)->first(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getExisting($email) |
86
|
|
|
{ |
87
|
|
|
if (trim($email) == '') { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return Contact::whereNotNull('user_id')->where('email', '=', $email)->first(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function getSubscribed($email) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
if (trim($email) == '') { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->business->contacts()->where('email', '=', $email)->first(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getRegisteredUserId($userId) |
104
|
|
|
{ |
105
|
|
|
return $this->business->contacts()->where('user_id', '=', $userId)->first(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function reuseExistingByUserId($userId) |
109
|
|
|
{ |
110
|
|
|
return $this->business->contacts()->where('user_id', '=', $userId)->first(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function remove(Contact $contact) |
114
|
|
|
{ |
115
|
|
|
return $this->business->contacts()->detach($contact->id); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function linkToUserId(Contact $contact, $userId) |
119
|
|
|
{ |
120
|
|
|
$contact->user()->associate($userId); |
121
|
|
|
$contact->save(); |
122
|
|
|
|
123
|
|
|
return $contact->fresh(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function copyFrom(Contact $existingContact, $userId) |
127
|
|
|
{ |
128
|
|
|
$existingContactData = $existingContact->toArray(); |
129
|
|
|
$this->sanitizeDate($existingContactData['birthdate']); |
130
|
|
|
$contact = Contact::create($existingContactData); |
131
|
|
|
$contact->user()->associate($userId); |
132
|
|
|
$contact->businesses()->detach(); |
133
|
|
|
$contact->save(); |
134
|
|
|
|
135
|
|
|
$this->business->contacts()->attach($contact); |
136
|
|
|
$this->business->save(); |
137
|
|
|
|
138
|
|
|
return $contact; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function updateNotes(Contact $contact, $notes = null) |
142
|
|
|
{ |
143
|
|
|
if ($notes) { |
144
|
|
|
$this->business->contacts()->find($contact->id)->pivot->update(compact('notes')); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function sanitizeDate(&$value) |
149
|
|
|
{ |
150
|
|
|
if (!is_string($value)) { |
151
|
|
|
return $value; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (trim($value) == '') { |
155
|
|
|
return $value = null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (strlen($value) == 19) { |
159
|
|
|
return $value = Carbon::parse($value); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (strlen($value) == 10) { |
163
|
|
|
return $value = Carbon::createFromFormat(trans('app.dateformat.carbon'), $value); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.