Shorthand if in PHP Array - php

Shorthand if in a PHP array won't work, even if I surround the true in braces.
The working code:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
[
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
]);
The not working code:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
Yii::$app->user->isGuest ?
(['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]) :
[
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
]);
I understand what the problem is, but can't figure out a workarround for it.
Any ideas?

You have to have have each part of the code evaluate to something legal - this is not legal:
Yii::$app->user->isGuest ?
(['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']])
because it would evaluate to
['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]
that would only be valid if their were another set of [] around the whole thing - for example, you could not do:
$x = ['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']];
but you could do
$x = [
['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]
];
Your code needs to be more like
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => Yii::$app->user->isGuest ?
[
['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]
] :
[
[
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
]
]
]);

This line looks wrong:
(['label' => 'Registrieren', 'url' => ['/site/register']]
You have one opening brace '(' but two closing ']' braces, you probably want to remove one of those

Related

Yii2 - create gridview customizing "model/style" for all gridview and call the style

I have customized my gridview in Yii2 to show columns, headers, and pager in a certain way
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'filterSelector' => 'select[name="per-page"]',
'tableOptions'=> ['class'=>'table datatable-header-footer datatable-header-footer'],
'showFooter' => true,
'layout'=>"{items}\n\n{summary}\n\n<div class='text-right'>{pager}</div>\n",
//'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}",
'summary' => " <br/> Affichage de {begin} à {end} des {totalCount} lignes <br/> <br/>",
'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ' - '],
'pager' => [
'nextPageLabel' => '→',
'prevPageLabel' => '←',
'firstPageLabel' => true,
'maxButtonCount' => 5,
'lastPageLabel' => true
],
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
//'id', ...
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:10%;'],
'header'=>'Actions',
'template' => '{all}',
'buttons' => [
'all' => function ($url, $model, $key) {
return ButtonDropdown::widget([
'encodeLabel' => false,
'label' => 'Choisir',
'dropdown' => [
'encodeLabels' => false,
'items' => [
[
'label' => \Yii::t('yii', '<i class="icon-search4"></i> Details'),
'url' => ['view', 'id' => $key],
],
[
'label' => \Yii::t('yii', '<i class="icon-pencil5"></i> Modifier'),
'url' => ['update', 'id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list"></i> Annonces'),
'url' => ['annonces', 'agence_id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list2"></i> Agents'),
'url' => ['professionnels', 'agence_id' =>$key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-bin"></i> Supprimer'),
'linkOptions' => [
'data' => [
'method' => 'post',
'confirm' => \Yii::t('yii', 'Are you sure you want to delete this item?'),
],
],
'url' => ['delete', 'id' => $key],
'visible' => true, // same as above
],
],
'options' => [
'class' => 'dropdown-menu-right', // right dropdown
],
],
'options' => [
'class' => 'btn-default',
'style' => 'padding-left: 5px; padding-right: 5px;', // btn-success, btn-info, et cetera
],
'split' => true,
]);
Im wondering if there is a way to create a gridview "style" so I can call and apply this style to every gridview in the project !
Yes, extend GridView class with your own, set all properties, override anything you need and use your class instead of default GridView.

Visibility in Yii2

I'm trying to create a page with a menu which has a different visibility for different users. I have 3 users: Users, Moderators and admins. Could somebody help me with this issue. Here is my code:
echo Menu::widget([
'items' => [
['label' => 'Acties', 'visible' => Yii::$app->user->getIdentity('user')],
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file', 'visible' => Yii::$app->user->getIdentity('moderator')],
['label' => 'Beheer afbeeldingen', 'url' => ['admin'], 'icon' => 'list-alt', 'visible' => Yii::$app->user->getIdentity('admin')],
],
]);
visible option is boolean. Defaults to true.
show this Menu widget
also show the getIdentity()
try below code
echo Menu::widget([
'items' => [
['label' => 'Acties', 'visible' => Yii::$app->user->getIdentity('user') ? true : false],
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file', 'visible' => Yii::$app->user->getIdentity('moderator') ? true : false],
['label' => 'Beheer afbeeldingen', 'url' => ['admin'], 'icon' => 'list-alt', 'visible' => Yii::$app->user->getIdentity('admin') ? true : false],
],
]);
You try to right if condition example:
if(Yii::$app->user->getIdentity('moderator')){
echo Menu::widget([
'items' => [
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file'],
],
]);
}

As in Yii 2.0 (Menu :: Widget ) use the span?

When I use the following code, the end result is " TEST " and should be "TEST" in the tags .
echo Menu::widget([
'options' => ['id' => 'navigate'],
'items' => [
[
'label' => '<span>TEST</span>',
'url' => ['#'],
],
['label' => '<span>TEST2</span>', 'url' => ['#']],
],
]);
I know I should use encodeLabel but does not work and returns an error. Can you do to help?
It works fine for me:
echo Menu::widget([
'options' => ['id' => 'navigate'],
'encodeLabels' => false,
'items' => [
[
'label' => '<span>TEST</span>',
'url' => ['#'],
],
['label' => '<span>TEST2</span>', 'url' => ['#']],
],
]);

Yii2 Menu Bar Visibility to a Specific User

In my main.php, I have a menu:
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Biz Staff', 'url' => ['bizstaff/index'], 'visible' => User::isBizAdmin(), 'items' => [
['label' => 'Staff List', 'url' => 'index.php?r=user/index'],
['label' => 'Add Staff', 'url' => 'index.php?r=user/create'],
]],
['label' => 'Transaction', 'url' => ['transactions/index'], 'visible' => User::isBizAdmin() || User::isBizStaff(), 'items' => [
['label' => 'Transactions', 'url' => 'index.php?r=transactions/index'],
['label' => 'Add Transactions', 'url' => 'index.php?r=transactions/create'],
]],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
Here, I am logged in as the superadmin (note that only Home and Logout menu is visible to superadmin user). Inside the homepage (Home menu) is a List of Biz Admins which is placed inside a GridView widget. It has an Action column where view, update and delete icons are placed. When I click the view icon of a specific Biz Admin, it will then render a detailed view page of that Biz Admin where its Store Name and List of Staff are seen. In this page, there is a View Store button which will redirect to the bizadmin view/page.
Whenever superadmin lands on bizadmin view/page, the menu bar should now change to:
Home, Biz Staff, Transaction, Logout
How do I do this? Is it set in the visible attribute?
Any of your answers would be highly appreciated. I am currently stuck in this problem.
Yes. You should use visible property. 'visible' => true or 'visible' => false.
Or you may assemble an array. Like that:
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
See in Advanced template - https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/views/layouts/main.php
You can make use of Yii::$app->controller->action->id or Yii::$app->controller->id to render menus based on your path.
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->controller->action->id == 'put your id here') {
$menuItems[] = ['render your menu here'];
} else {
'do something here'
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();

how can I add modal to navbar in yii2 using yii2 -bootstrap extension?

I'm trying to put modal in a navbar in my yii2 project. I'm using yii2-bootstrap extension.
Code for my nav:
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
//['label' => 'facilities', 'url' => ['/facilities/index']],
['label' => 'Hotel',
'items' => [
['label' => 'Facilities', 'url' => ['/facilities/index']],
// '<li class="divider"></li>',
// '<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Cuisines', 'url' => ['/cuisines/index']],
],
]
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
code for modal:
<?php
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me'],
]);
echo 'Say hello...';
Modal::end();
?>
can anyone please tell me how to add this modal to navbar?
First place the modal with an id on your site/index
<?php
use yii\bootstrap\Modal;
Modal::begin(['id' => 'modal',
'header' => '<h2>Hello world</h2>']);
echo "Say Hello...";
Modal::end();
?>
Then create a jQuery action in your controller/SiteController
function actionShowmodal(){
$js='$("#modal").modal("show")';
$this->getView()->registerJs($js);
return $this->render('index');
}
Finally in views\layouts\main add the link in your Nav::wigdet
['label' => 'Show Modal', 'url' => ['/site/showmodal']],
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
//['label' => 'facilities', 'url' => ['/facilities/index']],
['label' => 'Hotel',
'items' => [
['label' => 'Facilities', 'url' => ['/facilities/index']],
// '<li class="divider"></li>',
// '<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Cuisines', 'url' => ['/cuisines/index']],
// insert this line
'<li><a data-toggle="modal" data-target="#modal" style="cursor: pointer;">Click me gently!</a></li>',
],
]
];
And for the modal widget:
<?php
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me'],
'id' => 'modal', // <-- insert this modal's ID
]);
echo 'Say hello...';
Modal::end();
?>

Categories