-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
141 lines (115 loc) · 4.31 KB
/
Model.php
File metadata and controls
141 lines (115 loc) · 4.31 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
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseModel;
class TasksModel extends BaseModel {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Model
$this->init('tasks');
}
/**
* Process a record
*
* @param array $record
* @return array
*/
protected function process(array $record): array
{
// Call the parent constructor
$record = parent::process($record);
// Decode the process
if(!is_array($record['process'])){
$record['process'] = json_decode($record['process'] ?? "[]", 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('assignedTo', 'users', 'id');
return $Query;
}
/**
* Update a record
*
* @param int $id
* @param array $data
* @return int
*/
public function update(int $id, array $data): int
{
// Sanitize the Data
foreach($data as $key => $value){
// Add exceptions for specific fields
if($key === 'process' && is_array($value)){
// Loop through each step in the array
foreach($value as $stepKey => $step){
// Sanitize the step
// Boolean values
$value[$stepKey]['isCompleted'] = filter_var($step['isCompleted'], FILTER_VALIDATE_BOOLEAN);
// Loop through each tasks in the array
foreach($step['tasks'] as $taskKey => $task){
// Sanitize the task
// Boolean values
$value[$stepKey]['tasks'][$taskKey]['isCompleted'] = filter_var($task['isCompleted'], FILTER_VALIDATE_BOOLEAN);
$value[$stepKey]['tasks'][$taskKey]['isDisabled'] = filter_var($task['isDisabled'], FILTER_VALIDATE_BOOLEAN);
// Integer values
$value[$stepKey]['tasks'][$taskKey]['cost'] = intval($task['cost'] ?? 0);
// String/null values
$value[$stepKey]['tasks'][$taskKey]['onComplete'] = $task['onComplete'] ? trim($task['onComplete']) : null;
$value[$stepKey]['tasks'][$taskKey]['value'] = $task['value'] ? trim($task['value']) : null;
// Check if the task is completed and if it is the last task in the last step
if($value[$stepKey]['tasks'][$taskKey]['isCompleted'] && count($value) === $stepKey && count($step['tasks']) === $taskKey){
// Set task as completed
$data['isCompleted'] = $value[$stepKey]['tasks'][$taskKey]['isCompleted'];
$data['completedOn'] = $value[$stepKey]['tasks'][$taskKey]['onComplete'];
}
}
}
}
// Set the value back to the data array
$data[$key] = $value;
}
// Call the parent update method
return parent::update($id, $data);
}
/**
* Create a new record and return the id
*
* @param array $data
* @return int
*/
public function create(array $data): int
{
// Call the parent constructor
$id = parent::create($data);
// Check if the id is valid
if($id){
// Retrieve the task
$task = $this->fetch($id);
// Check if the task's root has a task
if(isset($task['root'],$task['root']['target'],$task['root']['target']['task']) && $task['root']['target']['task']){
// Retrieve root the task
$root = $this->fetch($task['root']['target']['task']);
// Check if the task priority matches the root task priority
if($task['priority'] != $root['priority']){
$this->update($id, ['priority' => $root['priority']]);
}
}
}
// Return the id
return $id;
}
}