Completed
Push — master ( fea2cb...118c60 )
by Litera
21s
created

MeetingModel::setHttpEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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
	/**
246
	 * @param  integer $meetingId
247
	 * @return $this
248
	 */
249
	public function setRegistrationHandlers($meetingId = 1)
250
	{
251
		$meeting = $this->getDatabase()
252
			->table($this->getTable())
253
			->select('id')
254
			->select('place')
255
			->select('DATE_FORMAT(start_date, "%Y") AS year')
256
			->select('UNIX_TIMESTAMP(open_reg) AS open_reg')
257
			->select('UNIX_TIMESTAMP(close_reg) AS close_reg')
258
			->where('id', $meetingId)
259
			->order('id DESC')
260
			->limit(1)
261
			->fetch();
262
263
		$this->setRegHeading($meeting->place . ' ' . $meeting->year);
264
		$this->setRegClosing($meeting->close_reg);
265
		$this->setRegOpening($meeting->open_reg);
266
267
		return $this;
268
	}
269
270
	/**
271
	 * @return string
272
	 */
273
	public function getRegOpening()
274
	{
275
		return $this->regOpening;
276
	}
277
278
	/**
279
	 * @param  string $value
280
	 * @return $this
281
	 */
282
	public function setRegOpening($value = '')
283
	{
284
		$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...
285
286
		return $this;
287
	}
288
289
	/**
290
	 * @return string
291
	 */
292
	public function getRegClosing()
293
	{
294
		return $this->regClosing;
295
	}
296
297
	/**
298
	 * @param  string $value
299
	 * @return $this
300
	 */
301
	public function setRegClosing($value = '')
302
	{
303
		$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...
304
305
		return $this;
306
	}
307
308
	/**
309
	 * @return string
310
	 */
311
	public function getRegHeading()
312
	{
313
		return $this->regHeading;
314
	}
315
316
	/**
317
	 * @param  string $value
318
	 * @return $this
319
	 */
320
	public function setRegHeading($value = '')
321
	{
322
		$this->regHeading = $value;
323
324
		return $this;
325
	}
326
327
	/**
328
	 * Is registration open?
329
	 *
330
	 * @return 	boolean
331
	 */
332
	public function isRegOpen($debug = false)
333
	{
334
		return (($this->getRegOpening() < time()) && (time() < $this->getRegClosing()) || $debug);
335
	}
336
337
	/**
338
	 * @param  integer $id
339
	 * @return string
340
	 */
341
	public function getProvinceNameById($id)
342
	{
343
		return $this->getDatabase()
344
			->table('kk_provinces')
345
			->select('province_name')
346
			->where('id', $id)
347
			->limit(1)
348
			->fetchField('province_name');
349
	}
350
351
	/**
352
	 * @return Row
353
	 */
354
	public function findEventId()
355
	{
356
		return $this->getDatabase()
357
			->table($this->getTable())
358
			->where('id', $this->getMeetingId())
359
			->limit(1)
360
			->fetchField('skautis_event_id');
361
	}
362
363
	/**
364
	 * @return Row
365
	 */
366
	public function findCourseId()
367
	{
368
		return $this->getDatabase()
369
			->table($this->getTable())
370
			->where('id', $this->getMeetingId())
371
			->limit(1)
372
			->fetchField('skautis_course_id');
373
	}
374
375
	/**
376
	 * @param  integer|string $meetingId
377
	 * @return ActiveRow
378
	 */
379
	public function getPlaceAndYear($meetingId)
380
	{
381
		return $this->getDatabase()
382
			->table($this->getTable())
383
			->select('place')
384
			->select('DATE_FORMAT(start_date, "%Y") AS year')
385
			->where('id = ? AND deleted = ?', $meetingId, '0')
386
			->limit(1)
387
			->fetch();
388
	}
389
390
	/**
391
	 * @return ActiveRow
392
	 */
393
	public function getMenuItems()
394
	{
395
		return $this->getDatabase()
396
			->table($this->getTable())
397
			->select('id AS mid')
398
			->select('place')
399
			->select('DATE_FORMAT(start_date, "%Y") AS year')
400
			->where('deleted', '0')
401
			->order('id DESC')
402
			->fetchAll();
403
	}
404
405
	/**
406
	 * @return integer
407
	 */
408
	public function getLastMeetingId()
409
	{
410
		return $this->getDatabase()
411
			->table($this->getTable())
412
			->select('id')
413
			->order('id DESC')
414
			->limit(1)
415
			->fetchField();
416
	}
417
418
}
419