Advertisement:

Author Topic: how to allow ads only lowest level categories  (Read 76 times)

zlotowinfo

  • Newbie
  • *
  • Posts: 18
  • https://oga.pl
how to allow ads only lowest level categories
« on: July 02, 2019, 09:13:39 pm »
i wish to block categories, to users could add only to lowest level categories

I wish to allow only lowest/last categories:
Praca i Usługi > Usługi > Inne

now is possible add to:
Praca i Usługi > Usługi  (need BLOCK)
Praca i Usługi > Usługi > Inne


--
https://oga.pl
« Last Edit: July 02, 2019, 09:15:18 pm by zlotowinfo »

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: how to allow ads only lowest level categories
« Reply #1 on: July 08, 2019, 01:52:56 pm »
Hello!

Note: This ain't a perfect solution. I spend some time testing but there may be bugs...

1. Open oc-content/themes/bender/functions.php
2. Paste this at the bottom of the file, but before ?>.
Code: [Select]
function wm_category_select_only_lowest($categories = null, $item = null, $default_item = null) {
    // Did user select a specific category to post in?
    $catId = Params::getParam('catId');
    if(Session::newInstance()->_getForm('catId') != "") {
        $catId = Session::newInstance()->_getForm('catId');
    }

    if($categories == null) {
        if(View::newInstance()->_exists('categories')) {
            $categories = View::newInstance()->_get('categories');
        } else {
            $categories = osc_get_categories();
        }
    }

    if ($item == null) { $item = osc_item(); }

    echo '<select name="catId" id="catId">';
    if(isset($default_item)) {
        echo '<option value="">' . $default_item . '</option>';
    } else {
        echo '<option value="">' . __('Select a category') . '</option>';
    }

    foreach($categories as $c) {           
        if(isset($c['categories']) && is_array($c['categories']) && count($c['categories'])) {
            echo '<optgroup label="' . $c['s_name'] . '">';
            wm_subcategory_select_only_lowest($c['categories'], $item, $default_item, 1);
            echo '</optgroup>';
        } else {
            $selected = ( (isset($item["fk_i_category_id"]) && $item["fk_i_category_id"] == $c['pk_i_id']) || (isset($catId) && $catId == $c['pk_i_id']) );
            echo '<option value="' . $c['pk_i_id'] . '"' . ($selected ? ' selected="selected"' : '' ). '>' . $c['s_name'] . '</option>';
        }
    }
    echo '</select>';
    return true;
}

function wm_subcategory_select_only_lowest($categories, $item, $default_item = null, $deep = 0) {
    // Did user select a specific category to post in?
    $catId = Params::getParam('catId');
    if(Session::newInstance()->_getForm('catId') != ""){
        $catId = Session::newInstance()->_getForm('catId');
    }
    // How many indents to add?
    $deep_string = "";
    for($var = 0;$var<$deep;$var++) {
        $deep_string .= '&nbsp;&nbsp;';
    }
    $deep++;

    foreach($categories as $c) {
        $selected = ( (isset($item["fk_i_category_id"]) && $item["fk_i_category_id"] == $c['pk_i_id']) || (isset($catId) && $catId == $c['pk_i_id']) );

        if(isset($c['categories']) && is_array($c['categories']) && count($c['categories'])) {
            echo '<optgroup label="' . $deep_string . $c['s_name'] . '">';
            wm_subcategory_select_only_lowest($c['categories'], $item, $default_item, $deep);
            echo '</optgroup>';
        } else {
            echo '<option value="' . $c['pk_i_id'] . '"' . ($selected ? ' selected="selected'.$item["fk_i_category_id"].'"' : '') . '>' . $deep_string . $c['s_name'] . '</option>';
        }
    }
}
3. Open oc-content/themes/bender/item-post.php
4. Find <?php ItemForm::category_select(null, null, __('Select a category', 'bender')); ?>
5. Replace with <?php wm_category_select_only_lowest(null, null, __('Select a category', 'bender')); ?>
6. Enjoy.

Regards.