~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to active_record2/validators/unique_validator.php

  • Committer: Emilio Silveira
  • Date: 2010-11-01 04:11:14 UTC
  • Revision ID: emilio.rst@gmail.com-20101101041114-t4yfhkonndisexpb
Cambiada estructura de validacion, ahora los validadores se manejan en 
pequeñas clases mas flexibles y personalizables

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * KumbiaPHP web & app Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://wiki.kumbiaphp.com/Licencia
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@kumbiaphp.com so we can send you a copy immediately.
 
14
 *
 
15
 * Realiza validacion para campo con valor unico
 
16
 *
 
17
 * @category   Kumbia
 
18
 * @package    ActiveRecord
 
19
 * @subpackage Validators
 
20
 * @copyright  Copyright (c) 2005-2010 Kumbia Team (http://www.kumbiaphp.com)
 
21
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
 
22
 */
 
23
class UniqueValidator implements ValidatorInterface
 
24
{
 
25
    /**
 
26
     * Metodo para validar
 
27
     *
 
28
     * @param ActiveRecord $object objeto ActiveRecord
 
29
     * @param string $column nombre de columna a validar
 
30
     * @param array $params parametros de configuracion
 
31
     * @param boolean $update indica si es operacion de actualizacion
 
32
     * @return boolean
 
33
     */
 
34
        public static function validate($object, $column, $params = NULL, $update = FALSE)
 
35
        {
 
36
                // Condiciones
 
37
                $q = $object->get();
 
38
                
 
39
                $values = array();
 
40
                
 
41
                // Si es para actualizar debe verificar que no sea la fila que corresponde
 
42
                // a la clave primaria
 
43
                if($update) {   
 
44
                        // Obtiene la clave primaria
 
45
                        $pk = $object->metadata()->getPK();
 
46
                        
 
47
                        if(is_array($pk)) {
 
48
                                // Itera en cada columna de la clave primaria
 
49
                                $conditions = array();
 
50
                                foreach($pk as $k) {
 
51
                                        // Verifica que este definida la clave primaria
 
52
                                        if(!isset($object->$k) || $object->$k === '') {
 
53
                                                throw new KumbiaException("Debe definir valor para la columna $k de la clave primaria");
 
54
                                        }
 
55
                                        
 
56
                                        $conditions[] = "$k = :pk_$k";
 
57
                                        $q->bindValue("pk_$k", $object->$k);
 
58
                                }
 
59
                                
 
60
                                $q->where('NOT (' . implode(' AND ', $conditions) . ')');
 
61
                        } else {
 
62
                                // Verifica que este definida la clave primaria
 
63
                                if(!isset($object->$pk) || $object->$pk === '') {
 
64
                                        throw new KumbiaException("Debe definir valor para la clave primaria $pk");
 
65
                                }
 
66
                                                
 
67
                                $q->where("NOT $pk = :pk_$pk");
 
68
                                $q->bindValue("pk_$pk", $object->$pk);
 
69
                        }
 
70
                }
 
71
                
 
72
                if(is_array($column)) { 
 
73
                        // Establece condiciones con with
 
74
                        foreach($column as $k) {
 
75
                                // En un indice UNIQUE si uno de los campos es NULL, entonces el indice
 
76
                                // no esta completo y no se considera la restriccion
 
77
                                if(!isset($object->$k) || $object->$k === '') {
 
78
                                        return TRUE;
 
79
                                }
 
80
                                
 
81
                                $values[$k] = $object->$k;
 
82
                                $q->where("$k = :$k");
 
83
                        }
 
84
                        
 
85
                        $q->bind($values);
 
86
                                
 
87
                        // Verifica si existe
 
88
                        if($object->existsOne()) {
 
89
                                if(!isset($params['message'])) {
 
90
                                        $v = implode("', '", array_values($values));
 
91
                                        $c = implode("', '", array_keys($values));
 
92
                                        $msg = "Los valores '$v' ya existen para los campos '$c'";
 
93
                                } else {
 
94
                                        $msg = $params['message'];
 
95
                                }
 
96
                                        
 
97
                                Flash::error($msg);
 
98
                                return FALSE;
 
99
                        }
 
100
                } else {                
 
101
                        $values[$column] = $object->$column;
 
102
                        
 
103
                        $q->where("$column = :$column")->bind($values);
 
104
                        // Verifica si existe
 
105
                        if($object->existsOne()) {
 
106
                                if(!isset($params['message'])) {
 
107
                                        $msg = "El valor '{$object->$column}' ya existe para el campo $column";
 
108
                                } else {
 
109
                                        $msg = $params['message'];
 
110
                                }
 
111
                                
 
112
                                Flash::error($msg);
 
113
                                return FALSE;
 
114
                        }
 
115
                }
 
116
                
 
117
                return TRUE;
 
118
        }
 
119
}