forked from GoteoFoundation/goteo-legacy
/
interest.php
161 lines (129 loc) · 4.73 KB
/
interest.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
<?php
/*
* Copyright (C) 2012 Platoniq y Fundación Goteo (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 {
class Interest extends \Goteo\Model\Category {
public
$id,
$name,
$description,
$used; // numero de usuarios que tienen este interés
/*
* Lista de intereses para usuarios
* @TODO añadir el numero de usos
*/
public static function getAll () {
$list = array();
$sql = "
SELECT
category.id as id,
IFNULL(category_lang.name, category.name) as name,
IFNULL(category_lang.description, category.description) as description,
( SELECT
COUNT(user_interest.user)
FROM user_interest
WHERE user_interest.interest = category.id
) as used,
category.order as `order`
FROM category
LEFT JOIN category_lang
ON category_lang.id = category.id
AND category_lang.lang = :lang
ORDER BY `order` ASC";
$query = static::query($sql, array(':lang'=>\LANG));
foreach ($query->fetchAll(\PDO::FETCH_CLASS, __CLASS__) as $interest) {
if ($interest->id == 15) continue;
$list[$interest->id] = $interest;
}
return $list;
}
}
}
/**
*
use Goteo\Library\Check;
// Devuelve datos de un interés
public static function get ($id) {
$query = static::query("
SELECT
id,
name,
description
FROM interest
WHERE id = :id
", array(':id' => $id));
$interest = $query->fetchObject(__CLASS__);
return $interest;
}
public function validate (&$errors = array()) {
if (empty($this->name))
$errors[] = Text::_('Falta nombre');
if (empty($errors))
return true;
else
return false;
}
public function save (&$errors = array()) {
if (!$this->validate($errors)) return false;
$fields = array(
'id',
'name',
'description'
);
$set = '';
$values = array();
foreach ($fields as $field) {
if ($set != '') $set .= ", ";
$set .= "`$field` = :$field ";
$values[":$field"] = $this->$field;
}
try {
$sql = "REPLACE INTO interest 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;
}
}
// Para quitar un interes de la tabla
public static function delete ($id) {
$sql = "DELETE FROM interest 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) {
return Check::reorder($id, 'up', 'interest', 'id', 'order');
}
// Para que salga despues (aumentar el order)
public static function down ($id) {
return Check::reorder($id, 'down', 'interest', 'id', 'order');
}
// Orden para añadirlo al final
public static function next () {
$query = self::query('SELECT MAX(`order`) FROM interest');
$order = $query->fetchColumn(0);
return ++$order;
}
*/