forked from romansklenar/nette-datagrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
executable file
·109 lines (88 loc) · 2.6 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace mzk\DataGrid;
use Nette;
use Nette\Utils\Html;
/**
* Representation of data grid action.
*
* @author Roman Sklenář
* @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
* @license New BSD License
* @example http://addons.nette.org/datagrid
* @package Nette\Extras\DataGrid
*/
class Action extends Nette\ComponentModel\Container implements IAction
{
/**#@+ special action key */
const WITH_KEY = TRUE;
const WITHOUT_KEY = FALSE;
/**#@-*/
/** @var Nette\Utils\Html action element template */
protected $html;
/** @var string */
static public $ajaxClass = 'datagrid-ajax';
/** @var string */
public $destination;
/** @var bool|string */
public $key;
/** @var Nette\Callback|\Closure */
public $ifDisableCallback;
/**
* Data grid action constructor.
* @note for full ajax support, destination should not change module,
* @note presenter or action and must be ended with exclamation mark (!)
*
* @param $title String
* @param String $destination link
* @param \Nette\Utils\Html $icon
* @param bool $useAjax
* @param bool $key link with argument? (if yes you can specify name of parameter
* otherwise variable mzk\DataGrid\DataGrid::$keyName will be used and must be defined)
* @return \mzk\DataGrid\Action
*/
public function __construct($title, $destination, Html $icon = NULL, $useAjax = FALSE, $key = self::WITH_KEY)
{
parent::__construct();
$this->destination = $destination;
$this->key = $key;
$a = Html::el('a')
->title($title);
if ($useAjax) $a->addClass(self::$ajaxClass);
if ($icon !== NULL && $icon instanceof Nette\Utils\Html) {
$a->addHtml($icon);
} else {
$a->setText($title);
}
$this->html = $a;
}
/**
* Generates action's link. (use before data grid is going to be rendered)
* @param array $args
* @return void
*/
public function generateLink(array $args = NULL)
{
$dataGrid = $this->lookup('mzk\DataGrid\DataGrid', TRUE);
$control = $dataGrid->lookup('Nette\Application\Ui\Control', TRUE);
switch ($this->key) {
case self::WITHOUT_KEY:
$link = $control->link($this->destination);
break;
case self::WITH_KEY:
default:
$key = $this->key == NULL || is_bool($this->key) ? $dataGrid->keyName : $this->key;
$link = $control->link($this->destination, array($key => $args[$dataGrid->keyName]));
break;
}
$this->html->href($link);
}
/********************* interface mzk\DataGrid\IAction *********************/
/**
* Gets action element template.
* @return Nette\Utils\Html
*/
public function getHtml()
{
return $this->html;
}
}