/**
  * Restituisce, paginati, gli articoli presenti sul database appartenenti alla categoria $category.
  * Se $onlyPublished è true, solo quelli mandati in pubblicazione. Se $onlyVisible è true, solo quelli
  * già pubblicati e già visibili.
  *
  * @param Category $category
  * @param $page
  * @param bool $onlyPublished
  * @param bool $onlyVisible
  * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  */
 public function getByCategory(Category $category, $page, $onlyPublished = false, $onlyVisible = false)
 {
     $query = $category->articles()->getQuery()->with(['user', 'categories', 'series'])->orderBy('published_at', 'desc');
     if ($onlyPublished) {
         $query->published();
     }
     if ($onlyVisible) {
         $query->visible();
     }
     return $query->paginate(Config::get('publications.articles_per_page'), ['*'], 'page', $page);
 }
 public function testGetByCategory()
 {
     $category = \LaravelItalia\Domain\Category::createFromName('Category');
     $category->save();
     $this->saveTestArticle(false, false, null, $category);
     $this->saveTestArticle(true, false, null, $category);
     $this->assertCount(1, $this->repository->getByCategory($category, 1, true));
     $this->assertCount(2, $this->repository->getByCategory($category, 1, false));
 }
 /**
  * Register any other events for your application.
  *
  * @param \Illuminate\Contracts\Events\Dispatcher $events
  */
 public function boot(DispatcherContract $events)
 {
     parent::boot($events);
     Article::observe(DetachCategoriesBeforeArticleDelete::class);
     Media::observe(UploadFileWhenAddingMedia::class);
     Media::observe(RemoveFileWhenDeletingMedia::class);
     Series::observe(RemoveArticlesWhenDeletingSeries::class);
     Category::observe(DetachArticlesWhenDeletingCategory::class);
 }
 /**
  * Salva una nuova categoria sul sito, il cui nome è contenuto in $request.
  *
  * @param SaveCategoryRequest $request
  * @param CategoryRepository $categoryRepository
  * @return \Illuminate\Http\RedirectResponse
  */
 public function postAdd(SaveCategoryRequest $request, CategoryRepository $categoryRepository)
 {
     $category = Category::createFromName($request->get('name'));
     try {
         $categoryRepository->save($category);
     } catch (NotSavedException $e) {
         return redirect('admin/categories')->with('error_message', 'Impossibile aggiungere la categoria. Riprovare.');
     }
     return redirect('admin/categories')->with('success_message', 'Categoria aggiunta con successo.');
 }
 private function prepareTestCategory($name)
 {
     $category = Category::createFromName($name);
     return $category;
 }
 /**
  * Rimuove dal database la categoria $category.
  *
  * @param Category $category
  * @throws NotDeletedException
  * @throws \Exception
  */
 public function delete(Category $category)
 {
     if (!$category->delete()) {
         throw new NotDeletedException();
     }
 }
 public function deleting(Category $category)
 {
     $category->articles()->detach();
 }
 private function findOrCreateCategoriesFor($article)
 {
     $categories = new Collection();
     /* @var CategoryRepository $categoryRepository */
     $categoryRepository = app(CategoryRepository::class);
     foreach ($article['categories'] as $oldCategory) {
         try {
             $category = $categoryRepository->findBySlug($oldCategory['slug']);
         } catch (NotFoundException $e) {
             $category = Category::createFromName($oldCategory['name']);
             $categoryRepository->save($category);
         }
         $categories->add($category);
     }
     return $categories;
 }