aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Model/Map.php
blob: e6d9f6bebe2c5a6e10558ad3270effeba3c0a30f (plain) (blame)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php

// EasyGiant is a PHP framework for creating and managing dynamic content
//
// Copyright (C) 2009 - 2014  Antonio Gallo (info@laboratoriolibero.com)
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
//
// EasyGiant 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 3 of the License, or
// (at your option) any later version.
//
// EasyGiant 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 EasyGiant.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');

class Model_Map extends Model_Base {

// 	//many to many model

	public $printAssError = 'yes'; //'yes': print association error if the association/dissociation is already present. 'no': doen't print association error
	public $boxIdentifierName = 'boxIdentifier';//key of the value of the $_POST array that represent the id of the Box that we want to associate with the Item having the id $_POST[$this->identifierName]
	protected $_boxIdentifierValue = null; //the id of the box that has to be associated with the item

	public function __construct()
	{
		//add some submit names (method => form_submit_value)
		$this->submitNames['associate'] = 'associateAction';
		$this->submitNames['dissociate'] = 'dissociateAction';
		//add the allowed methods 
		$this->_allowedDbMethods[] = 'associate';
		$this->_allowedDbMethods[] = 'dissociate';
		parent::__construct();
	}

	public function createMapWhere($choice)
	{ //create the where join clause
		//$choice=(first,last,all)
		$first = $this->_tablesArray[0].'.'.$this->_idFieldsArray[0].'='.$this->_tablesArray[2].'.'.$this->_idFieldsArray[0];
		$last = $this->_tablesArray[1].'.'.$this->_idFieldsArray[1].'='.$this->_tablesArray[2].'.'.$this->_idFieldsArray[1];
		switch ($choice) {
			case 'first':
				return $first;
				break;
			case 'last':
				return $last;
				break;
			case 'all':
				return $first. ' and '.$last;
				break;
		}
	}

	//check if a join query is necessary or not
	//$val: 0 or 1 (items or boxes)
	//$whereClauseLevel: array containing the field=>value statements of the where clause. If $whereClause = null than $this->where is considered
	public function checkIfJoinNeeded($val, $whereClauseLevel = null)
	{
		$whereClause = isset($whereClauseLevel) ? $whereClauseLevel : $this->where;
		
		foreach ($whereClause as $field => $value)
		{
			if (is_array($value))
			{
				if ($this->checkIfJoinNeeded($val, $value) === true) return true;
			}
			else
			{
				if (strcmp($this->getTableName($field),$this->_tablesArray[$val]) !== 0)
				{
					if (strcmp($value,Params::$nullQueryValue) !== 0 or (Params::$nullQueryValue === false)) return true;
				}
			}
		}
		//return false if no where clause has been defined
		return false;
	}

	//method to create the where clause and the list of tables of the select query
	public function mapQueryElements($val)
	{
// 		$val = $element === 'Items' ? 0 : 1;
		$tables = $this->_tablesArray[$val];
		$where = null;
		$fields = $this->_tablesArray[$val].'.*';
		
		if ($this->checkIfJoinNeeded($val))
		{
			$tables = $this->_tables;
			$fields = $this->_tablesArray[$val].'.*';
			$wherePlus = $this->createWhereClause();
			$wherePlus = isset($wherePlus) ? ' AND ' . $wherePlus : null;
			$where = $this->createMapWhere('all') . $wherePlus;
		}
		else
		{
			$where = $this->createWhereClause();
		}
		
		return array('tables' => $tables,'where' => $where,'fields' => $fields);
	}

	//$element: Items or Boxes.
	//get all Item or Boxes
	public function getAll($element = 'Items')
	{
		return $this->getFields('',$element);
	}
	
	//method to get the values of the selected fields
	//$fields: the fields that have to be excracted from the tableName
	public function getFields($fields = '',$element = 'Items')
	{
		//get all Item or Boxes
		if ((strcmp($element,'Items') !== 0) and (strcmp($element,'Boxes') !== 0))
		{
			throw new Exception('<b>"'.$element. '"</b> argument not allowed in <b>'.__METHOD__.'</b> method');
		}		
		$val = $element === 'Items' ? 0 : 1;
		
		$elements = $this->mapQueryElements($val);
		
		$queryFields = (strcmp($fields,'') === 0) ? $elements['fields'] : $fields;
		
		return $row = $this->db->select($elements['tables'],$queryFields,$elements['where'],$this->groupBy,$this->orderBy,$this->limit);

	}

