|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* NoteModel |
|
5
|
|
|
* This is basically a simple CRUD (Create/Read/Update/Delete) demonstration. |
|
6
|
|
|
*/ |
|
7
|
|
|
class NoteModel |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Get all notes (notes are just example data that the user has created) |
|
11
|
|
|
* @return array an array with several objects (the results) |
|
12
|
|
|
*/ |
|
13
|
|
|
public static function getAllNotes() |
|
14
|
|
|
{ |
|
15
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
|
16
|
|
|
|
|
17
|
|
|
$sql = "SELECT user_id, note_id, note_text FROM notes WHERE user_id = :user_id"; |
|
18
|
|
|
$query = $database->prepare($sql); |
|
19
|
|
|
$query->execute(array(':user_id' => Session::get('user_id'))); |
|
20
|
|
|
|
|
21
|
|
|
// fetchAll() is the PDO method that gets all result rows |
|
22
|
|
|
return $query->fetchAll(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get a single note |
|
27
|
|
|
* @param int $note_id id of the specific note |
|
28
|
|
|
* @return object a single object (the result) |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function getNote($note_id) |
|
31
|
|
|
{ |
|
32
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
|
33
|
|
|
|
|
34
|
|
|
$sql = "SELECT user_id, note_id, note_text FROM notes WHERE user_id = :user_id AND note_id = :note_id LIMIT 1"; |
|
35
|
|
|
$query = $database->prepare($sql); |
|
36
|
|
|
$query->execute(array(':user_id' => Session::get('user_id'), ':note_id' => $note_id)); |
|
37
|
|
|
|
|
38
|
|
|
// fetch() is the PDO method that gets a single result |
|
39
|
|
|
return $query->fetch(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set a note (create a new one) |
|
44
|
|
|
* @param string $note_text note text that will be created |
|
45
|
|
|
* @return bool feedback (was the note created properly ?) |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function createNote($note_text) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$note_text || strlen($note_text) == 0) { |
|
50
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_NOTE_CREATION_FAILED')); |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
|
55
|
|
|
|
|
56
|
|
|
$sql = "INSERT INTO notes (note_text, user_id) VALUES (:note_text, :user_id)"; |
|
57
|
|
|
$query = $database->prepare($sql); |
|
58
|
|
|
$query->execute(array(':note_text' => $note_text, ':user_id' => Session::get('user_id'))); |
|
59
|
|
|
|
|
60
|
|
|
if ($query->rowCount() == 1) { |
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// default return |
|
65
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_NOTE_CREATION_FAILED')); |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Update an existing note |
|
71
|
|
|
* @param int $note_id id of the specific note |
|
72
|
|
|
* @param string $note_text new text of the specific note |
|
73
|
|
|
* @return bool feedback (was the update successful ?) |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function updateNote($note_id, $note_text) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$note_id || !$note_text) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
|
82
|
|
|
|
|
83
|
|
|
$sql = "UPDATE notes SET note_text = :note_text WHERE note_id = :note_id AND user_id = :user_id LIMIT 1"; |
|
84
|
|
|
$query = $database->prepare($sql); |
|
85
|
|
|
$query->execute(array(':note_id' => $note_id, ':note_text' => $note_text, ':user_id' => Session::get('user_id'))); |
|
86
|
|
|
|
|
87
|
|
|
if ($query->rowCount() == 1) { |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_NOTE_EDITING_FAILED')); |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Delete a specific note |
|
97
|
|
|
* @param int $note_id id of the note |
|
98
|
|
|
* @return bool feedback (was the note deleted properly ?) |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function deleteNote($note_id) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$note_id) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$database = DatabaseFactory::getFactory()->getConnection(); |
|
107
|
|
|
|
|
108
|
|
|
$sql = "DELETE FROM notes WHERE note_id = :note_id AND user_id = :user_id LIMIT 1"; |
|
109
|
|
|
$query = $database->prepare($sql); |
|
110
|
|
|
$query->execute(array(':note_id' => $note_id, ':user_id' => Session::get('user_id'))); |
|
111
|
|
|
|
|
112
|
|
|
if ($query->rowCount() == 1) { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// default return |
|
117
|
|
|
Session::add('feedback_negative', Text::get('FEEDBACK_NOTE_DELETION_FAILED')); |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|