absences_Movement::getRequest()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.6

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 0
dl 0
loc 25
ccs 9
cts 15
cp 0.6
crap 6.6
rs 8.439
c 0
b 0
f 0
1
<?php
2
/************************************************************************
3
 * OVIDENTIA http://www.ovidentia.org                                   *
4
 ************************************************************************
5
 * Copyright (c) 2003 by CANTICO ( http://www.cantico.fr )              *
6
 *                                                                      *
7
 * This file is part of Ovidentia.                                      *
8
 *                                                                      *
9
 * Ovidentia is free software; you can redistribute it and/or modify    *
10
 * it under the terms of the GNU General Public License as published by *
11
 * the Free Software Foundation; either version 2, or (at your option)  *
12
 * any later version.													*
13
 *																		*
14
 * This program is distributed in the hope that it will be useful, but  *
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of			*
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.					*
17
 * See the  GNU General Public License for more details.				*
18
 *																		*
19
 * You should have received a copy of the GNU General Public License	*
20
 * along with this program; if not, write to the Free Software			*
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,*
22
 * USA.																	*
23
************************************************************************/
24
25
26
27 1
require_once dirname(__FILE__).'/record.class.php';
28 1
require_once dirname(__FILE__).'/right.class.php';
29 1
require_once dirname(__FILE__).'/agent.class.php';
30 1
require_once dirname(__FILE__).'/entry.class.php';
31 1
require_once dirname(__FILE__).'/cet_deposit_request.class.php';
32 1
require_once dirname(__FILE__).'/workperiod_recover_request.class.php';
33
34
/**
35
 * 
36
 * @property int	$id_user
37
 * @property int	$id_right
38
 * @property int	$id_request
39
 * @property string	$request_class
40
 * @property int	$id_author
41
 * @property string	$comment
42
 * @property string $createdOn
43
 * @property string $message
44
 * @property string $status
45
 */
