<?php

/**
 * User
 *
 * 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 User extends PluginUser
{
    static $platform = 'android';

    public function delete(Doctrine_Connection $conn = null)
    {
        foreach (Doctrine_Query::create()->select('ugu.*')->from('UserGroupUsers ugu')->where("ugu.user_id = " . $this->getId())->execute() as $group_link) {
            $group_link->delete();
        }

        Doctrine_Query::create()
            ->set("username", "?")
            ->update("User")
            ->where("id = " . $this->getId())
            ->execute(array($this->getId()));

        return parent::delete($conn);
    }

    public function preSave($event)
    {
        $this->setEmail(mb_strtolower($this->getEmail()));
        $this->setPhone(Page::perfectPhone($this->getPhone()));
        if($this->getUsername() !== 'root' && !$this->getSocial()){
            $this->setUsername($this->getPhone());
        }
        $this->setIsActive(true);

        parent::preSave($event);
    }

    public function save(Doctrine_Connection $conn = null)
    {
        return parent::save($conn);
    }

    public function postSave($event)
    {
        parent::postSave($event);
    }

    public function getShortInfo()
    {
        return $this['name'] . ', ' . Page::perfectPhone($this['phone'], true) . ', ' . $this['email'];
    }

    public function getPublicName($user = false, $full = false)
    {
        $user = isset($this) ? $this : $user;
        if ($user) {
            $name = trim($user['name']);
            return mb_strlen($name) > 0 ? $name : ('Пользователь #' . $user['id']);
        } else {
            return null;
        }
    }
}

