|
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
|
10 |
|
public function __construct($business) |
|
18
|
|
|
{ |
|
19
|
10 |
|
$this->business = $business; |
|
20
|
10 |
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function listing($limit) |
|
23
|
|
|
{ |
|
24
|
1 |
|
return $this->business->contacts()->orderBy('lastname', 'ASC')->simplePaginate($limit); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function find(Contact $contact) |
|
28
|
|
|
{ |
|
29
|
1 |
|
return $this->business->contacts()->find($contact->id); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function register($data) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$contact = $this->getSubscribed($data['email']); |
|
35
|
|
|
|
|
36
|
1 |
|
if ($contact) { |
|
37
|
|
|
return $contact; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
$this->sanitizeDate($data['birthdate']); |
|
41
|
|
|
|
|
42
|
1 |
|
$contact = Contact::create($data); |
|
43
|
|
|
|
|
44
|
1 |
|
$this->business->contacts()->attach($contact); |
|
45
|
1 |
|
$this->business->save(); |
|
46
|
|
|
|
|
47
|
1 |
|
if (array_key_exists('notes', $data)) { |
|
48
|
|
|
$this->updateNotes($contact, $data['notes']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return $contact; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function update(Contact $contact, $data = [], $notes = null) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$birthdate = array_get($data, 'birthdate'); |
|
57
|
1 |
|
$this->sanitizeDate($birthdate); |
|
58
|
|
|
|
|
59
|
1 |
|
$contact->firstname = array_get($data, 'firstname'); |
|
|
|
|
|
|
60
|
1 |
|
$contact->lastname = array_get($data, 'lastname'); |
|
|
|
|
|
|
61
|
1 |
|
$contact->email = array_get($data, 'email'); |
|
|
|
|
|
|
62
|
1 |
|
$contact->nin = array_get($data, 'nin'); |
|
|
|
|
|
|
63
|
1 |
|
$contact->gender = array_get($data, 'gender'); |
|
|
|
|
|
|
64
|
1 |
|
$contact->birthdate = $birthdate; |
|
|
|
|
|
|
65
|
1 |
|
$contact->mobile = array_get($data, 'mobile'); |
|
|
|
|
|
|
66
|
1 |
|
$contact->mobile_country = array_get($data, 'mobile_country'); |
|
|
|
|
|
|
67
|
1 |
|
$contact->postal_address = array_get($data, 'postal_address'); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
1 |
|
$contact->save(); |
|
70
|
|
|
|
|
71
|
1 |
|
$this->updateNotes($contact, $notes); |
|
72
|
|
|
|
|
73
|
1 |
|
return $contact; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function getSubscribed($email) |
|
77
|
|
|
{ |
|
78
|
2 |
|
if (trim($email) == '') { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return $this->business->contacts()->where('email', '=', $email)->first(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function getExisting($email) |
|
86
|
|
|
{ |
|
87
|
1 |
|
if (trim($email) == '') { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
return Contact::whereNotNull('user_id')->where('email', '=', $email)->first(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function getRegisteredUserId($userId) |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->business->contacts()->where('user_id', '=', $userId)->first(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function remove(Contact $contact) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->business->contacts()->detach($contact->id); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function linkToUserId(Contact $contact, $userId) |
|
105
|
|
|
{ |
|
106
|
1 |
|
$contact->user()->associate($userId); |
|
107
|
1 |
|
$contact->save(); |
|
108
|
|
|
|
|
109
|
1 |
|
return $contact->fresh(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
public function copyFrom(Contact $contact, $userId) |
|
113
|
|
|
{ |
|
114
|
1 |
|
$replicatedContact = $contact->replicate(['id']); |
|
115
|
1 |
|
$replicatedContact->user()->associate($userId); |
|
116
|
1 |
|
$replicatedContact->businesses()->detach(); |
|
117
|
1 |
|
$replicatedContact->save(); |
|
118
|
|
|
|
|
119
|
1 |
|
$this->business->contacts()->attach($replicatedContact); |
|
120
|
1 |
|
$this->business->save(); |
|
121
|
|
|
|
|
122
|
1 |
|
return $replicatedContact; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
protected function updateNotes(Contact $contact, $notes = null) |
|
126
|
|
|
{ |
|
127
|
1 |
|
if ($notes) { |
|
128
|
|
|
$this->business->contacts()->find($contact->id)->pivot->update(compact('notes')); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected function getDateFormat() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->business->pref('date_format'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
protected function sanitizeDate(&$value) |
|
138
|
|
|
{ |
|
139
|
2 |
|
if (!is_string($value)) { |
|
140
|
|
|
return $value; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
2 |
|
if (trim($value) == '') { |
|
144
|
|
|
return $value = null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
if (strlen($value) == 19) { |
|
148
|
2 |
|
return $value = Carbon::parse($value); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (strlen($value) == 10) { |
|
152
|
|
|
return $value = Carbon::createFromFormat('m/d/Y', $value); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
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@propertyannotation 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.