Popular list and counting views in octobercms - php

I am trying to create a trending or popular list of categories in my application but not sure how it works or if what i have works as it should. In my category table, i added a view_count column. In the php block i have
$viewed = Cat::where('parent_id','>',0)->first();
$viewed->increment('view_count',1);
$viewed->save();
$this['trending'] = Cat::orderBy('view_count', 'desc')->take(10)->get();
How do i correctly do this and add session to it so it does not count with each reload by a user?
Edit: I currently have this looking at how it is done in laravel. Dont know if it works differently with october
public function show(Cat $cat)
{
$id = $cat->id;
$cat = Cat::find($id)->where('parent_id', '>', 0)->increment('view_count');
$this['trending'] = Cat::orderBy('view_count')->take(10)->get();
}

I am assuming you would have category_id on trending category page so your code for incrementing view count must have been
Cat::find($category_id)->increment('view_count');
and that's it.
let me know if you need more help

Related

Laravel collection not saving added data

I'm building a Laravel 9x application where i will need to populate some pages with the google places API.
Reading at the ToS of Google Places, i can only store in my db, for at most 30 days, the place_id.
Now, I'm facing an issue; I have a restaurants.index blade page where I'm showing in a table all the records from the restaurants table.
The issue is that i need to show in each table row the name of the restaurant that i don't have in my db table but i need to fetch it from Google Places Api.
So far, in my RestaurantsController I have done the following :
public function index()
{
$getAllRecords = Restaurant::all();
$google_places_api = new PlacesApi(env('GOOGLE_MAPS_API_KEY'));
$restaurants = new Collection();
foreach($getAllRecords as $restaurant){
$response= $google_places_api->placeDetails($restaurant->google_place_id);
$restaurant['name'] = $response['result']['name'];
$restaurants->prepend($restaurant);
}
dd($restaurants);//When dd here, the collection shows correctly the added value.
$restaurants->paginate(8);
$categories = Category::where('type', 'Restaurant')->cursor();
return view('tenant.restaurants.index', compact('categories', 'restaurants'));
}
And it seems to work pretty well if I dd($restaurants) inside the RestaurantsController as you can see from the picture below :
But when i try to #dd($restaurants) from the restaurants.index page, the added name is completely disappeared as you can see from the picture below :
It's now about a whole day that I'm tryng to understand why this behavior happen, is there anybody who has an idea of what is happening?
Thank you

How to filter posts by author on edit.php

