|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WebApp\Component\Data; |
| 4 | + |
| 5 | +/** |
| 6 | + * Usage (with LinkBuilder or LinkBuilderService): |
| 7 | + * $menu = new ContextMenu($app, $dataObject, $linkBuilderService); |
| 8 | + * $group1 = $menu->createGroup(); |
| 9 | + * $item1 = $group1->createItem('View this object')->setAction('view'); |
| 10 | + * $item2 = $group1->createItem('Edit this object')->setAction('edit'); |
| 11 | + * |
| 12 | + * Usage (without LinkBuilder or LinkBuilderService): |
| 13 | + * $menu = new ContextMenu($app, $dataObject); |
| 14 | + * $group1 = $menu->createGroup(); |
| 15 | + * $item1 = $group1->createItem('View this object')->setLink('/path/to/view.html'); |
| 16 | + * $item2 = $group1->createItem('Edit this object')->setLink('/path/to/edit.html'); |
| 17 | + * |
| 18 | + * You can also give a label to the group: |
| 19 | + * $group1 = $menu->createGroup('Actions'); |
| 20 | + * |
| 21 | + * It is recommended to subclass ContextMenu and populate the menu after calling the parent |
| 22 | + * constructor. |
| 23 | + * |
| 24 | + * Use the context menu with DropDown (the context menu must be complete at this time): |
| 25 | + * $dropDown->setMenu($menu); |
| 26 | + */ |
| 27 | +class ContextMenu extends Div { |
| 28 | + |
| 29 | + protected $app; |
| 30 | + protected $subject; |
| 31 | + protected $groups; |
| 32 | + protected $linkBuilder; |
| 33 | + |
| 34 | + public function __construct($parent, $app, $subject, $linkBuilder = NULL) { |
| 35 | + parent::__construct($parent); |
| 36 | + $this->app = $app; |
| 37 | + $this->subject = $subject; |
| 38 | + $this->groups = array(); |
| 39 | + if ($linkBuilder != NULL) { |
| 40 | + if (is_a($linkBuilder, 'WebApp\\Service\\LinkBuilderService')) { |
| 41 | + $this->linkBuilder = $linkBuilder->getBuilder($subject); |
| 42 | + } else { |
| 43 | + $this->linkBuilder = $linkBuilder; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function createGroup($label = NULL) { |
| 49 | + $rc = new ContextMenuGroup($this, $this->app, $this->subject, $label); |
| 50 | + $rc->setLinkBuilder($this->linkBuilder); |
| 51 | + $this->groups[] = $rc; |
| 52 | + return $rc; |
| 53 | + } |
| 54 | + |
| 55 | + public function getMenu() { |
| 56 | + return $this->groups; |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments