Ejemplo n.º 1
0
 function admin_category()
 {
     $this->_check_cms();
     $this->swoole->tpl->assign('act_name', 'category');
     $config['tpl.add'] = 'admin_catelog_add.html';
     $config['tpl.list'] = 'admin_catelog_list.html';
     if (isset($config['limit']) and $config['limit'] === true) {
         $this->swoole->tpl->assign('limit', true);
     } else {
         $this->swoole->tpl->assign('limit', false);
     }
     $_model = createModel('Catelog');
     if (isset($_GET['add'])) {
         if (!empty($_POST['name'])) {
             $data['name'] = trim($_POST['name']);
             $data['pagename'] = trim($_POST['pagename']);
             $data['fid'] = intval($_POST['fid']);
             $data['intro'] = trim($_POST['intro']);
             $data['keywords'] = trim($_POST['keywords']);
             $data['uptime'] = time();
             #增加
             if (empty($_POST['id'])) {
                 $data['app'] = $this->app;
                 $_model->put($data);
                 Swoole_js::js_back('增加成功!');
             } else {
                 $_model->set((int) $_POST['id'], $data);
                 Swoole_js::js_back('修改成功!');
             }
         } else {
             if (!empty($_GET['id'])) {
                 $data = $_model->get((int) $_GET['id'])->get();
                 $this->swoole->tpl->assign('data', $data);
             }
             $this->swoole->tpl->display($config['tpl.add']);
         }
     } else {
         if (!empty($_GET['del'])) {
             $del_id = intval($_GET['del']);
             $cate = $_model->get($del_id);
             if ($cate->fid == 0 and getChildCount($del_id) > 0) {
                 Swoole_js::js_back('该分类下还有子分类,请删除全部子分类后重试!');
                 exit;
             }
             if ($cate->fid != 0 and getContentCount($this->app, $del_id, 'cid') > 0) {
                 Swoole_js::js_back('该分类下内容,请删除分类下的所有内容后重试!');
                 exit;
             }
             $_model->del($del_id);
             Swoole_js::js_back('删除成功!');
         }
         //Error::dbd();
         $get['fid'] = empty($_GET['fid']) ? 0 : (int) $_GET['fid'];
         $get['page'] = empty($_GET['page']) ? 1 : (int) $_GET['page'];
         $get['app'] = $this->app;
         $get['pagesize'] = 15;
         $pager = null;
         $list = $_model->gets($get, $pager);
         $this->swoole->tpl->assign('list', $list);
         $this->swoole->tpl->assign('pager', array('total' => $pager->total, 'render' => $pager->render()));
         $this->swoole->tpl->display($config['tpl.list']);
     }
 }
Ejemplo n.º 2
0
/**
 * Walk recursive through the array and translate the structure to a nested set "left" and "right" structure.
 * @note The $level parameter is 2 as the root itself is not included, the root itself would have left = 1 and right = (last right) + 1.
 * It is not included as, in my case, the root knows already left and right, this is used for updates only, therefor no need to change the root itself.
 * 
 * @param array/string $data
 * @param int $parent
 * @param int $level 
 * @return array
 */
function recursiveChildren($data, $parent = 0, $level = 2)
{
    // keep the array and build up
    static $array = array();
    // keep value
    static $left = 1;
    // validate array
    if (is_array($data) && count($data)) {
        // walk through array
        foreach ($data as $key => $value) {
            // increase left
            $left++;
            // set up the new array
            $array[$value['id']]['left'] = $left;
            $array[$value['id']]['data'] = $value;
            $array[$value['id']]['menuid'] = $value['id'];
            $array[$value['id']]['level'] = $level;
            // set up the parent id
            if ($parent > 0) {
                $array[$value['id']]['parent'] = $parent;
            } else {
                $array[$value['id']]['parent'] = $value['parent'];
            }
            // remove the children as not needed
            unset($array[$value['id']]['data']['children']);
            // if the item has children...
            if (isset($value['children']) && count($value['children'])) {
                // look up the count of children to get the "right" value
                $countChildren = getChildCount($value['children'], true);
                // as each item has two numbers, it needs to get multiplied by 2 and plus the 1 to close the open left
                $countChildren = $countChildren * 2 + 1;
                // the count has to get added the actual left to have the correct right
                $array[$value['id']]['right'] = $countChildren + $left;
                // restart the process
                recursiveChildren($value['children'], $value['id'], $array[$value['id']]['level'] + 1);
                // set left after knowing the latest right value, the increment (++) will go from there
                $left = $array[$value['id']]['right'];
            } else {
                // no children, need to increase left and set it as right number
                $left++;
                $array[$value['id']]['right'] = $left;
            }
        }
    }
    return $array;
}