Skip to content

Commit

Permalink
Merge branch 'CONF-663-menu-user-rights-rate' into 'master'
Browse files Browse the repository at this point in the history
Conf 663 menu user rights rate



See merge request !1
  • Loading branch information
Gustav Wellner Bou committed Jun 6, 2016
2 parents 998baa9 + 04ab800 commit c60caef
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Factory/FlatMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function buildOptions(array $options) {
public function buildItem(ItemInterface $item, array $options) {
// Set extra parameters

$params = ['icon', 'rate_rights', 'headline', 'hide_navbar', 'badge'];
$params = ['icon', 'rate_rights', 'headline', 'hide_navbar', 'badge', 'rate_alt_uri', 'user_rights'];

foreach($params as $param) {
if(isset($options[$param])) {
Expand Down
56 changes: 55 additions & 1 deletion src/Renderer/FlatMenuRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ protected function renderChildren(ItemInterface $item, array $options) {
* @return string
*/
protected function renderItem(ItemInterface $item, array $options) {
//apply user rights policy to item
$this->applyUserRights($item, $options);

// if we don't have access or this item is marked to not be shown
if (!$item->isDisplayed()) {
return '';
Expand Down Expand Up @@ -216,6 +219,7 @@ protected function renderItem(ItemInterface $item, array $options) {
//$html .= $this->format($item->getUri() ? $item->renderLink() : $item->renderLabel(), 'link', $item->getLevel());
$html .= $this->renderLink($item, $options);


// renders the embedded ul
$childrenClass = (array)$item->getChildrenAttribute('class');
$childrenClass[] = 'menu_level_' . $item->getLevel();
Expand All @@ -225,12 +229,60 @@ protected function renderItem(ItemInterface $item, array $options) {

$html .= $this->renderList($item, $childrenAttributes, $options);


// closing li tag
$html .= $this->format('</li>', 'li', $item->getLevel(), $options);

return $html;
}

/**
* Apply user rights to an item
* so we can hide and display items differently based on user rights & photographer rates
* Used in renderItem
*
* @author navihtot
* @param ItemInterface $item
* @param array $options The options to render the item
*/
protected function applyUserRights(ItemInterface &$item, array $options) {
$user_rights=$item->getExtra('ext:user_rights');
if (!empty($user_rights)) {
if (empty(array_intersect($user_rights, $options['user_rights']))) {
$item->setDisplay(false);
}
}

$rate_rights=$item->getExtra('ext:rate_rights');
$display_normal = false;
if (!empty($rate_rights)) {
//if one of item rate rights is in user rate right, then display item normaly
foreach ($rate_rights as $rate_right) {
if (isset($options['user_rate'][$rate_right]) && $options['user_rate'][$rate_right] == 1) {
$display_normal = true;
break;
}
}

//if user rate is in required rates for item
if (!$display_normal) {
$alt_uri = $item->getExtra('ext:rate_alt_uri');
if (!empty($alt_uri)) {
$item->setUri($alt_uri);
}
else if (!empty($this->defaultOptions['default_rate_alt_uri'])) {
$item->setUri($this->defaultOptions['default_rate_alt_uri']);
}
//set item class for css style if its unavailable for rate
$item_class = $item->getAttribute('class');
if ($item_class != '') $item_class .= ' ';
$item_class .= 'rate-unavailable';
$item->setAttribute('class',$item_class);
$item->setExtra('ext:rate-unavailable', true);
}
}
}

/**
* Renders the link in a a tag with link attributes or
* the label in a span tag with label attributes
Expand All @@ -250,7 +302,6 @@ protected function renderLink(ItemInterface $item, array $options = array()) {
} else {
$text = $this->renderSpanElement($item, $options);
}

return $this->format($text, 'link', $item->getLevel(), $options);
}

Expand Down Expand Up @@ -310,6 +361,9 @@ protected function renderLabel(ItemInterface $item, array $options) {
$label .= $this->escape($item->getLabel());
}

if (!empty($extendedOptions['rate-unavailable'])) {
$label .= '<i class="'.$this->defaultOptions['rate_unavailable_icon'].' rate-unavailable-icon"></i>';
}

return $label;
}
Expand Down

0 comments on commit c60caef

Please sign in to comment.