-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
125 lines (99 loc) · 3.65 KB
/
Model.php
File metadata and controls
125 lines (99 loc) · 3.65 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
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseModel;
class FollowupsModel extends BaseModel {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Model
$this->init('followups');
}
/**
* Initialize the Model
*
* @param string $table
* @param string|null $primary
* @return void
*/
protected function init(string $table, ?string $primary = 'id'): void
{
// Call the parent init method
parent::init($table, $primary);
// Loop through the additional tables to join
foreach($this->definition as $field => $col){
// Check if the field contains a dot
if(strpos($field, '.') === false) continue;
// Set the table
$table = in_array(explode('.',$field)[1],['owner', 'assignedTo']) ? 'users' : explode('.',$field)[1] . 's';
$table = in_array(explode('.',$field)[1],['category']) ? 'categories' : $table;
// Check if the field is linked to a table
if(in_array($table, $this->tables)){
// Initialize the Schema
$schema = $this->Database->schema()->define($table);
// Describe the table
foreach($schema->describe() as $col){
// Add the col to the definition
$this->definition[$field.'.'.$col['Field']] = $col;
}
};
}
}
/**
* Process a record
*
* @param array $record
* @return array
*/
protected function process(array $record): array
{
// Call the parent constructor
$record = parent::process($record);
// Check if the record has a task
if(array_key_exists('task', $record) && array_key_exists('process', $record['task'])){
// Check if the process is a valid JSON string
if(is_string($record['task']['process']) && $this->isJson($record['task']['process'])){
// Decode the JSON value
$record['task']['process'] = json_decode($record['task']['process'], true);
}
}
// Check if the vcard exists in the record
if(array_key_exists('vcard', $record)){
// Check if the vcard has tags
if(array_key_exists('tags', $record['vcard'])){
// Check if the process is a valid JSON string
if(is_string($record['vcard']['tags']) && $this->isJson($record['vcard']['tags'])){
// Decode the JSON value
$record['vcard']['tags'] = json_decode($record['vcard']['tags'], true);
}
}
// Check if the vcard has industries
if(array_key_exists('industries', $record['vcard'])){
// Check if the process is a valid JSON string
if(is_string($record['vcard']['industries']) && $this->isJson($record['vcard']['industries'])){
// Decode the JSON value
$record['vcard']['industries'] = json_decode($record['vcard']['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('vcard', 'vcards', 'id')
->join('task', 'tasks', 'id')
->join('task.assignedTo', 'users', 'id');
return $Query;
}
}