<?php

/**
 * UserLog
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class UserLog extends PluginUserLog
{
    static public $log_types = array('create' => 'Добавление', 'update' => 'Изменение', 'delete' => 'Удаление', 'return' => 'Восстановление',);

    static public function saveObject($record)
    {
        if (count($record['object']->getModified(true, true)) > 0) {
            self::logObject($record['object'], 'update');
        }
    }

    static public function deleteObject($record)
    {
        self::logObject($record['object'], 'delete');
    }

    static public function logObject($object, $log_type)
    {
        if (get_class($object) != 'UserLog') {
            $user_id = -1;
            if (strpos($_SERVER['PHP_SELF'], './symfony') === false) {
                $context = sfContext::getInstance();
                if ($context->getUser() && $context->getUser()->getUserId()) {
                    $user_id = $context->getUser()->getUserId();
                }
            }
            $user_log = new UserLog();
            $user_log->setUserId($user_id);
            if ($object->isNew()) {
                $log_type = 'create';
            }

            $user_log->setLogType($log_type);
            $user_log->setModel(get_class($object));
            $user_log->setModelId($object->getPrimaryKey());
            $user_log->setLog(serialize($object));
            $user_log->save();


            if (in_array(get_class($object), array('Course', 'ProfessionCourse', 'DivisionCourse', 'Protocol', 'Bid'))) {
                ProjectUtils::cubeRecreate();
            }
        }
    }

    public function getModule()
    {
        return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), '\\1_\\2', $this->getModel()));
    }
}
