Location: PHPKode > projects > Blandware AtLeap Lite - CMS on PHP > atleaplite/include/dataobject/Category.php
<?php
/*
 *  Copyright 2008 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Table Definition for category
 *
 * @package     AtleapLite
 * @author      Roman Puchkovskiy
 * @license     http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
 */

/**
 */
require_once 'dbdo_ext.php';

/**
 * Represents commodity category. Category may contain subcategories (for which
 * it's parent) and commodities.
 *
 * @package     AtleapLite
 */
class DataObjects_Category extends DB_DataObject_Base 
{
    ###START_AUTOCODE
    /* the code below is auto generated do not remove the above tag */

    var $__table = 'category';                        // table name
    var $id;                              // int(11)  not_null primary_key auto_increment
    var $title;                           // string(765)  not_null
    var $description;                     // blob(65535)  blob
    var $parent_id;                       // int(11)  multiple_key
    var $pos;                             // int(11)  not_null

    /* ZE2 compatibility trick*/
    function __clone() { return $this;}

    /* Static get */
    function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('DataObjects_Category',$k,$v); }

    /* the code above is auto generated do not remove the tag below */
    ###END_AUTOCODE

    /**
     * Returns parent category.
     *
     * @return object|null parent or null if no parent
     */
    function getParent() {
        if ($this->parent_id !== null) {
            $parent =& $this->getLink('parent_id', $this->__table, 'id');
        } else {
            $parent = null;
        }
        return $parent;
    }

    /**
     * Returns an array of ancestors of this category.
     *
     * @return array ancestors; last element is parent, previos is its parent
     * and so on, so first element is free category (category with no parent)
     */
    function getAncestors() {
        $parent = $this->getParent();
        if ($parent == null) {
            $result = array();
        } else {
            $ancestors = $parent->getAncestors();
            $ancestors[] = $parent;
            $result = $ancestors;
        }
        return $result;
    }

    /**
     * Returns an array of ancestors of this category plus this category.
     *
     * @return array ancestors; last element is this, previos is its parent
     * and so on, so first element is free category (category with no parent)
     */
    function getAncestorsAndThis() {
        $result = $this->getAncestors();
        $result[] = $this;
        return $result;
    }

    /**
     * Returns subcategories of this category.
     *
     * @return array subcategories ordered by position
     */
    function getSubCategories() {
        $category =& getDao('category');
        $category->parent_id = $this->id;
        $category->orderBy($this->quoteIdentifier('pos'));
        return $category->fetchAll();
    }

    /**
     * Returns free categories (i.e. categories with no parent).
     *
     * @return array free categories
     */
    function getFreeCategories() {
        $category =& getDao('category');
        $category->whereAdd($this->quoteIdentifier('parent_id') . ' is null');
        $category->orderBy($this->quoteIdentifier('pos'));
        return $category->fetchAll();
    }

    /**
     * Returns commodities owned by this category.
     *
     * @return array commodities
     */
    function getCommodities() {
        $commodity =& getDao('commodity');
        $commodity->category_id = $this->id;
        $commodity->orderBy($this->quoteIdentifier('pos'));
        return $commodity->fetchAll();
    }

    /**
     * Deletes all non-cascaded subobjects owned by this category
     * (subcategories and commodities).
     *
     * @return boolean true
     */
    function deleteSubObjects() {
        $categories = $this->getSubCategories();
        foreach ($categories as $category) {
            $category->deleteSubObjects();
            $category->delete();
        }

        $commodities = $this->getCommodities();
        foreach ($commodities as $commodity) {
            $commodity->deleteSubObjects();
            $commodity->delete();
        }
        return true;
    }

    /**
     * Adds a parent condition to where conditions.
     *
     * @param int|null $parentId    ID of parent category
     * @access private
     */
    function addParentCondition($parentId) {
        if ($parentId === null || $parentId === '') {
            $this->whereAdd('(' . $this->quoteIdentifier('parent_id') . ' is null)');
        } else {
            $this->whereAdd($this->quoteIdentifier("parent_id") . " = $parentId");
        }
    }

    /**
     * Returns position which is free for a given parent.
     *
     * @param int|null $parentId    ID of parent category or null
     * @return int free position
     */
    function getFreePos($parentId) {
        $this->whereAdd();
        $this->addParentCondition($parentId);
        $this->selectAdd();
        $this->selectAdd('count(' . $this->quoteIdentifier('id') . ') as __c');
        $this->selectAdd('max(' . $this->quoteIdentifier('pos') . ') as __m');
        $this->find(true);
        if ($this->__c == 0) {
            return 0;
        } else {
            return $this->__m + 1;
        }
    }

    /**
     * Tries to swap position with category with given ID.
     *
     * @param int $id   category ID
     * @access private
     */
    function trySwapPos($id) {
        $dao =& staticGet('category', $id);
        if ($dao) {
            $tmp = $this->pos;
            $this->pos = $dao->pos;
            $dao->pos = $tmp;
            $dao->update();
            $this->update();
        }
    }

    /**
     * Tries to lift a category.
     */
    function tryLift() {
        $dao =& getDao('category');
        $dao->whereAdd();
        $dao->addParentCondition($this->parent_id);
        $dao->whereAdd('(' . $this->quoteIdentifier('pos') . ' < ' . $this->pos . ')');
        $dao->orderBy($this->quoteIdentifier('pos') . ' DESC');
        $dao->limit(1);
        $dao->selectAdd();
        $dao->selectAdd($this->quoteIdentifier('id'));
        $dao->find(true);

        $this->trySwapPos($dao->id);
    }

    /**
     * Tries to lower a category.
     */
    function tryLower() {
        $dao =& getDao('category');
        $dao->whereAdd();
        $dao->addParentCondition($this->parent_id);
        $dao->whereAdd('(' . $this->quoteIdentifier('pos') . ' > ' . $this->pos . ')');
        $dao->orderBy($this->quoteIdentifier('pos') . ' ASC');
        $dao->limit(1);
        $dao->selectAdd();
        $dao->selectAdd($this->quoteIdentifier('id'));
        $dao->find(true);

        $this->trySwapPos($dao->id);
    }
}
Return current item: Blandware AtLeap Lite - CMS on PHP