	public function send($element = 'Items')
	{
		$table = $this->getFields($this->select, $element);
		
		if ($this->toList)
		{
			$key = $this->listArray[0];
			$value = isset($this->listArray[1]) ? $this->listArray[1] : null;
			$this->toList = false;
			return $this->getList($table, $key, $value);
		}
		else
		{
			return $table;
		}
	}
	
	//the fields that have to be extracted from the table
	public function getTable($fields = null)
	{
		return isset($fields) ? $this->getFields($fields) : $this->getAll();
	}

	//select the values of a specified record
	//$id: the id (primary key) of the record
	//$fields: the comma separated list of fields that have to be extracted
	public function selectId($id,$fields = null)
	{
		$id = (int)$id;

		$tempWhere = $this->where;
		$this->setWhereQueryClause(array($this->_idFieldsArray[0] => $id));

		if (isset($fields))
		{
			$values = $this->getFields($fields,'Items');
		}
		else
		{
			$values = $this->getAll('Items');
		}

		$this->where = $tempWhere;
		
		return (count($values) > 0) ? $values[0][$this->_tablesArray[0]] : array();
	}

	//get the number of records (items or boxes)
	public function recordNumber($element)
	{
		$val = $element === 'Items' ? 0 : 1;

		$elements = $this->mapQueryElements($val);
		return $this->db->get_num_rows($elements['tables'],$elements['where'],$this->groupBy);
	}

	//get the number of records (only items)
	public function rowNumber()
	{
		return $this->recordNumber('Items');
	}

	public function getMax($field)
	{
		$elements = $this->mapQueryElements(0);
		return $this->db->getMax($elements['tables'],$field,$elements['where'],$this->groupBy);
	}

	public function getMin($field)
	{
		$elements = $this->mapQueryElements(0);
		return $this->db->getMin($elements['tables'],$field,$elements['where'],$this->groupBy);
	}

	public function getSum($field)
	{
		$elements = $this->mapQueryElements(0);
		return $this->db->getSum($elements['tables'],$field,$elements['where'],$this->groupBy);
	}

	public function getAvg($field)
	{
		$elements = $this->mapQueryElements(0);
		return $this->db->getAvg($elements['tables'],$field,$elements['where'],$this->groupBy);
	}
	
	//check if the table has the field $field equal to $value
	public function has($field,$value)
	{
		$elements = $this->mapQueryElements(0);
		return $this->db->recordExists($elements['tables'],$field,$value,$elements['where'],$this->groupBy);
	}
	
	//associate an item with a box
	//$idItem : name of the field of the Items table, $idGroup : name of the field of the Boxes table
	public function associate($idItem = null,$idGroup = null)
	{
		$this->queryResult = false;
		if (isset($idItem) and isset($idGroup))
		{
			$idItem = (int)$idItem;
			$idGroup = (int)$idGroup;
			$values = array($idItem,$idGroup); //values relative to the fields $this->_idFields
			$var = $this->checkAssociation($idItem,$idGroup);
			if (!$var)
			{
				$result = $this->db->insert($this->_tablesArray[2],$this->_idFields,$values);
				$this->setNotice($result);
				return $result;
			}
			else
			{
				if (strcmp($this->printAssError,'yes') === 0) $this->notice = $this->_resultString->getString('linked');
				$this->result = false;
			}
		}
		else
		{
			$this->notice = $this->_resultString->getString('no-id');
			$this->result = false;
		}
		return false;
	}

	//associate an item with a box
	//$idItem : name of the field of the Items table, $idGroup : name of the field of the Boxes table
	public function dissociate($idItem = null,$idGroup = null)
	{
		$this->queryResult = false;
		if (isset($idItem) and isset($idGroup))
		{
			$idItem = (int)$idItem;
			$idGroup = (int)$idGroup;
			$var = $this->checkAssociation($idItem,$idGroup);
			if ($var)
			{
				$result = $this->db->del($this->_tablesArray[2],$this->_idFieldsArray[0].'='.$idItem.' and '.$this->_idFieldsArray[1].'='.$idGroup);
				$this->setNotice($result);
				return $result;
			}
			else
			{
				if (strcmp($this->printAssError,'yes') === 0) $this->notice = $this->_resultString->getString('not-linked');
				$this->result = false;
			}
		}
		else
		{
			$this->notice = $this->_resultString->getString('no-id');
			$this->result = false;
		}
		return false;
	}

