Edit content of export menu in yii2 - yii2

I use export menu on my yii2 project. I download it from demos.krajee.com. I have been success to use it, but I want to edit content like in pdf, so it have title/header or footer or page number. How to make it?

There are at least two ways to do that.
For example you can set it as default setting in
Yii::$container->set('kartik\grid\GridView', [
'summary' => "Showing {begin} - {end} from {totalCount} records",
'captionOptions' => ['style' => 'font-size:0em;'],
'panel' => [
'heading' => false,
'before' => '{summary}',
'after' => '{pager}',
'footer' => false,
],
'hover' => true,
'toolbar' => [
'{export}',
'{toggleData}',
],
'export' =>[
'showConfirmAlert' => false,
],
'exportConfig' => [
kartik\grid\GridView::PDF => [
'label' => 'Save as PDF',
'config' => [
'methods' => [
'SetHeader' => ['Header'],
'SetFooter' => ['Footer whatever: ' . strftime("%c") . '||Page {PAGENO}'],
]
],
],
],
...
Or pass the settings individually to each widget in every view.
You can use methods in wrapper http://demos.krajee.com/mpdf#settings (look for methods) or directly from mpdf class if you need something more complicated.

Related

Controller / Action for guest users (Does not require authentication) using Yii2-user

I am using Yii2 (basic) and Yii2-user for a website with users. For most actions it's necessary to be authenticated. How could I make a controller / action accessible as a guest?
I have tried things like this in the guest's controller:
'rules' => [
[
'allow' => true,
'actions' => ['index', 'confirm', 'download-form', 'upload-form'],
]
],
And this should be enough. But nope. I suspect that it is Yii2-user module who gets in the way and always redirects me to login.
And I have added the module in the web.php configuration like this:
'components' => [
...
...
'user' => [
'class' => 'nkostadinov\user\components\User',
'identityClass' => 'nkostadinov\user\models\User',
'enableConfirmation' => false,
'as firstLoginPolicy' => [
'class' => 'nkostadinov\user\behaviors\FirstLoginPolicyBehavior'
],
],
],
Any idea?
I have solved it as follows.
In my web.php configuration I had this:
'modules' => [
...
],
'as access' => [
'class' => \yii\filters\AccessControl::className(),//AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'request', 'change-password'],
'allow' => true,
'roles' => ['?']
],
[
//'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['#'],
],
],
],
'params' => [ ... ]
So, I have added this new rule to grant guest users access to all actions of this controller:
[
'controllers' => ['mymodule/my-controller'],
'allow' => true,
],
And that's it.
i suggest you to use mdmsoft/yii2-admin for authentication

yii2 url manager rules accept not pattern

I need configure my Yii2 UrlManager rules like this:
change http://domain/site/action to http://domain/action
change http://domain/module/default to http://domain/module
so far what I have done:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<module:(!site)>' => '<module>/default',
'<action:\w+>' => 'site/<action>',
],
],
when I trying access module it return 404. But when I remove '<action:\w+>' => 'site/<action>', access module again will show as module/default page. So how to solve this?
You can try this code
'<action:(about|contact)>' => 'site/<action>',
insted of this
'<action:\w+>' => 'site/<action>',
and it will not work then change sequence of rules
'rules' => [
'<action:\w+>' => 'site/<action>', <-----------it will be first
'<module:(!site)>' => '<module>/default',
],

RBAC Routes add default route of module to all the routes of project?

If I add this configuration of the cms module to the config file
'cms' => [
'class' => 'yii2mod\cms\Module',
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => '',
'froalaEditorOptions' => [
'clientOptions' => [
'heightMin' => 300,
'theme' => 'dark',
'imageUploadURL' => 'upload-image',
'imageManagerDeleteURL' => 'delete-image',
'imageManagerDeleteMethod' => 'POST',
'imageManagerLoadURL' => 'images'
],
'excludedPlugins' => [
'file',
'emoticons'
]
],
'enableMarkdown' => false
]
It adds the default route of this module to all the routes like this
/cms/site/login /cms/site/index /cms/site/error. Why this is happening and how i can remove this?
If you want to remove the /cms module prefix by default, you can add a global route to backend/config/main.php(If you use advanced templates): '<controller:[\w-]+>/<action:[\w-]+>' =>'cms/<controller>/<action>'.
for example:
// backend/config/main.php
return [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:[\w-]+>/<action:[\w-]+>' =>'cms/<controller>/<action>'
],
],
];
Access in your bowser: www.xxx.com/site/index, it is forwarded to: /cms/site/index

