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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
| <?php
// require_once 'phpx.php';
require_once 'active-record.php';
require_once 'email-model.php';
require_once 'delegation-model.php';
require_once 'phone-model.php';
require_once 'document-model.php';
require_once 'milestone-model.php'; // Milestone::find
require_once 'todotype.php'; // EMAIL, DELEGATION, PHONE, DOCUMENT
abstract class Todo extends ActiveRecord
{
public $milestone, $ord, $priority, $shortname, $longname, $description, $contact, $deadline, $done;
public abstract function todotype();
public abstract function view();
public function initialize($milestone, $ord, $priority, $shortname, $longname, $description, $contact, $deadline)
{
$this->id = NULL; // unsaved yet
$this->milestone = $milestone;
$this->ord = $ord;
$this->priority = $priority;
$this->shortname = $shortname;
$this->longname = $longname;
$this->description = $description;
$this->contact = $contact;
$this->deadline = $deadline;
$this->done = NULL; // not accomplished yet
}
public function milestone()
{
return $this->milestone;
}
public function project()
{
return $this->milestone->project;
}
protected function getInsertSql()
{
return "INSERT INTO todo (milestone_id, todotype, ord, priority, shortname, longname, description, contact_id, deadline, done) VALUES (:milestone_id, :todotype, :ord, :priority, :shortname, :longname, :description, :contact_id, :deadline, :done)";
}
protected function bindAllButId($st)
{
$st->bindParam(':milestone_id', $this->milestone->id);
$todotype = $this->todotype();
$st->bindParam(':todotype', $todotype);
$st->bindParam(':ord', $this->ord);
$st->bindParam(':priority', $this->priority);
$st->bindParam(':shortname', $this->shortname);
$st->bindParam(':longname', $this->longname);
$st->bindParam(':description', $this->description);
$contact_id = isset($this->contact) ? $this->contact->id : NULL;
$st->bindParam(':contact_id', $contact_id);
$st->bindParam(':deadline', $this->deadline);
$st->bindParam(':done', $this->done);
}
public function delete($pdo)
{
$saved = isset($this->id);
if ($saved) {
$sql = 'DELETE FROM todo WHERE id = :id';
$st = $pdo->prepare($sql);
$st->bindParam(':id', $this->id);
$st->execute();
}
$this->id = NULL; // `delete' is the opposite of `insert'
return $saved;
}
protected function commit($pdo)
{
$sql = 'UPDATE todo SET milestone_id = :milestone_id, todotype = :todotype, ord = :ord, priority = :priority, shortname = :shortname, longname = :longname, description = :description, contact_id = :contact_id, deadline = :deadline, done = :done WHERE id = :id';
$st = $pdo->prepare($sql);
$this->bindAllButId($st);
$st->bindParam(':id', $this->id);
$st->execute();
}
public static function factory($milestone, $todotype, $ord, $priority, $shortname, $longname, $description, $contact, $deadline)
{
switch ($todotype) {
case EMAIL:
return new Email($milestone, $ord, $priority, $shortname, $longname, $description, $contact, $deadline);
case DELEGATION:
return new Delegation($milestone, $ord, $priority, $shortname, $longname, $description, $contact, $deadline);
case PHONE:
return new Phone($milestone, $ord, $priority, $shortname, $longname, $description, $contact, $deadline);
case DOCUMENT:
return new Document($milestone, $ord, $priority, $shortname, $longname, $description, $contact, $deadline);
}
}
public static function create($pdo, $milestone, $todotype, $ord, $priority, $shortname, $longname, $description, $contact, $deadline)
{
$todo = self::factory($milestone, $todotype, $ord, $priority, $shortname, $longname, $description, $contact, $deadline);
$todo->insert($pdo);
return $todo;
}
public static function find($id, $pdo)
{
$sql = "SELECT id, milestone_id, todotype, ord, priority, shortname, longname, description, contact_id, deadline, done FROM todo WHERE id = :id";
$st = $pdo->prepare($sql);
$st->bindParam(':id', $id);
$st->execute();
$rec = $st->fetch(PDO::FETCH_ASSOC);
return self::rec2ob($rec, $pdo); // it will lookup $milestone
}
public static function rec2ob($rec, $pdo) // $pdo needed for looking up $milestone!
{
$milestone = Milestone::find($rec['milestone_id'], $pdo);
$person = Person::find($rec['contact_id'], $pdo);
$todo = self::factory(
$milestone,
$rec['todotype'], $rec['ord'], $rec['priority'],
$rec['shortname'], $rec['longname'], $rec['description'],
$person,
$rec['deadline'], $rec['done']
);
$todo->id = $rec['id'];
return $todo;
}
// Qualified records (recs with q. attrs and with all-direct data) can be converted without need of $pdo acceess. No lookup!
public static function qrec2ob($qrec, $pdo) // $pdo not needed!
{
$contact = Person::find($qrec['todo_contactId'], $pdo);
$projectmanager = Account::find($qrec['project_projectmanagerId'], $pdo);
$project = new Project(
$projectmanager,
$qrec['project_shortname'], $qrec['project_longname'], $qrec['project_description']
);
$project->id = $qrec['project_id'];
$milestone = new Milestone(
$project,
$qrec['milestone_ord'],
$qrec['milestone_shortname'], $qrec['milestone_longname'], $qrec['milestone_description']
);
$milestone->id = $qrec['milestone_id'];
$todo = self::factory(
$milestone,
$qrec['todo_todotype'], $qrec['todo_ord'], $qrec['todo_priority'],
$qrec['todo_shortname'], $qrec['todo_longname'], $qrec['todo_description'],
$contact,
$qrec['todo_deadline'], $qrec['todo_done']
);
$todo->id = $qrec['todo_id'];
return $todo;
}
public function coTodos($pdo)
{
return $this->milestone->todos($pdo);
}
/*
public static function xqrec2ob($qrec)
{
$attrs = array_keys($qrec);
$indirectmilestone = in_array('milestone.id');
$indirectproject = in_array('project.id');
$todo_id = $qrec['todo.id'];
$milestone_id = $indirectmilestone ? $qrec['milestone.id']
: $qrec['todo.milestone_id'];
if ($indirectmilestone) {
$milestone = new Milestone(
$qrec['milestone.ord'],
$qrec['milestone.shortname'],
$qrec['milestone.longname'],
$qrec['milestone.description']
);
$milestone->id = $milestone_id;
} else {
$milestone = Milestone::find($milestone_id);
}
if ($indirectproject) {
$project_id = $qrec['project.id'];
$project = new Project(
$qrec['project.shortname'],
$qrec['project.longname'],
$qrec['project.description']
);
$project->id = $project_id;
} else {
$project_id = $indirectmilestone ? $qrec['milestone.project_id']
: $milest
}
}
$milestone_id =
$milestone_id = $qrec['todo.milestone_id'];
if (in_array('milestone.id'), $attrs)
$milestone_id = $qrec['milestone.id'];
if ($projectdata) {
$project = new Project(
$qrec['project.shortname'],
$qrec['project.longname'],
$qrec['project.description']
);
$project->id = $qrec['project.id'];
$project = Milestone::find($qrec['milestone.milestone_id']);
} else {
$milestonedata = in_array('milestone.shortname', $qrec);
if ($milestonedata) {
$milestone = new Milestone(
$qrec['milestone.ord'],
$qrec['milestone.shortname'],
$qrec['milestone.longname'],
$qrec['milestone.description']
);
} else {
$milestone = Milestone::find($qrec['todo.milestone_id']);
}
$projectdata = in_array('project.shortname', $attrs);
if ($projectdata) {
$project = new Project(
$qrec['project.shortname'],
$qrec['project.longname'],
$qrec['project.description']
);
$project->id = $qrec['project.id'];
$project = Milestone::find($qrec['milestone.milestone_id']);
} else {
$project->id = $qrec['milestone.project_id'];
}
$todo = new Todo (
$milestone, $rec['todotype'], $rec['ord'], $rec['priority'],
$rec['shortname'], $rec['longname'], $rec['description'],
$rec['deadline'], $rec['done']
);
$todo->id = $rec['id'];
return $todo;
}*/
}
|