/** * Removes tags with no associated posts * * @return $this->listRefresh() */ public function index_onRemoveOrphanedTags() { if (!($delete = Tag::has('posts', 0)->delete())) { return Flash::error('An unknown error has occured.'); } Flash::success('Successfully deleted orphaned tags.'); return $this->listRefresh(); }
/** * Return the blog tags */ public function tags() { $tags = Tag::with('posts'); if ($this->property('hideOrphans')) { $tags->has('posts', '>', 0); } return $tags->get(); }
/** * Load a page of posts */ public function onLoadPage($page = false) { // Determine which page we're attempting to load $this->currentPage = $page ?: intval(post('page')); // Calculate the pagination variables $this->calculatePagination(); // Query the tag with it's posts $this->tag = Tag::where('name', $this->property('tag'))->with(['posts' => function ($posts) { $posts->skip($this->resultsPerPage * ($this->currentPage - 1))->take($this->resultsPerPage); }])->first(); // Count the posts being returned $this->postsOnPage = $this->tag ? count($this->tag->posts) : 0; }
/** * Query and return blog posts * * @return Illuminate\Database\Eloquent\Collection */ public function onRun() { // Start building the tags query $query = Tag::with('posts'); // Hide orphans if ($this->property('hideOrphans')) { $query->has('posts', '>', 0); } // Sort the tags $subQuery = DB::raw('( select count(*) from `bedard_blogtags_post_tag` where `bedard_blogtags_post_tag`.`tag_id` = `bedard_blogtags_tags`.`id` )'); $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); } $this->tags = $query->get(); }
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); } }); }
/** * Attach new tags to a blog post */ private function attachTags($newTags) { // The post we're adding tags to $post = $this->model->id ? $this->model : new Post(); // firstOrCreate and attach new tags foreach ($newTags as $tag) { $tag = Tag::firstOrCreate(['name' => $tag]); $post->tags()->add($tag, $this->sessionKey); } }
/** * Load a page of posts */ public function onLoadPage($page = false) { // Determine which page we're attempting to load $this->currentPage = $page ?: intval(post('page')); // Calculate the pagination variables $this->calculatePagination(); // Query the tag with it's posts $this->tag = Tag::where('name', $this->property('tag'))->with(['posts' => function ($posts) { $posts->skip($this->resultsPerPage * ($this->currentPage - 1))->take($this->resultsPerPage); }])->first(); // Store the posts in a better container if (empty($this->tag)) { $this->posts = null; $this->postsOnPage = 0; } else { $this->posts = $this->tag->posts; $this->postsOnPage = count($this->posts); // Add a "url" helper attribute for linking to each post $this->posts->each(function ($post) { $post->setUrl($this->postPage, $this->controller); if ($post->categories->count()) { $post->categories->each(function ($category) { $category->setUrl($this->categoryPage, $this->controller); }); } }); } }
public function boot() { $plugin_manager = \System\Classes\PluginManager::instance(); // Walidacja tablicy Validator::extend('arrayed', function ($attribute, $value, $params) { foreach ($value as $key => $val) { $validation = true; for ($i = 1; $i < count($params); $i++) { $validation = $validation && Validator::make($val, [$params[$i] => $params[0]])->fails(); } if ($validation === true) { return false; } } return true; }); // Walidacja numeru telefonu Validator::extend('phone', function ($attribute, $value, $parameters) { return preg_match('/^(\\+|(00))?(\\([0-9]{2}\\))?([ -]?[0-9]{2,})+$/i', $value) && mb_strlen($value) >= 9 && mb_strlen($value) <= 18; }); /** * Rozdzerzenie menu o prawidłowe urle do artykułów */ if ($plugin_manager->hasPlugin('Flynsarmy.Menu')) { \Flynsarmy\Menu\Models\Menuitem::extend(function ($model) { $model->addDynamicMethod('beforeSave', function () use($model) { $classes = ['Flynsarmy\\Menu\\MenuItemTypes\\BlogPost' => 'rainlab_blog_posts', 'Flynsarmy\\Menu\\MenuItemTypes\\BlogCategory' => 'rainlab_blog_categories']; $column = ['post_id', 'category_id']; if (array_key_exists(post('master_object_class'), $classes)) { $object = Db::table($classes[post('master_object_class')])->find($model->master_object_id); if ($object) { $url = $object->url; $model->url = $url; $model->{$column[array_search(post('master_object_class'), $classes)]} = $model->master_object_id; $model->selected_item_id = $model->selected_item_id ?: $url; } } // usunięcie hosta z urla $model->url = str_replace(\URL::to('/'), '', $model->url); }); }); } // Rozszerzenie pluginu Tags if ($plugin_manager->hasPlugin('Bedard.BlogTags')) { Tag::extend(function ($model) { $model->belongsToMany = ['posts' => ['Bm\\Field\\Models\\Post', 'table' => 'bedard_blogtags_post_tag', 'order' => 'published_at desc']]; }); // Extend the model Post::extend(function ($model) { // Relationship $model->belongsToMany['tags'] = ['Bedard\\BlogTags\\Models\\Tag', 'table' => 'bedard_blogtags_post_tag', 'order' => 'name']; // getTagboxAttribute() $model->addDynamicMethod('getTaglistAttribute', function () use($model) { return $model->tags()->lists('name'); }); // setTagboxAttribute() $model->addDynamicMethod('setTaglistAttribute', function ($tags) use($model) { $this->tags = $tags; }); }); // Attach tags to model Post::saved(function ($model) { if ($this->tags) { $ids = []; foreach ($this->tags as $name) { $create = Tag::firstOrCreate(['name' => $name]); $ids[] = $create->id; } $model->tags()->sync($ids); } }); } /** * Problem z grupowaniem w zarządzaniu grupami * @todo usunąć po poprawce w octoberze */ Backend\Models\UserGroup::extend(function ($model) { $model->belongsToMany['users_count'] = ['Backend\\Models\\User', 'table' => 'backend_users_groups', 'count' => true, 'key' => 'user_id']; }); /** * Rozszerzanie pól bloga */ Event::listen('backend.form.extendFields', function ($form) { if ($form->getController() instanceof \Bm\Field\Controllers\Posts && in_array($form->getContext(), ['create', 'update'])) { // Nadpisanie pola z tagami if (class_exists('\\Bedard\\BlogTags\\Plugin')) { $form->removeField('tagbox'); } // Generowanie pól szablonu $form->addTabFields($form->model->generateFields()); // Ustawianie domyślnej daty publikacji $form->data->published_at = $form->data->published_at ?: Carbon::now(); } }); /** * Rozdzerzenie pól menu o id artykułukategorii */ Event::listen('backend.list.extendColumns', function ($widget) { if ($widget->getController() instanceof \Flynsarmy\Menu\Controllers\Menus) { $widget->addColumns(['post_id' => ['hidden' => true], 'category_id' => ['hidden' => false]]); } }); // Rejestracja rozszerzeń Twiga Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) { $twig = $controller->getTwig(); $twig->addExtension(new \Bm\Field\Classes\TwigExcerpt()); $twig->addExtension(new \Bm\Field\Classes\TwigThumbnail()); $twig->addExtension(new \Bm\Field\Classes\TwigPostUrl()); if ($page) { // Ustawianie aktywnego urla w menu $page->menu_url = empty($page->url) ? '/' : $page->url; } }); }