Пример #1
0
 /**
  * get articles
  *
  * @param Request                $request         request
  * @param BoardPermissionHandler $boardPermission board permission
  * @param null                   $id              document id
  * @return mixed
  */
 public function articles(Request $request, BoardPermissionHandler $boardPermission, $menuUrl, $id = null)
 {
     if (Gate::denies(BoardPermissionHandler::ACTION_LIST, new Instance($boardPermission->name($this->instanceId)))) {
         throw new AccessDeniedHttpException();
     }
     $query = $this->handler->getModel($this->config)->where('instanceId', $this->instanceId)->visible();
     if ($this->config->get('category') === true) {
         $query->leftJoin('board_category', sprintf('%s.%s', $query->getQuery()->from, 'id'), '=', sprintf('%s.%s', 'board_category', 'targetId'));
     }
     if ($request->has('favorite') === true) {
         $query->leftJoin('board_favorites', sprintf('%s.%s', $query->getQuery()->from, 'id'), '=', sprintf('%s.%s', 'board_favorites', 'targetId'));
         $query->where('board_favorites.userId', Auth::user()->getId());
     }
     $this->handler->makeWhere($query, $request, $this->config);
     $this->handler->makeOrder($query, $request, $this->config);
     // eager loading favorite list
     $query->with(['favorite' => function ($favoriteQuery) {
         $favoriteQuery->where('userId', Auth::user()->getId());
     }, 'slug', 'data']);
     Event::fire('xe.plugin.board.articles', [$query]);
     if ($id !== null) {
         $request->query->set('page', $this->handler->pageResolver($query, $this->config, $id));
     }
     $paginate = $query->paginate($this->config->get('perPage'))->appends($request->except('page'));
     $fieldTypes = (array) $this->configHandler->getDynamicFields($this->config);
     $categories = [];
     if ($this->config->get('category') === true) {
         $categoryItems = Category::find($this->config->get('categoryId'))->items;
         foreach ($categoryItems as $categoryItem) {
             $categories[] = ['value' => $categoryItem->id, 'text' => $categoryItem->word];
         }
     }
     return XePresenter::makeApi(['paginate' => $paginate, 'categories' => $categories, 'fieldTypes' => $fieldTypes]);
 }