implement function for hiding buttons for guest users Yii2 - php

First i want to say that i know there is many post about jQuery and Yii2 around the network but still can't make it clear for me! My question is pretty simple for you i think. When a user is with User role permission he shouldn't be able to update or delete others posts. And so if the user is this kind of guy i want to make both anchors ('Edit' and 'Delete') with opacity 0.5.
This is my view part:
<?php $form = \yii\bootstrap\ActiveForm::begin([
'method' => 'post'
]);
?>
<div class="col-md-8 text-left">
<?= Html::a('Edit', ['update', 'id' => $model->post_id], ['class' => 'btn btn-primary']); ?>
<?=
Html::a('Delete', ['delete', 'id' => $model->post_id],
['data-method' => 'POST', 'class' => 'btn btn-danger']);
?>
</div>
<div class="col-md-4 text-right">
<?= Html::a('Back', 'index', ['class' => 'btn btn-warning']); ?>
</div>
<?php \yii\bootstrap\ActiveForm::end(); ?>
I have added my file to AppAsset.php(test.js):
public $js = [
'js/slide-show.js',
'js/test.js'
];
I know how to make my fucntion but do not know how to implement it. Think i should do it with if statement (if(user->isGuest){ execute function and make anchros with opacity 0.5 }). Can you guys teach me the right way? Will be thankful for every advice! Thank you in advance!

You could assign a disable option so the button is displayed but disabled
if (Yii::$app->user->isGuest) {
echo Html::a('Edit', ['update', 'id' => $model->post_id], ['class' => 'btn btn-primary',
'disabled' => 'disabled']);
echo Html::a('Delete', ['delete', 'id' => $model->post_id],
['data-method' => 'POST', 'class' => 'btn btn-danger',
'disabled' => 'disabled']);
}

Related

CakePHP 4 FormProtector instance has not been created error

