Yii2 Class 'DateRangePicker' not found - yii2

Answer
php composer.phar require kartik-v/yii2-widget-datepicker "#dev"
I want filter in GridView::widget by date range
But I got error "Class 'DateRangePicker' not found"
Helpres does not find this module
[
'attribute' => 'Date',
'label' => 'Data',
'value' => function ($model, $key, $index, $column) {
return $model->create_at;
},
'filter' => DateRangePicker::widget([
'model'=>$searchModel,
'attribute' => 'createdAt',
'pluginOptions'=> [
'locale'=>['format' => 'Y-m-d'],
"opens"=>"left",
]
])
]
How to properly filter by date range?

You need to install this package
php composer.phar require kartik-v/yii2-date-range "dev-master"
and add use kartik\daterange\DateRangePicker;

Related

MediaWiki, how to add filters to the SpecialPages:Categories page?

To better understand the code of MediaWiki, I'm trying to modify directly the source code without creating an extension.
In the page Special:Categories I'd like to show only the categories which are not created by a Template. How can I modify the query?
https://doc.wikimedia.org/mediawiki-core/1.28.0/php/classSpecialCategories.html
https://doc.wikimedia.org/mediawiki-core/1.28.0/php/classCategoryPager.html
I've found the solution
The source code for the Special Pages is inside the folder includes/specials. There are many scripts here about categories and by comparing some of those files I could find out the syntax used to manipulate queries in mediawiki which is defined through the method getQueryInfo() inside each file.
I'm posting the queries defined each in a different Special Page related to categories
public function getQueryInfo() {
return [
'tables' => [ 'category' ],
'fields' => [ 'title' => 'cat_title',
'namespace' => NS_CATEGORY,
'value' => 'cat_pages' ],
'conds' => [ 'cat_pages > 0' ],
];
}
public function getQueryInfo() {
return [
'tables' => [ 'categorylinks', 'page' ],
'fields' => [
'namespace' => 'page_namespace',
'title' => 'page_title',
'value' => 'COUNT(*)'
],
'conds' => [
'page_namespace' => $this->namespaceInfo->getContentNamespaces()
],
'options' => [
'HAVING' => 'COUNT(*) > 1',
'GROUP BY' => [ 'page_namespace', 'page_title' ]
],
'join_conds' => [
'page' => [
'LEFT JOIN',
'page_id = cl_from'
]
]
];
}

Transfer of date to the database

the beginner in Yii, connected DatePicker in GridView, but when I set date, in the Database nothing changes, there are old values. I do not understand how to make so that data came to the Database.
View:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'avito_login',
'avito_password',
'Region',
"City",
"legal_entity",
"manager_contact_phone",
"avito_user_id",
"teg",
[
'label' => 'Date begin subscription',
'value' => function ($model, $key, $value) {
return \kartik\date\DatePicker::widget([
'name' => 'date_subscription',
'model' => $model,
'value' => $model->date_subscription,
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'autoclose' => true,
],
'pluginEvents'=>[
'changeDate'=>"function(e) {[jQuery.ajax({ url: '/admin/clients/date-subscription', type: 'post', contentType: 'application/x-www-form-urlencoded', dataType: 'html', data: { data: this.value }, success: function (data) { alert(data) });
}",
],
]);
},
'format' => 'raw',
],
?>
Controller
public function actionDateSubscription()
{
$datesubscription = Yii::$app->request->post('date_subscription');
}
Nothing occurs, fields with date stopped being active
You basically need to bind an ajax call to the datepicker which sends the date to the server controller/action which updates the new date into the database and sends the response back to the frontend, where if everything is OK it refreshes the gridview to reflect the new date.
Here is a similar answer which uses toggle-switch to update the database status column, you can have the general idea about the process.
The only thing that is different in your case is that you have to use the pluginEvents option to specify the changeDate event and bind the ajax call to that event like below
[
'label' => 'Date begin subscription',
'value' => function ($model, $key, $value) {
return \kartik\date\DatePicker::widget([
'name' => 'date_subscription',
'model' => $model,
'value' => $model->date_subscription,
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'autoclose' => true,
],
'pluginEvents'=>[
'changeDate'=>"function(e) { //call your ajax fuction here }",
]
]);
},
'format' => 'raw',
],
Note: if you have aloowed date_subscription to be empty/null then make sure you call the ajax in the clearDate event too along with the changeDate.
For a complete list of events See the DOCS

yii2 console unknown command

Trying to run function _googleanalytics in controller ProcessingController, but getting an error:
unknown command
command:
./yii processing/_googleanalytics '2017-02-27' '2017-02-27'
controller path:
/console/controllers/
action
public function _googleanalytics($start, $finish) {...
controller
namespace console\controllers;
class ProcessingController extends Controller
{...
/console/config/main.php
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\fixtures',
],
],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning','info'],
'exportInterval' => 1,
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'exportInterval' => 1,
'logVars' => [],
'categories' => ['calls'],
'logFile' => '#app/runtime/logs/calls.log',
'maxFileSize' => 1024 * 2,
'maxLogFiles' => 20,
],
],
],
],
'modules'=>[
'user-management' => [
'class' => 'webvimark\modules\UserManagement\UserManagementModule',
'controllerNamespace'=>'vendor\webvimark\modules\UserManagement\controllers', // To prevent yii help from crashing
],
'googleanalytics' => [
'class' => 'console\modules\googleanalytics\Module',
]
],
'params' => $params,
];
what I am doing wrong?
You need to make an action to access it via console/terminal the same way as we access the actions via our browser.
For example if i create a Test Controller like below inside console/controllers directory
<?php
namespace console\controllers;
class TestController extends \yii\console\Controller{
public function actionIndex($param1,$param2){
echo "\nIndex";
echo "\n$param1 $param2\n";
}
public function actionMango(){
echo "\nMango";
}
}
and then type ./yii and hit Enter it will show all default commands available along with the following at the end.
This is Yii version 2.0.14.1.
The following commands are available:
....
...
- test
test/index (default)
test/mango
which means it registers all the actions inside the controller as commands and if you write in the terminal the following command,
./yii test/index omer aslam
it will show you the output
Index
omer aslam
where omer and aslam are the 2 params passed to the function.
So you just need to prepend keyword action to your function name i would suggest using action names according to the convention, change the function from
public function _googleanalytics($start, $finish) {
to
public function actionGoogleanalytics($start, $finish) {
and then access it via
./yii process/googleanalytics 2017-02-27 2017-02-27
you can wrap with quotes but it isnt necessary to add one a space identitifies between separate params.
Hope it helps

How to set a Yii2 module to return JSON response using config/main.php

I am currently using the following lines of code on every controller in the API module in order to return JSON response/data.
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
return $behaviors;
}
It works well. But how can i achieve the same using main configuration file?
I tried the following on my frontend/config/main.php
'api' => [
'class' => 'app\modules\api\Module',
'components' => [
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
'enableSession' => false,
'loginUrl' => null,
],
'response' => [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => \yii\web\Response::FORMAT_JSON,
],
]
],// Module component
],
above configuration still returns XML response only. What is the correct configuration to set all the controllers in the API module to return JSON data.Thanks
Configure your response component as follows:
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
// ...
]
formats is an array containing the available formats. format is the actual output format.
Add this is your config/main-local.php
use yii\web\Response;
$config['bootstrap'][]=
[
'class' => '\yii\filters\ContentNegotiator',
'formats' => [
'text/html' => Response::FORMAT_JSON,
]
];

Yii2 Apply a function to all requests

Is there any way to apply a function to all requests and queries in Yii2?
I want to replace specific characters for all of them.
I am using Yii2 advanced app
Thanks.
This is the config file:
$config = [
'language' => 'en',
'components' => [
'request' => [
'cookieValidationKey' => 'something',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
'defaultRoles' => ['guest'],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
];
return $config;
Without extending custom code on each request can be exexuted like so (add this to your application config):
return [
'on beforeRequest' => function () {
if (!Yii::$app->get('user', false)) {
return;
}
$user = User::getCurrent();
if ($user) {
Yii::$app->setTimeZone($user->time_zone);
}
},
'on afterRequest' => function () {
...
},
];
Depending on when you need to execute code (before or after the request) use 'on beforeRequest' or 'on afterRequest' accordingly.
yii2 have a request component. You can extend yii\web\request and define your custom implementation.
[
...
'components' =>
'request' => [
'class' => '\common\MyRequest',
'addGeoLocationForExample' => true,
]
...