Example #1
0
 /**
  * Change content item rating action
  * @param string $type
  * @param int $id
  * @throws NativeException
  * @throws ForbiddenException
  * @throws NotFoundException
  * @return string
  */
 public function actionChangerate($type, $id)
 {
     // check input params
     if (!Arr::in($type, ['plus', 'minus']) || !Obj::isLikeInt($id)) {
         throw new NativeException('Bad conditions');
     }
     // get current user and check is authed
     $user = App::$User->identity();
     if ($user === null || !App::$User->isAuth()) {
         throw new ForbiddenException(__('Authorization is required!'));
     }
     // set ignored content id to rate in session
     $ignored = App::$Session->get('content.rate.ignore');
     $ignored[] = $id;
     App::$Session->set('content.rate.ignore', $ignored);
     // find content record
     $record = ContentRecord::find($id);
     if ($record === null || $record->count() < 1) {
         throw new NotFoundException(__('Content item is not founded'));
     }
     // check if author rate him-self content
     if ($record->author_id === $user->getId()) {
         throw new ForbiddenException(__('You can not rate your own content'));
     }
     // initialize model
     $model = new ContentRatingChange($record, $type, $user);
     // check if content items is already rated by this user
     if ($model->isAlreadyRated()) {
         throw new ForbiddenException(__('You have already rate this!'));
     }
     // make rate - add +1 to content rating and author rating
     $model->make();
     return json_encode(['status' => 1, 'rating' => $model->getRating()]);
 }
Example #2
0
 /**
  * Show content global delete
  * @return string
  * @throws NotFoundException
  * @throws SyntaxException
  * @throws NativeException
  */
 public function actionGlobdelete()
 {
     // get content ids from request
     $ids = $this->request->query->get('selected');
     // check if input is array
     if (!Obj::isArray($ids) || count($ids) < 1) {
         throw new NotFoundException(__('Nothing to delete is founded'));
     }
     // get all records as object from db
     $records = ContentEntity::find($ids);
     if ($records->count() < 1) {
         throw new NotFoundException(__('Nothing to delete is founded'));
     }
     // init model and pass objects
     $model = new FormContentGlobDelete($records);
     // check if delete is submited
     if ($model->send() && $model->validate()) {
         $model->make();
         App::$Session->getFlashBag()->add('success', __('Content are successful removed'));
         $this->response->redirect('content/index');
     }
     // return response
     return $this->view->render('content_glob_delete', ['model' => $model]);
 }