Yii2 Kartik Grid View Export Config - php

Summary: Trying to add export in the for csv & pdf download.
Followed documantation to install. Grid view is otherwise working.
Also added as module in config/web.php's $config array -
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
//'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '#kvgrid/messages',
//'forceTranslation' => false
]
]
],
N.B: I am using basic template and new in yii2. I have tried other fixes like composer update etc as suggested in various posts but really stuck with the problem.
the thing causing problem is - Yii::t('kvgrid', 'Reset Grid')
Can somene give me a direction here. I guess it is very simple issue :(

You was got above error because of you have not configure i18n component in web.php file.
You need to configure i18n component like as,
'i18n' => [
'translations' => [
'kvgrid*' => [
'class' => 'yii\i18n\PhpMessageSource',
],
]
],
Or uncomment the gridview modules's i18n configuration
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#kvgrid/messages',
'forceTranslation' => true
]
]

You have to replace
Yii::t('kvgrid', 'Reset Grid')
to
Yii::t('app', 'Reset Grid')
in your view file

I suggest better to generate CRUD using the Ajax Crud Generator, it will do all the required task for CRUD and export as well...try this

Related

Laravel log file specific to a package

I'm writing a couple of laravel packages and I'm wondering if it is possible to have the package write to a specific log file but only for messages related to the package?
I tried making a logging.php file in the packages/myorg/mypackage/config (below) but it doesn't seem to do anything.
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
]
];
I am using "jeroen-g/laravel-packager" to set up the packages. It appears to manually load the mypackage.config in the ServiceProvider bootForConsole
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
], 'mypackage.config');
}
I'm not sure how to add custom logging to that though. I'm still learning Laravel and I'm not quite sure what or how the main applications config/logging.php is read so I'm not quite sure how to inject a custom version for an add-on package.
EDIT:
I found a post that suggested using the following in the ServiceManager boot() method:
$this->app->make('config')->set('logging.channels.mychannel', [
/* settings */
]);
I used the package config to set a 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] and could then do the same thing as above with:
$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');
But that still required something in the code. The next best thing I have found thus far is to change my config/logging.php to config/logging.channels.php and include something like:
return [
'mychannel' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
];
Then in the service provider register() method add:
$this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');
I tried doing it from the original 'logging.php' with channels array nested in a 'logging' key, but array_merge doesn't appear to merge the nested elements so my channel never showed up in logging.channels.
I'm not sure if this is ideal, however. I'd still like to know if there is a 'better' or best practices way of adding custom package logging parameters and whether there is a need to publish it in any way (and how).

Yii2 Translate Manager doesn't work

I m using a basic template for a small project on Yii2. I have already set the module Language Picker of Lajax (Doc) and I am trying now to manage the translation with the module Translate Manager of Lajax (Github). The plugin is scanning perfectly the project and getting the translatable texts. I even set some translations through this module and everything is saved in the database, but these translations are not set when changing the language.
here are my web.php Configurations:
'language' => 'en-GB',
'components' => [
...
'languagepicker' => [
'class' => 'lajax\languagepicker\Component',
'languages' => ['en-GB', 'fr-FR']
],
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'db',
'sourceLanguage' => 'en-GB',
'sourceMessageTable' => '{{%language_source}}',
'messageTable' => '{{%language_translate}}',
'forceTranslation' => true,
'cachingDuration' => 86400,
'enableCaching' => true,
],
],
],
...
]
'modules' => [
...
'translatemanager' => [
'class' => 'lajax\translatemanager\Module',
'root' => '#app', // The root directory of the project scan.
'scanRootParentDirectory' => false, // Whether scan the defined `root` parent directory, or the folder itself.
// IMPORTANT: for detailed instructions read the chapter about root configuration.
'layout' => 'language', // Name of the used layout. If using own layout use 'null'.
'allowedIPs' => ['127.0.0.1'], // IP addresses from which the translation interface is accessible.
'roles' => ['#'], // For setting access levels to the translating interface.
'tmpDir' => '#runtime', // Writable directory for the client-side temporary language files.
// IMPORTANT: must be identical for all applications (the AssetsManager serves the JavaScript files containing language elements from this directory).
'phpTranslators' => ['::t'], // list of the php function for translating messages.
'jsTranslators' => ['lajax.t'], // list of the js function for translating messages.
'patterns' => ['*.js', '*.php'],// list of file extensions that contain language elements.
'ignoredCategories' => ['yii'], // these categories won't be included in the language database.
'ignoredItems' => ['config'], // these files will not be processed.
'scanTimeLimit' => null, // increase to prevent "Maximum execution time" errors, if null the default max_execution_time will be used
'searchEmptyCommand' => '!', // the search string to enter in the 'Translation' search field to find not yet translated items, set to null to disable this feature
'defaultExportStatus' => 1, // the default selection of languages to export, set to 0 to select all languages by default
'defaultExportFormat' => 'json',// the default format for export, can be 'json' or 'xml'
'tables' => [ // Properties of individual tables
[
'connection' => 'db', // connection identifier
'table' => '{{%language}}', // table name
'columns' => ['name', 'name_ascii'],// names of multilingual fields
'category' => 'database-table-name',// the category is the database table name
]
],
'scanners' => [ // define this if you need to override default scanners (below)
'\lajax\translatemanager\services\scanners\ScannerPhpFunction',
'\lajax\translatemanager\services\scanners\ScannerPhpArray',
'\lajax\translatemanager\services\scanners\ScannerJavaScriptFunction',
'\lajax\translatemanager\services\scanners\ScannerDatabase',
],
],
...
]
I always use something like this im code for translatable strings:
<?= Yii::t('app','Test') ?>
Am I doing something wrong?

