1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Grido\Components\Actions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Href action. |
16
|
|
|
* |
17
|
|
|
* @package Grido |
18
|
|
|
* @subpackage Components\Actions |
19
|
|
|
* @author Petr Bugyík |
20
|
|
|
* |
21
|
|
|
* @property-write array $customHref |
22
|
|
|
* @property-read string $destination |
23
|
|
|
* @property-read array $arguments |
24
|
|
|
*/ |
25
|
|
|
class Href extends Action |
26
|
1 |
|
{ |
27
|
|
|
/** @var string first param for method $presenter->link() */ |
28
|
|
|
protected $destination; |
29
|
|
|
|
30
|
|
|
/** @var array second param for method $presenter->link() */ |
31
|
|
|
protected $arguments = []; |
32
|
|
|
|
33
|
|
|
/** @var callback for custom href attribute creating */ |
34
|
|
|
protected $customHref; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \Grido\Grid $grid |
38
|
|
|
* @param string $name |
39
|
|
|
* @param string $label |
40
|
|
|
* @param string $destination - first param for method $presenter->link() |
41
|
|
|
* @param array $arguments - second param for method $presenter->link() |
42
|
|
|
*/ |
43
|
|
|
public function __construct($grid, $name, $label, $destination = NULL, array $arguments = []) |
44
|
|
|
{ |
45
|
1 |
|
parent::__construct($grid, $name, $label); |
46
|
|
|
|
47
|
1 |
|
$this->destination = $destination; |
48
|
1 |
|
$this->arguments = $arguments; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets callback for custom link creating. |
53
|
|
|
* @param callback $callback |
54
|
|
|
* @return Href |
55
|
|
|
*/ |
56
|
|
|
public function setCustomHref($callback) |
57
|
|
|
{ |
58
|
1 |
|
$this->customHref = $callback; |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/**********************************************************************************************/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $row |
66
|
|
|
* @return \Nette\Utils\Html |
67
|
|
|
* @internal |
68
|
|
|
*/ |
69
|
|
|
public function getElement($row) |
70
|
|
|
{ |
71
|
1 |
|
$element = parent::getElement($row); |
72
|
|
|
|
73
|
1 |
|
if ($this->customHref) { |
74
|
1 |
|
$href = call_user_func_array($this->customHref, [$row]); |
75
|
1 |
|
} else { |
76
|
1 |
|
$primaryKey = $this->getPrimaryKey(); |
77
|
1 |
|
$primaryValue = $this->grid->getProperty($row, $primaryKey); |
78
|
|
|
|
79
|
1 |
|
$this->arguments[$primaryKey] = $primaryValue; |
80
|
1 |
|
$href = $this->presenter->link($this->getDestination(), $this->arguments); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$element->href($href); |
84
|
|
|
|
85
|
1 |
|
return $element; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
* @internal |
91
|
|
|
*/ |
92
|
|
|
public function getDestination() |
93
|
|
|
{ |
94
|
1 |
|
if ($this->destination === NULL) { |
95
|
1 |
|
$this->destination = $this->getName(); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
return $this->destination; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
* @internal |
104
|
|
|
*/ |
105
|
|
|
public function getArguments() |
106
|
|
|
{ |
107
|
1 |
|
return $this->arguments; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|