How to add Multiple Yii2 submenuTemplate in one Menu::widget? - menu

I am creating a web application based on yii2 and learn-plus dashboard theme, the collapsible sidebar navigation menu has been implemented with an id that acts as a targeted link (#menu_id) for the menu to open when parent link is clicked, Now I am trying to add Yii2 Menu::widget with Multiple submenuTemplate so that I can accommodate those ID's as each submenu should have its own id.
I have created the menu and it works perfectly with a single collapsible menu, the problem comes when I add more than one collapsible part of a menu.
<?php
use yii\widgets\Menu;
use yii\helpers\Html;
/**
* Created by PhpStorm.
* User: Mr Daud Mabena
* Date: 01/05/19
* Time: 01:34 PM
*/
echo Menu::widget([
'activeCssClass' => "active",
'activateParents' => true,
'encodeLabels' => false,
'submenuTemplate' => "\n<ul class='sidebar-submenu sm-indent collapse' id='setup_menu'>\n{items}\n</ul>\n",
'options' => ['class' => 'sidebar-menu'],
'items' => [
[
'label' => 'Security Dashboard',
'url' => ['/security/'],
'options' => ['class' => 'sidebar-menu-item'],
'template' => "<a class='sidebar-menu-button' href='{url}'>
<i class='sidebar-menu-icon sidebar-menu-icon--left material-icons'>bar_chart</i>
{label}
</a>",
],
[
'id' => 'components_menu',
'template' => "<a class='sidebar-menu-button sidebar-js-collapse' data-toggle='collapse' href='#setup_menu'>
<i class='sidebar-menu-icon sidebar-menu-icon--left material-icons'>tune</i>
List of Users
<span class='ml-auto sidebar-menu-toggle-icon'></span>
</a>",
'items' => [
[
'label' => 'Staffs',
'url' => ['/security/users/staffs'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Applicant',
'url' => ['/security/users/applicants'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Students',
'url' => ['/security/users/students'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
]
],
[
"label" => "Add New Staff",
"url" => ["/security/users/create"],
"template" => "<a class='sidebar-menu-button' href='{url}'><i class='fa fa-bar-chart' aria-hidden='true'></i> {label}</a>",
],
[
"label" => "Logout",
"url" => ["/admission/applicants/reports"],
"template" => Html::beginForm(['/security/users/logout'], 'post') . Html::submitButton('Logout', ['class' => 'sidebar-menu-button logout']) . Html::endForm(),
],
],
]);
I need to have multiple
'submenuTemplate' => "\n<ul class='sidebar-submenu sm-indent collapse' id='setup_menu'>\n{items}\n</ul>\n",
with different id, the first one is setup_menu wish to add other id like access_levels etc

Move the submenuTemplate inside the parent item where you want a sub-menu to appear, currently, you are using one template for all the submenus whereas you need to use a separate template for each submenu, and assign different ids for all the submenus and the href for the parent item.
For example, see the below image where you have multi-level collapsable submenus under List of Users 2 Levels Collapsable and other under the Add New Staff 1 Level.
You can use the following code, I copied the items under the existing submenu so you can change it accordingly but the idea is there. I used setup_menu_1 format to assign 3 different ids to the menus, change them too
echo Menu::widget(
[
'activeCssClass' => "active",
'activateParents' => true,
'encodeLabels' => false,
'options' => ['class' => 'sidebar-menu'],
'items' => [
[
'label' => 'Security Dashboard',
'url' => ['/security/'],
'options' => ['class' => 'sidebar-menu-item'],
'template' => "<a class='sidebar-menu-button' href='{url}'><i class='sidebar-menu-icon sidebar-menu-icon--left material-icons'>bar_chart</i>{label}</a>",
],
[
'id' => 'components_menu',
'template' => "<a class='sidebar-menu-button sidebar-js-collapse' data-toggle='collapse' href='#setup_menu_1'><i class='sidebar-menu-icon sidebar-menu-icon--left material-icons'>tune</i>List of Users<span class='ml-auto sidebar-menu-toggle-icon'></span></a>",
'submenuTemplate' => "\n<ul class='sidebar-submenu sm-indent collapse' id='setup_menu_1'>\n{items}\n</ul>\n",
'items' => [
[
'label' => 'Staffs',
'template' => "<a class='sidebar-menu-button sidebar-js-collapse' data-toggle='collapse' href='#setup_menu_3'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
'submenuTemplate' => "\n<ul class='sidebar-submenu sm-indent collapse' id='setup_menu_3'>\n{items}\n</ul>\n",
'items' => [
[
'label' => 'Staffs',
'url' => ['/security/users/staffs'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Applicant',
'url' => ['/security/users/applicants'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Students',
'url' => ['/security/users/students'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
]
],
[
'label' => 'Applicant',
'url' => ['/security/users/applicants'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Students',
'url' => ['/security/users/students'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
]
],
[
"label" => "Some Other Menu",
'submenuTemplate' => "\n<ul class='sidebar-submenu sm-indent collapse' id='setup_menu_2'>\n{items}\n</ul>\n",
"template" => "<a class='sidebar-menu-button sidebar-js-collapse' data-toggle='collapse' href='#setup_menu_2'><i class='fa fa-bar-chart' aria-hidden='true'></i> {label}</a>",
'items' => [
[
'label' => 'Staffs',
'url' => ['/security/users/staffs'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Applicant',
'url' => ['/security/users/applicants'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
[
'label' => 'Students',
'url' => ['/security/users/students'],
'template' => "<a class='sidebar-menu-button' href='{url}'><span class=\"sidebar-menu-text\">{label}</span></a>",
'options' => ['class' => 'sidebar-menu-item'],
],
]
],
[
"label" => "Logout",
"url" => ["/admission/applicants/reports"],
"template" => Html::beginForm(['/security/users/logout'], 'post') . Html::submitButton('Logout', ['class' => 'sidebar-menu-button logout']) . Html::endForm(),
],
],
]
);

Related

Store metadata in a subscription with Stripe

I'm trying to figure out how to store metadata directly into a Stripe CC payments subscription. I'm using the PHP SDK and have:
$test = $stripe->checkout->sessions->create([
'customer_email' => $_GET["who"],
'success_url' => $success_url,
'payment_method_types' => ['card'],
'cancel_url' => "https://www.example.com",
'line_items' => [
[
'price' => $price_plan_id,
'quantity' => 1,
],
],
'payment_intent_data' => [
'metadata' => [
'who' => $_GET["who"],
'total' => $_GET["total"],
'period' => $_GET["period"],
'description' => $_GET["description"],
'district' => $_GET["district"],
'what' => $_GET["what"],
'ip' => $_SERVER["REMOTE_ADDR"]
]
],
'mode' => $mode 'subscription',
]);
This gives me an error:
You can not pass payment_intent_data in subscription mode.
I've tried just doing:
$test = $stripe->checkout->sessions->create([
'customer_email' => $_GET["who"],
'success_url' => $success_url,
'payment_method_types' => ['card'],
'cancel_url' => "https://www.example.com",
'line_items' => [
[
'price' => $price_plan_id,
'quantity' => 1,
],
],
'metadata' => [
'who' => $_GET["who"],
'total' => $_GET["total"],
'period' => $_GET["period"],
'description' => $_GET["description"],
'district' => $_GET["district"],
'what' => $_GET["what"],
'ip' => $_SERVER["REMOTE_ADDR"]
],
'mode' => $mode 'subscription',
]);
And while it kind of works, its not assigned to the subscription (when you view it metadata is empty)
How do I go passing this along? I want to keep this data stored in the subscription element (not just the payment)
Thanks
You want to pass the metadata on to the resulting Subscription object by setting it in subscription_data.metadata: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata

Sorting calculated fields in Yii2 (Grid View) getter param

How get i sort my rows by field i get from model getter like this
* Returns the score for this team
*/
public function getScore() {
$score = 0;
if (!empty($this->bonus_points)) {
$score += $this->bonus_points;
}
foreach (Attempt::find()->where(['team_id' => $this->id, 'marked' => 1])->all() as $attempt) {
$score -= $attempt->cost;
$score += $attempt->reward;
}
return $score;
}
View Code
GridView::widget([
'id' => 'quickfire-grid',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'filterPosition' => GridView::FILTER_POS_HEADER,
'tableOptions' => [
'class' => 'table'
],
'layout' => "{summary}\n{items}\n{pager}",
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
'name',
[
'attribute' => 'bonus_points',
'label' => 'Adjustment',
'filter' => false
],
[
'attribute' => 'score',
'label' => 'Score',
'filter' => false,
],
[
'label' => 'Adjust score',
'format' => 'raw',
'value' => function ($data) use ($game) {
return Html::input('text', 'score-' . $data->id, '0', ['id' => 'score-' . $data->id]) . ' ' . Html::a('Adjust score', '#', ['class' => 'btn btn-danger btn-adjust-score', 'data-team-id' => $data->id, 'data-href' => Url::toRoute(['adjust-scores', 'game_id' => $game->id, 'skipConfirmation' => true])]);
},
],
],
]);
Is it possible to sort grid by score field ? I think need to add some javascript code. I have read this article but there is no solution https://www.yiiframework.com/wiki/621/filter-sort-by-calculatedrelated-fields-in-gridview-yii-2-0.
Team::find()->select(['total_score' => 'ifnull(s.score, 0)+team.bonus_points'])
->leftJoin([
's' => Attempt::find()
->select('team_id, SUM(reward-cost) as score')
->where(['marked' => 1])
->groupBy('team_id')
], 's.team_id=team.id')
->orderBy('total_score')
Something like this) Modify select with your needs...

Yii2 search with empty param throws all records

There are 3 search fields in view2.php, search is working properly when i enter something into search field and hit search button. But the problem is when I hit search/Enter button without entering anything into search fields, it displays all the entries related to that model from DB.
Below is my Machine model:
public function search($params)
{
$query = Supplier::find();
$query->joinWith(['user', 'cities', 'industrialareas','supplierMachines', 'subCategory', 'types0','supplierCertificates' ]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
if (!($this->load($params) && $this->validate())) {
$query->where('1 <> 1');
}
else {
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'yoe' => $this->yoe,
]);
$query->andFilterWhere(['like', 'company_constitution', $this->company_constitution])
->andFilterWhere(['like', 'street', $this->street])
->andFilterWhere(['like', 'locality', $this->locality])
}
return $dataProvider;
}
}
machine controller which calls view2 function(it displays search fields)
public function actionView2()
{
//Display machines based on the customer search
$searchModel = new SupplierMachineSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
// render
return $this->render('view2', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
View2.php
<?= $form->field($searchModel, 'enter_city')->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a city ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($data, 'city_name'), ],
]) ?>
<?= $form->field($searchModel, 'enter_iarea')->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a iarea ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($data1, 'iarea_name'), ],
]) ?>
<?= $form->field($searchModel, 'machine')->widget(AutoComplete::classname(), [
'options' => ['placeholder' => 'Select a iarea ...', 'class' => 'form-control'],
'clientOptions' => [
'source' => ArrayHelper::getColumn($all, 'name' ), ],
]) ?>
<?= Html::activeHiddenInput($searchModel, 'id')?>
<div class="form-group">
<?= Html::submitButton('Apply', ['class' => 'btn btn-success']) ?>
<?= Html::a('Reset', ['view2']);?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?=
ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_viewmain',
'viewParams' => [
'fullView' => false,
'context' => 'main-page',
],
]);
Check if the search parameters, once loaded, are empty:
if ( !($this->load($params) && $this->validate()) or
(empty($this->search_param1) and empty($this->search_param2) and empty($this->search_param3)) ) {
$query->where('1 <> 1');
}

How to change export sheet/doc properties in Yii2 krajee grid export?

We have used yii2 krajee export for exporting grid data.How can we modify default title, description , lastmodifiedby?
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'target' => ExportMenu::TARGET_SELF,
'exportConfig' => [
ExportMenu::FORMAT_PDF => false,
],
'filename' => 'export-list_' . date('Y-m-d_H-i-s'),
]);
What I did to get my task done-
I have in my header as-
use yii\helpers\Html;
use kartik\grid\GridView;
Then in gridView -
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'id' => 'grid',
'toolbar' => [
['content'=>
Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['ctrl-action'], ['data-pjax'=>false, 'class' => 'btn btn-default', 'title'=>'Reset Grid'])
],
'{export}',
'{toggleData}'
],
'panel' => [
'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-cloud"></i> Output Daily Data </h3>',
'type'=>'primary',
'before'=>Html::a(''),
'after'=>Html::a(''),
'showFooter'=>false
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
'id',
'name'
['class' => 'yii\grid\ActionColumn'],
],
'responsive'=>true,
'hover'=>true,
'exportConfig' => [
GridView::CSV => ['label' => 'Export as CSV', 'filename' => 'File_Name-'.date('d-M-Y')],
GridView::HTML => ['label' => 'Export as HTML', 'filename' => 'File_Name -'.date('d-M-Y')],
GridView::PDF => ['label' => 'Export as PDF', 'filename' => 'File_Name -'.date('d-M-Y')],
GridView::EXCEL=> ['label' => 'Export as EXCEL', 'filename' => 'File_Name -'.date('d-M-Y')],
GridView::TEXT=> ['label' => 'Export as TEXT', 'filename' => 'File_Name -'.date('d-M-Y')],
],
'export' => [
'fontAwesome' => true
],
]); ?>
That worked for me.
just make changes in 'exportConfig' section for rename file.
Try This :
Here is a example of CSV. You can do like wise for others also.
use yii\helpers\Html;
use kartik\grid\GridView;
use kartik\export\ExportMenu;
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'desc',
['class' => 'yii\grid\ActionColumn'],
];
// Renders a export dropdown menu
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'exportConfig'=> [
GridView::CSV=>[
'label' => 'CSV',
'icon' => '',
'iconOptions' => '',
'showHeader' => false,
'showPageSummary' => false,
'showFooter' => false,
'showCaption' => false,
'filename' => 'yii',
'alertMsg' => 'created',
'options' => ['title' => 'Semicolon - Separated Values'],
'mime' => 'application/csv',
'config' => [
'colDelimiter' => ";",
'rowDelimiter' => "\r\n",
],
],
],
]);

