<?php

class CronController extends Controller {

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
        $cron = new Cron_log;

        $pets_user = Pets_user::model()->findAll();

        foreach ($pets_user as $p) {
            $time_to_food = $p->timefood - time();
            if ($time_to_food <= 0) {
                $isset_corm = Inventory::model()->findByAttributes(array('user' => $p->user, 'type' => 11));
                if ($isset_corm) {
                    //кушает
                    if ($isset_corm->item == 220) {
                        $timetoadd = 3600 * 12;
                    }
                    if ($isset_corm->item == 221) {
                        $timetoadd = 3600 * 24;
                    }
                    Pets_user::model()->updateByPk($p->id, array('timefood' => time() + $timetoadd));
                    Inventory::model()->deleteByPk($isset_corm->id);
                } else {
                    Pets_user::model()->deleteByPk($p->id);
                    echo 2;
                }
            }
        }

        $cron->save(false);
    }

}