Yii2 tabs active not working

I have a tab widget like below.
<?php
echo \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Administrators',
'url' => Url::to(['/admin/admin']),
],
[
'label' => 'Members Tracker',
'url' => Url::to(['/admin/members']),
],
[
'label' => 'Pages',
'url' => Url::to(['/admin/pages']),
],
],
]);
?>
Tabs are appearing correctly. But the active is not working for other tabs. Only active appears to first tab. If we click on other tabs('member Tracker','Pages') it routes correctly and can view the page contents. But tab1('Administrators") is in open state and other tabs in closed state. How to make a tab active when we click on it and others closed.Please help me.
In Yii2 Bootstrap Tabs Widget you can use active option.
For example in AdminController set $action to current action and pass to the view file:
<?= \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Administrators',
'url' => Url::to(['/admin/admin']),
'active' => $action == 'admin',
],
[
'label' => 'Members Tracker',
'url' => Url::to(['/admin/members']),
'active' => $action == 'members',
],
[
'label' => 'Pages',
'url' => Url::to(['/admin/pages']),
'active' => $action == 'pages',
],
],
]); ?>
What i understand is that when you click on any of the tabs your page redirects, and you donot want the page to reload or redirect when you click on any of the tabs.
If that is correct then this is because you are using the url option and it takes a full route and redirects to that url, to use basic tab functionality you have to use the content option to display the text for the desired tab or you can load the view too see below
<?php
echo \yii\bootstrap\Tabs::widget([
'items' => [
[
'label' => 'Administrators',
//using a view file
'content' => $this->render('some-view'),
],
[
'label' => 'Members Tracker',
//using static content
'content' => 'This is some content for Member Tracker Tab'
],
[
'label' => 'Pages',
'content' => 'This is some content for Pages Tab'
],
],
])
?>

After using amnah authentication the backend crud of user module shows the Yii2 User Module not the User CRUD

We've been working on a project with Yii2 Advanced App, with a custom bootstrap template. I've generated the crud using gii. All other CRUDs work fine. But the User crud displays Yii2 User Module not the CRUD.
I've gone through amnah's complete documentations and couldn't find any solutions in any other places either. I even tried Yii2 documentations and it wasn't any help either.
This is my backend config
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => 'sahasa/index',
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'request' => [
'baseUrl' => $baseUrl,
],
'user' => [
'class' => 'amnah\yii2\user\components\User',
],
// 'user' => [
// 'identityClass' => 'common\models\User',
// 'enableAutoLogin' => true,
// ],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
'modules' => [
'user' => [
'class' => 'amnah\yii2\user\Module',
// set custom module properties here ...
],
'debug' => [
'class' => 'yii\debug\Module',
],
],
];
This is what I get when I goto localhost/app/backend/web/index.php?r=user
I want it to display a CRUD similar to this
I'm stuck there. Without the CRUD it'll be difficult to manage the USERS.
Any help is appreciated.
Thanks in advance.
I think you need to redefine the controller too. You need a new controller derived form the original controller for correctly addressing the new CRUD element.
I suppose your controller is in backend and is named user otherwise change properly the following example (part of config/main.php):
'user' => [
...
'controllerMap' => [
'user' => 'backend\controllers\user',
],
],
You can add this code to Controller in backend
public function init()
{
$user_id = Yii::$app->getUser()->id;
if($user_id){
$user = \amnah\yii2\user\models\User::findOne($user_id);
if ($user->can("admin")) {
// do something
}else{
throw new HttpException(403, 'You are not allowed to perform this action.');
}
}else{
throw new HttpException(403, 'You are not allowed to perform this action.');
}
parent::init();
}
Just click the link /user/admin
You'll get CRUD for users.