Exemplo n.º 1
0
 /**
  * @Route("category/edit", name="admin/category/edit")
  * @Access("download: manage categories")
  * @Request({"id": "int"})
  */
 public function editCategoryAction($id = 0)
 {
     if (!($category = Category::where(compact('id'))->related('files')->first())) {
         if ($id) {
             App::abort(404, __('Invalid file id.'));
         }
         $category = Category::create(['status' => 1, 'slug' => '']);
         $category->set('markdown', $this->download->config('markdown'));
     }
     return ['$view' => ['title' => $id ? __('Edit category') : __('Add category'), 'name' => 'bixie/download/admin/category.php'], '$data' => ['roles' => array_values(Role::findAll()), 'category' => $category], 'category' => $category];
 }
 /**
  * @Route("/", methods="POST")
  * @Route("/{id}", methods="POST", requirements={"id"="\d+"})
  * @Request({"category": "array", "id": "int"}, csrf=true)
  */
 public function saveAction($data, $id = 0)
 {
     if (!($category = Category::where(compact('id'))->related('files')->first())) {
         $category = Category::create();
         unset($data['id']);
     }
     if (!($data['slug'] = App::filter($data['slug'] ?: $data['title'], 'slugify'))) {
         App::abort(400, __('Invalid slug.'));
     }
     $category->updateOrdering($data);
     //unset array typed files
     unset($data['files']);
     $category->save($data);
     return ['message' => 'success', 'category' => $category];
 }
 /**
  * {@inheritdoc}
  */
 public function match(array $parameters = [])
 {
     if (isset($parameters['id'])) {
         return $parameters;
     }
     if (!isset($parameters['slug'])) {
         App::abort(404, 'File not found.');
     }
     if (!isset($parameters['category_id'])) {
         App::abort(404, 'No category provided.');
     }
     $slug = $parameters['slug'];
     $category_id = $parameters['category_id'];
     $id = false;
     foreach ($this->cacheEntries as $entry) {
         if ($entry['slug'] === $slug && $entry['category_id'] === $category_id) {
             $id = $entry['id'];
         }
     }
     if (!$id) {
         /** @var File $file */
         if (!($file = File::where(compact('slug'))->related('categories')->first())) {
             App::abort(404, 'File not found.');
         }
         if (!in_array($category_id, $file->getCategoryIds())) {
             App::abort(400, 'File not in category');
         }
         if (!($category = Category::where(['id' => $category_id])->first())) {
             App::abort(404, 'Category not found.');
         }
         $this->addCache($file, $category->id, $category->slug);
         $category_id = $category->id;
         $id = $file->id;
     }
     $parameters['id'] = $id;
     $parameters['category_id'] = $category_id;
     return $parameters;
 }
Exemplo n.º 4
0
 /**
  * @Route("/{id}", name="id")
  * @Request({"id":"int", "category_id":"int"})
  */
 public function fileAction($id = 0, $category_id = 0)
 {
     /** @var File $file */
     if (!($file = File::where(['id = ?', 'status = ?'], [$id, '1'])->where(function ($query) {
         return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
     })->first())) {
         App::abort(404, __('File not found.'));
     }
     $file->setActiveCategory($category_id);
     App::trigger('bixie.prepare.file', [$file, App::view()]);
     $file->content = App::content()->applyPlugins($file->content, ['file' => $file, 'markdown' => $file->get('markdown')]);
     $previous = File::getPrevious($file);
     $next = File::getNext($file);
     /** @var Category $category */
     if ($category_id && !($category = Category::where(['id = ?', 'status = ?'], [$category_id, '1'])->where(function ($query) {
         return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
     })->related('files')->first())) {
         App::abort(404, __('Category not found.'));
     }
     if ($breadcrumbs = App::module('bixie/breadcrumbs')) {
         if ($category_id) {
             $cat = $category;
             $crumbs = [['title' => $category->title, 'url' => $category->getUrl()]];
             while ($parent_id = $cat->parent_id) {
                 if ($cat = $cat->find($parent_id, true)) {
                     $crumbs[] = ['title' => $cat->title, 'url' => $cat->getUrl()];
                 }
             }
             foreach (array_reverse($crumbs) as $data) {
                 $breadcrumbs->addUrl($data);
             }
         }
         //add file
         $breadcrumbs->addUrl(['title' => $file->title, 'url' => $file->getUrl()]);
     }
     return ['$view' => ['title' => __($file->title), 'name' => 'bixie/download/file.php'], 'download' => $this->download, 'config' => $this->download->config(), 'previous' => $previous, 'next' => $next, 'file' => $file, 'node' => App::node()];
 }