Error: This page can't be reached in Kartik Yii2 Export

In Kartik Yii2 Export, While exporting as Excel am getting This Page Cant't Reached Error in Localhost.
if i export as Text or CSV, export get worked but if i open the exported file Text or CSV, Half the
report is printing like html code
Help will be really appreciated.
GridCode:
<?php $gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'membercode',
'member_name',
[
'attribute' => 'payment_category',
'format' => 'raw',
'label' => 'Payment Category',
'value' => function($model, $key, $index, $grid) {
$temp = $model->payment_category;
$si = Category::find()->where(['category_id' => $temp])->one();
return $si['category_name'];
},
],
'member_gender',
'member_address:ntext',
'payment_date',
'amount',
'receipt_no',
'payment_mode',
'pledge_amount',
'young_amount',
'tv_amount',
'building_amount',
[
'attribute' => 'payment_subcategory',
'format' => 'raw',
'value' => function($model, $key, $index, $grid) {
$exp = explode(',', $model->payment_subcategory);
$relation_name = ArrayHelper::map(Subcategory::find()->where(['subcategory_id' => $exp])->all(), 'subcategory_id', 'subcategory_name');
$relation = implode(',', $relation_name);
return $relation;
},
'filter' => Html::activeDropDownList($searchModel, 'payment_subcategory', ArrayHelper::map(Subcategory::find()->asArray()->all(), 'id', 'subcategory_name'),['class'=>'form-control','multiple' => true]),
],
['class' => 'yii\grid\ActionColumn'],
]; ?>
<?= ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'columnSelectorOptions'=>[
'label' => 'Columns',
'class' => 'btn btn-danger'
],
'fontAwesome' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-primary'
]
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
]); ?>
Above is my Grid view Code.
Help will be really appreciated.
Updated:
Error geeting while exporting as CSV:
Error geeting while exporting as EXCEL
There is an issue in your gridview, one of the field in gridview carries "=" equal to sign. please check it out PhpOffice/PhpExcel
Try exportConfig settings this
<?= ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'columnSelectorOptions'=>[
'label' => 'Columns',
'class' => 'btn btn-danger'
],
'fontAwesome' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-primary'
]
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
],
]); ?>
Try this code for your GridVew::Widget:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'exportConfig'=> [
GridView::CSV=>[
'label' => 'CSV',
'icon' => '',
'iconOptions' => '',
'showHeader' => false,
'showPageSummary' => false,
'showFooter' => false,
'showCaption' => false,
'filename' => 'yii',
'alertMsg' => 'created',
'options' => ['title' => 'Semicolon - Separated Values'],
'mime' => 'application/csv',
'config' => [
'colDelimiter' => ";",
'rowDelimiter' => "\r\n",
],
],
],
]);

Resources