| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright   Copyright (c) 2015 Giant.cz <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author      Pavel Janda <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package     Giant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Ublaboo\DataGrid\Column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Nette\Utils\Html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Ublaboo\DataGrid\DataGrid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Ublaboo\DataGrid\Row; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ColumnLink extends Column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	protected $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	protected $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @var DataGrid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	protected $grid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	protected $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	protected $href; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @param DataGrid $grid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @param string $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param string $href | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	public function __construct(DataGrid $grid, $column, $name, $href, $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		parent::__construct($column, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$this->href   = $href; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->grid   = $grid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->params = $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * Render row item into template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param  Row   $row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	public function render(Row $row) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		/** | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		 * Renderer function may be used | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 		 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 71 |  | View Code Duplication | 		if ($renderer = $this->getRenderer()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 			if (!$renderer->getConditionCallback()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 				return call_user_func_array($renderer->getCallback(), [$row->getItem()]); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 			if (call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 				return call_user_func_array($renderer->getCallback(), [$row->getItem()]); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		$value = parent::render($row); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 			$parent = $this->grid->getParent(); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		} catch (DataGridHasToBeAttachedToPresenterComponentException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 			$parent = $this->grid->getPresenter(); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 		$href = $parent->link($this->href, $this->getItemParams($row)); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		$a = Html::el('a') | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			->href($href) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 			->setText($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 		if ($this->title) { $a->title($this->title); } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 		if ($this->class) { $a->class($this->class); } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 		return $a; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * Set attribute title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * @param string $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	public function setTitle($title) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$this->title = $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * Get attribute title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	public function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		return $this->title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * Set attribute class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @param string $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	public function setClass($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		$this->class = $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * Get attribute class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	public function getClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		return $this->class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * Get item params (E.g. action may be called id => $item->id, name => $item->name, ...) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * @param  Row   $row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 147 |  | View Code Duplication | 	protected function getItemParams(Row $row) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$return = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		foreach ($this->params as $param_name => $param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.