Open in a new window in yii2 [For a button dropdown] - yii2

how can one open a link in a new tab. in my case i wanna do it in the following code
if( Yii::$app->session->get('department_id') == 5 )
{
$items[] = ['label' => 'Offer Letter',
'url' => Yii::$app->urlManager->createUrl(['dashboard/print_offer_letter', 'id' => $data->id]),
];
i tried like this but didn't work
$items[] = ['label' => 'Offer Letter', ['title'=>'go','target'=>'_blank'],
'url' => Yii::$app->urlManager->createUrl(['dashboard/print_offer_letter', 'id' => $data->id]),
];
any help would be appreciated.

I think you can use something like below:
$items[] = [
'label' => 'items',
'url' => Yii::$app->urlManager->createUrl(['dashboard/print_offer_letter', 'id' => $data->id]),
'linkOptions' => ['target'=>'_blank']
];

Related

Listview pagination and GET parameters on the next page

I have this UrlManager rule:
'<pageSlug>-pa<pageId:\d+>' => 'page/page',
Then I have page with url like this domain.com/blog-pa54
In this example I have
pageSlug = blog and pageId = 54
In my view I have ListView with my blog posts.
I set my dataprovider like this:
$dataProvider = new ActiveDataProvider([
'query' => BlogPost::find()
->where([
'active' => 1
]),
'pagination' => [
'defaultPageSize' => 3,
//'params' => [], when I set this, it only shows get param "page"
'pageParam' => 'page',
'route' => $pageURL, // this variable is equal to "blog-pa54"
],
]);
My route is current url slug - blog-pa54
When I go to next page I recieve this url: blog-pa54?pageSlug=blog&pageId=54&page=2
How can I remove $_GET params pageSlug and pageId from url?
I try to set pagination > params = [] and it remove this get param, but when I go to other page it doesn't change items in my ListView
Here is also my ListView and LinkPager
<div class="blog-post-wrapper">
<?= ListView::widget([
'dataProvider' => $blogPostsDataprovider,
'itemOptions' => ['tag' => null],
'options' => [
'tag' => false,
],
'layout' => "{summary}<div class='row'>{items}</div>",
'itemView' => function ($model, $key, $index, $widget) use ($page) {
return $this->render('//blog-post/_blogPostList', [
'page' => $page,
'model' => $model
]);
},
]); ?>
</div>
<?= \yii\widgets\LinkPager::widget([
'pagination' => $blogPostsDataprovider->pagination,
'linkContainerOptions' => [
'class' => 'page-item',
],
'linkOptions' => [
'class' => 'page-link',
],
]); ?>
Okay, I figured it out.
params attribute must not be empty.
You can set it like this:
'params' => [
'page' => isset($_GET['page']) ? $_GET['page'] : 1,
]
Or if you have another get params like get filters:
$getParams = [];
if(isset($_GET)){
foreach($_GET as $getKey => $getValue){
if(in_array($getKey, ['pageSlug', 'pageId'])){ //here I skip my params from UrlManager rule
continue;
}
$getParams[$getKey] = $getValue;
}
}
.....
'params' => $getParams

Is there a way to dynamically change maximumSelectionLength on kartik/select2 in yii2?

I am trying to update through javascript the maximumSelectionLength parameter of a select2 field (listeProduits) based on the value of a second select2 (nom_id) when it is changed.
I'm first storing the maxChildren values in a js table before loading the form so that I can access it on the client side when a new nom_id is selected.
The below approach doesn't work though as it messes up the listeProduits select2.
<script>
var nomMaxChildren = <?= json_encode(ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'max_children')); ?>;
alert(nomMaxChildren[1]);
</script>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'nom_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'nom'),
'options' => ['placeholder' => Yii::t('app','Select a name...')],
'pluginEvents' => [
"select2:select" => "function() {
$('select[name =\"listeProduits[]\"]').select2({
maximumSelectionLength: nomMaxChildren[\$(this).val()]
});
}",
]
]);?>
<label class="control-label"><?= Yii::t('app', 'Produits')?></label>';
<?= Select2::widget([
'attribute' => 'listeProduits',
'name' => 'listeProduits',
'data' => ArrayHelper::map(Produit::find()->asArray()->where(['statut_id' => 2])->andWhere(['boite_id' => null])->orWhere(['boite_id' => $model->id])->all(), 'id', 'numero_de_serie'),
'value' => (is_null($model->id)) ? null : ArrayHelper::map(Produit::find()->asArray()->where(['boite_id' => $model->id])->all(), 'id', 'id'),
'options' => ['placeholder' => Yii::t('app','Select products')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'maximumSelectionLength' => (isset($model->nom)) ? $model->nom->max_children : null,
],
'showToggleAll' => false,
]);?>
Also if you have any idea on how to validate the fact that the maximumSelectionLength is ok on client side, it would be great :D
set maximumSelectionLength property dynamically :
$('#demo-select').select2({ maximumSelectionLength: 1 });

Call to undefined method app\models\User::instance() setting up kartik export yii2