How to Fix Cache JavaScript in Yii 2

In Yii 2, when I add a few code rows to my JavaScript file, that has a cache, it will not accept my additional code to the JavaScript file.
How can I fix this problem?
You need to add forceCopy option in asset manager under components :
'components' => [
'assetManager' => [
'class' => 'yii\web\AssetManager',
'forceCopy' => true,
],
],
In my case I did it by chanding version in asset file where i include this js:
public $js = [
'js/script.js?0.0.1',
];

translation view for Yii app not found

I set up an i18n page, where I translate messages using yii\i18n\PhpMessageSource with the following config part:
(config/web.php)
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['debug'],
'language' => 'de-DE',
'components' => [
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'fileMap' => [
'app' => 'app.php',
],
'forceTranslation' => true,
],
],
]]
...byt the way: this works fine.
For a kind of static content -like an imprint-, I like to use an complete translated view.
So I added some sub-directories in the views - folder, with the view insight:
#app/views/myController/de-DE/myview.php
#app/views/myController/en-US/myview.php
So my action does the following:
public function actionImpressum() {
\Yii::$app->language = 'en-US';
return $this->render('myview');
}
...which results in an invalid parameter
yii\base\InvalidParamException: The view file does not
exist: /path/to/my/app/views/myCtrl/myview.php
This error is valid, because there is no view at this path. But shouldn't the render() method use the path for the translation views, like:
/path/to/my/app/views/myCtrl/en-US/myview.php ??
Is there something I forgot?
Thank you.
Since there is no sourceLanguage set in your configuration I assume you have not changed it and the source language of your app is en-US (default one).
When the source language is the same as target language view is not translated.
See documentation about this:
Note: If the target language is the same as source language original view will be rendered regardless of presence of translated view.
So for en-US it looks for /path/to/my/app/views/myCtrl/myview.php file.

Override Yii2 assetManager config in controller

I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
],
'assetManager' => [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/flick/jquery-ui.css'],
],
],
],
],
];
I tried the following to override this configuration item in the controller actions method:
public function actions() {
Yii::$app->components['assetManager'] = [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/dot-luv/jquery-ui.css'],
],
],
];
return parent::actions();
}
Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?
You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.
Yii::$app->assetManager->bundles = [
'yii\jui\JuiAsset' => [
'css' => ['themes/dot-luv/jquery-ui.css'],
],
];
Or if you want to keep other bundles config :
Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
'css' => ['themes/dot-luv/jquery-ui.css'],
];
You are going about this all wrong, you want to change the JUI theme for 1 controller alone because of a few controls. You are applying 2 css files to different parts of the website that have the potential to change styles in the layouts too. The solution you found works but it is incredibly bad practice.
If you want to change just some controls do it the proper way by using JUI scopes.
Here are some links that will help you:
http://www.filamentgroup.com/lab/using-multiple-jquery-ui-themes-on-a-single-page.html
http://jqueryui.com/download/
In this way you are making the website easier to maintain and you do not create a bigger problem for the future than you what solve.

Categories