Completed
Pull Request — devel (#144)
by Litera
03:43
created

BlockModel::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Entities\BlockEntity;
6
use Nette\Database\Context;
7
use Nette\Reflection\ClassType;
8
use App\Entities\IEntity;
9
use \Exception;
10
11
/**
12
 * Blocks
13
 *
14
 * class for handling program blocks
15
 *
16
 * @created 2012-09-14
17
 * @author Tomas Litera <[email protected]>
18
 */
19
class BlockModel extends BaseModel implements IModel
20
{
21
22
	/** @var string */
23
	protected $table = 'kk_blocks';
24
25
	/** @var array */
26
	public $columns = [
27
		'guid',
28
		'name',
29
		'day',
30
		'from',
31
		'to',
32
		'program',
33
		'display_progs',
34
		'description',
35
		'tutor',
36
		'email',
37
		'category',
38
		'material',
39
		'capacity',
40
		//"meeting",
41
	];
42
43
	private static $connection;
44
45
	/**
46
	 * @param Context  $database
47
	 */
48
	public function __construct(Context $database)
49
	{
50
		$this->setDatabase($database);
51
		self::$connection = $this->getDatabase();
52
	}
53
54
	/**
55
	 * @return	ActiveRow
56
	 */
57
	public function all()
58
	{
59
		return $this->getDatabase()
60
			->query('SELECT blocks.guid AS guid,
61
						blocks.id AS id,
62
						blocks.name AS name,
63
						cat.name AS cat_name,
64
						day,
65
						DATE_FORMAT(`from`, "%H:%i") AS `from`,
66
						DATE_FORMAT(`to`, "%H:%i") AS `to`,
67
						description,
68
						tutor,
69
						email,
70
						style
71
				FROM kk_blocks AS blocks
72
				LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
73
				WHERE blocks.meeting = ? AND blocks.deleted = ?
74
				ORDER BY day, `from` ASC',
75
				$this->getMeetingId(), '0')
76
			->fetchAll();
77
	}
78
79
    /**
80
     * @param  int $id
81
     * @return BlockEntity
82
     */
83
	public function find($id): BlockEntity
84
	{
85
		$block = parent::find($id);
86
		return $this->hydrate($block);
87
	}
88
89
    /**
90
     * @param IEntity $entity
91
     * @return bool|mixed
92
     * @throws Exception
93
     */
94
	public function save(IEntity $entity)
95
	{
96
        $this->guardToGreaterThanFrom($entity->from, $entity->to);
0 ignored issues
show
Bug introduced by
Accessing from on the interface App\Entities\IEntity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing to on the interface App\Entities\IEntity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
97
98
		if ($entity->getId() === null) {
99
			$values = $entity->toArray();
100
101
			$id = $this->create($values);
102
			$result = $this->setIdentity($entity, $id);
103
104
		} else {
105
			$values = $entity->toArray();
106
			$result = $this->update($entity->getId(), $values);
107
		}
108
109
		return $result;
110
	}
111
112
	/**
113
	 * Return blocks that contents programs
114
	 *
115
	 * @param	int		meeting ID
116
	 * @return	array	result and number of affected rows
117
	 */
118
	public function getProgramBlocks($meetingId)
119
	{
120
		$data = $this->getDatabase()
121
			->query('SELECT id,
122
					day,
123
					DATE_FORMAT(`from`, "%H:%i") AS `from`,
124
					DATE_FORMAT(`to`, "%H:%i") AS `to`,
125
					name,
126
					program
127
				FROM kk_blocks
128
				WHERE deleted = ? AND program = ? AND meeting = ?
129
				ORDER BY `day`, `from` ASC',
130
				'0', '1', $meetingId)->fetchAll();
131
132
		return $data;
133
	}
134
135
	/**
136
	 * @param  integer $meetingId
137
	 * @return ActiveRow
138
	 */
139
	public function idsFromCurrentMeeting($meetingId)
140
	{
141
		return $this->getDatabase()
142
			->table($this->getTable())
143
			->select('id')
144
			->where('meeting ? AND program ? AND deleted ?', $meetingId, '1', '0')
145
			->fetchAll();
146
	}
147
148
	/**
149
	 * @param  integer $meetingId
150
	 * @param  string  $dayVal
151
	 * @return ActiveRow
152
	 */
153
	public function getExportBlocks($meetingId, $dayVal)
154
	{
155
		$result = $this->getDatabase()
156
			->query('SELECT blocks.id AS id,
157
						day,
158
						DATE_FORMAT(`from`, "%H:%i") AS `from`,
159
						DATE_FORMAT(`to`, "%H:%i") AS `to`,
160
						blocks.name AS name,
161
						program,
162
						display_progs,
163
						style,
164
						cat.id AS category
165
				FROM kk_blocks AS blocks
166
				LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
167
				/* 18 - pauzy */
168
				WHERE blocks.deleted = ? AND day = ? AND meeting = ? AND category != ?
169
				ORDER BY `from` ASC',
170
				'0', $dayVal, $meetingId, '18')->fetchAll();
171
172
		return $result;
173
	}
174
175
	/**
176
	 * Get tutor e-mail address
177
	 *
178
	 * @param int $blockId id of block item
179
	 * @return Nette\Database\Table\ActiveRow object with e-mail address
180
	 */
181
	public function getTutor($blockId)
182
	{
183
		return $this->getDatabase()
184
			->table($this->getTable())
185
			->select('guid, email, tutor')
186
			->where('id ? AND deleted ?', $blockId, '0')
187
			->limit(1)
188
			->fetch();
189
	}
190
191
	/**
192
	 * @param  int $meetingId
193
	 * @return Database
194
	 */
195
	public function findByMeeting($meetingId)
196
	{
197
		return $this->getDatabase()
198
			->table($this->getTable())
199
			->select('id, day, from, to, name')
200
			->where('meeting ? AND program ? AND deleted ?', $meetingId, '1', '0')
201
			->fetchAll();
202
	}
203
204
	/**
205
	 * @param  string $day
206
	 * @return Row
207
	 */
208
	public function findByDay($day = '')
209
	{
210
		return $this->getDatabase()
211
				->query('SELECT	blocks.id AS id,
212
							day,
213
							DATE_FORMAT(`from`, "%H:%i") AS `from`,
214
							DATE_FORMAT(`to`, "%H:%i") AS `to`,
215
							blocks.name AS name,
216
							program,
217
							style
218
					FROM kk_blocks AS blocks
219
					LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
220
					WHERE blocks.deleted = ? AND day = ? AND blocks.meeting = ?
221
					ORDER BY `from` ASC',
222
					'0', $day, $this->getMeetingId())
223
				->fetchAll();
224
	}
225
226
	/**
227
	 * Return blocks that contents programs
228
	 *
229
	 * @param	int		meeting ID
230
	 * @return	array	result and number of affected rows
231
	 */
232
	public function findProgramBlocksByMeeting(int $meetingId)
233
	{
234
		return $this->getDatabase()
235
			->query('SELECT id,
236
					day,
237
					DATE_FORMAT(`from`, "%H:%i") AS `from`,
238
					DATE_FORMAT(`to`, "%H:%i") AS `to`,
239
					name,
240
					program
241
				FROM kk_blocks
242
				WHERE deleted = ? AND program = ? AND meeting = ?
243
				ORDER BY `day`, `from` ASC',
244
				'0', '1', $meetingId)->fetchAll();
245
	}
246
247
	public function findByProgramId(int $programId)
0 ignored issues
show
Unused Code introduced by
The parameter $programId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
248
	{
249
		return $this->getDatabase()
250
			->query()
251
			->fetch();
252
	}
253
254
    /**
255
     * @param  $values
256
     * @return BlockEntity
257
     */
258
	public function hydrate($values): BlockEntity
259
	{
260
		$entity = new BlockEntity();
261
		//$this->setIdentity($entity, $values->id);
262
263
		// unset($values['id']);
264
		foreach ($values as $property => $value) {
265
			$entity->$property = $value;
266
		}
267
268
		return $entity;
269
	}
270
271
    /**
272
     * @param  $block
273
     * @return mixed
274
     */
275
	public function transform($block)
276
    {
277
        $block->from = date('H:i:s', mktime($block->start_hour, $block->start_minute, 0, 0, 0, 0));
278
        $block->to = date('H:i:s', mktime($block->end_hour, $block->end_minute, 0, 0, 0, 0));
279
        $block->meeting = $this->getMeetingId();
280
        $block->program = strval($block->program) ?: '0';
281
        $block->display_progs = strval($block->display_progs) ?: '0';
282
283
        unset($block->start_hour);
284
        unset($block->end_hour);
285
        unset($block->start_minute);
286
        unset($block->end_minute);
287
        unset($block->backlink);
288
289
        return $block;
290
    }
291
292
    /**
293
     * @param  $item
294
     * @param  $id
295
     * @return mixed
296
     */
297
	private function setIdentity($item, $id)
298
	{
299
		$ref = new ClassType($item);
300
		$idProperty = $ref->getProperty('id');
301
		$idProperty->setAccessible(true);
302
		$idProperty->setValue($item, $id);
303
304
		return $item;
305
	}
306
307
    /**
308
     * @param  date $from
309
     * @param  date $to
310
     * @return void
311
     * @throws Exception
312
     */
313
    private function guardToGreaterThanFrom($from, $to)
314
    {
315
        if($from > $to) {
316
            throw new Exception('Starting time is greater then finishing time.');
317
        }
318
    }
319
320
}
321