Advertisement:

Author Topic: Change refine category  (Read 2169 times)

Adyyda

  • Sr. Member
  • ****
  • Posts: 435
Change refine category
« on: May 15, 2015, 10:28:06 pm »
Hello. We have in left sidebar Refine category.
This comes from
Code: [Select]
function bender_sidebar_category_search($catId = null)
{
    $aCategories = array();
    if($catId==null) {
        $aCategories[] = Category::newInstance()->findRootCategoriesEnabled();
    } else {
        // if parent category, only show parent categories
        $aCategories = Category::newInstance()->toRootTree($catId);
        end($aCategories);
        $cat = current($aCategories);
        // if is parent of some category
        $childCategories = Category::newInstance()->findSubcategoriesEnabled($cat['pk_i_id']);
        if(count($childCategories) > 0) {
            $aCategories[] = $childCategories;
        }
    }

    if(count($aCategories) == 0) {
        return "";
    }

    bender_print_sidebar_category_search($aCategories, $catId);
}

function bender_print_sidebar_category_search($aCategories, $current_category = null, $i = 0)
{
    $class = '';
    if(!isset($aCategories[$i])) {
        return null;
    }

    if($i===0) {
        $class = 'class="category"';
    }

    $c   = $aCategories[$i];
    $i++;
    if(!isset($c['pk_i_id'])) {
        echo '<ul '.$class.'>';
        if($i==1) {
            echo '<li><a href="'.osc_esc_html(osc_update_search_url(array('sCategory'=>null, 'iPage'=>null))).'">'.__('All categories', 'bender')."</a></li>";
        }
        foreach($c as $key => $value) {
    ?>
            <li>
                <a id="cat_<?php echo osc_esc_html($value['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $value['pk_i_id'], 'iPage'=>null))); ?>">
                <?php if(isset($current_category) && $current_category == $value['pk_i_id']){ echo '<strong>'.$value['s_name'].'</strong>'; }
                else{ echo 
$value['s_name']; } ?>

                </a>

            </li>
    <?php
        
}
        if(
$i==1) {
        echo 
"</ul>";
        } else {
        echo 
"</ul>";
        }
    } else {
    
?>

    <ul <?php echo $class;?>>
        <?php if($i==1) { ?>
        <li><a href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=>null'iPage'=>null))); ?>"><?php _e('All categories''bender'); ?></a></li>
        <?php ?>
            <li>
                <a id="cat_<?php echo osc_esc_html($c['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $c['pk_i_id'], 'iPage'=>null))); ?>">
                <?php if(isset($current_category) && $current_category == $c['pk_i_id']){ echo '<strong>'.$c['s_name'].'</strong>'; }
                      else{ echo 
$c['s_name']; } ?>

                </a>
                <?php bender_print_sidebar_category_search($aCategories$current_category$i); ?>
            </li>
        <?php if($i==1) { ?>
        <?php ?>
    </ul>
<?php
    
}
}

I would like to know how can i show just the subcategories. So, lose "Refine category", "All categories" and parent category, the proper way.

I can remove certain lines and end up with what i want, but, like i've said, i am interested in doing this the proper way.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Change refine category
« Reply #1 on: May 15, 2015, 10:37:02 pm »
Hi,

Put here your modified code. If you already have a solution, why start from scatch? ???

Regards

Adyyda

  • Sr. Member
  • ****
  • Posts: 435
Re: Change refine category
« Reply #2 on: May 15, 2015, 10:49:00 pm »
Hello Teseo.

Code: [Select]
function bender_sidebar_category_search($catId = null)
{
    $aCategories = array();
    if($catId==null) {
        $aCategories[] = Category::newInstance()->findRootCategoriesEnabled();
    } else {
        // if parent category, only show parent categories
        $aCategories = Category::newInstance()->toRootTree($catId);
        end($aCategories);
        $cat = current($aCategories);
        // if is parent of some category
        $childCategories = Category::newInstance()->findSubcategoriesEnabled($cat['pk_i_id']);
        if(count($childCategories) > 0) {
            $aCategories[] = $childCategories;
        }
    }

    if(count($aCategories) == 0) {
        return "";
    }

    bender_print_sidebar_category_search($aCategories, $catId);
}

function bender_print_sidebar_category_search($aCategories, $current_category = null, $i = 0)
{
    $class = '';
    if(!isset($aCategories[$i])) {
        return null;
    }

    if($i===0) {
        $class = 'class="category"';
    }

    $c   = $aCategories[$i];
    $i++;
    if(!isset($c['pk_i_id'])) {
        echo '<ul '.$class.'>';
        foreach($c as $key => $value) {
    ?>
            <li>
                <a id="cat_<?php echo osc_esc_html($value['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $value['pk_i_id'], 'iPage'=>null))); ?>">
                <?php if(isset($current_category) && $current_category == $value['pk_i_id']){ echo '<strong>'.$value['s_name'].'</strong>'; }
                else{ echo 
$value['s_name']; } ?>

                </a>

            </li>
    <?php
        
}
        if(
$i==1) {
        echo 
"</ul>";
        } else {
        echo 
"</ul>";
        }
    } else {
    
?>

    <ul <?php echo $class;?>>
            <li>
                <?php bender_print_sidebar_category_search($aCategories$current_category$i); ?>
            </li>
        <?php if($i==1) { ?>
        <?php ?>
    </ul>
<?php
    
}
}

That shows just the subcategories like i want but i would like a clean solution if posible

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Change refine category
« Reply #3 on: May 16, 2015, 12:54:43 am »
???

There is no cleaner solution, the only cleanable thing I see there is this:

Code: [Select]
        if($i==1) {
            echo "</ul>";
        } else {
            echo "</ul>";
        }

It's repeated, but then it's just a little thing that comes with Bender.

Regards

Adyyda

  • Sr. Member
  • ****
  • Posts: 435
Re: Change refine category
« Reply #4 on: May 16, 2015, 01:00:01 am »
Thanks but turns out that i tried for nothing. I wanted to show subcategories like that and in addition move them from "sidebar" into "main". When i do this, i see all subcategories and on click they do not change even if the page goes where it should. So they are stuck to 1st level category and will not show second level when i click on one. This is beyond me

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Change refine category
« Reply #5 on: May 16, 2015, 01:29:19 am »
Well, recursive estructures are always tricky (bender_print_sidebar_category_search() function calls itself to "descend" through the tree of subcategories), it's easy to get lost.  :o

Regards