/**
* 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 datails.
*/
function PortaMx_getCatDetails($category, $allcats)
{
    global $txt;
    if (empty($category['id']) || !is_array($category)) {
        $catclass = 'cat_none';
        $parent = $txt['pmx_chg_articlnocats'];
        $level = '0';
        $name = $category['name'];
    } elseif (is_array($category['childs'])) {
        $catclass = 'cat_child';
        if (empty($category['parent'])) {
            $catclass = 'cat_rootchild';
            $parent = $txt['pmx_categories_rootchild'];
            $level = '0';
            $name = $category['name'];
        } else {
            $pcat = PortaMx_getCatByID($allcats, $category['parent']);
            $catclass = 'cat_child';
            $parent = sprintf($txt['pmx_categories_child'], $pcat['name']);
            $level = $category['level'];
            $name = $category['name'];
        }
    } elseif (!empty($category['parent'])) {
        $pcat = PortaMx_getCatByID($allcats, $category['parent']);
        $catclass = 'cat_level';
        $parent = sprintf($txt['pmx_categories_child'], $pcat['name']);
        $level = $category['level'];
        $name = $category['name'];
    } else {
        $catclass = 'cat_root';
        $parent = $txt['pmx_categories_root'];
        $level = '0';
        $name = $category['name'];
    }
    return array('class' => $catclass, 'parent' => $parent, 'level' => $level, 'name' => $name);
}
 /**
  * InitContent.
  * Checks the cache status and create the content.
  */
 function pmxc_InitContent()
 {
     global $context, $pmxCacheFunc;
     // requested category and limited to admins??
     if (empty($this->cfg['static_block'])) {
         if (!empty($this->cfg['config']['request']) && !allowPmx('pmx_admin')) {
             $this->visible = false;
         }
         // requested category ecl check
         if (!empty($this->cfg['config']['check_ecl']) && !pmx_checkECL_Cookie(!empty($this->cfg['config']['check_eclbots']))) {
             $this->visible = false;
         }
     }
     // if visible init the content
     if ($this->visible) {
         $this->postKey = 'pmxpost_' . (!empty($this->cfg['static_block']) ? 'cat_block' : $this->cfg['blocktype']) . $this->cfg['id'] . (!empty($this->inBlockCall) ? '_0' : '');
         $this->postarray[$this->postKey] = array('cat' => '', 'child' => '', 'art' => '', 'pg' => '0');
         // cleanup $_GET for vars we need here
         $GETpost = null;
         if (!empty($_GET)) {
             $GETpost = $_GET;
             $tmp = array_diff(array_keys($this->postarray[$this->postKey]), array_keys($GETpost));
             while (list($d, $key) = each($tmp)) {
                 unset($GETpost[$key]);
             }
         }
         if (empty($GETpost) && empty($_POST[$this->postKey]) && !empty($_SESSION['PortaMx'][$this->postKey])) {
             $this->postarray[$this->postKey] = array_merge($this->postarray[$this->postKey], $_SESSION['PortaMx'][$this->postKey]);
         }
         if (empty($this->postarray[$this->postKey]['cat']) && !empty($this->cfg['static_block'])) {
             $this->postarray[$this->postKey]['cat'] = $this->cfg['config']['settings']['category'];
         }
         // reset current page ?
         if (!empty($_POST[$this->postKey])) {
             $data = array('cat' => '', 'child' => '', 'art' => '', 'pg' => '0');
             if (!empty($_POST[$this->postKey])) {
                 $this->postarray[$this->postKey] = pmx_GetPostKey($this->postKey, $data);
             } else {
                 if (isset($_SESSION['PortaMx'][$this->postKey])) {
                     $this->postarray[$this->postKey] = $_SESSION['PortaMx'][$this->postKey];
                 }
                 $data = $this->postarray[$this->postKey] = array_merge($this->postarray[$this->postKey], $GETpost);
             }
         } elseif (empty($GETpost) && empty($_POST[$this->postKey]) && isset($_SESSION['PortaMx'][$this->postKey])) {
             $this->postarray[$this->postKey] = $_SESSION['PortaMx'][$this->postKey];
         }
         if (!empty($GETpost)) {
             $this->postarray[$this->postKey] = array_merge($this->postarray[$this->postKey], $GETpost);
         }
         $_SESSION['PortaMx'][$this->postKey] = $this->postarray[$this->postKey];
         // called from static category block?
         if (!empty($this->cfg['static_block'])) {
             // get the category
             if (!empty($this->cfg['cache'])) {
                 $cachedata = $pmxCacheFunc['get']($this->cache_key, $this->cache_mode);
             } else {
                 $cachedata = null;
             }
             if ($cachedata !== null) {
                 list($this->categories, $this->articles) = $cachedata;
                 $cats = $this->categories[$this->cfg['config']['settings']['category']];
             } else {
                 $cats = PortaMx_getCatByID(null, $this->cfg['config']['settings']['category']);
                 $cats['config'] = unserialize($cats['config']);
                 // inherit acs from block?
                 if (!empty($this->cfg['config']['settings']['inherit_acs'])) {
                     $cats['acsgrp'] = $this->cfg['acsgrp'];
                 }
                 // Main cat can show?
                 if (!allowPmxGroup($cats['acsgrp'])) {
                     return false;
                 }
                 // Main cat ecl check
                 if (!empty($cats['config']['check_ecl']) && !pmx_checkECL_Cookie(!empty($cats['config']['check_eclbots']))) {
                     return false;
                 }
                 // get category(s) and articles
                 $this->getCatsAndChilds($cats, $cats['acsgrp'], !empty($cats['config']['settings']['inherit_acs']));
                 if (!empty($this->cfg['cache'])) {
                     $pmxCacheFunc['put']($this->cache_key, array($this->categories, $this->articles), $this->cache_time, $this->cache_mode);
                 }
             }
             // check category elc
             foreach ($this->categories as $eclKey => $eclCat) {
                 if (!empty($eclCat['config']['check_ecl']) && !pmx_checkECL_Cookie(!empty($eclCat['config']['check_eclbots']))) {
                     unset($this->categories[$eclKey]);
                 }
             }
             $this->postarray[$this->postKey]['cat'] = $cats['name'];
             $this->firstcat = !empty($this->postarray[$this->postKey]['child']) ? $this->postarray[$this->postKey]['child'] : $cats['name'];
             $this->curCat = null;
             if (isset($this->categories[$this->firstcat])) {
                 $this->curCat = $this->categories[$this->firstcat];
             }
             if (!is_null($this->curCat) && !empty($this->postarray[$this->postKey]['art'])) {
                 $found = false;
                 foreach ($this->articles[$this->curCat['name']] as $article) {
                     $found = $article['name'] == $this->postarray[$this->postKey]['art'] ? true : $found;
                 }
             } else {
                 $found = true;
             }
             if (!is_null($this->curCat) && !empty($found)) {
                 if ($this->cfg['config']['settings']['usedframe'] == 'block') {
                     $this->curCat['config']['visuals'] = $this->cfg['config']['visuals'];
                     $this->curCat['config']['csfile'] = $this->cfg['config']['cssfile'];
                 }
                 // check framemode
                 if ($this->cfg['config']['settings']['usedframe'] == 'cat') {
                     $this->cfg['config']['skip_outerframe'] = true;
                     $this->curCat['catid'] = $this->curCat['id'];
                 } else {
                     $this->curCat['config']['skip_outerframe'] = true;
                     $this->curCat['config']['visuals']['frame'] = $this->cfg['config']['visuals']['frame'];
                     $this->cfg['catid'] = $this->cfg['id'];
                     $this->cfg['blocktype'] = 'catblock';
                 }
             } else {
                 $this->visible = false;
             }
         } else {
             // get cat data and all childs
             $cats = PortaMx_getCatByID(null, $this->postarray[$this->postKey]['cat']);
             // Main cat can show?
             if (!allowPmxGroup($cats['acsgrp'])) {
                 return false;
             }
             // Main cat ecl check
             if (!empty($cats['config']['check_ecl']) && !pmx_checkECL_Cookie(!empty($cats['config']['check_eclbots']))) {
                 return false;
             }
             // get categoy(s) and articles
             $this->getCatsAndChilds($cats, $cats['acsgrp'], !empty($cats['config']['settings']['inherit_acs']));
             $this->firstcat = !empty($this->postarray[$this->postKey]['child']) ? $this->postarray[$this->postKey]['child'] : $this->postarray[$this->postKey]['cat'];
             $this->curCat = null;
             if (isset($this->categories[$this->firstcat])) {
                 $this->curCat = $this->categories[$this->firstcat];
             }
             if (!is_null($this->curCat) && !empty($this->postarray[$this->postKey]['art'])) {
                 $found = false;
                 foreach ($this->articles[$this->curCat['name']] as $article) {
                     $found = $article['name'] == $this->postarray[$this->postKey]['art'] ? true : $found;
                 }
             } else {
                 $found = true;
             }
             if (!is_null($this->curCat) && !empty($found)) {
                 // save titles for linktree
                 $context['pmx']['pagenames']['cat'] = $this->categories[$cats['name']]['title'];
                 if (empty($context['pmx']['pagenames']['cat'])) {
                     $context['pmx']['pagenames']['cat'] = htmlspecialchars($cats['name'], ENT_QUOTES);
                 }
                 if (!empty($this->postarray[$this->postKey]['child'])) {
                     $context['pmx']['pagenames']['child'] = $this->curCat['title'];
                     if (empty($context['pmx']['pagenames']['child'])) {
                         $context['pmx']['pagenames']['child'] = htmlspecialchars($this->curCat['name'], ENT_QUOTES);
                     }
                 }
                 $this->cfg['uniID'] = 'cat' . $this->categories[$cats['name']]['id'];
                 $this->cfg['config']['skip_outerframe'] = true;
                 $this->curCat['catid'] = $this->curCat['id'];
             } else {
                 $this->visible = false;
             }
         }
     }
     if (!empty($this->visible) && !empty($this->articles)) {
         // handle special php articles
         foreach ($this->articles as $cn => $artlist) {
             foreach ($artlist as $id => $article) {
                 if ($article['ctype'] == 'php' && preg_match('~\\[\\?pmx_initphp(.*)pmx_initphp\\?\\]~is', $article['content'], $match)) {
                     eval($match[1]);
                 }
                 if (!empty($article['config']['cssfile'])) {
                     $this->getCustomCSS($article);
                 }
             }
         }
     }
     return $this->visible;
 }