/**
* Insert categories
**/
function pmx_insert_cat($place, $id, $category)
{
    global $smcFunc, $pmxCacheFunc;
    // get max catorder
    $request = $smcFunc['db_query']('', '
		SELECT MAX(catorder)
		FROM {db_prefix}portamx_categories', array());
    list($maxorder) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);
    // category table empty?
    if (empty($maxorder)) {
        $category['catorder'] = 1;
        return $category;
    }
    // handle the placement
    $allcats = PortaMx_getCategories();
    if ($category['id'] == $id) {
        $placeCat = $category;
    } else {
        $placeCat = PortaMx_getCatByID($allcats, $id);
    }
    // insert before
    if ($place == 'before') {
        $category['catorder'] = $placeCat['catorder'];
        $category['parent'] = $placeCat['parent'];
        $category['level'] = $placeCat['level'];
    } elseif ($place == 'after') {
        $lastFnd = $placeCat;
        if (($placeCat['level'] < $category['level'] || $placeCat['level'] == 0) && is_array($placeCat['childs'])) {
            while (is_array($lastFnd['childs']) || !empty($lastFnd['parent'])) {
                $lastFnd = PortaMx_getCatByOrder($allcats, PortaMx_getNextCat($lastFnd['catorder']));
            }
        } else {
            $lastFnd['catorder'] = PortaMx_getNextCat($lastFnd['catorder']);
        }
        if (empty($lastFnd)) {
            $category['catorder'] = $maxorder + 1;
            $category['parent'] = 0;
            $category['level'] = 0;
        } else {
            $category['catorder'] = $lastFnd['catorder'];
            $category['parent'] = $placeCat['parent'];
            $category['level'] = $placeCat['level'];
        }
        unset($lastFnd);
    } elseif ($place == 'child') {
        $category['catorder'] = PortaMx_getNextCat($placeCat['catorder']);
        $category['parent'] = $placeCat['id'];
        $category['level'] = $placeCat['level'] + 1;
    }
    // shiftup the catorder
    $smcFunc['db_query']('', '
		UPDATE {db_prefix}portamx_categories
		SET catorder = catorder + 1
		WHERE catorder >= {int:corder}', array('corder' => $category['catorder']));
    // cleanup..
    unset($allcats);
    unset($placeCat);
    // clear cache
    $pmxCacheFunc['clean']();
    return $category;
}
    /**
     * Get category and his childs
     */
    function getCatsAndChilds($cats, $acs, $acs_inherit = false)
    {
        global $context, $smcFunc;
        $catIDs = array();
        $catNames = array();
        $this->categories = array();
        $corder = $cats['catorder'];
        $cat = PortaMx_getCatByOrder(array($cats), $corder);
        while (is_array($cat)) {
            if (!is_array($cat['config'])) {
                $cat['config'] = unserialize($cat['config']);
            }
            // check ecl
            if (empty($cat['config']['check_ecl']) || !empty($cat['config']['check_ecl']) && pmx_checkECL_Cookie(!empty($cat['config']['check_eclbots']))) {
                if (!empty($cat['artsum'])) {
                    // get custom css if set
                    if (!empty($cat['config']['cssfile'])) {
                        $this->getCustomCSS($cat);
                    }
                    // inherit acs from block?
                    if (!empty($acs_inherit)) {
                        $cat['acsgrp'] = $acs;
                    }
                    if (allowPmxGroup($cat['acsgrp'])) {
                        $ttl = $this->getUserTitle($cat);
                        if (empty($ttl)) {
                            $ttl = htmlspecialchars($cat['name'], ENT_QUOTES);
                        }
                        $this->categories[$cat['name']] = array('id' => $cat['id'], 'name' => $cat['name'], 'artsort' => $cat['artsort'], 'acsgrp' => $cat['acsgrp'], 'config' => $cat['config'], 'side' => $this->cfg['side'], 'blocktype' => 'category', 'customclass' => '', 'title' => $ttl);
                        $catIDs[] = $cat['id'];
                        $catNames[$cat['id']] = $cat['name'];
                    }
                }
            } else {
                break;
            }
            $addSub = !empty($cat['config']['settings']['addsubcats']) && $cat['config']['settings']['showmode'] == 'sidebar' || !empty($cat['config']['settings']['showsubcats']) && $cat['config']['settings']['showmode'] == 'pages';
            if (!empty($addSub)) {
                $corder = PortaMx_getNextCat($corder);
                $cat = PortaMx_getCatByOrder(array($cats), $corder);
            } else {
                break;
            }
        }
        if (!empty($catIDs)) {
            // get articles for any cat
            $request = $smcFunc['db_query']('', '
				SELECT a.id, a.name, a.acsgrp, a.catid, a.ctype, a.config, a.owner, a.active, a.created, a.updated, a.approved, a.content, CASE WHEN m.real_name = {string:empty} THEN m.member_name ELSE m.real_name END AS mem_name
				FROM {db_prefix}portamx_articles AS a
				LEFT JOIN {db_prefix}members AS m ON (a.owner = m.id_member)
				WHERE a.catid IN ({array_int:cats}) AND a.active > 0 AND a.approved > 0
				ORDER BY a.id', array('cats' => $catIDs, 'empty' => ''));
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $row['config'] = unserialize($row['config']);
                // check ecl
                if (empty($row['config']['check_ecl']) || !empty($row['config']['check_ecl']) && pmx_checkECL_Cookie(!empty($row['config']['check_eclbots']))) {
                    if (!empty($this->categories[$catNames[$row['catid']]]['config']['settings']['inherit_acs'])) {
                        $row['acsgrp'] = $this->categories[$catNames[$row['catid']]]['acsgrp'];
                    }
                    if (allowPmxGroup($row['acsgrp'])) {
                        if (!empty($this->categories[$catNames[$row['catid']]]['config']['settings']['catstyle'])) {
                            $row['config']['visuals'] = $this->categories[$catNames[$row['catid']]]['config']['visuals'];
                            $row['config']['cssfile'] = $this->categories[$catNames[$row['catid']]]['config']['cssfile'];
                        }
                        $row['side'] = $this->cfg['side'];
                        $row['blocktype'] = !empty($this->cfg['config']) ? 'static_article' : 'article';
                        $row['member_name'] = $row['mem_name'];
                        // get custom css if set
                        if (!empty($row['config']['cssfile'])) {
                            $this->getCustomCSS($row);
                        }
                        $this->articles[$catNames[$row['catid']]][] = $row;
                    }
                }
            }
            $smcFunc['db_free_result']($request);
        }
        // articles found?
        $ccats = $this->categories;
        foreach ($ccats as $cname => $cdata) {
            if (!empty($this->articles[$cname])) {
                $this->articles[$cname] = PortaMx_ArticleSort($this->articles[$cname], $this->categories[$cname]['artsort']);
                // if article reqested, get the tile
                if (!empty($this->postarray[$this->postKey]['art']) && $cname == (empty($this->postarray[$this->postKey]['child']) ? $this->postarray[$this->postKey]['cat'] : $this->postarray[$this->postKey]['child'])) {
                    foreach ($this->articles[$cname] as $art) {
                        if ($art['name'] == $this->postarray[$this->postKey]['art']) {
                            $context['pmx']['pagenames']['art'] = $this->getUserTitle($art);
                            if (empty($context['pmx']['pagenames']['art'])) {
                                $context['pmx']['pagenames']['art'] = htmlspecialchars($art['name'], ENT_QUOTES);
                            }
                            break;
                        }
                    }
                }
            } else {
                unset($this->categories[$cname]);
            }
        }
    }