Completed
Push — master ( 1ce70a...0adf65 )
by Litera
11:25 queued 21s
created

MeetingModel::setRegistrationHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Nette\Database\Context;
6
use App\Models\ProgramModel;
7
use App\Models\BaseModel;
8
9
/**
10
 * Meeting
11
 *
12
 * class for handling meeting
13
 *
14
 * @created 2012-11-09
15
 * @author Tomas Litera <[email protected]>
16
 */
17
class MeetingModel extends BaseModel
0 ignored issues
show
Coding Style introduced by
The property $form_names is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
18
{
19
20
	/**
21
	 * @var array
22
	 */
23
	private $weekendDays = [];
24
25
	/**
26
	 * @var array
27
	 */
28
	public $form_names = [];
29
30
	/**
31
	 * @var array
32
	 */
33
	public $dbColumns = [];
34
35
	/**
36
	 * @var DateTime
37
	 */
38
	public $regOpening = NULL;
39
40
	/**
41
	 * @var DateTime
42
	 */
43
	public $regClosing = NULL;
44
45
	/** @var string registration heading text */
46
	public $regHeading = '';
47
48
	public $eventId;
49
	public $courseId;
50
51
	private $configuration;
0 ignored issues
show
Unused Code introduced by
The property $configuration is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
53
	private $program;
54
	private $httpEncoding;
55
	private $dbTable;
56
57
	protected $table = 'kk_meetings';
58
59
	/** Constructor */
60
	public function __construct(Context $database, ProgramModel $program)
61
	{
62
		$this->weekendDays = array("pátek", "sobota", "neděle");
63
		$this->form_names = array(
64
			"place",
65
			"start_date",
66
			"end_date",
67
			"open_reg",
68
			"close_reg",
69
			"contact",
70
			"email",
71
			"gsm",
72
			"cost",
73
			"advance",
74
			"numbering",
75
			'skautis_event_id',
76
			'skautis_course_id',
77
		);
78
		$this->dbColumns = array(
79
			"place",
80
			"start_date",
81
			"end_date",
82
			"open_reg",
83
			"close_reg",
84
			"contact",
85
			"email",
86
			"gsm",
87
			"cost",
88
			"advance",
89
			"numbering",
90
			'skautis_event_id',
91
			'skautis_course_id',
92
		);
93
		$this->dbTable = "kk_meetings";
94
		$this->setDatabase($database);
95
		$this->program = $program;
96
	}
97
98
	/**
99
	 * @param string $encoding
100
	 */
101
	public function setHttpEncoding($encoding)
102
	{
103
		$this->httpEncoding = $encoding;
104
	}
105
106
	/**
107
	 * Create new or return existing instance of class
108
	 *
109
	 * @return	mixed	instance of class
110
	 */
111
	public static function getInstance()
112
	{
113
		if(self::$instance === false) {
114
			self::$instance = new self();
0 ignored issues
show
Bug introduced by
The call to MeetingModel::__construct() misses some required arguments starting with $database.
Loading history...
115
		}
116
		return self::$instance;
117
	}
118
119
	/**
120
	 * @return Nette\Database\Table\IRow
121
	 */
122
	public function all()
123
	{
124
		return $this->getDatabase()
125
				->table($this->getTable())
126
				->where('deleted', '0')
127
				->fetchAll();
128
	}
129
130
	/**
131
	 * @param  int $id
132
	 * @return Nette\Database\Table\IRow
133
	 */
134
	public function find($id)
135
	{
136
		return $this->getDatabase()
137
				->table($this->getTable())
138
				->where('deleted ? AND id ?', '0', $id)
139
				->fetch();
140
	}
141
142
	/**
143
	 * Create a new record
144
	 *
145
	 * @param	mixed	array of data
146
	 * @return	boolean
147
	 */
148
	public function create(array $data)
149
	{
150
		$data['guid'] = md5(uniqid());
151
		$result = $this->getDatabase()->query('INSERT INTO ' . $this->getTable(), $data);
152
153
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (Nette\Database\ResultSet) is incompatible with the return type of the parent method App\Models\BaseModel::create of type Nette\Database\Table\IRow|integer|boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
154
	}
155
156
	/**
157
	 * Modify record
158
	 *
159
	 * @param	int		$id			ID of record
160
	 * @param	array	$db_data	array of data
0 ignored issues
show
Bug introduced by
There is no parameter named $db_data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
161
	 * @return	bool
162
	 */
163
	public function update($id, array $data)
164
	{
165
		$result = $this->getDatabase()->table($this->getTable())->where('id', $id)->update($data);
166
167
		return $result;
168
	}
169
170
	/**
171
	 * Delete one or multiple record/s
172
	 *
173
	 * @param	int		ID/s of record
174
	 * @return	boolean
175
	 */
176 View Code Duplication
	public function delete($ids)
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...
177
	{
178
		$data = array('deleted' => '1');
179
		$result = $this->getDatabase()->table($this->getTable())->where('id', $ids)->update($data);
180
181
		return $result;
182
	}
183
184
	/**
185
	 * Return meeting data
186
	 *
187
	 * @return  Nette\Database\Table\IRow
188
	 */
189
	public function getData($meetingId = null)
190
	{
191
		if(isset($meetingId)) {
192
			$data = $this->find($meetingId);
193
		} else {
194
			$data = $this->all();
195
		}
196
197
		if(!$data) {
198
			return 0;
199
		} else {
200
			return $data;
201
		}
202
	}
203
204
	/**
205
	 * @param  string $priceType cost|advance
206
	 * @return integer
207
	 */
208
	public function getPrice($priceType)
209
	{
210
		return $this->getDatabase()
211
			->table($this->getTable())
212
			->select($priceType)
213
			->where('id', $this->getMeetingId())
214
			->limit(1)
215
			->fetchField();
216
	}
217
218
	/**
219
	 * Render HTML Provinces <select>
220
	 *
221
	 * @param	int	ID of selected province
222
	 * @return	string	html <select>
223
	 */
224
	public function renderHtmlProvinceSelect($selectedProvince)
225
	{
226
		$html_select = "<select style='width: 195px; font-size: 10px' name='province'>\n";
227
228
		$result = $this->getDatabase()
229
			->table('kk_provinces')
230
			->fetchAll();
231
232
		foreach($result as $data) {
233
			if($data['id'] == $selectedProvince) {
234
				$sel = "selected";
235
			}
236
			else $sel = "";
237
			$html_select .= "<option value='" . $data['id'] . "' " . $sel . ">" . $data['province_name'] . "</option>";
238
		}
239
240
		$html_select .= "</select>\n";
241
242
		return $html_select;
243
	}
244
245
	/** Public program same as getPrograms*/
246
	public function getPublicPrograms($blockId)
247
	{
248
		$result = $this->getDatabase()
249
			->query('SELECT progs.id AS id,
250
						progs.name AS name,
251
						style
252
				FROM kk_programs AS progs
253
				LEFT JOIN kk_categories AS cat ON cat.id = progs.category
254
				WHERE block = ? AND progs.deleted = ?
255
				LIMIT 10',
256
				$blockId, '0')
257
			->fetchAll();
258
259
		if(!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type Nette\Database\IRow[] 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...
260
			$html = '';
261
		} else {
262
			$html = "<table>\n";
263
			$html .= " <tr>\n";
264
			foreach($result as $data) {
265
				$html .= "<td class='category cat-".$data['style']."' style='text-align:center;'>\n";
266
				$html .= "<a class='programLink' rel='programDetail' href='#' rel='programDetail' title='" . $this->program->getDetail($data['id'], 'program', $this->httpEncoding) . "'>" . $data['name'] . "</a>\n";
267
				$html .= "</td>\n";
268
			}
269
			$html .= " </tr>\n";
270
			$html .= "</table>\n";
271
		}
272
		return $html;
273
	}
274
275
	public function renderPublicProgramOverview($meetingId)
276
	{
277
		$days = array("pátek", "sobota", "neděle");
278
		$html = "";
279
280
		foreach($days as $dayKey => $dayVal) {
281
			$html .= "<table>\n";
282
			$html .= " <tr>\n";
283
			$html .= "  <td class='day' colspan='2' >" . $dayVal . "</td>\n";
284
			$html .= " </tr>\n";
285
286
			$result = $this->getDatabase()
287
				->query('SELECT	blocks.id AS id,
288
							day,
289
							DATE_FORMAT(`from`, "%H:%i") AS `from`,
290
							DATE_FORMAT(`to`, "%H:%i") AS `to`,
291
							blocks.name AS name,
292
							program,
293
							display_progs,
294
							style
295
					FROM kk_blocks AS blocks
296
					LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
297
					WHERE blocks.deleted = ? AND day = ? AND meeting = ?
298
					ORDER BY `from` ASC',
299
					'0', $dayVal, $meetingId)
300
				->fetchAll();
301
302
			if(!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type Nette\Database\IRow[] 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...
303
				$html .= "<td class='emptyTable' style='width:400px;'>Nejsou žádná aktuální data.</td>\n";
304
			} else {
305
				foreach($result as $data) {
306
					$html .= "<tr>\n";
307
					$html .= "<td class='time'>" . $data['from'] . " - " . $data['to'] . "</td>\n";
308
					if(($data['program'] == 1) && ($data['display_progs'] == 1)) {
309
						$html .= "<td class='category cat-" . $data['style'] . "' class='daytime'>\n";
310
						$html .= "<div>\n";
311
						$html .= "<a class='programLink rel='programDetail' href='#' rel='programDetail' title='" . $this->program->getDetail($data['id'], 'block', $this->httpEncoding) . "'>" . $data['name'] . "</a>\n";
312
						$html .= "</div>\n";
313
						$html .= $this->getPublicPrograms($data['id']);
314
						$html .= "</td>\n";
315
					} else {
316
						$html .= "<td class='category cat-" . $data['style'] . "'>";
317
						$html .= "<a class='programLink rel='programDetail' href='#' rel='programDetail' title='" . $this->program->getDetail($data['id'], 'block', $this->httpEncoding) . "'>" . $data['name'] . "</a>\n";
318
						$html .= "</td>\n";
319
					}
320
					$html .= "</tr>\n";
321
				}
322
			}
323
			$html .= "</table>\n";
324
		}
325
326
		return $html;
327
	}
328
329
	/**
330
	 * @param  integer $meetingId
331
	 * @return $this
332
	 */
333
	public function setRegistrationHandlers($meetingId = 1)
334
	{
335
		$meeting = $this->getDatabase()
336
			->table($this->getTable())
337
			->select('id')
338
			->select('place')
339
			->select('DATE_FORMAT(start_date, "%Y") AS year')
340
			->select('UNIX_TIMESTAMP(open_reg) AS open_reg')
341
			->select('UNIX_TIMESTAMP(close_reg) AS close_reg')
342
			->where('id', $meetingId)
343
			->order('id DESC')
344
			->limit(1)
345
			->fetch();
346
347
		$this->setRegHeading($meeting->place . ' ' . $meeting->year);
348
		$this->setRegClosing($meeting->close_reg);
349
		$this->setRegOpening($meeting->open_reg);
350
351
		return $this;
352
	}
353
354
	/**
355
	 * @return string
356
	 */
357
	public function getRegOpening()
358
	{
359
		return $this->regOpening;
360
	}
361
362
	/**
363
	 * @param  string $value
364
	 * @return $this
365
	 */
366
	public function setRegOpening($value = '')
367
	{
368
		$this->regOpening = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type string is incompatible with the declared type object<App\Models\DateTime> of property $regOpening.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
369
370
		return $this;
371
	}
372
373
	/**
374
	 * @return string
375
	 */
376
	public function getRegClosing()
377
	{
378
		return $this->regClosing;
379
	}
380
381
	/**
382
	 * @param  string $value
383
	 * @return $this
384
	 */
385
	public function setRegClosing($value = '')
386
	{
387
		$this->regClosing = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type string is incompatible with the declared type object<App\Models\DateTime> of property $regClosing.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
388
389
		return $this;
390
	}
391
392
	/**
393
	 * @return string
394
	 */
395
	public function getRegHeading()
396
	{
397
		return $this->regHeading;
398
	}
399
400
	/**
401
	 * @param  string $value
402
	 * @return $this
403
	 */
404
	public function setRegHeading($value = '')
405
	{
406
		$this->regHeading = $value;
407
408
		return $this;
409
	}
410
411
	/**
412
	 * Is registration open?
413
	 *
414
	 * @return 	boolean
415
	 */
416
	public function isRegOpen($debug = false)
417
	{
418
		return (($this->getRegOpening() < time()) && (time() < $this->getRegClosing()) || $debug);
419
	}
420
421
	/**
422
	 * @param  integer $id
423
	 * @return string
424
	 */
425
	public function getProvinceNameById($id)
426
	{
427
		return $this->getDatabase()
428
			->table('kk_provinces')
429
			->select('province_name')
430
			->where('id', $id)
431
			->limit(1)
432
			->fetchField('province_name');
433
	}
434
435
	/**
436
	 * @return Row
437
	 */
438
	public function findEventId()
439
	{
440
		return $this->getDatabase()
441
			->table($this->getTable())
442
			->where('id', $this->getMeetingId())
443
			->limit(1)
444
			->fetchField('skautis_event_id');
445
	}
446
447
	/**
448
	 * @return Row
449
	 */
450
	public function findCourseId()
451
	{
452
		return $this->getDatabase()
453
			->table($this->getTable())
454
			->where('id', $this->getMeetingId())
455
			->limit(1)
456
			->fetchField('skautis_course_id');
457
	}
458
459
	/**
460
	 * @param  integer|string $meetingId
461
	 * @return ActiveRow
462
	 */
463
	public function getPlaceAndYear($meetingId)
464
	{
465
		return $this->getDatabase()
466
			->table($this->getTable())
467
			->select('place')
468
			->select('DATE_FORMAT(start_date, "%Y") AS year')
469
			->where('id = ? AND deleted = ?', $meetingId, '0')
470
			->limit(1)
471
			->fetch();
472
	}
473
474
	/**
475
	 * @return ActiveRow
476
	 */
477
	public function getMenuItems()
478
	{
479
		return $this->getDatabase()
480
			->table($this->getTable())
481
			->select('id AS mid')
482
			->select('place')
483
			->select('DATE_FORMAT(start_date, "%Y") AS year')
484
			->where('deleted', '0')
485
			->order('id DESC')
486
			->fetchAll();
487
	}
488
489
	/**
490
	 * @return integer
491
	 */
492
	public function getLastMeetingId()
493
	{
494
		return $this->getDatabase()
495
			->table($this->getTable())
496
			->select('id')
497
			->order('id DESC')
498
			->limit(1)
499
			->fetchField();
500
	}
501
502
}
503