Completed
Push — master ( 118c60...254398 )
by Litera
03:47
created

MealModel::renderHtmlMealsSelect()   C

Complexity

Conditions 7
Paths 25

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
cc 7
eloc 17
nc 25
nop 2
rs 6.7272
1
<?php
2
3
namespace App\Models;
4
5
use Nette\Database\Context;
6
use Nette\Database\Table\ActiveRow;
7
8
/**
9
 * Meal
10
 *
11
 * class for handling meals
12
 *
13
 * @created 2012-11-11
14
 * @author Tomas Litera <[email protected]>
15
 */
16
class MealModel extends BaseModel
17
{
18
19
	/**
20
	 * @var array
21
	 */
22
	static public $meals = [
23
		'fry_dinner'    => 'páteční večeře',
24
		'sat_breakfast' => 'sobotní snídaně',
25
		'sat_lunch'     => 'sobotní oběd',
26
		'sat_dinner'    => 'sobotní večeře',
27
		'sun_breakfast' => 'nedělní snídaně',
28
		'sun_lunch'     => 'nedělní oběd',
29
	];
30
31
	/**
32
	 * @deprecated
33
	 * @var array
34
	 */
35
	static public $dayMeal = [
36
		"páteční večeře"	=>	"fry_dinner",
37
		"sobotní snídaně"	=>	"sat_breakfast",
38
		"sobotní oběd"		=>	"sat_lunch",
39
		"sobotní večeře"	=>	"sat_dinner",
40
		"nedělní snídaně"	=>	"sun_breakfast",
41
		"nedělní oběd"		=>	"sun_lunch"
42
	];
43
44
	/**
45
	 * @var string
46
	 */
47
	protected $table = 'kk_meals';
48
49
	/**
50
	 * @var array
51
	 */
52
	protected $columns = [
53
		"visitor",
54
		"fry_dinner",
55
		"sat_breakfast",
56
		"sat_lunch",
57
		"sat_dinner",
58
		"sun_breakfast",
59
		"sun_lunch"
60
	];
61
62
	/**
63
	 * @param Context $database
64
	 */
65
	public function __construct(Context $database)
66
	{
67
		$this->setDatabase($database);
68
	}
69
70
	/**
71
	 * @return array
72
	 */
73
	public function getColumns(): array
74
	{
75
		return $this->columns;
76
	}
77
78
	/**
79
	 * @param  integer $visitorId
80
	 * @param  array   $data
81
	 * @return int
82
	 */
83
	public function updateByVisitor($visitorId, array $data)
84
	{
85
		return $this->getDatabase()
86
			->table($this->getTable())
87
			->where('visitor', $visitorId)
88
			->update($data);
89
	}
90
91
    /**
92
     * @param  int   $visitorId
93
     * @param  array $values
94
     * @return ActiveRow|bool
95
     */
96
	public function updateOrCreate(int $visitorId, array $values)
97
    {
98
        $result = $this->updateByVisitor($visitorId, $values);
99
100
        if(!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
101
            $values['visitor'] = $visitorId;
102
            $result = $this->create($values);
103
        }
104
105
        return $result;
106
    }
107
108
	/**
109
	 * Get meals data by visitor id
110
	 *
111
	 * @param	integer	visitor id
112
	 * @return	array
113
	 */
114
	public function findByVisitorId($visitorId): array
115
	{
116
		$meals = $this->getDatabase()
117
			->table($this->getTable())
118
			->where('visitor', $visitorId)
119
			->limit(1)
120
			->fetch();
121
122
		if(!$meals) {
123
		    $meals = [];
124
        } else {
125
		    $meals = $meals->toArray();
126
        }
127
128
        return $meals;
129
130
	}
131
132
}
133