46
class absences_Movement extends absences_Record
47
{
48
49
	
50
	
51
	
52
	/**
53
	 * 
54
	 * @var absences_Right
55
	 */
56
	private $right;
57
	
58
	/**
59
	 * 
60
	 * @var absences_Agent
61
	 */
62
	private $agent;
63
	
64
	
65
	/**
66
	 *
67
	 * @var absences_Request
68
	 */
69
	private $request;
70
	
71
	
72
	public static function getById($id)
73
	{
74
		$movement = new absences_Movement;
75
		$movement->id = $id;
76
		
77
		return $movement;
78
	}
79
	
80
	
81 16 View Code Duplication
	public function __get($property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
	{
83 16
	    $row = $this->getRow();
84
	
85 16
	    if (!isset($row[$property]) && 'status' !== $property)
86 16
	    {
87
	        require_once $GLOBALS['babInstallPath'].'utilit/devtools.php';
88
	        bab_debug_print_backtrace();
89
	        bab_debug($row);
90
	        throw new Exception(sprintf('Failed to load property %s on %s', $property, get_class($this)));
91
	    }
92
	
93 16
	    return $row[$property];
94
	}
95
	
96
	
97
	
98
	/**
99
	 * Table row as an array
100
	 * @return array
101
	 */
102 16
	public function getRow()
103
	{
104 16
		if (null === $this->row)
105 16
		{
106
			global $babDB;
107
	
108
			$query = 'SELECT * FROM absences_movement WHERE ';
109
			if (isset($this->id))
110
			{
111
				$query .= 'id='.$babDB->quote($this->id);
112
			}
113
	
114
			$res = $babDB->db_query($query);
115
			$this->setRow($babDB->db_fetch_assoc($res));
116
	
117
			if (!$this->row)
118
			{
119
				throw new Exception('This movement does not exists id='.$this->id.'');
120
			}
121
		}
122
	
123 16
		return $this->row;
124
	}
125
	
126
	
127
	
128
	
129
	/**
130
	 *
131
	 * @param absences_Agent $agent
132
	 */
133 12
	public function setAgent(absences_Agent $agent)
134
	{
135 12
		$this->agent = $agent;
136 12
		return $this;
137
	}
138
	
139
	/**
140
	 *
141
	 * @param absences_Right $right
142
	 */
143 16
	public function setRight(absences_Right $right)
144
	{
145 16
		$this->right = $right;
146 16
		return $this;
147
	}
148
	
149
	
150
	/**
151
	 * @return absences_Right
152
	 */
153
	public function getRight()
154
	{
155
		if (!isset($this->right))
156
		{
157
			$row = $this->getRow();
158
			$this->right = new absences_Right($row['id_right']);
159
		}
160
	
161
		return $this->right;
162
	}
163
	
164
	/**
165
	 * @return absences_Agent
166
	 */
167 View Code Duplication
	public function getAgent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
	{
169
		if (!isset($this->agent))
170
		{
171
			$row = $this->getRow();
172
			$this->agent = absences_Agent::getFromIdUser($row['id_user']);
173
		}
174
	
175
		return $this->agent;
176
	}
177
	
178
	
179
	
180
	/**
181
	 *
182
	 * @param absences_Request $request
183
	 */
184 4
	public function setRequest(absences_Request $request)
185
	{
186 4
		$this->request = $request;
187 4
		return $this;
188
	}
189
	
190
	
191
	/**
192
	 * @return absences_Request
193
	 */
194 16
	public function getRequest()
195
	{
196 16
		if (!isset($this->request))
197 16
		{
198 12
			$row = $this->getRow();
199 12
			$class = $row['request_class'];
200
			
201 12
			if (empty($class)||empty($row['id_request']))
202 12
			{
203 12
				return null;
204
			}
205
			
206
			$request = call_user_func(array($class, 'getById'), $row['id_request']);
207
			/*@var $request absences_Request */
208
			
209
			if (!$request->getRow())
210
			{
211
				return null;
212
			}
213
			
214
			$this->request = $request;
215
		}
216
	
217 4
		return $this->request;
218
	}
219
	
220
	
221
	/**
222
	 * Save entry to database
223
	 * without validity checking
224
	 *
225
	 * @return bool
226
	 */
227 16
	public function save()
228
	{
229
		// save entry
230
	
231 16
		global $babDB;
232
	
233
	
234 16
		if (!isset($this->comment))
235 16
		{
236
			$this->comment = '';
237
		}
238
	
239 16
		if (!isset($this->message))
240 16
		{
241
			throw new Exception('Message is mandatory');
242
		}
243
	
244 16
		if (!isset($this->id_user))
245 16
		{
246 16
			if (isset($this->agent))
247 16
			{
248 12
				$this->id_user = $this->agent->getIdUser();
249 12
			} else {
250 9
				$this->id_user = 0;
251
			}
252
			
253 16
		}
254
		
255 16
		if (!isset($this->id_right))
256 16
		{
257 16
			if (isset($this->right))
258 16
			{
259 12
				$this->id_right = $this->right->id;
260 12
			} else {
261 4
				$this->id_right = 0;
262
			}
263
			
264 16
		}
265
		
266
		
267
		
268 16
		if (!isset($this->id_request))
269 16
		{
270 16
			if (isset($this->request))
271 16
			{
272 4
				$this->id_request = $this->request->id;
273 4
			} else {
274 12
				$this->id_request = 0;
275
			}
276
		
277 16
		}
278
		
279
		
280 16
		if (!isset($this->request_class))
281 16
		{
282 16
			if (isset($this->request))
283 16
			{
284 4
				$this->request_class = get_class($this->request);
285 4
			} else {
286 12
				$this->request_class = '';
287
			}
288
		
289 16
		}
290
		
291 16
		if (!isset($this->status))
292 16
		{
293 16
		    $this->status = null;
294 16
		    $request = $this->getRequest();
295 16
		    if (isset($request))
296 16
			{
297 4
				$this->status = $request->status;
298 4
			}
299 16
		}
300
		
301
		
302 16
		if (!isset($this->id_author))
303 16
		{
304 7
			$this->id_author = bab_getUserId();
305 7
		}
306
		
307 16
		if (!isset($this->createdOn))
308 16
		{
309 12
		    $this->createdOn = date('Y-m-d H:i:s');
310 12
		}
311
	
312
	
313 16 View Code Duplication
		if (isset($this->id))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
314 16
		{
315
	
316
			$babDB->db_query("
317
				UPDATE absences_movement
318
				SET
319
					comment	=".$babDB->quote($this->comment)." 
320
				WHERE
321
					id=".$babDB->quote($this->id)."
322
			");
323
	
324
	
325
		} else {
326
	
327 16
			$babDB->db_query("
328
				INSERT INTO absences_movement
329
				(
330
					id_user,
331
					id_right,
332
					id_request,
333
					request_class,
334
					id_author,
335
					comment,
336
					createdOn,
337
					message,
338
			        status
339
				)
340
				VALUES
341
					(
342 16
					".$babDB->quote($this->id_user).",
343 16
					".$babDB->quote($this->id_right).",
344 16
					".$babDB->quote($this->id_request).",
345 16
					".$babDB->quote($this->request_class).",
346 16
					".$babDB->quote($this->id_author).",
347 16
					".$babDB->quote($this->comment).",
348 16
					".$babDB->quote($this->createdOn).",
349 16
					".$babDB->quote($this->message).",
350 16
			        ".$babDB->quoteOrNull($this->status)."
351
				)
352 16
			");
353
	
354 16
			$this->id = $babDB->db_insert_id();
355
		}
356 16
	}
357
}
358
359
360
361
362
363
364
365
366
class absences_MovementIterator extends absences_Iterator
367
{
368
	protected $agent;
369
370
	protected $right;
371
	
372
	protected $request;
373
	
374
	/**
375
	 * get movements before date
376
	 * @var string
377
	 */
378
	public $createdOn;
379
380
381
	public function setRight(absences_Right $right)
382
	{
383
		$this->right = $right;
384
	}
385
386
	public function setAgent(absences_Agent $agent)
387
	{
388
		$this->agent = $agent;
389
	}
390
	
391 7
	public function setRequest(absences_Request $request)
392
	{
393 7
		$this->request = $request;
394 7
	}
395
396
397 4
	public function getObject($data)
398
	{
399 4
		$movement = new absences_Movement;
400 4
		$movement->setRow($this->getRowByPrefix($data, 'movement'));
401
402 4
		if ($right_row = $this->getRowByPrefix($data, 'right'))
403 4
		{
404 4
			$right = new absences_Right($right_row['id']);
405 4
			$right->setRow($right_row);
406 4
			$movement->setRight($right);
407 4
		}
408
		
409 4
		if ($agent_row = $this->getRowByPrefix($data, 'agent'))
410 4
		{
411 4
			$agent = new absences_Agent();
412 4
			$agent->setRow($agent_row);
413 4
			$movement->setAgent($agent);
414 4
		}
415
		
416 4
		return $movement;
417
	}
418
419 7
	public function executeQuery()
420
	{
421 7
		if(is_null($this->_oResult))
422 7
		{
423 7
			global $babDB;
424
425
			$query = '
426
			SELECT
427
				m.id 				movement__id,
428
				m.id_user			movement__id_user,
429
				m.id_right			movement__id_right,
430
				m.id_request		movement__id_request,
431
				m.request_class		movement__request_class,
432
				m.id_author			movement__id_author,
433
				m.comment			movement__comment,
434
				m.createdOn			movement__createdOn,
435
				m.message			movement__message, 
436
                m.status            movement__status,
437
				
438
				r.id				right__id,
439
				r.kind				right__kind,
440
				r.id_creditor		right__id_creditor,
441
			    r.createdOn  		right__createdOn,
442
				r.date_entry		right__date_entry,
443
				r.date_begin 		right__date_begin,
444
				r.date_end 			right__date_end,
445
				r.quantity			right__quantity,
446
				r.quantity_unit 	right__quantity_unit,
447
				r.id_type			right__id_type,
448
				r.description		right__description,
449
				r.active			right__active,
450
				r.cbalance			right__cbalance,
451
				r.date_begin_valid	right__date_begin_valid,
452
				r.date_end_valid	right__date_end_valid,
453
				r.date_end_fixed	right__date_end_fixed,
454
				r.date_begin_fixed	right__date_begin_fixed,
455
			    r.hide_empty	    right__hide_empty,
456
				r.no_distribution	right__no_distribution,
457
				r.id_rgroup			right__id_rgroup,
458
				r.earlier					right__earlier,
459
	 			r.earlier_begin_valid		right__earlier_begin_valid,
460
	  			r.earlier_end_valid			right__earlier_end_valid,
461
	  			r.later						right__later,
462
	  			r.later_begin_valid			right__later_begin_valid,
463
	  			r.later_end_valid			right__later_end_valid,
464
	  			r.require_approval			right__require_approval,
465
				r.delay_before				right__delay_before,  
466
	  			
467
	  			
468
	  			a.id_user			agent__id_user,
469
	  			a.id_coll			agent__id_coll,
470
	  			a.id_sa				agent__id_sa,
471
	  			a.id_sa_cet			agent__id_sa_cet,
472
	  			a.id_sa_recover		agent__id_sa_recover 
473
			
474
			FROM 
475
				absences_movement m 
476
					LEFT JOIN absences_personnel a ON a.id_user=m.id_user 
477
					LEFT JOIN absences_rights r ON r.id=m.id_right 
478 7
			    
479
			';
480 7
			
481
			$where = array();
482 7
483 7
			if (isset($this->agent))
484
			{
485
				$where[] = 'm.id_user='.$babDB->quote($this->agent->getIdUser());
486
			}
487 7
488 7
			if (isset($this->right))
489
			{
490
				$where[] = 'm.id_right='.$babDB->quote($this->right->id);
491
			}
492 7
			
493 7
			if (isset($this->request))
494 7
			{
495 7
				$where[] = 'm.id_request='.$babDB->quote($this->request->id);
496 7
				$where[] = 'm.request_class='.$babDB->quote(get_class($this->request));
497
			}
498 7
			
499 7
			if (isset($this->createdOn)) {
500 7
			    $where[] = 'm.createdOn<='.$babDB->quote($this->createdOn);
501
			}
502
			
503 7
			if($where)
0 ignored issues
show
Bug Best Practice introduced by
The expression $where of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
504 7
			{
505 7
				$query .= ' WHERE '.implode(' AND ', $where);
506
			}
507 7
			
508
			$query .= ' ORDER BY m.createdOn DESC, m.id DESC';
509
			
510 7
511 7
			$this->setMySqlResult($this->getDataBaseAdapter()->db_query($query));
512 7
		}
513
	}
514
515
}
516