<?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 commodity
*
* @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 a commodity. Commodity belongs to a category, has some properties,
* attributes and may have image associated with it.
*
* @package AtleapLite
*/
class DataObjects_Commodity extends DB_DataObject_Base
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
var $__table = 'commodity'; // table name
var $id; // int(11) not_null primary_key auto_increment
var $title; // string(765) not_null
var $description; // blob(65535) blob
var $order_number; // string(765) not_null
var $image_id; // int(11)
var $category_id; // int(11) not_null 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_Commodity',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
/**
* Returns whether this commodity has image.
*
* @return bool true if has image
*/
function hasImage() {
return $this->image_id !== null;
}
/**
* Deletes all non-cascaded subobjects owned by this commodity (image).
*
* @return boolean true
*/
function deleteSubObjects() {
if ($this->hasImage()) {
deleteInternalImage($this->image_id);
}
return true;
}
/**
* Returns attributes which belong to this commodity.
*
* @return array attributes
*/
function getAttributes() {
$dao =& getDao('attribute');
$dao->commodity_id = $this->id;
$dao->orderBy('number');
return $dao->fetchAll();
}
/**
* Deletes all attributes from this commodity.
*/
function deleteAttributes() {
if ($this->id === null) {
return;
}
$dao =& getDao('attribute');
$dao->commodity_id = $this->id;
$dao->delete();
}
/**
* Returns position which is free for a given category.
*
* @param int $categoryId ID of category
* @return int free position
*/
function getFreePos($categoryId) {
$this->whereAdd();
$this->whereAdd("(category_id = $categoryId)");
$this->selectAdd();
$this->selectAdd('count(id) as __c');
$this->selectAdd('max(pos) as __m');
$this->find(true);
if ($this->__c == 0) {
return 0;
} else {
return $this->__m + 1;
}
}
/**
* Tries to swap position with commodity with given ID.
*
* @param int $id commodity ID
* @access private
*/
function trySwapPos($id) {
$dao =& staticGet('commodity', $id);
if ($dao) {
$tmp = $this->pos;
$this->pos = $dao->pos;
$dao->pos = $tmp;
$dao->update();
$this->update();
}
}
/**
* Tries to lift a commodity.
*/
function tryLift() {
$dao =& getDao('commodity');
$dao->whereAdd();
$dao->whereAdd("(category_id = " . $this->category_id . ")");
$dao->whereAdd('(pos < ' . $this->pos . ')');
$dao->orderBy('pos DESC');
$dao->limit(1);
$dao->selectAdd();
$dao->selectAdd('id');
$dao->find(true);
$this->trySwapPos($dao->id);
}
/**
* Tries to lower a commodity.
*/
function tryLower() {
$dao =& getDao('commodity');
$dao->whereAdd();
$dao->whereAdd("(category_id = " . $this->category_id . ")");
$dao->whereAdd('(pos > ' . $this->pos . ')');
$dao->orderBy('pos ASC');
$dao->limit(1);
$dao->selectAdd();
$dao->selectAdd('id');
$dao->find(true);
$this->trySwapPos($dao->id);
}
}