Trying to set up kratik grid export in Yii after doing everything I keep getting
Call to undefined method app\models\User::instance()
here is my code
$exportedValues = [['class' => 'kartik\\grid\\SerialColumn'], ['attribute' => 'deduction_date'], ['attribute' => 'deduction_amount'], ['attribute' => 'deduction_remark'], ['class' => 'kartik\\grid\\ActionColumn']];
$export = ExportMenu::widget(['dataProvider' => $dataProvider, 'columns' => $exportedValues, 'noExportColumns' => [0, 3], 'columnSelectorOptions' => ['label' => 'Columns', 'class' => 'btn btn-danger'], 'target' => '_blank', 'fontAwesome' => true, 'dropdownOptions' => ['label' => 'Export', 'class' => 'btn btn-success']]);
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel,
'columns' => [['class' => 'yii\\grid\\SerialColumn'],
'first_name', 'last_name', 'email_id', 'phone', ['class' => 'yii\\grid\\ActionColumn']]]); ?>
Try to install kartik, or check your kartik version
php composer.phar require kartik-v/yii2-widgets "*"
Had the same issue.
on the source code of ExportMenu.php I replaced:
$model = $modelClass::instance();
by:
$model = new $modelClass;
solved it for me.

Yii2 captcha always incorrect

Whan I add 'on' => 'create' in my rule my captcha always incorrect
Off that work good why?
I try to let it to other controller support/captcha but still incorrect
MyView
<?= Captcha::widget([
'id' => 'captcha',
'model' => $model,
'attribute' => 'verifyCode',
'options' => ['class' => 'form-control',
'data-v-rule' => '',
'captchaAction' => 'support/captcha',
'data-v-msg' => $i18n->t('please.input.verify.code'),
'placeholder' => $i18n->t('please.input.verify.code', '', false)]
]); ?>
Myrule
...
[['verifyCode'], 'captcha', 'message' => $i18n->t('error.verifyCode'),'captchaAction' => 'support/captcha' , 'on' => 'create'],
...
I get seesion
$_SESSION = [
'__flash' => [],
'__captcha/site/captcha' => 'nnbioo',
'__captcha/site/captchacount' => 1,
'__captcha/support/captcha' => 'cacijq',
'__captcha/support/captchacount' => 1,
];
Mycontroller
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => CaptchaAction::className(),
],
];
}
$customerServiceModel->load($post) is not working, because attribute verifyCode is not safe in this case. Your rule is specified with 'on' => 'create' - so it means, it's save on scenario create. You didn't assigned this scenario to the model so there's 2 solutions:
Remove 'on' => 'create' from rule
Assign create scenario to model by $customerServiceModel->scenario = 'create'; before using load().

Yii2 Kartik SideNav How to set 'active' option

I have Kartik's SideNav (http://demos.krajee.com/sidenav-demo/profile/default) working properly by setting the url's to controller/action, but don't understand how to set the active option so that the menu stays open and the correct menu item stays selected.
I've tried setting 'active'=> 'controller/action' (see below) as well as
'active' => 'controller/action/default#demo' but to no avail. When I do set it as 'employee/employee-dashboard/default#demo' it does keep that one tab highlighted but won't change the others when I do it the same way on those tabs. The original was set to 'active'=> 'home' which I assume is just the action but that didn't work for me either.
$type = SideNav::TYPE_DEFAULT;
$heading = '<i class="glyphicon glyphicon-cog"></i> Employee Menu';
$item = '/employee/employee-dashboard/default#demo';
echo SideNav::widget([
'type' => $type,
'encodeLabels' => false,
'heading' => $heading,
'items' => [
// Important: you need to specify url as 'controller/action',
// not just as 'controller' even if default action is used.
//
// NOTE: The variable `$item` is specific to this demo page that determines
// which menu item will be activated. You need to accordingly define and pass
// such variables to your view object to handle such logic in your application
// (to determine the active status).
//
['label' => 'Dashboard', 'icon' => 'home', 'url' => Url::to(['/employee/employee-dashboard', 'type'=>$type]), 'active' => ($item == '/employee/employee-dashboard/default#demo')]
['label' => 'Paycheck Info', 'icon' => 'book', 'items' => [
['label' => '<span class="pull-right badge">10</span> W2 Forms', 'url' => Url::to(['/employee/w2-form', 'type'=>$type]), 'active' => ($item == 'w2-form')],
'active'=>($item == 'about')
where
$item = Yii::$app->controller->action->id;
This is how i used
<?php
$item = Yii::$app->controller->action->id;
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'Options',
'items' => [
[
'url' => Yii::$app->homeUrl,
'label' => 'Home',
'icon' => 'home',
'active'=>($item == 'index')
],
[
'label' => 'Help',
'icon' => 'question-sign',
'items' => [
['label' => 'About', 'icon'=>'info-sign', 'url'=>Yii::$app->homeUrl.'site/about', 'active'=>($item == 'about')],
['label' => 'Contact', 'icon'=>'phone', 'url'=>Yii::$app->homeUrl.'site/contact', 'active'=>($item == 'contact')],
],
],
],
]);
?>
One solution I found if you want to set the active menu item on a per page basis is:
Create a separate view file (e.g. sidemenu.php) with just the SideNav widget, which accepts a parameter called 'currentPage'.
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'Options',
'items' => [
[
'url' => Url::toRoute('/site/index'),
'label' => 'Home',
'icon' => 'file',
'active'=>($currentPage == 'home'),
],
[
'url' => Url::toRoute('/site/about'),
'label' => 'About',
'icon' => 'file',
'active'=>($currentPage == 'about'),
],
Then in the view page where you are rendering the menu, set the currentPage property -- for example:
echo $this->render('sidemenu', ['currentPage'=>'home']);
or
echo $this->render('sidemenu', ['currentPage'=>'about']);