Projekt

Obecné

Profil

Stáhnout (9.43 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2
/* SVN FILE: $Id: app_model.php 4409 2007-02-02 13:20:59Z phpnut $ */
3

    
4
/**
5
 * Application model for Cake.
6
 *
7
 * This file is application-wide model file. You can put all
8
 * application-wide model-related methods here.
9
 *
10
 * PHP versions 4 and 5
11
 *
12
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
13
 * Copyright 2005-2007, Cake Software Foundation, Inc.
14
 *								1785 E. Sahara Avenue, Suite 490-204
15
 *								Las Vegas, Nevada 89104
16
 *
17
 * Licensed under The MIT License
18
 * Redistributions of files must retain the above copyright notice.
19
 *
20
 * @filesource
21
 * @copyright		Copyright 2005-2007, Cake Software Foundation, Inc.
22
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
23
 * @package			cake
24
 * @subpackage		cake.app
25
 * @since			CakePHP(tm) v 0.2.9
26
 * @version			$Revision: 4409 $
27
 * @modifiedby		$LastChangedBy: phpnut $
28
 * @lastmodified	$Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
29
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
30
 */
31

    
32
/**
33
 * Application model for Cake.
34
 *
35
 * Add your application-wide methods in the class below, your models
36
 * will inherit them.
37
 *
38
 * @package		cake
39
 * @subpackage	cake.app
40
 */
41
class AppModel extends Model{
42

    
43

    
44
	/**
45
	 * Pridana jina validace - sestava se z tohoto souboru + error helperu
46
	 * napsano podle
47
	 * http://cakebaker.wordpress.com/2006/02/06/yet-another-data-validation-approach/
48
	 * byly nutne nejake zmeny...
49
	 *
50
	 * pouziti:
51
	 *
52
	 * v controlleru pridat:
53
	 * ---------------------
54
	 * do pole helpers error helper
55
	 * Pr:
56
	 * 		var $helpers = array('Html', 'Form', 'Error');
57
	 *
58
	 * v pohledech pouzivat:
59
	 * ---------------------
60
	 * namisto:
61
	 * 		echo $html->tagErrorMsg('ModelName/polozka', 'Zadejte prosГ­m polozku.');
62
	 * jen jednodussi:
63
	 * 		echo $error->showMessage('ModelName/polozka');
64
	 *
65
	 * v modelech se validace definuje takto:
66
	 * --------------------------------------
67
	 * Pr:
68
	 * class User extends AppModel
69
	 *	{
70
	 *	    var $validate = array(
71
 	 *			'username' =>
72
  	 *				array(
73
	 *					array(VALID_NOT_EMPTY, 'Username is required'),
74
	 *					array('isUsernameUnique', 'Not unique')
75
	 *				)
76
	 *		);
77
	 *
78
	 *	    function isUsernameUnique()
79
	 *	    {
80
	 *	        return (!$this->hasAny(array('User.username' =>
81
	 *	                  $this->data[$this->name]['username'])));
82
	 *	    }
83
	 *	}
84
	 *
85
	 *
86
	 *
87
	 */
88
	function invalidFields ($data = array())
89
	{
90
	    if(!$this->beforeValidate($data))
91
	    {
92
	        return false;
93
	    }
94

    
95
	    if (!isset($this->validate) || !empty($this->validationErrors))
96
	    {
97
	        if (!isset($this->validate))
98
	        {
99
	            return true;
100
	        }
101
	        else
102
	        {
103
	            return $this->validationErrors;
104
	        }
105
	    }
106

    
107
	    //pr($data);
108

    
109
	    if (isset($this->data))
110
	    {
111
	        $data = array_merge($data, $this->data);
112
	    }
113

    
114
	    //pr($data);
115

    
116
	    $errors = array();
117
	    $this->set($data);
118

    
119
	    foreach ($data as $table => $field)
120
	    {
121
	    	//pridano kvuli wizardu - ale logickyu to sem patri
122
			if($this->name === $table)
123
			{
124
		        foreach ($this->validate as $field_name => $validators)
125
		        {
126
		            foreach($validators as $validator)
127
		            {
128
		                if (isset($validator[0]))
129
		                {
130
		                    if (method_exists($this, $validator[0]))
131
		                    {
132
		                        //if (isset($data[$table][$field_name]) &&
133
		                        if(array_key_exists($field_name, $data[$table]) &&
134
		                        !call_user_func(array(&$this, $validator[0])))
135
		                        {
136
		                            if (!isset($errors[$field_name]))
137
		                            {
138
		                                $errors[$field_name] = isset($validator[1]) ?
139
		                                                               $validator[1] : 1;
140
		                            }
141
		                        }
142
		                    }
143
		                    else
144
		                    {
145
		                    	//echo ($field_name." = ");
146
		                    	//var_dump($data[$table][$field_name]);
147
		                    	//var_dump($_POST['data'][$table][$field_name]);
148
		                    	//pr($validator);
149

    
150
								//pr(bl(isset($data[$table][$field_name])));
151
								//pr(bl(preg_match($validator[0], $data[$table][$field_name])));
152

    
153
		                        ////////if (isset($data[$table][$field_name]) &&
154
		                        if(array_key_exists($field_name, $data[$table]) &&
155
		                			!preg_match($validator[0], $data[$table][$field_name]))
156
		                        {
157
		                        	//pr("NEPROSLO");
158
		                        	if (!isset($errors[$field_name]))
159
		                            {
160
		                                $errors[$field_name] = isset($validator[1]) ?
161
		                                                               $validator[1] : 1;
162
		                            }
163
		                        }
164
		                    }
165
		                    //slouzi pro extra validaci polozek nastavenych jako not_empty
166
		                    if ($validator[0] == VALID_NOT_EMPTY &&
167
		                    		!array_key_exists($field_name, $data[$table]))
168
		                    {
169
								if (!isset($errors[$field_name]))
170
	                            {
171
	                                $errors[$field_name] = isset($validator[1]) ?
172
	                                                               $validator[1] : 1;
173
	                            }
174
								//var_dump($data[$table][$field_name]);
175
				            }
176
		                }
177
		            }
178
		        }
179
			}
180
	    }
181
	    //pr($data);
182
	    //pr($errors);
183
	    //flush(1);flush(0);
184
	    $this->validationErrors = $errors;
185
	    return $errors;
186
	}
187

    
188
/**
189
 * Kopirovano z cake/model_php5.php a upraveno, tak, aby misto $valuePath retezce prijimalo $valuePath
190
 * jako pole a do vystupu je spojovalo mezerami...
191
 *
192
 *
193
 * Returns a resultset array with specified fields from database matching given conditions.
194
 * Method can be used to generate option lists for SELECT elements.
195
 *
196
 * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...)
197
 * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC")
198
 * @param int $limit SQL LIMIT clause, for calculating items per page
199
 * @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
200
 * @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
201
 * @return array An associative array of records, where the id is the key, and the display field is the value
202
 * @access public
203
 */
204
	function generateListArray($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $separator = null) {
205
		if ($separator == null) $separator = ' ';
206
		if ($keyPath == null && $valuePath == null && $this->hasField($this->displayField)) {
207
			$fields = array($this->primaryKey, $this->displayField);
208
		} else {
209
			$fields = null;
210
		}
211
		$recursive = $this->recursive;
212

    
213
		if($recursive >= 1) {
214
			$this->recursive = -1;
215
		}
216
		$result = $this->findAll($conditions, $fields, $order, $limit);
217
		$this->recursive = $recursive;
218

    
219
		if(!$result) {
220
			return false;
221
		}
222

    
223
		if ($keyPath == null) {
224
			$keyPath = '{n}.' . $this->name . '.' . $this->primaryKey;
225
		}
226

    
227
		if ($valuePath == null) {
228
			//tady uprava
229
			$valuePath = array(
230
				'{n}.' . $this->name . '.' . $this->displayField
231
			);
232
		}
233

    
234
		$keys = Set::extract($result, $keyPath);
235
		$vals = array();
236
		//$vals[count($keys)];
237

    
238
		foreach($valuePath as $valueString) {
239
			$val = Set::extract($result, $valueString);
240
			$i = 0;
241
			foreach($val as $str) {
242
				if(!array_key_exists($i, $vals))
243
					$vals[$i] = $str;
244
				else
245
					$vals[$i] = $vals[$i] . $separator . $str;
246
				$i++;
247
			}
248
		}
249

    
250
		if (!empty($keys) && !empty($vals)) {
251
			$return = array_combine($keys, $vals);
252
			return $return;
253
		}
254
		return null;
255
	}
256

    
257
	/**
258
	 * Tuhle metodu to chce jeste poradne ozkouset!!!
259
	 * */
260
	function beforeSave($data = array()) {
261
		//zkontroluje cisla a prevede je z formatu x,y na format x.y
262

    
263
		if (isset($this->data))
264
	    {
265
	        $data = array_merge($data, $this->data);
266
	    }
267

    
268
	    //echo('$dataStara');pr($data);
269

    
270
		foreach ($this->validate as $validateItemName => $validateItemArray) {
271
			foreach ($validateItemArray as $validateItem) {
272
				//echo "ValidateItem: ";pr($validateItem);
273
				if ($validateItem['0'] == VALID_NUMBER) {
274
					//echo"zabralo";
275
					foreach ($data as $dataItemName => $dataArray) {
276

    
277
						if (!empty($data[$dataItemName][$validateItemName])) {
278
							//pr($data[$dataItemName][$validateItemName]);
279
							$pattern = '/(\d+),(\d+)/i';
280
							$data[$dataItemName][$validateItemName] = preg_replace($pattern, '$1' . '.' . '$2', $dataArray[$validateItemName]);
281
						} else $data[$dataItemName][$validateItemName] = null;
282
					}
283
				}
284
			}
285
		}
286

    
287
		//echo('$dataNova');pr($data);
288

    
289
		//zkontroluje datum
290
		/*foreach ($data as $modelDataName => $modelData) {
291
			foreach ($modelData as $itemName => $item) {
292
				if ($item == '--') {
293
					if (array_key_exists($itemName, $this->validate)) {
294
						$isDate = false;
295
						$isNotEmpty = false;
296
						foreach ($this->validate[$itemName] as $validateArray) {
297
							foreach ($validateArray as $validateItem) {
298
								if ($validateItem == VALID_DATE) $isDate = true;
299
								if ($validateItem == VALID_NOT_EMPTY) $isNotEmpty = true;
300
							}
301
						}
302
						if ($isDate == true && $isNotEmpty == false)
303
								$data[$modelDataName][$itemName] = null;
304
					}
305
				}
306
			}
307
		}
308
		//priradit upravena data!!!*/
309
		$this->data = $data;
310

    
311
		//pr($this->validate);
312
		//pr($data);
313

    
314
		return true; //musi tu byt
315
	}
316

    
317
}
(3-3/4)