1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ViewComponents\Grids\Component; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use ViewComponents\Grids\Grid; |
7
|
|
|
use ViewComponents\ViewComponents\Base\Compound\PartInterface; |
8
|
|
|
use ViewComponents\ViewComponents\Base\Compound\PartTrait; |
9
|
|
|
use ViewComponents\ViewComponents\Base\DataViewComponentInterface; |
10
|
|
|
use ViewComponents\ViewComponents\Base\Html\TagInterface; |
11
|
|
|
use ViewComponents\ViewComponents\Component\Compound; |
12
|
|
|
use ViewComponents\ViewComponents\Component\DataView; |
13
|
|
|
use ViewComponents\ViewComponents\Resource\ResourceManager; |
14
|
|
|
use ViewComponents\ViewComponents\Service\Services; |
15
|
|
|
|
16
|
|
|
class DetailsRow extends SolidRow implements PartInterface |
17
|
|
|
{ |
18
|
|
|
use PartTrait { |
19
|
|
|
PartTrait::attachToCompound as private attachToCompoundInternal; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
const ID = 'details_row'; |
23
|
|
|
|
24
|
|
|
protected $view; |
25
|
|
|
/** @var ResourceManager */ |
26
|
|
|
private $resourceManager; |
27
|
|
|
private $jquery; |
28
|
|
|
|
29
|
|
|
public function __construct(DataViewComponentInterface $view, ResourceManager $resourceManager = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
$this->getRowTag() |
33
|
|
|
->setAttribute('style', 'display:none;') |
34
|
|
|
->setAttribute('data-details-row', '1'); |
35
|
|
|
$this->addChild($this->view = $view); |
36
|
|
|
$this->setDestinationParentId(Grid::COLLECTION_VIEW_ID); |
37
|
|
|
$this->setId('details_row'); |
38
|
|
|
$this->resourceManager = $resourceManager ?: Services::resourceManager(); |
39
|
|
|
$this->jquery = $this->resourceManager->js('jquery'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function render() |
43
|
|
|
{ |
44
|
|
|
$this->view->setData($this->getGrid()->getCurrentRow()); |
|
|
|
|
45
|
|
|
return parent::render(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return null|Grid |
50
|
|
|
*/ |
51
|
|
|
protected function getGrid() |
52
|
|
|
{ |
53
|
|
|
return $this->root; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function attachToCompound(Compound $root, $prepend = false) |
57
|
|
|
{ |
58
|
|
|
$isAlreadyAttached = $this->root !== null; |
59
|
|
|
$this->attachToCompoundInternal($root, $prepend); |
|
|
|
|
60
|
|
|
if ($isAlreadyAttached) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
$tr = $this->getGrid()->getRecordView(); |
|
|
|
|
64
|
|
|
if (!$tr instanceof TagInterface) { |
65
|
|
|
throw new RuntimeException( |
66
|
|
|
"Details row works only with record_view components implementing TagInterface" |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
$tr->setAttribute('data-row-with-details', 1); |
70
|
|
|
$this->getGrid()->children() |
71
|
|
|
->add($this->jquery, 1) |
72
|
|
|
->add($this->getScript());; |
73
|
|
|
// fix zebra styled tables |
74
|
|
|
$this->parent()->addChild(new DataView('<tr style="display: none"></tr>')); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getScript() |
79
|
|
|
{ |
80
|
|
|
$source = $this->getScriptSource(); |
81
|
|
|
return new DataView("<script>jQuery(function(){ $source });</script>"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getScriptSource() |
85
|
|
|
{ |
86
|
|
|
return ' |
87
|
|
|
jQuery(\'tr[data-row-with-details="1"]\').click(function() { |
88
|
|
|
jQuery(this).next().toggle(\'slow\'); |
89
|
|
|
}); |
90
|
|
|
'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: