Yii GroupGridView buttons - yii

I use this extension(GroupGridView) in my yii project.
Now, I need help. How to add crud buttons for the grouped elements, to get something like this:

I don't really know is it right sollution.
I've created component GroupButtonColumn:
class GroupButtonColumn extends CButtonColumn {
public $name = 'buttons';
public $value; }
And my view
$this->widget('ext.groupGridView.GroupGridView', array(
'dataProvider' => $model->search(),
'summaryText' => false,
'filter' => $model,
'mergeColumns' => ['t_id', 'buttons'],
'columns' => [
[
'name' => 't_id',
'header' => '№',
'value' => 'CHtml::link($data->t_id, ["/task/view", "id"=>$data->t_id])',
'filter' => false,
'type' => 'raw'
],
/* ... */
[
'class' => 'GroupButtonColumn',
'template' => '{update}, {delete}',
'buttons' => [
'update' => [
'url' => '$this->grid->controller->createUrl("/task/update", array("id"=>$data->t_id))'
],
'delete' => [
'url' => '$this->grid->controller->createUrl("/task/delete", array("id"=>$data->t_id))'
]
]
],
],
));

Try this way:
'mergeColumns' => array('project_id', 'project_id')
/* ... */
'columns' => [
'project_id',
'id',
/* ... */
[
'name' => 'project_id',
'header' => 'Action',
'type'=>'raw',
'value' => function($data){
return 'Start';
}
],

Related

CakePHP4 - How to create multiple user login with the Authentication plugin?

There are two types of users in the application I am developing. Users (table users.sql) for frontend users and AdminUsers (table admin_users.sql) for administration.
In CakePHP3, I solved this problem as follows with AuthComponent in AppController:
public function initialize()
{
parent::initialize();
// ...
//user login
if (!empty($this->request->params['prefix']) AND
$this->request->params['prefix'] == 'admin'
) {
$this->setAdminLogin();
}else{
$this->setUserLogin();
$this->Auth->allow();
}
// ...
}
//frontend users
public function setUserLogin()
{
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'edit'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login'
],
'authError' => false,
'authenticate' => [
'Xety/Cake3CookieAuth.Cookie' => [
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => ['username' => 'email','password' => 'password'],
],
'Form' => [
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => ['username' => 'email','password' => 'password'],
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => ['Default']
]
],
],
'storage' => ['className' => 'Session', 'key' => 'Auth.User']
]);
}
//admin users
public function setAdminLogin()
{
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'loginAction' => [
'controller' => 'AdminUsers',
'action' => 'login',
],
'loginRedirect' => [
'controller' => 'AdminHelps',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'AdminUsers',
'action' => 'login'
],
'authError' => false,
'authenticate' => [
'Form' => [
'userModel' => 'AdminUsers',
'scope' => ['AdminUsers.active' => 1],
'fields' => ['username' => 'email','password' => 'password'],
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => ['Default']
]
],
],
'storage' => ['className' => 'Session', 'key' => 'Auth.AdminUser']
]);
}
How can I do the same thing in CakePHP4 version with Authentication plugin? How can I create multiple user login?

How to add 'sub elements' in Zend Form

