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 */
23 class Href extends Action
24 {
25 /** @var string first param for method $presenter->link() */
26 protected $destination;
27
28 /** @var array second param for method $presenter->link() */
29 protected $arguments = array();
30
31 /** @var callback for custom href attribute creating */
32 protected $customHref;
33
34 /**
35 * @param \Grido\Grid $grid
36 * @param string $name
37 * @param string $label
38 * @param string $destination - first param for method $presenter->link()
39 * @param array $args - second param for method $presenter->link()
40 */
41 public function __construct($grid, $name, $label, $destination = NULL, array $args = NULL)
42 {
43 parent::__construct($grid, $name, $label);
44
45 $this->destination = $destination;
46 $this->arguments = $args;
47 }
48
49 /**
50 * Sets callback for custom link creating.
51 * @param callback $callback
52 * @return Href
53 */
54 public function setCustomHref($callback)
55 {
56 $this->customHref = $callback;
57 return $this;
58 }
59
60 /**********************************************************************************************/
61
62 /**
63 * @param mixed $row
64 * @return \Nette\Utils\Html
65 * @internal
66 */
67 public function getElement($row)
68 {
69 $element = parent::getElement($row);
70 $href = '';
71
72 if ($this->customHref) {
73 $href = callback($this->customHref)->invokeArgs(array($row));
74 } else {
75 $primaryKey = $this->getPrimaryKey();
76 $primaryValue = $this->propertyAccessor->getProperty($row, $primaryKey);
77
78 $this->arguments[$primaryKey] = $primaryValue;
79 $href = $this->presenter->link($this->getDestination(), $this->arguments);
80 }
81
82 $element->href($href);
83
84 return $element;
85 }
86
87 /**
88 * @return string
89 * @internal
90 */
91 public function getDestination()
92 {
93 if ($this->destination === NULL) {
94 $this->destination = $this->getName();
95 }
96
97 return $this->destination;
98 }
99
100 /**
101 * @return array
102 */
103 public function getArguments()
104 {
105 return $this->arguments;
106 }
107 }
108