/
base_model.class.php
333 lines (313 loc) · 11.4 KB
/
base_model.class.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
<?php
/*
+----------------------------------------------------------------------+
| andromeda: miguel Framework, written in PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2003,2004 miguel Development Team |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program 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 General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 02111-1307, USA. The GNU GPL license is also available through |
| the world-wide-web at http://www.gnu.org/copyleft/gpl.html |
+----------------------------------------------------------------------+
| Authors: Jesus A. Martinez Cerezal <jamarcer@inicia.es> |
| miguel Development Team |
| <e-learning-desarrollo@listas.hispalinux.es> |
+----------------------------------------------------------------------+
*/
/**
* Todo el patrón MVC se define es este paquete llamado framework
* @package framework
* @subpackage model
*/
/**
*
*/
include_once(MIGUELBASE_DIR.'model/classes/ddbb/base_ddbberror.class.php');
/**
* Define la clase modelo base de miguel.
* Se definen los elementos básicos del acceso al modelo de datos para miguelOO.
* @author Jesus A. Martinez Cerezal <jamarcer@inicia.es>
* @author miguel development team <e-learning-desarrollo@listas.hispalinux.es>
* @copyright GPL - Ver LICENCE
* @package framework
* @subpackage model
* @version 1.0.0
*
*/
class base_Model extends base_ddbbError
{
/**
*
*/
var $obj_data = null;
/**
*
*/
var $bol_dataMeth = false;
/**
*
*/
var $bol_hasDDBB = false;
/**
* This is the constructor.
*
*/
function base_model($connect = true)
{
$this->base_ddbbError();
$this->bol_hasDDBB = $connect;
}
/**
* Prepara el acceso del modelo a los diferentes tipos de datos
*/
function openModel()
{
if($this->bol_hasDDBB){
$this->useDDBB();
}
}
/**
* Prepara el acceso del modelo a los diferentes tipos de datos
*/
function closeModel()
{
if($this->bol_hasDDBB){
$this->closeDDBB();
}
}
/**
* Prepara al modelo para acceder a la base de datos
*/
function useDDBB()
{
//Include database access class
require_once Util::base_Path('model/classes/ddbb/base_ddbbdata.class.php');
//Instanciamos la clase responsable
$this->obj_data = new base_ddbbData();
$this->obj_data->openConnection();
//Debug::oneVar($this->obj_data, __FILE__, __LINE__);
//Activamos los métodos asociados
$this->bol_dataMeth = true;
}
/**
* Cierra el acceso a base de datos
*/
function closeDDBB()
{
$this->obj_data->closeConnection();
//Desactivamos los métodos asociados
$this->bol_dataMeth = false;
}
/**
* Obtiene la sentencia SQL procesada.
* @return string Sentencia SQL.
*/
function getSQLQuery()
{
if($this->bol_dataMeth) {
return $this->obj_data->getSQLQuery();
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Obtiene los registros de una tabla que cumplen la condición dada.
* @param string $str_table Nombre de la tabla en la que se consulta
* @param string $str_fields Campos a buscar (cadena formada por los campos separados por comas)
* @param string $str_cond Condición a cumplir
* @return array Registros obtenidos.
*/
function Select($str_table, $str_fields, $str_cond = '' )
{
if($this->bol_dataMeth) {
return $this->obj_data->_select($str_table, 0, $str_fields, '', $str_cond, false);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Obtiene los registros de varias tablas que cumplen la condición dada.
* @param string $str_table Nombre de las tablas en la que se consulta (cadena formada por los campos separados por comas)
* @param string $str_fields Campos a buscar (cadena formada por los campos separados por comas)
* @param string $str_cond Condición a cumplir
* @return array Registros obtenidos.
*/
function SelectMultiTable($str_table, $str_fields, $str_cond = '' )
{
if($this->bol_dataMeth) {
return $this->obj_data->_select($str_table, 2, $str_fields, '', $str_cond, false);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Obtiene todos los campos de los registros de una tabla que cumplen la condición dada.
* @param string $str_table Nombre de la tabla en la que se consulta
* @param string $str_cond Condición a cumplir
* @return array Registros obtenidos.
*/
function SelectAll($str_table, $str_cond = '' )
{
if($this->bol_dataMeth) {
return $this->obj_data->_select($str_table, 0, '*', '', $str_cond, false);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Obtiene todos los registros diferentes de una tabla y que cumplen la condición dada.
* @param string $str_table Nombre de la tabla.
* @param string $str_fields Campos a buscar (cadena formada por los campos separados por comas)
* @param string $str_cond Condición a cumplir
* @return array Registros obtenidos.
*/
function SelectDistinct($str_table, $str_fields, $str_cond = '' )
{
if($this->bol_dataMeth) {
return $this->obj_data->_select($str_table, 1, $str_fields, '', $str_cond, false);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Obtiene todos los registros (diferentes o no) de una tabla que cumplen
*la condición dada. Los ordena en función de los campos y tipo especificado
* @param string $str_table Nombre de la tabla.
* @param string $str_fields Campos a buscar (cadena formada por los campos separados por comas)
* @param string $str_sort_by Campos por los que ordenar(cadena formada por los campos separados por comas)
* @param string $str_cond Condición a cumplir
* @param boolean $bol_reverse Tipo de ordenación: false -> ascendente; true -> descendente
* @param boolean $bol_distinct Select type: false -> normal; true -> distinct
* @return array Registros obtenidos.
*/
function SelectOrder($str_table, $str_fields, $str_sort_by, $str_cond = '', $bol_reverse = false, $bol_distinct = false)
{
if($this->bol_dataMeth) {
return $this->obj_data->_select($str_table, $bol_distinct, $str_fields,
$str_sort_by, $str_cond, $bol_reverse);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Selects the table entries that fulfil the given condition.
* Orders them by specified fields and order type (reverse or normal)
* @param string $str_table Tables names where we select fields from (it's a string
* with tables names separated with commas)
* @param string $str_fields Fields to select (also separated with commas but every fields
* preceed by "<table name>.")
* @param string $str_sort_by Fileds to order by and separated with commas
* @param string $str_cond Condition to fullfil
* @param boolean $bol_reverse Order type: false -> asc; true -> desc
* @return array Selected entries.
*/
function SelectMultiTableOrder($str_table, $str_fields, $str_sort_by, $str_cond = '',
$bol_reverse = false)
{
if($this->bol_dataMeth) {
return $this->obj_data->_select($str_table, 2, $str_fields, $str_sort_by, $str_cond, $bol_reverse);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Cuenta los registros de una tabla que cumplen la condición dada.
* @param string $str_table Nombre de la tabla en la que se consulta
* @param string $str_cond Condición a cumplir
* @return array Total registros
*/
function SelectCount($str_table, $str_cond = '' )
{
if($this->bol_dataMeth) {
return $this->obj_data->_selectCount($str_table, $str_cond);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Inserta un registro en la tabla.
* @param string $table Nombre de la tabla.
* @param string $fields Campos a insertar (cadena formada por los campos separados por comas)
* @param mixed $values Valores a insertar (puede recibirse los valores separados por comas, o en un
* array ordenado como los $fields. Se aconseja usar la opción del array)
* @return array Éxito de la inserción.
*/
function Insert($table, $fields, $values)
{
if($this->bol_dataMeth) {
return $this->obj_data->_insert($table, $fields, $values);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Borra los registros de una tabla que cumplen una condición.
* @param string $str_table Nombre de la tabla.
* @param string $str_cond Condición a cumplir
* @return array Éxito del borrado.
*/
function Delete($str_table, $str_cond)
{
if($this->bol_dataMeth) {
return $this->obj_data->_delete($str_table, $str_cond);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Actualiza los registros en la tabla que cumplen la condición..
* @param string $str_table Nombre de la tabla.
* @param string $str_fields Campos a modificar (cadena formada por los campos separados por comas)
* @param string $str_values Valores a modificar (puede recibirse los valores separados por comas, o en un
* array ordenado como los $fields. Se aconseja usar la opción del array)
* @param string $str_cond Condición a cumplir
* @return array Éxito de la modificación.
*/
function Update($str_table, $str_fields, $str_values, $str_cond='')
{
//Debug::oneVar($str_table,__FILE__, __LINE__);
//Debug::oneVar($str_fields,__FILE__, __LINE__);
//Debug::oneVar($str_values,__FILE__, __LINE__);
//Debug::oneVar($str_cond,__FILE__, __LINE__);
if($this->bol_dataMeth) {
return $this->obj_data->_update($str_table, $str_fields, $str_values, $str_cond);
} else {
$this->_setError('Model :: DDBB service not active');
}
}
/**
* Intenta ejecutar cualquier sentencia SQL que se pasa como parámetro
*
* Sólo en la versión de desarrollo. Se eliminará en producción.
*
* @param string $str_sql Sentencia SQL
* @return mixto Retorno de la ejecución
*/
function freeExec($str_sql)
{
return $this->obj_data->_exec($str_sql);
}
/**
* Escribe un mensaje en el log
* @param string $message Mensaje a guardar en el Log
* @param string $priority Nivel de log
*/
function log($message, $priority)
{
include_once(Util::base_Path('include/classes/loghandler.class.php'));
LogHandler::log($message, $this->str_moduleName.'_controller', $priority);
}
}
?>