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 * Event action.
16 *
17 * @package Grido
18 * @subpackage Components\Actions
19 * @author Josef Kříž <pepakriz@gmail.com>
20 * @author Petr Bugyík
21 *
22 */
23 class Event extends Action
24 {
25 /** @var array callback */
26 public $onClick = array();
27
28 /**
29 * @param \Grido\Grid $grid
30 * @param string $name
31 * @param string $label
32 * @param callback $onClick
33 */
34 public function __construct($grid, $name, $label, $onClick = NULL)
35 {
36 parent::__construct($grid, $name, $label);
37
38 if ($onClick !== NULL) {
39 $this->onClick[] = $onClick;
40 }
41 }
42
43 /**
44 * Sets on-click handler.
45 * @param callback $onClick
46 * @return \Grido\Components\Actions\Event
47 */
48 public function setOnClick($onClick)
49 {
50 $this->onClick = $onClick;
51 return $this;
52 }
53
54 /**********************************************************************************************/
55
56 /**
57 * @param mixed $row
58 * @return \Nette\Utils\Html
59 * @internal
60 */
61 public function getElement($row)
62 {
63 $element = parent::getElement($row);
64
65 $primaryValue = $this->propertyAccessor->getProperty($row, $this->getPrimaryKey());
66 $element->href($this->link('click!', $primaryValue));
67
68 return $element;
69 }
70
71 /**********************************************************************************************/
72
73 /**
74 * @param $id
75 * @internal
76 */
77 public function handleClick($id)
78 {
79 $this->onClick($id, $this);
80 }
81 }
82