Let's suppose the user visits the page of
http://mywpsite/wp-admin/edit.php?author=john-doe
Let's suppose there is an author with the name of 'John Doe' and the given author has three posts. Yet, when I visit the page, I see an empty grid, as if there were no posts created by this author.
I would like to search for posts created by the given user. Based on my research, I can see that people are claiming that something like this should work:
function posts_for_current_author($query) {
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
Source.
However, here the author's ID is expected as input, yet, I do not know how to get the author's id by the slug of 'john-doe'.
How can I get the ID of the author by slug and search for posts based on that ID?
EDIT:
This is one failed try, based on NATH's comment:
function wpshock_search_filter( $query ) {
if ((is_admin()) && (isset($_GET["author"])) && (preg_match('/[^a-zA-Z_0-9]/i', $_GET["author"]))) {
$query->set("author_name", $_GET["author"]);
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
The query still returns no elements. I have var_dumped $query and seen that $query->query["author"] has the textual problem, which is a potential problem. Also, $query->tax_query contains data related to author. I am sorry if this question is worthy of down-votes, I thought others might be confused by Wordpress's database handling as well and thus this question might be useful. Maybe I was wrong.
This should work for you :
get_user_by('slug','john-doe');
EDIT :
From this you can get author object and you can carry forward with your code.
Link
WordPress provides an author_name query variable to you. You can use the following by default:
http://mywpsite/wp-admin/edit.php?author_name=john-doe
Read more about public query variables in the Codex.
I'm wotking on wp front-end and reach similar feauture you are requested
The code you provide are woking current on wp-admin page but any one uses that code prevent his own SuperAdmin account form reviewing other editors posts .
just add !is_super_admin() to your first contition like this :-
if($query->is_admin && !is_super_admin() )
BTW the author's ID are already provided with global $user_ID;
and when you add this $query->set('author',$user_ID); the main query will filter posts result accurding to author ID , see WP_Query();
See this-
https://wordpress.stackexchange.com/questions/89990/how-we-can-get-the-author-id-by-its-name
And, a better way to get posts by an author would be-
get_posts('author'=>AUTHOR_ID);

Hardcode page_list blog in Concrete5

Previously I've worked out how to hardcode content areas and the autonav block into my templates. I'm trying to do the same for a page_list which displays pages with a certain page type news entry, using pagination and just showing the title.
Here's how far I got:
<?php
$archive = BlockType::getByHandle("page_list");
$archive->controller->orderBy = "chrono_desc";
$archive->controller->ctID = "news";
$archive->controller->paginate = true;
$archive->render("view");
?>
But this doesn't seem to display any pages on the site. What have I done wrong?
It looks like you're supplying a page type handle instead of an page type ID to ctID.
You should be able to do something like so:
$sweetPageType = PageType::getByHandle('news');
if(is_object($sweetPageType)) { // let's be extra safe, eh?
$sweetPageTypeID = $sweetPageType->getPageTypeID();
}
And then, in your hardcoded block (you could test that you have an ID, although I think if it's null it would just have no effect):
$archive->controller->ctID = $sweetpageTypeID;
Dunno if you're using 5.6 or 5.7 but I believe it should be the same for both. Here's a relevant link to the c5 API:
http://concrete5.org/api/class-Concrete.Core.Page.Type.Type.html

Get store ID that a page belongs to

I'm trying to find out what store a page belongs to in Magento. We currently have along these lines:
$cms_pages = Mage::getModel('cms/page')->getCollection();
$cms_pages->load();
foreach($cms_pages as $_page) {
$data = $_page->getData();
}
How would I get the store ID for each page? Ideally I'd like something as simple as $data->store_id(); but I've not found anything of use yet. Can anyone point me in the right direction?
Add Store id to your collection and it will only return the pages you need:
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::ap‌​p()->getStore()->getId())
->setOrder('sortorder', 'asc');

How to get random products if I don't have related products in magento

I have an issue. I want to show 4 related products in the product page. That is simple and I have done that. But if the product doesn't have any or has less than 4 products I want the remaining product to randomly appear in the page.
To select 4 random products first you need rewrite class that responsible for related block (or just move this file to local folder) and change logic for function that returns collection to something like next code:
$productsCollection = Mage::getResourceModel('catalog/product_collection');
$productsCollection->getSelect()->order('rand()');
$productsCollection->getSelect()->limit(4);
Hope, it will be helpful
If you're only interested in creating this functionality on the product page you can find most of the magic in Mage_Catalog_Block_Product_List_Related::_prepareData().
To fill out your related products with random products, we first need to know exactly how many random products we'll need. In this case, it's (4 - related products found):
// Existing Magento code near end of method
$this->_itemCollection->load();
// Our code
$numRandomsToGet = 4 - count($this->_itemCollection);
Then we can fetch the appropriate number of random products and add them to the collection:
// Our code
$randCollection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($randCollection);
$randCollection->getSelect()->order('rand()');
$randCollection->addStoreFilter();
$randCollection->setPage(1, $numRandomsToGet);
$randCollection->addIdFilter($this->_itemCollection->getAllIds(), true);
foreach($randCollection as $randProduct)
{
$this->_itemCollection->addItem($randProduct);
}
// Existing Magento code
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
Caveat/Plug: I pulled this code from our Related Products Manager extension for Magento so there might be some fiddling external to this method that needs to get done but I don't think so. If you get stuck you can try downloading the extension and examining the code in its entirety. Or, of course, you can just use the extension as-is.

Categories