This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAction.php
105 lines (83 loc) · 2.61 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
<?php
namespace DataGrid;
use Nette;
/**
* 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\Component implements IAction
{
/**#@+ special action key */
const WITH_KEY = TRUE;
const WITHOUT_KEY = FALSE;
/**#@-*/
/** @var Nette\Web\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 string textual title
* @param string textual link destination
* @param Nette\Web\Html element which is added to a generated link
* @param bool use ajax? (add class self::$ajaxClass into generated link)
* @param mixed generate link with argument? (if yes you can specify name of parameter
* otherwise variable DataGrid\DataGrid::$keyName will be used and must be defined)
* @return void
*/
public function __construct($title, $destination, Nette\Web\Html $icon = NULL, $useAjax = FALSE, $key = self::WITH_KEY)
{
parent::__construct();
$this->destination = $destination;
$this->key = $key;
$a = Nette\Web\Html::el('a')->title($title);
if ($useAjax) $a->addClass(self::$ajaxClass);
if ($icon !== NULL && $icon instanceof Nette\Web\Html) {
$a->add($icon);
} else {
$a->setText($title);
}
$this->html = $a;
}
/**
* Generates action's link. (use before data grid is going to be rendered)
* @return void
*/
public function generateLink(array $args = NULL)
{
$dataGrid = $this->lookup('DataGrid\DataGrid', TRUE);
$control = $dataGrid->lookup('Nette\Application\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 DataGrid\IAction *********************/
/**
* Gets action element template.
* @return Nette\Web\Html
*/
public function getHtml()
{
return $this->html;
}
}