-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Concerns #253
- Loading branch information
Showing
2 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |