-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
153 lines (130 loc) · 4.2 KB
/
Copy pathModel.php
File metadata and controls
153 lines (130 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseModel;
class VcardsModel extends BaseModel {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Model
$this->init('vcards');
}
/**
* Process a record
*
* @param array $record
* @return array
*/
protected function process(array $record): array
{
// Call the parent constructor
$record = parent::process($record);
// Process the JSON fields
if(!is_array($record['role'])){
$record['role'] = json_decode($record['role'] ?? "[]", true);
}
if(!is_array($record['tags'])){
$record['tags'] = json_decode($record['tags'] ?? "[]", true);
}
if(!is_array($record['industries'])){
$record['industries'] = json_decode($record['industries'] ?? "[]", true);
}
// Return the processed record
return $record;
}
/**
* Apply Joins to the Query
*
* @param Query $Query
* @return Query
*/
protected function joins(object $Query): object
{
// Apply Joins
$Query->join('avatar', 'files', 'id')
->join('country', 'countries', 'code')
->join('state', 'states', 'code');
return $Query;
}
/**
* Retrieve a single record by email
*
* @param string $email
* @return array
*/
public function fetchByEmail(string $email): array
{
// Create the Query
$Query = $this->Database->query()
->table($this->table)
->select('*')
->join('owner', 'users', 'username')
->join('owner.vcard', 'vcards', 'id')
->filter()
->where('id', 9999, '<>')
->filter()
->where('email', $email)
->limit(1);
// Verify the Organization
if(array_key_exists('organization',$this->definition)){
if($this->Auth->isAuthenticated()){
$Query->join('organization', 'organizations', 'id')
->join('organization.vcard', 'vcards', 'id')
->where('organization', $this->Auth->user()->organization()->id);
}
}
// Apply Joins
$Query = $this->joins($Query);
// Retrieve the record
$records = $Query->fetch();
// Loop through the records to process them
foreach($records as $key => $record){
// Overwrite the record with the processed one
$records[$key] = $this->process($record);
}
// Return the record or an empty array if not found
return $records[array_key_first($records)] ?? [];
}
/**
* Retrieve a single record by website
*
* @param string $website
* @return array
*/
public function fetchByWebsite(string $website): array
{
// Create the Query
$Query = $this->Database->query()
->table($this->table)
->select('*')
->join('owner', 'users', 'username')
->join('owner.vcard', 'vcards', 'id')
->filter()
->where('id', 9999, '<>')
->filter()
->where('website', $website)
->limit(1);
// Verify the Organization
if(array_key_exists('organization',$this->definition)){
if($this->Auth->isAuthenticated()){
$Query->join('organization', 'organizations', 'id')
->join('organization.vcard', 'vcards', 'id')
->where('organization', $this->Auth->user()->organization()->id);
}
}
// Apply Joins
$Query = $this->joins($Query);
// Retrieve the record
$records = $Query->fetch();
// Loop through the records to process them
foreach($records as $key => $record){
// Overwrite the record with the processed one
$records[$key] = $this->process($record);
}
// Return the record or an empty array if not found
return $records[array_key_first($records)] ?? [];
}
}