Skip to content

Commit

Permalink
Simple adding to favorites
Browse files Browse the repository at this point in the history
Concerns #253
  • Loading branch information
mikron-ia committed Oct 6, 2023
1 parent 21ed6c8 commit f24e1ae
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 9 deletions.
114 changes: 114 additions & 0 deletions frontend/controllers/ScribbleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace frontend\controllers;

use common\models\Scribble;
use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ServerErrorHttpException;

/**
* ScribbleController implements the CRUD actions for Scribble model.
*/
class ScribbleController extends Controller
{
public function behaviors()
{
return array_merge(
parent::behaviors(),
[
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => [
'reverse-favorite'
],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
],
]
);
}

public function actionReverseFavorite(int $id)
{
$scribble = $this->getModelWithValidation($id);
$scribble->favorite = !$scribble->favorite;
if (!$scribble->save()) {
throw new ServerErrorHttpException();
}
}

public function actionSetAsFavorite(int $id)
{
$scribble = $this->getModelWithValidation($id);
$scribble->favorite = true;
if (!$scribble->save()) {
throw new ServerErrorHttpException();
}
}

public function actionUnsetAsFavorite(int $id)
{
$scribble = $this->getModelWithValidation($id);
$scribble->favorite = false;
if (!$scribble->save()) {
throw new ServerErrorHttpException();
}
}

/**
* @param int $scribbleId
*
* @return Scribble
*
* @throws ForbiddenHttpException
* @throws MethodNotAllowedHttpException
* @throws NotFoundHttpException
*/
private function getModelWithValidation(int $scribbleId): Scribble
{
if (!Yii::$app->request->isAjax) {
throw new MethodNotAllowedHttpException(Yii::t('app', 'ERROR_AJAX_REQUESTS_ONLY'));
}

$scribble = $this->findModel($scribbleId);

if (!$scribble->scribblePack->canUserControlYou()) {
throw new ForbiddenHttpException(Yii::t('app', 'SCRIBBLE_DENIED_ACCESS'));
}

return $scribble;
}

/**
* Finds the Scribble model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param int $scribble_id Scribble ID
* @return Scribble the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($scribble_id)
{
if (($model = Scribble::findOne(['scribble_id' => $scribble_id])) !== null) {
return $model;
}

throw new NotFoundHttpException('The requested page does not exist.');
}
}
40 changes: 31 additions & 9 deletions frontend/views/scribble/_modal_box.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
<?php

use yii\widgets\DetailView;
use yii\helpers\Html;

/** @var yii\web\View $this */
/** @var common\models\Scribble $model */

$favoriteButtonTexts = [
false => Yii::t('app', 'SCRIBBLES_TITLE_NO'),
true => Yii::t('app', 'SCRIBBLES_TITLE_YES'),
];

?>
<div class="scribble-view">

<?= DetailView::widget([
'model' => $model,
'attributes' => [
'scribble_id',
'scribble_pack_id',
'user_id',
'favorite',
],
<?= Html::button($favoriteButtonTexts[$model->favorite], [
'id' => 'favorite-button',
'class' => 'btn btn-primary btn-block',
'data-scribble-id' => $model->scribble_id,
]) ?>

</div>

<script>
let favorite = <?= (int)$model->favorite ?>;
const isFavoriteText = "<?= $favoriteButtonTexts[true] ?>";
const isNotFavoriteText = "<?= $favoriteButtonTexts[false] ?>";

$('#favorite-button').on('click', function () {
$.ajax(
'../scribble/reverse-favorite',
{
method: "GET",
data: {
id: <?= $model->scribble_id ?>
}
}
).done(function () {
favorite = !favorite;
$(this).val(favorite ? isFavoriteText : isNotFavoriteText);
});
});
</script>

0 comments on commit f24e1ae

Please sign in to comment.