/** * @return static */ public function getList() { if ($this->model->hasParent()) { $model = ArticleModel::whereCategoryId($this->model->getAttribute('id')); } else { $relations = $this->getRelationCategoryList(); $list = Collection::make(); $list->push($this->model->getAttribute('id')); foreach ($relations as $relation) { $list->push($relation->getId()); } $model = ArticleModel::whereIn('category_id', $list->toArray()); } $data = $model->paginate(15); //$list = Collection::make(); //foreach($data as $value) { // $list->push(new Article($value->getAttribute('id'))); //} return $data; }
/** * @param $key * @param array $opinions * @return mixed */ public function position($key, $opinions = []) { $config = $this->config->get('page.recommends'); $config = array_merge($config[$key], $opinions); $articles = ArticleRecommend::wherePosition($key); if (isset($opinions['limit']) && $opinions['limit'] > 0) { $articles->take($config['limit']); } $articles = $articles->orderBy('created_at', 'desc')->get(); if (isset($config['thumbnail']) && is_array($config['thumbnail'])) { foreach ($articles as $k => $article) { $article = Article::find($article->article_id); if ($article) { preg_match_all("/<img([^>]*)\\s*src=('|\")([^'\"]+)('|\")/", $article->content, $matches); $hash = ''; $thumbnail = ''; if ($matches && $matches[3]) { $matches = array_unique($matches[3]); if ($matches[0] && $this->file->exists(public_path($matches[0]))) { $thumbnail = $matches[0]; $hash = hash_file('md5', public_path($matches[0]), false); } } if ($thumbnail && $hash) { $path = '/uploads/thumbnails/' . $config['thumbnail']['width'] . 'X' . $config['thumbnail']['height'] . '/' . $hash . '.' . $this->file->extension(public_path($thumbnail)); $directory = public_path('/uploads/thumbnails/' . $config['thumbnail']['width'] . 'X' . $config['thumbnail']['height'] . '/'); if (!$this->file->isDirectory($directory)) { $this->file->makeDirectory($directory, 0777, true, true); } if (!$this->file->exists(public_path($path))) { $image = Image::make($thumbnail, $config['thumbnail']); $image->save(public_path($path)); } $article->thumbnail = $path; } $articles->put($k, $article); } else { $articles->forget($k); } } } return $this->view->make($config['template'])->withArticles($articles); }
/** * @return int */ public function countArticles() { $count = Article::whereCategoryId($this->attributes['id'])->count(); return $count ? $count : 0; }
/** * @return mixed */ public function getTitle() { return $this->model->getAttribute('title'); }
/** * @param \Notadd\Article\Requests\ArticleEditRequest $request * @param $id * @return $this|\Illuminate\Http\RedirectResponse */ public function update(ArticleEditRequest $request, $id) { $article = Article::findOrFail($id); $request->offsetSet('user_id', $this->app->make('auth')->user()->id); $request->offsetSet('created_at', new Carbon($request->offsetGet('created_at'))); if ($article->update($request->all())) { return $this->redirect->to('admin/article'); } else { return $this->redirect->back()->withInput()->withErrors('保存失败!'); } }
/** * @return bool */ public function isCurrentRoute() { if (strpos($this->attributes['link'], 'http://') === false && strpos($this->attributes['link'], 'https://') === false) { if (strpos($this->attributes['link'], 'category') !== false) { $tmp = trim($this->attributes['link'], '/'); $tmp = explode('/', $tmp); if (is_numeric($tmp[1])) { $id = $tmp[1]; $collection = Collection::make(); $ids = []; if (Container::getInstance()->make('request')->route("article")) { $article = Article::find(Container::getInstance()->make('request')->route("article")); if ($article instanceof Article) { Category::getAllParentCategories($article->category->id, $collection); } foreach ($collection as $category) { $ids[] = $category->id; } $ids = array_unique($ids); if (in_array($id, $ids)) { return true; } } else { Category::getAllSubCategories($id, $collection); $ids[] = $id; foreach ($collection as $category) { $ids[] = $category->id; } $ids = array_unique($ids); $current = Container::getInstance()->make('request')->route("category"); if (in_array($current, $ids)) { return true; } } } } else { return Container::getInstance()->make('request')->is($this->attributes['link'] . '*') ? true : false; } } }
/** * @param \Notadd\Category\Events\BeforeCategoryDelete $event */ public function onDelete(BeforeCategoryDeleteEvent $event) { $category = $event->getCategory(); Article::whereCategoryId($category->id)->delete(); }