Completed
Pull Request — devel (#146)
by Litera
24:35 queued 19:02
created

BasePresenter::unauthorized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Presenters;
4
5
use App\Components\INavbarRightControlFactory;
6
use App\Components\NavbarRightControl;
7
use Nette,
8
	App\Model;
9
use Nette\Utils\ArrayHash;
10
use App\Models\SunlightModel;
11
use Nette\Caching\Cache;
12
use App\Traits\Loggable;
13
14
/**
15
 * Base presenter for all application presenters.
16
 */
17
abstract class BasePresenter extends Nette\Application\UI\Presenter
18
{
19
20
	use Loggable;
21
22
	const FLASH_TYPE_OK    = 'success';
23
	const FLASH_TYPE_ERROR = 'error';
24
	const ROLE_ADMIN = 'admin';
25
	const ROLE_GUEST = 'guest';
26
27
	/**
28
	 * @var string
29
	 */
30
	public $backlink = '';
31
32
	/** @var Model */
33
	protected $model;
34
35
	/** @var Nette\DI\Container */
36
	protected $container;
37
38
	/** @var Latte */
39
	protected $latte;
40
41
	/** @var Router */
42
	protected $router;
43
44
	/** @var string */
45
	protected $action;
46
47
	/** @var Nette\Http\Request */
48
	protected $request;
49
50
	/** @var integer */
51
	protected $meetingId;
52
53
	/**
54
	 * @var INavbarRightControlFactory
55
	 */
56
	protected $navbarRightControlFactory;
57
58
	/**
59
	 * @param  INavbarRightControlFactory $factory
60
	 * @return BasePresenter
61
	 */
62
	public function injectNavbarRightControlFactory(INavbarRightControlFactory $factory): self
63
	{
64
		$this->navbarRightControlFactory = $factory;
65
66
		return $this;
67
	}
68
69
	/**
70
	 * Startup
71
	 */
72
	protected function startup()
73
	{
74
		parent::startup();
75
76
		$meetingId = $this->getHttpRequest()->getQuery('mid', '');
77
78
		$backlink = $this->getHttpRequest()->getQuery('backlink');
79
		if(!empty($backlink)) {
80
			$this->setBacklink($backlink);
81
		}
82
83
		if($meetingId){
84
			$_SESSION['meetingID'] = $meetingId;
85
		} elseif(!isset($_SESSION['meetingID'])) {
86
			$meeting = $this->getContainer()->getService('meeting');
87
			$_SESSION['meetingID'] = $meeting->getLastMeetingId();
88
		}
89
90
		$this->setMeetingId($_SESSION['meetingID']);
91
92
		$model = $this->getModel();
93
		if($model) {
94
			$model->setMeetingId($_SESSION['meetingID']);
95
		}
96
97
		$this->debugMode = $this->getContainer()->getParameters()['debugMode'];
98
	}
99
100
101
	/**
102
	 * Before render
103
	 * Prepare variables for template
104
	 */
105
	public function beforeRender()
106
	{
107
		parent::beforeRender();
108
109
		$template = $this->getTemplate();
110
		$meeting = $this->getContainer()->getService('meeting');
111
112
		$template->baseDir = ROOT_DIR;
0 ignored issues
show
Bug introduced by
Accessing baseDir on the interface Nette\Application\UI\ITemplate 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...
113
		$template->wwwDir = HTTP_DIR;
0 ignored issues
show
Bug introduced by
Accessing wwwDir on the interface Nette\Application\UI\ITemplate 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...
114
		$template->cssDir = CSS_DIR;
0 ignored issues
show
Bug introduced by
Accessing cssDir on the interface Nette\Application\UI\ITemplate 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...
115
		$template->jsDir = JS_DIR;
0 ignored issues
show
Bug introduced by
Accessing jsDir on the interface Nette\Application\UI\ITemplate 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...
116
		$template->imgDir = IMG_DIR;
0 ignored issues
show
Bug introduced by
Accessing imgDir on the interface Nette\Application\UI\ITemplate 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...
117
		$template->catDir = CAT_DIR;
0 ignored issues
show
Bug introduced by
Accessing catDir on the interface Nette\Application\UI\ITemplate 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...
118
		$template->blockDir = BLOCK_DIR;
0 ignored issues
show
Bug introduced by
Accessing blockDir on the interface Nette\Application\UI\ITemplate 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...
119
		$template->progDir = PROG_DIR;
0 ignored issues
show
Bug introduced by
Accessing progDir on the interface Nette\Application\UI\ITemplate 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...
120
		$template->visitDir = VISIT_DIR;
0 ignored issues
show
Bug introduced by
Accessing visitDir on the interface Nette\Application\UI\ITemplate 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...
121
		$template->expDir = EXP_DIR;
0 ignored issues
show
Bug introduced by
Accessing expDir on the interface Nette\Application\UI\ITemplate 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...
122
		$template->meetDir = MEET_DIR;
0 ignored issues
show
Bug introduced by
Accessing meetDir on the interface Nette\Application\UI\ITemplate 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...
123
124
		$template->categories = $this->remember('categories:all', 10, function () {
0 ignored issues
show
Bug introduced by
Accessing categories on the interface Nette\Application\UI\ITemplate 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...
125
			return $this->getContainer()->getService('category')->all();
126
		});
127
/*
128
		if(isset($_SESSION[SESSION_PREFIX.'user'])) {
129
			$template->user = $this->getSunlight()->findUser($_SESSION[SESSION_PREFIX.'user']);
130
		}
131
*/
132
		$template->meeting = $meeting->getPlaceAndYear($_SESSION['meetingID']);
0 ignored issues
show
Bug introduced by
Accessing meeting on the interface Nette\Application\UI\ITemplate 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...
133
		$template->menuItems = $meeting->getMenuItems();
0 ignored issues
show
Bug introduced by
Accessing menuItems on the interface Nette\Application\UI\ITemplate 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...
134
		$template->meeting_heading	= $meeting->getRegHeading();
0 ignored issues
show
Bug introduced by
Accessing meeting_heading on the interface Nette\Application\UI\ITemplate 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...
135
		$template->meetingId = $this->getMeetingId();
0 ignored issues
show
Bug introduced by
Accessing meetingId on the interface Nette\Application\UI\ITemplate 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...
136
		//$template->backlinkUrl = $this->getBacklinkUrl();
137
		$template->backlink = $this->getBacklink();
0 ignored issues
show
Bug introduced by
Accessing backlink on the interface Nette\Application\UI\ITemplate 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...
138
139
		//$this->template->production = $this->context->parameters['environment'] === 'production' ? 1 : 0;
140
		//$this->template->version = $this->context->parameters['site']['version'];
141
	}
142
143
	/**
144
	 * template
145
	 * @var string
146
	 */
147
	protected $template = 'listing';
148
149
	/**
150
	 * template directory
151
	 * @var string
152
	 */
153
	protected $templateDir = '';
154
155
	/**
156
	 * category ID
157
	 * @var integer
158
	 */
159
	protected $itemId = NULL;
160
161
	/**
162
	 * action what to do
163
	 * @var string
164
	 */
165
	protected $cms = '';
166
167
	/**
168
	 * page where to return
169
	 * @var string
170
	 */
171
	protected $page = '';
172
173
	/**
174
	 * heading tetxt
175
	 * @var string
176
	 */
177
	protected $heading = '';
178
179
	/**
180
	 * action what to do next
181
	 * @var string
182
	 */
183
	protected $todo = '';
184
185
	/**
186
	 * data
187
	 * @var array
188
	 */
189
	protected $data = array();
190
191
	/**
192
	 * error handler
193
	 * @var string
194
	 */
195
	protected $error = '';
196
197
	/**
198
	 * database connection
199
	 * @var string
200
	 */
201
	protected $database = NULL;
202
203
	/**
204
	 * debug mode
205
	 * @var boolean
206
	 */
207
	protected $debugMode = false;
208
209
	protected $sunlight;
210
211
	protected function parseTutorEmail($item)
212
	{
213
		$mails = explode(',', $item->email);
214
		$names = explode(',', $item->tutor);
215
216
		$i = 0;
217
		$recipient = [];
218
		foreach ($mails as $mail) {
219
			$mail = trim($mail);
220
			$name = trim($names[$i]);
221
222
			$recipient['email'] = $mail;
223
			$recipient['name'] = ($name) ? $name : '';
224
			$recipients[] = ArrayHash::from($recipient);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recipients was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recipients = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
225
		}
226
227
		return $recipients;
0 ignored issues
show
Bug introduced by
The variable $recipients does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
228
	}
229
230
	// zaheshovane udaje, aby se nedali jen tak ziskat data z databaze
231
	protected function formKeyHash($id, $meetingId)
232
	{
233
		return ((int)$id . $meetingId) * 116 + 39147;
234
	}
235
236
	/**
237
	 * @return Model
238
	 */
239
	protected function getModel()
240
	{
241
		return $this->model;
242
	}
243
244
	/**
245
	 * @param  Model $model
246
	 * @return $this
247
	 */
248
	protected function setModel($model)
249
	{
250
		$this->model = $model;
251
		return $this;
252
	}
253
254
	/**
255
	 * @return Container
256
	 */
257
	protected function getContainer()
258
	{
259
		return $this->context;
0 ignored issues
show
Documentation introduced by
The property $context is declared private in Nette\Application\UI\Presenter. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
260
	}
261
262
	/**
263
	 * @param  Container $container
264
	 * @return $this
265
	 */
266
	protected function setContainer($container)
267
	{
268
		$this->context = $container;
0 ignored issues
show
Documentation introduced by
The property $context is declared private in Nette\Application\UI\Presenter. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
It seems like $container of type object<App\Presenters\Container> is incompatible with the declared type object<Nette\DI\Container> of property $context.

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...
269
		return $this;
270
	}
271
272
	/**
273
	 * @return Router
274
	 */
275
	protected function getRouter()
276
	{
277
		return $this->router;
278
	}
279
280
	/**
281
	 * @param  Router $router
282
	 * @return $this
283
	 */
284
	protected function setRouter($router)
285
	{
286
		$this->router = $router;
287
		return $this;
288
	}
289
290
	/**
291
	 * @return Latte
292
	 */
293
	protected function getLatte()
294
	{
295
		return $this->latte;
296
	}
297
298
	/**
299
	 * @param  Latte $latte
300
	 * @return $this
301
	 */
302
	protected function setLatte($latte)
303
	{
304
		$this->latte = $latte;
305
		return $this;
306
	}
307
308
	/**
309
	 * @return string
310
	 */
311
	public function getAction($fullyQualified = false)
312
	{
313
		return $this->action;
314
	}
315
316
	/**
317
	 * @param  string $action
318
	 * @return $this
319
	 */
320
	public function setAction($action)
321
	{
322
		$this->action = $action;
323
		return $this;
324
	}
325
326
	/**
327
	 * @return SunlightModel
328
	 */
329
	public function getSunlight()
330
	{
331
		if(empty($this->sunlight)) {
332
			$this->setSunlight($this->getContainer()->getService('sunlight'));
333
		}
334
335
		return $this->sunlight;
336
	}
337
338
	/**
339
	 * @param  SunlightModel $sunlight
340
	 * @return $this
341
	 */
342
	public function setSunlight(SunlightModel $sunlight)
343
	{
344
		$this->sunlight = $sunlight;
345
		return $this;
346
	}
347
348
	/**
349
	 * @return integer
350
	 */
351
	protected function getMeetingId()
352
	{
353
		return $this->meetingId;
354
	}
355
356
	/**
357
	 * @param  integer  $meetingId
358
	 * @return $this
359
	 */
360
	protected function setMeetingId($meetingId)
361
	{
362
		$this->meetingId = $meetingId;
363
		return $this;
364
	}
365
366
	/**
367
	 * @param  string $guid
368
	 * @param  array  $data
369
	 * @return ActiveRow
370
	 */
371
	protected function updateByGuid($guid, array $data)
372
	{
373
		return $this->getModel()->updateBy('guid', $guid, $data);
374
	}
375
376
	public function remember($key, $minutes, \Closure $callback)
377
	{
378
		// If the item exists in the cache we will just return this immediately
379
		// otherwise we will execute the given Closure and cache the result
380
		// of that execution for the given number of minutes in storage.
381
		if (! is_null($data = $this->getCache()->load($key))) {
382
			$items = [];
383
384
			foreach($data as $item) {
385
				$object = new \stdClass();
386
				foreach ($item as $key => $value) {
387
					$object->$key = $value;
388
				}
389
				$items[] = $object;
390
			}
391
392
			return $items;
393
		}
394
395
		$data = $callback();
396
		$serialized = [];
397
		foreach ($data as $item) {
398
			$serialized[] = $item->toArray();
399
		}
400
401
		$this->getCache()->save(
402
			$key,
403
			$serialized,
404
			[
405
				Cache::EXPIRE => $minutes . ' minutes',
406
			]
407
		);
408
409
		return $data;
410
	}
411
412
	/**
413
	 * @return NavbarRightControl
414
	 */
415
	protected function createComponentNavbarRight(): NavbarRightControl
416
	{
417
		return $this->navbarRightControlFactory->create();
418
	}
419
420
	protected function getCache()
421
	{
422
		return $this->getContainer()->getService('cache');
423
	}
424
425
	/**
426
	 * @return string
427
	 */
428
	protected function getDebugMode()
429
	{
430
		return $this->debugMode;
431
	}
432
433
	/**
434
	 * @return string
435
	 */
436
	protected function getBacklink()
437
	{
438
		return $this->backlink;
439
	}
440
441
	/**
442
	 * @param  string $backlink
443
	 * @return $this
444
	 */
445
	protected function setBacklink($backlink)
446
	{
447
		$this->backlink = $backlink;
448
449
		return $this;
450
	}
451
452
	/**
453
	 * @return string
454
	 */
455
	protected function getBacklinkUrl()
456
	{
457
		if($this->getBacklink()) {
458
			return $this->link(
459
				$this->getBacklink(),
460
				[
461
					'backlink' => null
462
				]
463
			);
464
		}
465
	}
466
467
	/**
468
	 * @param  Nette\Utils\ArrayHash $array
469
	 * @return self
470
	 */
471
	protected function setBacklinkFromArray(ArrayHash $array): self
472
	{
473
		if(array_key_exists('backlink', $array) && !empty($array['backlink'])) {
474
			$this->setBacklink($array['backlink']);
475
			unset($array['backlink']);
476
		}
477
478
		return $this;
479
	}
480
481
	/**
482
	 * Flashes success message
483
	 *
484
	 * @param  string $message Message
485
	 */
486
	protected function flashSuccess(string $message = '')
487
	{
488
		$this->flashMessage($message, self::FLASH_TYPE_OK);
489
	}
490
491
	/**
492
	 * Flashes failure message
493
	 *
494
	 * @param  string $message Message
495
	 */
496
	protected function flashFailure(string $message = '')
497
	{
498
		$this->flashMessage($message, self::FLASH_TYPE_ERROR);
499
	}
500
501
	/**
502
	 * Flashes error message
503
	 *
504
	 * @param  string $message Message
505
	 */
506
	protected function flashError(string $message = '')
507
	{
508
		$this->flashMessage($message, self::FLASH_TYPE_ERROR);
509
	}
510
511
	/**
512
	 * @throws Nette\Application\AbortException
513
	 */
514
	protected function unauthorized()
515
	{
516
		$message = 'Nemáte oprávnění pro tuto stránku. Prosím, přihlašte se nebo požádejte administrátora.';
517
		$this->flashFailure($message);
518
		$this->error($message, Nette\Http\IResponse::S403_FORBIDDEN );
519
	}
520
521
	/**
522
	 * @throws Nette\Application\AbortException
523
	 */
524
	protected function allowAdminAccessOnly()
525
	{
526
		$user = $this->getUser();
527
		if(!$user->isInRole(self::ROLE_ADMIN)) {
528
			$this->unauthorized();
529
		}
530
	}
531
532
}
533