	public function checkAssociation($idItem,$idGroup)
	{
		$idItem = (int)$idItem;
		$idGroup = (int)$idGroup;
		$numRow = $this->db->get_num_rows($this->_tablesArray[2],$this->_idFieldsArray[0].'='.$idItem.' and '.$this->_idFieldsArray[1].'='.$idGroup);
		if ($numRow === 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	//check what items are associate to a box
	//itemsArray:array of items to check
	public function checkAssociationDeep($itemsArray)
	{
		$associatedItems = array();
		$itemsArray = is_array($itemsArray) ? array_values($itemsArray) : array($itemsArray);
		foreach ($itemsArray as $item) {
			if ($this->db->recordExists($this->_tablesArray[2],$this->_idFieldsArray[0],$item))
			{
				$associatedItems[] = $item;
			}
		}
		return $associatedItems;
	}

	//method to call the delete query (overriding of the del method of Model.php)
	//check the referential integrity
	public function del($id = null, $whereClause = null)
	{
		$this->queryResult = false;
		
		if (isset($whereClause))
		{
			return parent::del(null,$whereClause);
		}
		else
		{
			if ($this->_onDelete === 'check')
			{
				if ($this->db->recordExists($this->_tablesArray[2],$this->_idFieldsArray[0],(int)$id))
				{
					$this->notice = $this->_resultString->getString('associate');
					$this->identifierValue = null;
					$this->result = false;
				}
				else
				{
					return parent::del((int)$id);
				}
			}
			else if ($this->_onDelete === 'nocheck')
			{
				return parent::del((int)$id);
			}
		}
		return false;
	}

	//override of the updateTable method of the parent class
	//method that calls the function indicated in $this->submitNames. Ex: if $_POST['delAction'] is found, then the "del" method is called.
	public function updateTable($methodsList = '',$id = null)
	{
		$allowedMethodsArray = explode(',',$methodsList);
		$resultArray = array();
		$this->identifierValue = null;
		if (isset($id))
		{
			$this->identifierValue = (int)$id;
		}
		else if (isset($_POST[$this->identifierName]))
		{
			$this->identifierValue = (int)$_POST[$this->identifierName];
		}
		foreach ($this->submitNames as $methodName => $submitName) {
			if (array_key_exists($submitName,$_POST))
			{
				$this->submitName = $submitName;
				if (method_exists($this,$methodName))
				{
					if (in_array($methodName,$allowedMethodsArray))
					{
						if ($this->checkConditions($methodName,$this->identifierValue))
						{
							$this->notice = null;
							$methodArray = array('associate','dissociate');
							if (in_array($methodName,$methodArray))
							{
								$this->_boxIdentifierValue = null;
								if (isset($_POST[$this->boxIdentifierName]))
								{
									$this->_boxIdentifierValue = (int)$_POST[$this->boxIdentifierName];
								}
								call_user_func_array(array($this,$methodName),array($this->identifierValue,$this->_boxIdentifierValue));
							}
							else
							{
								call_user_func_array(array($this,$methodName),array($this->identifierValue));
							}
						}
					}
				}
				else
				{
					throw new Exception('method "'.$methodName. '" not defined in class '.__CLASS__.'; error in method '.__METHOD__);
				}
				return; //only one cycle!
			}
		}
	}

	//method to obtain one columns from the tables $this->_tablesArray as an associative array
	//$valueField: the column that have to be extracted (array_values of the resulting associative array), $keyField: the column that have to play the role of array_keys
	//$valueField = field:table, $keyField = field:table
	public function getFieldArray($valueField,$keyField = null, $groupBy = null, $orderBy = null, $limit = null)
	{

		$keyField = isset($keyField) ? $keyField : $valueField;
		$valueFieldArray = explode(':',$valueField);
		$keyFieldArray = explode(':',$keyField);

		$keyFieldTable = $keyFieldArray[0];
		$valueFieldTable = $valueFieldArray[0];

		$keyFieldName = $keyFieldArray[1];
		$valueFieldName = $valueFieldArray[1];

		$fields = implode('.',$keyFieldArray) . ',' . implode('.',$valueFieldArray);

		$temp = $this->where; //save the $this->where array
		$this->where = array();

		$val = array_search($keyFieldTable,$this->_tablesArray);

		if (strcmp($keyFieldTable,$valueFieldTable) !== 0)
		{
			throw new Exception("the tables '$valueFieldTable' and '$keyFieldTable' do not match in ".__METHOD__);
		}

		if ($val === false or !in_array($val,array(0,1)))
		{
			throw new Exception("the table '$keyFieldTable' is not allowed in ".__METHOD__);
		}

		$elements = $this->mapQueryElements($val);

		$table = $this->db->select($elements['tables'],$fields,$elements['where'],$groupBy,$orderBy,$limit);
		$this->where = $temp;

		$returnArray = array();
		foreach ($table as $record) {
			$returnArray[$record[$keyFieldTable][$keyFieldName]] = $record[$valueFieldTable][$valueFieldName];
		}

		return $returnArray;

	}

}