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
|
<?php namespace controller;
class News {
public function get() { $db = \MysqliDb::getInstance();
$cols = Array ("ID", "Titlu", "Imagine", "Datas", "Activs", "Stickys"); $ret = $db->get('stiri', null, $cols);
$ret = array_map(function($item) { return array( 'id' => $item['ID'], 'title' => $item['Titlu'], "image" => $item['Imagine'], "date" => $item['Datas'], "publish" => $item['Activs'], "isSticky" => $item['Stickys'] ); }, $ret);
return json_encode($ret); }
public function getNews($id) { $db = \MysqliDb::getInstance(); $cols = Array ("ID", "Titlu", "Continut", "Content_Short", "Imagine", "Datas", "Activs", "Stickys");
$db->where ('ID', $id); $ret = $db->get('stiri', null, $cols);
if(count($ret) === 0) { header("Status: 404 Not Found"); return ""; }
// Split the DATETIME into date + time pieces so the HTML date input // (which only accepts YYYY-MM-DD) renders, and the time picker shim // can pre-fill itself from the `time` field. $rawDatas = (string) $ret[0]['Datas']; $dateOnly = substr($rawDatas, 0, 10); $timeOnly = ''; if (strlen($rawDatas) >= 16) { $hhmm = substr($rawDatas, 11, 5); if ($hhmm !== '00:00') { $timeOnly = $hhmm; } }
$item = array( 'id' => $ret[0]['ID'], 'title' => $ret[0]['Titlu'], 'content' => $ret[0]['Continut'], 'content_short' => $ret[0]['Content_Short'], "image" => $ret[0]['Imagine'], "fileName" => null, "date" => $dateOnly, "time" => $timeOnly, "publish" => $ret[0]['Activs'], "isSticky" => $ret[0]['Stickys'] );
return json_encode($item); }
public function updateNews() { $body = file_get_contents('php://input'); $requstOb = json_decode($body,true);
$db = \MysqliDb::getInstance();
// If the admin form sent a date-only string (10 chars "YYYY-MM-DD"), // preserve whatever HH:MM:SS is already stored in the DB. Otherwise // use exactly what was sent (allows admin to override time later). $datas = $requstOb['date']; if (is_string($datas) && strlen($datas) === 10) { $db->where('ID', $requstOb['id']); $existing = $db->getOne('stiri', ['Datas']); if ($existing && !empty($existing['Datas'])) { $existingTime = date('H:i:s', strtotime($existing['Datas'])); $datas = $datas . ' ' . $existingTime; } }
$data = Array ( 'Titlu' => $requstOb['title'], "Continut" => $requstOb['content'], "Content_Short" => $requstOb['content_short'], "Datas" => $datas, "Activs" => $requstOb['publish'], "Stickys" => $requstOb['isSticky'] ); if(!empty($requstOb['fileName'])) { // copy file from temp $srcPath = $GLOBALS['paths']['images_temp'] . $requstOb['fileName']; $destPath = $GLOBALS['paths']['news_images'] . $requstOb['fileName']; if(!rename($srcPath, $destPath)) { return json_encode(array('status'=> 0, 'message'=>'Unable to move uploaded file.', 'item'=>null)); } $data["Imagine"] = $requstOb['fileName']; }
$db->where ('id', $requstOb['id']); $db->update ('stiri', $data);
$item = array( 'id' => $requstOb['id'], 'title' => $requstOb['title'], "date" => $requstOb['date'], "publish" => $requstOb['publish'], "isSticky" => $requstOb['isSticky']); if(isset($data['Imagine'])) { $item["image"] = $requstOb['fileName']; }
return json_encode(array('status'=>1, 'message'=>'News updated.', 'item'=>$item));
}
public function addNews() { $body = file_get_contents('php://input'); $requstOb = json_decode($body,true);
$db = \MysqliDb::getInstance();
// For new news, if admin sent only a date, stamp the current server // time so the listing can show HH:MM and same-day items are ordered // chronologically. $datas = $requstOb['date']; if (is_string($datas) && strlen($datas) === 10) { $datas = $datas . ' ' . date('H:i:s'); }
$data = Array ( 'Titlu' => $requstOb['title'], "Continut" => $requstOb['content'], "Content_Short" => $requstOb['content_short'], "Datas" => $datas, "Activs" => $requstOb['publish'], "Stickys" => $requstOb['isSticky'] ); if(!empty($requstOb['fileName'])) { // copy file from temp $srcPath = $GLOBALS['paths']['images_temp'] . $requstOb['fileName']; $destPath = $GLOBALS['paths']['news_images'] . $requstOb['fileName']; if(!rename($srcPath, $destPath)) { return json_encode(array('status'=> 0, 'message'=>'Unable to move uploaded file.', 'item'=>null)); } $data["Imagine"] = $requstOb['fileName']; }
$id = $db->insert ('stiri', $data); $item = array( 'id' => $id, 'title' => $requstOb['title'], "date" => $requstOb['date'], "publish" => $requstOb['publish'], "isSticky" => $requstOb['isSticky']); if(isset($data['Imagine'])) { $item["image"] = $requstOb['fileName']; }
return json_encode(array('status'=>1, 'message'=>'News added.', 'item'=>$item)); }
public function deleteNews() { $body = file_get_contents('php://input'); $requstOb = json_decode($body,true); $db = \MysqliDb::getInstance(); $db->where('ID', $requstOb['id']); $id = $db->delete ('stiri');
// TODO: delete image
$item = array('id' => $requstOb['id']);
return json_encode(array('status'=> 1, 'message'=>'News deleted.', 'item'=> $item)); }
} ?>
|