<?php

class Inventory extends CActiveRecord {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'inventory';
    }

    public function rules() {
        return array(
                // array('text', 'length', 'max' => 1000, 'min' => 3),
                // array('title, text, author', 'required'),
        );
    }

    public function attributeLabels() {
        return array(
        );
    }

    public function DopingThrow() {
        self::model()->deleteAllByAttributes(array('put' => '1', 'type' => '7', 'user' => Yii::app()->user->id));
    }

        public static function findInventory($id) {
        $inventory = self::model()->findByPk($id);
        if (!$inventory) {
            throw new CHttpException(404, 'Предмета существует');
        } else {
            return $inventory;
        }
    }

    public static function countItems() {
        $count = self::model()->count('user = :user', array('user' => Yii::app()->user->id));
        return $count;
    }

    public static function putOn($id) {
        $model = self::findInventory($id);
        if ($model->type > 5 AND $model->type != 8 AND $model->type != 7) {
            return 4;
        } else {
            if ($model->user == Yii::app()->user->id) {
                $put_already = self::model()->count('user = :user AND put = :put AND type = :type', array(
                    'user' => Yii::app()->user->id,
                    'put' => '1',
                    'type' => $model->type,
                ));
                if ($put_already == 0) {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return 3;
            }
        }
    }

    public static function pullOff($id) {
        $model = self::findInventory($id);
        if ($model->user == Yii::app()->user->id) {
            if ($model->put == 1) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 3;
        }
    }

}