I am currently in the process of updating a (rather big) application from CakePHP 3 to 4.
I have this template:
<?= $this->Form->create(
$dmpLayer,
[
'url' => [
'controller' => 'DmpLayers',
'action' => 'edit',
],
]
); ?>
<div class="row">
<div class="col-4">
<?= $this->element('DataLayers/layer-table'); ?>
</div>
<div id="form-div" class="col-6">
<div class="layer-form">
<?= $this->element('DataLayers/form') ?>
</div>
</div>
<div class="col-2">
<div class="layer-form">
<h2>Form Actions</h2>
<?= $this->Form->submit('Create/Update Layer', ['class' => 'btn btn-success']); ?>
</div>
</div>
</div>
<?= $this->Form->end(); ?>
<?= $this->Html->script('data-layers'); ?>
which includes the DataLayers/form element:
<div class="row">
<div class="col-12">
<h4>Artist Layer</h4>
<?php
echo $this->Html->tag('fieldset', $this->element(
'actions/add',
[
'url' => [
'prefix' => 'Admin',
'plugin' => false,
'controller' => 'SegmentCores',
'action' => 'add',
],
]
)
. $this->Form->control('artist_layer.segment_cores[]', [
'multiple',
'options' => $segmentCores,
'label' => 'Segment Core',
'value' => $selectedValues['segment_cores'],
])
. $this->Form->control('artist_layer.segment_potentials[]', [
'multiple',
'options' => $segmentPotentials,
'label' => 'Segment Potential',
'value' => $selectedValues['segment_potentials'],
])
. $this->Form->control('artist_layer.layer_tags[]', [
'multiple',
'options' => $layerTags,
'label' => 'Artist Tag',
'value' => $selectedValues['artist_tags'],
])
. $this->Form->control('artist_layer.genres[]', [
'empty' => 'No genre set',
'options' => $genres,
'label' => 'Genre',
'value' => $selectedValues['genres'],
]);
?>
</div>
</div>
<?php
$this->Form->unlockField('artist_layer.genres');
$this->Form->unlockField('artist_layer.segment_cores');
$this->Form->unlockField('artist_layer.segment_potentials');
$this->Form->unlockField('artist_layer.layer_tags');
?>
In the initialize function of the AppController I have this:
$this->loadComponent('Security');
When I visit the page, it doesn't render and I immediately get this error:
FormProtector instance has not been created. Ensure you have loaded the FormProtectionComponent in your controller and called FormHelper::create() before calling FormHelper::unlockField()
This is the only form in my application for which this error happens. Every other form is working fine, and I am calling the Form->unlockField() function in many of them.
I am obviously calling Form->create() in my code, so is this because I am including an element to add fields to the form that is defined in the "main" template? Or is there some other explanation?
I have already attempted adding
$this->loadComponent('FormProtection');
to my AppController, but this causes a whole lot more problems in many other places in the app, and it doesn't solve the problem anyway (the page renders, but I get an error when submitting the form to save the data).

delete button with confirmation in yii2

This view.php is generated with CRUD in Yii2 but the delete button doesn't confirm window.
But in index.php generated with CRUD in Yii2, The confirm window for delete worked.
<p>
<?= Html::a('Edit', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
<?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
</p>
Delete Button in index.php:
<span class="glyphicon glyphicon-trash"></span>
Delete Button in view.php:
<a class="btn btn-danger" href="var/delete?id=2" data-confirm="Are you sure you want to delete this item?" data-method="post">Delete</a>
The section about yii.js is still in progress.
To get default support of confirmation you should register yii.js.
Using yii\web\YiiAsset
use yii\web\YiiAsset;
YiiAsset::register($this);
or registering script files (not recommend, just for example)
$this->registerJsFile('#yii/assets/yii.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

How to identify the two button clicks of the same form in Yii2

I have two buttons named search,create in the same form called search.How to identify these two button clicks in the controller function.
<?= Html::a(Yii::t('app', 'Search'), ['search'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Create'), ['search'], ['class' => 'btn btn-success']) ?>
It is a little strange what you are asking. But here goes nothing.
First you can always just add a parameter to the link, for example:
<?= Html::a(Yii::t('app', 'Search'), ['search', 'button' => 'search'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Create'), ['search', 'button' => 'create'], ['class' => 'btn btn-success']) ?>
This will create 2 different links and you can use the GET parameter to figure out what was clicked.
What I actually think you are trying to do is to submit a form. In Bootstrap the buttons and links look the same. You actually have 2 links, you do not have 2 buttons. The simple solution would just to turn those links into actual buttons and give them a name and a value.
<?= Html::button(Yii::t('app', 'Search'),
[
'name'=>'button',
'value'=>"search",
'class' => 'btn btn-success'
]
)?>
<?= Html::button(Yii::t('app', 'Create'),
[
'name'=>'button',
'value'=>"create",
'class' => 'btn btn-success'
]
)?>
If your form is using GET to transmit data check the GET parameter to figure out what was clicked, otherwise use POST to figure out the same thing.

create pjax form inside a active form yii2

I can't create a Pjax form inside an active form. What I've found is that when I put the Pjax inside an active form it doesn't work (it submits the entire page) but when I put it outside the active form its works fine:
<?php
$form = ActiveForm::begin([
'id' => 'CouplePartnerForm',
'action' => ['couple-partner/savecouplepartner'],
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'validateOnSubmit' => true,
]);
?>
<div class="row">
<?php Pjax::begin([]); ?>
<?= Html::beginForm(['couple-partner/form-submission'], 'post', ['data-pjax' => '0', 'class' => 'form-inline']); ?>
<?= Html::input('text', 'string', Yii::$app->request->post('string'), ['class' => 'form-control']) ?>
<?= Html::submitButton('Hash String', ['class' => 'btn btn-lg btn-primary', 'name' => 'hash-button']) ?>
<?= Html::endForm() ?>
<h3><?= isset($stringHash) ?$stringHash :"" ?></h3>
<?php Pjax::end(); ?>
</div>
<?php ActiveForm::end(); ?>
So what is the problem here?

Action buttons only visible to admin yii2

I tried to add extra action buttons. Admin only view this button after button click update in a single field in the database.
<p>
<?php
if(!Yii::$app->user->isGuest ){
echo Html::a('Recommended', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to reject this application?',
'method' => 'post',
],
]);
} else if(Yii::$app->user->can('admin')){
}
?>
</p>
My problem is that I have 3 users that are: applicant, faculty and admin (or hod). In this case after faculty recommendation, the admin (or hod) sanctioned the leave.
I create leave application and faculty recommended, so now I want get the recommended data when admin login to the site.
If admin is the username you should follow this way :
<p>
<?php
if(!Yii::$app->user->isGuest ){
echo Html::a('Recommended', ['update', 'id' => $model->id],
['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to reject this application?',
'method' => 'post',
],
]);
} else if(Yii::$app->user->identity->username == 'admin' ){
echo Html::a('Your Button Label for Admin',
['yourActionForAdmin', 'id' => $model->id],
['class' => 'btn btn-primary']);
}
?>
</p>

Categories