public function onStart() { $slug = $this->param('slug'); $this['category'] = Category::where('slug', '=', $slug)->first(); if ($this['category']) { $post = new Post(); $query = $post->isPublished()->orderBy('published_at', 'desc')->with('categories'); $this['posts'] = $post->scopeFilterCategories($query, [$this['category']->id])->get(); } }
/** * Load the blog posts * * @param int $maxItems * * @return mixed */ public function loadData($maxItems = self::MAX_NO_ITEMS) { $posts = null; if (!empty($this->data)) { return $this->data; } $model = new Post(); $posts = $model->listFrontEnd(['sort' => 'published_at DESC', 'perPage' => $maxItems]); foreach ($posts as $post) { $post->setUrl($this->page, $this->controller); $post->comments_url = "{$post->url}/#{$this->commentsAnchor}"; $post->feed_content = true === $this->displayFullContent ? $post->content : $post->summary; } return $this->data = $posts; }
/** * Load post and start building query for related posts */ public function onRun() { // Load the target post $post = Post::where('slug', $this->property('slug'))->with('tags')->first(); // Abort if there is no source, or it has no tags if (!$post || !($tagIds = $post->tags->lists('id'))) { return; } // Start building our query for related posts $query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($tag) use($tagIds) { $tag->whereIn('id', $tagIds); })->with('tags'); // Sort the related posts $subQuery = DB::raw('( select count(*) from `bedard_blogtags_post_tag` where `bedard_blogtags_post_tag`.`post_id` = `rainlab_blog_posts`.`id` and `bedard_blogtags_post_tag`.`tag_id` in (' . implode(', ', $tagIds) . ') )'); $key = $this->property('orderBy') ?: $subQuery; $query->orderBy($key, $this->property('direction')); // Limit the number of results if ($take = intval($this->property('results'))) { $query->take($take); } // Execute the query $this->posts = $query->get(); }
public function boot() { Post::extend(function ($model) { $model->bindEvent('model.afterFetch', function () use($model) { $model->view += 1; $model->save(); }); }); }
/** * * Returns array of post_count random posts * * @return Collection */ protected function getPosts() { $count = $this->property('postsCount'); $posts = Post::orderBy(DB::raw('RAND()'))->take($count)->get(); foreach ($posts as $post) { $post->url = $post->setUrl($this->property('postPage'), $this->controller); } $this->cachePosts($posts); return $posts; }
public function onStart() { $slug = $this->param('slug'); $this['post'] = Post::where('slug', '=', $slug)->isPublished()->with('categories')->first(); if (!$this['post']) { return Redirect::to('/404'); } // only to change the title to be more pleasant $this->page->settings['title'] = "Post - " . $this["post"]->title; }
/** * * Returns date (Carbon object) set to the date of the very first post * or to current date if there are no posts * * @return Carbon */ protected static function getFirstDate() { $post = BlogPost::orderBy('published_at', 'asc')->isPublished()->first(); if (!$post) { $date = new Carbon(); } else { $date = new Carbon($post->published_at); } $date->setTime(0, 0); return $date; }
public function run() { Post::create(['title' => 'First blog post', 'slug' => 'first-blog-post', 'content' => ' This is your first ever **blog post**! It might be a good idea to update this post with some more relevant content. You can edit this content by selecting **Blog** from the administration back-end menu. *Enjoy the good times!* ', 'excerpt' => 'The first ever blog post is here. It might be a good idea to update this post with some more relevant content.', 'published_at' => Carbon::now(), 'published' => true]); Category::create(['name' => trans('rainlab.blog::lang.categories.uncategorized'), 'slug' => 'uncategorized']); }
protected function listPosts() { /* * List all the posts */ $posts = BlogPost::leftJoin('vdomah_blogviews_views as pv', 'pv.post_id', '=', 'rainlab_blog_posts.id')->orderBy('views', 'DESC')->limit($this->postsLimit)->get(); $posts->each(function ($post) { $post->setUrl($this->postPage, $this->controller); }); return $posts; }
public function boot() { PostModel::extend(function ($model) { $model->attachMany['featured_files'] = ['System\\Models\\File', 'order' => 'sort_order', 'delete' => true]; }); PostsController::extendFormFields(function ($form, $model) { if (!$model instanceof PostModel) { return; } $form->addSecondaryTabFields(['featured_files' => ['label' => 'hambern.featuredfiles::lang.plugin.name', 'tab' => 'rainlab.blog::lang.post.tab_manage', 'type' => 'fileupload', 'mode' => 'file']]); }); }
public function onRun() { $all_data = RssModel::all(); foreach ($all_data as $rss_data) { // for loading sources $rss = simplexml_load_file($rss_data->url); $last_updated_date = (int) $rss_data->last_build_date; foreach ($rss->channel->item as $item) { if ($last_updated_date >= (int) strtotime($item->pubDate)) { break; } else { // update rainlab blog RssModel here $blog_post = new Post(); $blog_post->title = $item->title; $temp_slug = str_replace(array("http://www.gamespot.com/articles/", "http://www.gamespot.com/reviews/", "http://ap.ign.com/"), "", $item->link); $blog_post->slug = str_replace("/", "-", $temp_slug); $blog_post->content = str_replace("<iframe src=\" ", "<iframe src= \"www.gamespot.com", $item->description); $blog_post->excerpt = substr(strip_tags($item->description), 0, 500); $blog_post->published = '0'; // set it to 0 if moderator wants to publish it manually $blog_post->published_at = strtotime($item->pubDate); $blog_post->save(); } } if ($last_updated_date != (int) strtotime($rss->channel->item[0]->pubDate)) { $rss_data->last_build_date = strtotime($rss->channel->item[0]->pubDate); $rss_data->save(); } } // these following codes are required for updating rss_sources table //$rss_post = new RssModel(); //$rss_post->name = "Gamespot News "; //$rss_post->name = "Gamespot Reviews "; //$rss_post->name = "Ign Rss "; //$rss_post->url = "http://www.gamespot.com/feeds/news/"; //$rss_post->url = "http://www.gamespot.com/feeds/reviews/"; //$rss_post->url = "http://ap.ign.com/feed.xml"; //$rss_post->last_build_date = ""; //$rss_post->save(); }
protected function loadPost() { $slug = $this->property('slug'); $post = BlogPost::isPublished()->where('slug', $slug)->first(); /* * Add a "url" helper attribute for linking to each category */ if ($post && $post->categories->count()) { $post->categories->each(function ($category) { $category->setUrl($this->categoryPage, $this->controller); }); } return $post; }
/** * Returns an array of translated values for this field * @return array */ public function getSaveValue($value) { $localeData = $this->getLocaleSaveData(); /* * Set the translated values to the model */ if ($this->model->methodExists('setTranslateAttribute')) { foreach ($localeData as $locale => $value) { $this->model->setTranslateAttribute('content', $value, $locale); $this->model->setTranslateAttribute('content_html', Post::formatHtml($value), $locale); } } return array_get($localeData, $this->defaultLocale->code, $value); }
/** * {@inheritdoc} */ public function boot() { // Extend attachment file to the post relation RainLabPost::extend(function (\October\Rain\Database\Model $model) { $model->attachMany['attachments'] = [\System\Models\File::class]; }); RainLabPosts::extendFormFields(function (\Backend\Widgets\Form $form, $model, $context) { if (!$model instanceof RainLabPost) { return; } // Add attachments to the "Manage" tab $form->addSecondaryTabFields(['attachments' => ['mode' => 'file', 'label' => 'reizinixc.blogattachment::lang.post.attachments', 'tab' => 'rainlab.blog::lang.post.tab_manage', 'type' => 'fileupload']]); }); }
/** * Add tags field to blog posts */ public function boot() { // Extend the posts model to establish the new tags relationship PostModel::extend(function ($model) { $model->belongsToMany['tags'] = ['Bedard\\BlogTags\\Models\\Tag', 'table' => 'bedard_blogtags_post_tag', 'order' => 'name']; }); // Extend the controller and add the tags field PostsController::extendFormFields(function ($form, $model, $context) { if (!$model instanceof PostModel) { return; } $form->addSecondaryTabFields(['tags' => ['label' => 'Tags', 'tab' => 'rainlab.blog::lang.post.tab_categories', 'type' => 'tagbox']]); }); }
protected function listPosts() { /* * List all the posts, eager load their categories */ $posts = BlogPost::listFrontEnd(['page' => $this->property('pageNumber'), 'sort' => $this->property('sortOrder'), 'perPage' => $this->property('postsPerPage')]); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->postPage, $this->controller); }); return $posts; }
private function loadLatestPost($numberOfPost) { $posts = Post::with('categories')->isPublished()->orderBy('published_at', 'desc')->take($numberOfPost)->get(); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->property('postPage'), $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->property('categoryPage'), $this->controller); }); }); return $posts; }
private function loadMostViewedPost($numberOfPost) { $posts = Post::isPublished()->where('view', '>', $this->property('minimalView'))->orderBy('view', 'desc')->take($numberOfPost)->get(); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->property('postPage'), $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->property('categoryPage'), $this->controller); }); }); return $posts; }
protected function loadPosts() { /* * List all the posts, eager load their categories */ $posts = Post::with('tags')->whereHas('tags', function ($tag) { $tag->whereSlug($this->property('slug')); })->listFrontEnd(['page' => $this->property('pageNumber'), 'sort' => $this->property('sortOrder'), 'perPage' => $this->property('postsPerPage')]); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->postPage, $this->controller); }); return $posts; }
protected function loadPost() { // @deprecated remove if year >= 2015 $deprecatedSlug = $this->propertyOrParam('idParam'); $slug = $this->property('slug', $deprecatedSlug); $post = BlogPost::isPublished()->where('slug', '=', $slug)->first(); /* * Add a "url" helper attribute for linking to each category */ if ($post && $post->categories->count()) { $post->categories->each(function ($category) { $category->setUrl($this->categoryPage, $this->controller); }); } return $post; }
public function boot() { PostComponent::extend(function ($component) { if ($this->app->runningInBackend()) { return; } if (!Session::has('postsviewed')) { Session::put('postsviewed', []); } $post = PostModel::where('slug', $component->getController()->getRouter()->getParameters()['slug'])->first(); if (!is_null($post) && !in_array($post->getKey(), Session::get('postsviewed'))) { $this->setViews($post); Session::push('postsviewed', $post->getKey()); } return true; }); }
/** * Load post and start building query for related posts */ public function onRun() { $post = Post::where('slug', $this->property('slug'))->with('tags')->first(); if (!$post) { return; } $this->tagIds = []; foreach ($post->tags as $tag) { $this->tagIds[] = $tag->id; } if (empty($this->tagIds)) { return; } $this->query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($query) { $query->whereIn('id', $this->tagIds); })->with('tags'); }
protected function listPosts() { $categories = $this->category ? $this->category->id : null; /* * List all the posts, eager load their categories */ $posts = BlogPost::with('categories')->listFrontEnd(['page' => 1, 'sort' => $this->property('sortOrder'), 'perPage' => 4, 'categories' => $categories]); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->postPage, $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->categoryPage, $this->controller); }); }); return $posts; }
public function boot() { // extend post model PostModel::extend(function ($model) { // add a hasone relationship with the featuredvideo model $model->hasOne['featuredvideo'] = ['Simplicitylab\\BlogFeaturedVideo\\Models\\FeaturedVideo']; }); // extend form fields PostsController::extendFormFields(function ($widget, $model, $context) { if ($context != 'update') { return; } if (!FeaturedVideo::getFromPost($model)) { return; } // add featured video textarea $widget->addFields(['featuredvideo[iframe_content]' => ['label' => 'simplicitylab.blogfeaturedvideo::lang.backend.featuredvideo', 'tab' => 'rainlab.blog::lang.post.tab_manage', 'type' => 'textarea', 'size' => 'small', 'comment' => 'simplicitylab.blogfeaturedvideo::lang.backend.description']], 'secondary'); }); }
private function loadRelatedPost($numberOfPost) { $post = Post::isPublished()->where('slug', $this->property('slug'))->with('categories')->firstOrFail(); if ($this->property('filter') == 'tag') { $posts = $post->tags()->first()->posts()->isPublished()->with('categories')->orderBy('published_at', 'desc')->take($numberOfPost)->get(); } else { $posts = $post->categories()->first()->posts()->isPublished()->with('categories')->orderBy('published_at', 'desc')->take($numberOfPost)->get(); } /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->property('postPage'), $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->property('categoryPage'), $this->controller); }); }); return $posts; }
/** * Extend rainlab.blog plugin * * @return void */ public function boot() { /** * create relationship */ PostModel::extend(function ($model) { $model->belongsToMany['tags'] = ['Rahman\\BlogTags\\Models\\Tag', 'table' => 'rahman_blogtags_posts_tags']; }); /** * extend Rainlab\Blog\Controllers\Post * add tag form widget */ PostsController::extendFormFields(function ($widget, $model, $context) { if (!$model instanceof \Rainlab\Blog\Models\Post) { return; } $widget->addSecondaryTabFields(['tags' => ['Label' => 'Tags box', 'type' => 'Rahman\\BlogTags\\FormWidgets\\Tagbox', 'tab' => 'Tags']]); }); }
/** * Load the most related posts based on associated tags * * @return Collection */ protected function loadRelatedPosts() { $post = Post::with('tags')->whereSlug($this->property('slug'))->first(); if (!$post->tags->count()) { return; } $tagIds = $post->tags->lists('id'); // a collection of related posts $query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($tag) use($tagIds) { $tag->whereIn('id', $tagIds); })->with('tags'); $orderBy = DB::raw('( select count(*) from `rahman_blogtags_posts_tags` where `rahman_blogtags_posts_tags`.`post_id` = `rainlab_blog_posts`.`id` and `rahman_blogtags_posts_tags`.`tag_id` in (' . implode(', ', $tagIds) . '))'); // order by most related tags $query->orderby($orderBy, 'desc'); if ($take = intVal($this->property('results'))) { $query->take($take); } return $query->get(); }
public function fixValidations() { CmsPage::extend(function ($page) { $page->rules['url'] = ['required', 'regex:/^\\/[۰-۹آا-یa-z0-9\\/\\:_\\-\\*\\[\\]\\+\\?\\|\\.\\^\\\\$]*$/iu']; }); //edit blog url validation rule if (PluginManager::instance()->exists('rainlab.blog')) { \RainLab\Blog\Models\Post::extend(function ($post) { $post->rules['slug'] = ['required', 'regex:/^[۰-۹آا-یa-z0-9\\/\\:_\\-\\*\\[\\]\\+\\?\\|]*$/iu', 'unique:rainlab_blog_posts']; }); } //extending rainlab.pages if (PluginManager::instance()->exists('rainlab.pages')) { //edit rainlab page url validation rule \RainLab\Pages\Classes\Page::extend(function ($page) { $page->rules['url'] = ['required', 'regex:/^\\/[۰-۹آا-یa-z0-9\\/_\\-]*$/iu', 'uniqueUrl']; }); //edit rainlab page filename in crating \RainLab\Pages\Classes\Page::creating(function ($page) { $page->fileName = \Str::ascii($page->fileName); }, -1); } }
public function boot() { /* * Register menu items for the RainLab.Pages plugin */ Event::listen('pages.menuitem.listTypes', function () { return ['blog-category' => 'rainlab.blog::lang.menuitem.blog_category', 'all-blog-categories' => 'rainlab.blog::lang.menuitem.all_blog_categories', 'blog-post' => 'rainlab.blog::lang.menuitem.blog_post', 'all-blog-posts' => 'rainlab.blog::lang.menuitem.all_blog_posts']; }); Event::listen('pages.menuitem.getTypeInfo', function ($type) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::getMenuTypeInfo($type); } elseif ($type == 'blog-post' || $type == 'all-blog-posts') { return Post::getMenuTypeInfo($type); } }); Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::resolveMenuItem($item, $url, $theme); } elseif ($type == 'blog-post' || $type == 'all-blog-posts') { return Post::resolveMenuItem($item, $url, $theme); } }); }
public function boot() { // Extend the navigation Event::listen('backend.menu.extendItems', function ($manager) { $manager->addSideMenuItems('RainLab.Blog', 'blog', ['tags' => ['label' => 'Tags', 'icon' => 'icon-tags', 'code' => 'tags', 'owner' => 'RainLab.Blog', 'url' => Backend::url('bedard/blogtags/tags')]]); }); // Extend the controller PostsController::extendFormFields(function ($form, $model, $context) { if (!$model instanceof PostModel) { return; } $form->addSecondaryTabFields(['tagbox' => ['label' => 'Tags', 'tab' => 'rainlab.blog::lang.post.tab_categories', 'type' => 'owl-tagbox', 'slugify' => true]]); }); // Extend the model PostModel::extend(function ($model) { // Relationship $model->belongsToMany['tags'] = ['Bedard\\BlogTags\\Models\\Tag', 'table' => 'bedard_blogtags_post_tag', 'order' => 'name']; // getTagboxAttribute() $model->addDynamicMethod('getTagboxAttribute', function () use($model) { return $model->tags()->lists('name'); }); // setTagboxAttribute() $model->addDynamicMethod('setTagboxAttribute', function ($tags) use($model) { $this->tags = $tags; }); }); // Attach tags to model PostModel::saved(function ($model) { if ($this->tags) { $ids = []; foreach ($this->tags as $name) { $create = Tag::firstOrCreate(['name' => $name]); $ids[] = $create->id; } $model->tags()->sync($ids); } }); }