What will be the code for Include Category - php

The following joomla plugin code is to select for Exclude Category from Joomla Category and I would like to know how to make it Include Category.
class plgContentExtcustomhtml extends JPlugin {
public function onContentAfterDisplay($context, &$row, &$params, $page=0) {
if($context == 'com_content.article') {
//exclude
$ext_exclude_categories = $this->params->get('ext_exclude_categories', 0);
$ext_exclude_articles = $this->params->get('ext_exclude_articles', '');
//exclude category
if(!empty($ext_exclude_categories) AND strlen(array_search($row->catid, $ext_exclude_categories))){
return false;
}

the code above is from a plugin.
It works only if the current page is to show an article and the category isnot in the excluded categories.
If you want to change the logic and force it to work only for the excluded categories, then I think that you neeed to change:
//exclude category
if(!empty($ext_exclude_categories) AND strlen(array_search($row->catid, $ext_exclude_categories))){
return false;
}
to this:
//exclude category
if(empty($ext_exclude_categories) OR !strlen(array_search($row->catid, $ext_exclude_categories))){
return false;
}
Backup your code first. :-)

Related

On Silverstripe, how can we auto publish child pages if rearrange?

I'm trying to auto publish child pages if they are rearrange. Right now when we arrange those child pages it says modified.
We're trying to achieve an automation on how to auto publish it once we started drag and drop to their position.
The idea may be to check the change of the Sort field and execute the doPublish(). Please, note that you must check if the page wasn't in the Draft state.
class FooPage extends Page {
private $wasPublishedBeforeWrite = false;
protected function onBeforeWrite()
{
parent::onBeforeWrite();
// check if the page is published
$this->wasPublishedBeforeWrite = !$this->isArchived() && !$this->isOnDraftOnly() && !$this->isModifiedOnDraft();
}
protected function onAfterWrite()
{
parent::onAfterWrite();
if ($this->isChanged('Sort') && $this->wasPublishedBeforeWrite) {
$this->publishSingle();
}
}
}

Show latest blog posts on custom page template

I need some help converting this code from SS3 to SS4.
I used the code below to pull my latest blog posts through to my custom homepage template. This no longer works for me in SS4. Not sure how what needs to be added to fix it.
class IndexPageController extends PageController {
public function LatestPostsHome()
{
return BlogCategory::get()
->filter('Title', 'Featured')
->relation('BlogPosts')
->sort('PublishDate', 'DESC');
}
}
Thank,
Do you have several BlogCategories with the same title?
If you only have one BlogCategory with title 'Featured' then this should work:
public function LatestPostsHome()
{
$blogCategory = BlogCategory::get()->filter('Title', 'Featured')->first();
if (!$blogCategory) {
return null;
}
// Get the corresponding has_many/many_many objects.
$blogPosts = $blogCategory->BlogPosts()->sort('PublishDate', 'DESC');
return $blogPosts;
}
If you have multiple categories with the same title, then you can maybe use something like this:
public function LatestPostsHome()
{
$blogCategories = BlogCategory::get()->filter('Title', 'Featured');
if (!$blogCategories->exists()) {
return null;
}
// Option 1 (not tested)
$categoryIDs = $blogCategories->column('ID');
$blogPosts = BlogPost::get()->byIDs($categoryIDs);
return $blogPosts;
// Option 2 (not tested)
$blogPosts = new \SilverStripe\ORM\ArrayList();
foreach ($blogCategories as $category) {
$posts = $category->BlogPosts();
$blogPosts->push($posts->toNestedArray());
}
return $blogPosts;
}

How to render 2 views of different actions of different controllers in Zend 1.11

I've got 2 controllers.
CategoryController
public function readAction()
{
$id = $this->_request->getParam('id');
$categoryModel = new Model_Category();
if(!$categoryModel)
{
throw new \Exception(__METHOD__.', could not create category model');
}
$category = $categoryModel->getCategory($id);
$parent = $category->findParentRow(new Model_Category());
if(!$parent)
{
$this->view->parent = 'no parent';
}
else
{
$this->view->parent = $parent->name;
}
// I need to pass category object to read.phtml of controller category
// so that each attributes of the category could be displayed.
$this->view->category = $category;
// Below the category section, I also need to list all
// the business entities which are the child rows of that category.
// So I forward to controller business, action list.
// Normally I use list.phtml in controller business to do that.
$this->_forward('list', 'business');
}
BusinessController
But after I call _forward, the read.phtml is not diplayed, I could see only the list.phtml of controller business.
My question is, what could I do if I want to call read.phtml of controller category, and list.phtml of controller business at the same time?
Render current action before forwarding:
public function readAction()
{
// ...
$this->render();
$this->_forward('list', 'business');
}
In your read.phtml view, you could use :
echo $this->render('business/list.phtml');
to display the other view.
You can use $viewRenderer->setResponseSegment('someOtherThanContent') and render it lately in layout using
$this->layout()->someOtherThanContent;

Number of New Products Displayed in Magento Home CMS Page

I am using
{
{
block type="catalog/product_new"
name="home.catalog.product.new" alias="product_homepage"
template="catalog/product/new.phtml"
}
}
in cms page to show new products. Now it is showing only one product.
Is there any way to change it to show a specified number of products? eg : 16 products
Here is what I have:
{{block type="catalog/product_list_random" num_products="9" category_id="231" template="catalog/product/list_no_toolbar.phtml" columnCount="3"}}
num_products is the param where you say how many products to show on that page.
in app/code/local create Mage/Catalog/Block/Product/List
In your new List directory create the following file called Random.php
<?php
class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$categoryID = $this->getCategoryId();
if($categoryID)
{
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID); // this is category id
$collection = $category->getProductCollection();
} else
{
$collection = Mage::getResourceModel('catalog/product_collection');
}
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 3;
$collection->setPage(1, $numProducts)->load();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
$collection->getSelect()->order('rand()'); is the line that set random, you can comment this out.
Your code works fine in my site. Open /app/code/core/Mage/Catalog/Block/Product/New.php
Around line 38 you should see something like this:
const DEFAULT_PRODUCTS_COUNT = 5;
Chances are that number is set to "1". If not then look in /app/code/local/Mage/Catalog/Block/Product/New.php to make sure someone didn't create a local override of your core file.
If the number is "1" then you should create a module to change that value that extends Mage_Catalog_Block_Product_New.

ProductListing based on Attribute in Magento

I would like to show "Suggestions" in my product listing in Magento. I made an attribute "Suggestion" which is Yes/No and global active. Now in the listing I would like to show the suggestions first, then some text and stuff, and then the rest of products.
I tried it like this:
$_productCollection=$this->getLoadedProductCollection()
/* .... */
$_productCollection->clear()->addAttributeToFilter('suggestion', 1)->load();
But this ends in an exception:
You cannot define a correlation name '_price_rule' more than once
Now the question is, how to solve this?
Ok, the solution for me was a custom module, which extends the List Functionality. I added the following file:
/app/code/local/Mage/Catalog/Block/Product/List.php
With the following extending code:
protected function _getProductCollectionSuggestion()
{
$layer = Mage::getSingleton('catalog/layer');
/* #var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->_productCollection->addAttributeToFilter('suggestion', 1);
if($this->_productCollection->count()) {
foreach($this->_productCollection as $_productKey => $_product) {
if($_product->getSuggestion() == 0 || !$_product->getSuggestion()) {
}
}
}
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
return $this->_productCollection;
}
Then I loaded this method in the List.phtml and it worked :)
Thanks for reading anyway! Maybe this code helps someone!

Categories