I have a Zend Form with several fields for the user to complete. In the admin system I want to display the exact same form but with additional Elements next to the existing ones where the administrator will be able to provide feedback to the user's input.
Below you can see the code I am using to create the form that the user sees.
Before posting this question I had a look on Zend Form Decorator but I did not understand if that's what I need to resolve this issue.
public function __construct()
{
parent::__construct('user-feedback-form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
$this->add([
'name' => 'name',
'type' => Text::class,
'attributes' => [
'id' => 'name',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'surname',
'type' => Text::class,
'attributes' => [
'id' => 'surname',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'age',
'type' => Number::class,
'attributes' => [
'id' => 'age',
'required' => true,
'readonly' => false,
],
]);
}
To reuse certain parts of a form zend provides the fieldsets.
Instead of adding the elements to your form add them toa filedset and add the fieldset to the form.
class UserFeedbackFieldset extends Zend\Form\Fieldset
{
public function init()
{
$this->add([
'name' => 'name',
'type' => Text::class,
'attributes' => [
'id' => 'name',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'surname',
'type' => Text::class,
'attributes' => [
'id' => 'surname',
'required' => true,
'readonly' => false,
],
]);
$this->add([
'name' => 'age',
'type' => Number::class,
'attributes' => [
'id' => 'age',
'required' => true,
'readonly' => false,
],
]);
}
}
Then in your forms add the fieldset:
class UserFeedbackForm extends Zend\Form\Form
{
public function __construct()
{
parent::__construct('user-feedback-form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
}
public function init()
{
$this->add([
'type' => UserFeedbackFieldset::class,
'name' => 'user',
'options' => [
'use_as_base_fieldset' => true,
],
]);
}
}
class AdminUserFeedbackForm extends Zend\Form\Form
{
public function __construct()
{
parent::__construct('user-feedback-form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
}
public function init()
{
$this->add([
'type' => UserFeedbackFieldset::class,
'name' => 'user',
'options' => [
'use_as_base_fieldset' => true,
],
]);
$this->add([
'name' => 'someotherfield',
'type' => Text::class,
'attributes' => [
'id' => 'someotherfield',
'required' => true,
'readonly' => false,
],
]);
}
}
Then you can use the other form on your admin page instead of the original one.

Not added to data to table

I'm trying to validate the form.
I do not understand why form validation does not take place. Logically, it should work with the simple presence of symbols, but in fact, a simple test:
if (! $form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
returned " echo 'not valid in check' "
What is the problem?
Input filter are very simple:
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'text',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000,
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}
Form? for added post:
class PostForm extends Form
{
public function __construct($name = null)
{
// We will ignore the name provided to the constructor
parent::__construct('post');
$this->add([
'name' => 'id',
'type' => 'hidden',
]);
$this->add([
'name' => 'title',
'type' => 'text',
'options' => [
'label' => 'Title',
],
]);
$this->add([
'name' => 'text',
'type' => 'textarea',
'options' => [
'label' => 'Text',
],
]);
$this->add([
'name' => 'submit',
'type' => 'submit',
'attributes' => [
'value' => 'Go',
'id' => 'submitbutton',
],
]);
}
}
Add action, if it need:
public function addAction()
{
$form = new PostForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if (! $request->isPost()) {
return ['form' => $form];
}
// echo 'test';
$post = new Post();
$form->setInputFilter($post->getInputFilter());
$form->setData($request->getPost());
// echo 'before valid';
if (! $form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
echo '\n';
echo 'valid';
$post->exchangeArray($form->getData());
$this->table->savePost($post);
return $this->redirect()->toRoute('post');
}
public function addAction()
{
---------------
if (!$form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
---------
}
Validation:
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'text',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000,
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$inputFilter->add([
'name' => 'disc',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}

Kartik Grid Editable Column with expandrow styling not applying

When I pull in a kartik expandable row grid, using pjax the editablecolumn styling is not being applied. What can I do to apply css to this column?
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => false,
'columns' => [
[
'class' => 'kartik\grid\ExpandRowColumn',
'value' => function ($model, $key, $index, $column){
return GridView::ROW_COLLAPSED;
},
'detailUrl' => 'index.php?r=controller/detail'
],
Expanded View
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'pjax'=> true,
'columns' => [
[
'attribute' => 'brand_name',
'value' => function($model,$key,$idx,$col){
return $model['brand_name'];
},
],
[
'class'=>'kartik\grid\EditableColumn',
'hAlign'=>'center',
'vAlign'=>'middle',
'value' => function($model,$key,$idx,$col){
return 100;
},
'editableOptions' => [
'name' => 'Test',
'header' => 'Test22',
'inputType' => Editable::INPUT_TEXT,
'formOptions' => ['action' => ['/book/editbook']],
'options' => [
'convertFormat'=>true,
'pluginOptions' => ['format' => 'php:Y-m-d']
]
]
],
Maybe you should try to set HTML with containerOptions or contentOptions attribute in editableOptions.

access to subfolders controller and view in YII2

hi i built a controller in a folder inside controllers folder
i tryed to access my controller and its view but i couldnt always error 404
please tell me what is the problem
here is the details
this is SiteUserController in Controllers/userzone/ folder
namespace app\controllers\userzone;
use yii\web\Controller;
use app\models\UserZone;
/**
* Default controller for the `dashboard` module
*/
class SiteUserController extends Controller
{
/**
* Renders the index view for the module
* #return string
*/
public function actionIndex()
{
$id = \Yii::$app->user->id;
$model = UserZone::find()->where(['id_zone'=>$id])->with('user')->one();
// $model->joinWith('companiesCompany');
return $this->render('siteuser/index',[
'model'=>$model
]);
}
}
the view file is in Views/siteuser/index.php directory .
i changed url manager to
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'userzone/<controller:\w+>/<action:\w+>'=>'userzone/<controller>/<action>',
],
],
In Controller:
return $this->render('index',[
'model'=>$model
]);
Its Working
You need to change you controllerNamespace in config/main file i.e. below code for config/main.php
<?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')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\controllers\userzone', //here is your controller path
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#frontend/views' => '#themes/frontend/views',
],
],
],
'request' => [
'csrfParam' => '_csrf-frontend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];