forked from GoteoFoundation/goteo-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.php
364 lines (300 loc) · 11.7 KB
/
post.php
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/*
* Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
* This file is part of Goteo.
*
* Goteo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Goteo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
*
*/
namespace Goteo\Model {
use Goteo\Model\Project\Media,
Goteo\Model\Image,
Goteo\Model\Project,
Goteo\Model\User,
Goteo\Library\Check;
class Post extends \Goteo\Core\Model {
public
$id,
$title,
$text,
$image,
$gallery = array(), // array de instancias image de post_image
$media,
$author,
$order,
$node; // las entradas en portada para nodos se guardan en la tabla post_node con unos metodos alternativos
/*
* Devuelve datos de una entrada
*/
public static function get ($id) {
$query = static::query("
SELECT
post.id as id,
IFNULL(post_lang.title, post.title) as title,
IFNULL(post_lang.text, post.text) as `text`,
post.blog as blog,
post.image as image,
post.media as `media`,
DATE_FORMAT(post.date, '%d | %m | %Y') as fecha,
post.author as author,
post.order as `order`
FROM post
LEFT JOIN post_lang
ON post_lang.id = post.id
AND post_lang.lang = :lang
WHERE post.id = :id
", array(':id' => $id, ':lang'=>\LANG));
$post = $query->fetchObject(__CLASS__);
// galeria
$post->gallery = Image::getAll($id, 'post');
$post->image = $post->gallery[0];
// video
$post->media = new Media($post->media);
// autor
if (!empty($post->author)) $post->user = User::getMini($post->author);
return $post;
}
/*
* Lista de entradas
*/
public static function getAll ($position = 'home', $node = \GOTEO_NODE) {
if (!in_array($position, array('home', 'footer'))) {
$position = 'home';
}
$list = array();
$values = array(':lang'=>\LANG);
if ($node == \GOTEO_NODE || empty($node)) {
// portada goteo, sacamos todas las de blogs tipo nodo
// que esten marcadas en la tabla post
$sqlFilter = " WHERE post.$position = 1
AND post.publish = 1
";
$sqlField = "post.order as `order`,";
} else {
// portada nodo, igualmente las entradas de blogs tipo nodo
// perosolo la que esten en la tabla de entradas en portada de ese nodo
$sqlFilter = " WHERE post.id IN (SELECT post FROM post_node WHERE node = :node)
AND post.publish = 1
";
$values[':node'] = $node;
$sqlField = "(SELECT `order` FROM post_node WHERE node = :node AND post = post.id) as `order`,";
}
$sql = "
SELECT
post.id as id,
post.blog as blog,
IFNULL(post_lang.title, post.title) as title,
IFNULL(post_lang.text, post.text) as `text`,
post.image as `image`,
post.media as `media`,
$sqlField
DATE_FORMAT(post.date, '%d-%m-%Y') as date,
DATE_FORMAT(post.date, '%d | %m | %Y') as fecha,
post.publish as publish,
post.author as author,
post.home as home,
post.footer as footer,
blog.type as owner_type,
blog.owner as owner_id
FROM post
INNER JOIN blog
ON blog.id = post.blog
LEFT JOIN post_lang
ON post_lang.id = post.id
AND post_lang.lang = :lang
$sqlFilter
ORDER BY `order` ASC, title ASC
";
$query = static::query($sql, $values);
foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $post) {
// galeria
$post->gallery = Image::getAll($post->id, 'post');
$post->image = $post->gallery[0];
$post->media = new Media($post->media);
$post->type = $post->home == 1 ? 'home' : 'footer';
// datos del autor
switch ($post->owner_type) {
case 'project':
$proj_blog = Project::getMini($post->owner_id);
$post->author = $proj_blog->owner;
$post->user = $proj_blog->user;
$post->owner_name = $proj_blog->name;
$sql = "UPDATE post SET author = '.$proj_blog->owner.' WHERE post.id = ?";
self::query($sql, array($post->id));
break;
case 'node':
$post->user = User::getMini($post->author);
// (Nodesys)
break;
}
$list[$post->id] = $post;
}
return $list;
}
/*
* Entradas en portada o pie
*/
public static function getList ($position = 'home', $node = \GOTEO_NODE) {
if (!in_array($position, array('home', 'footer'))) {
$position = 'home';
}
$list = array();
$values = array(':lang'=>\LANG);
if ($node == \GOTEO_NODE || empty($node)) {
// portada goteo, sacamos todas las de blogs tipo nodo
// que esten marcadas en la tabla post
$sqlFilter = " WHERE post.$position = 1
";
} else {
// portada nodo, igualmente las entradas de blogs tipo nodo
// perosolo la que esten en la tabla de entradas en portada de ese nodo
$sqlFilter = " WHERE post.id IN (SELECT post FROM post_node WHERE node = :node)
";
$values[':node'] = $node;
}
$sql = "
SELECT
post.id as id,
IFNULL(post_lang.title, post.title) as title,
post.order as `order`
FROM post
INNER JOIN blog
ON blog.id = post.blog
LEFT JOIN post_lang
ON post_lang.id = post.id
AND post_lang.lang = :lang
$sqlFilter
ORDER BY `order` ASC, title ASC
";
$query = static::query($sql, $values);
foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $post) {
$list[$post->id] = $post->title;
}
return $list;
}
public function validate (&$errors = array()) {
if (empty($this->title))
$errors[] = Text::_('Falta título');
if (empty($errors))
return true;
else
return false;
}
public function save (&$errors = array()) {
if (!$this->validate($errors)) return false;
$fields = array(
'id',
'blog',
'title',
'text',
'media',
'legend',
'order',
'publish',
'home',
'footer',
'author'
);
$set = '';
$values = array();
foreach ($fields as $field) {
if ($set != '') $set .= ", ";
$set .= "`$field` = :$field ";
$values[":$field"] = $this->$field;
}
try {
$sql = "REPLACE INTO post SET " . $set;
self::query($sql, $values);
if (empty($this->id)) $this->id = self::insertId();
return true;
} catch(\PDOException $e) {
$errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage();
return false;
}
}
/*
* Actualizar una entrada en portada
* si es de nodo se guarda en otra tabla con el metodo update_node
*/
public function update (&$errors = array()) {
if (!$this->id) return false;
$fields = array(
'order',
'home',
'footer'
);
$set = '';
$values = array(':id'=>$this->id);
foreach ($fields as $field) {
if (!isset ($this->$field))
continue;
if ($set != '') $set .= ", ";
$set .= "`$field` = :$field ";
$values[":$field"] = $this->$field;
}
if ($set == '') {
$errors[] = Text::_('Sin datos');
return false;
}
try {
$sql = "UPDATE post SET " . $set . " WHERE post.id = :id";
self::query($sql, $values);
return true;
} catch(\PDOException $e) {
$errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage();
return false;
}
}
/*
* Para quitar una entrada
*/
public static function remove ($id, $from = null) {
if (!in_array($from, array('home', 'footer'))) {
return false;
}
$sql = "UPDATE post SET `$from`=0, `order`=NULL WHERE id = :id";
if (self::query($sql, array(':id'=>$id))) {
return true;
} else {
return false;
}
}
/*
* Para que salga antes (disminuir el order)
*/
public static function up ($id, $type = 'home') {
$extra = array (
$type => 1
);
return Check::reorder($id, 'up', 'post', 'id', 'order', $extra);
}
/*
* Para que un proyecto salga despues (aumentar el order)
*/
public static function down ($id, $type = 'home') {
$extra = array (
$type => 1
);
return Check::reorder($id, 'down', 'post', 'id', 'order', $extra);
}
/*
* Orden para añadirlo al final
*/
public static function next ($type = 'home') {
$query = self::query('SELECT MAX(`order`) FROM post WHERE '.$type.'=1');
$order = $query->fetchColumn(0);
return ++$order;
}
}
}