<?php

require_once dirname(__FILE__) . '/../lib/userGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/userGeneratorHelper.class.php';

/**
 * user actions.
 *
 * @package    sf
 * @subpackage user
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends autoUserActions
{
    protected function buildQuery()
    {
        $query = parent::buildQuery()
            ->andWhere("r.username <> 'root'");
        return $query;
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = $this->configuration->getForm();
        if((int)$this->getTab() === 2){
            $this->form->setDefault('is_super_admin', true);
        }

        $this->setPartialFilter();

        $this->user = $this->form->getObject();
    }

    public function executeEdit(sfWebRequest $request)
    {
        $this->user = $this->getRoute()->getObject();
        $this->forward404Unless($this->user->getUsername() !== 'root');
        $this->forward404Unless($this->getUser()->getUsername() === 'root' || !$this->user->getIsSuperAdmin());
        $user = $this->getUser();
        $this->form = $this->configuration->getForm($this->user);
        $this->setPartialFilter();
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->user = $this->getRoute()->getObject();
        $this->forward404Unless($this->user->getUsername() !== 'root');
        $this->forward404Unless($this->getUser()->getUsername() === 'root' || !$this->user->getIsSuperAdmin());
        $this->form = $this->configuration->getForm($this->user);

        $this->setPartialFilter();

        $user = $this->getUser();
        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

        $user = $this->getUser();

        $object = $this->getRoute()->getObject();

        $this->forward404Unless($object->getUsername() !== 'root');
        $this->forward404Unless($this->getUser()->getUsername() === 'root' || !$object->getIsSuperAdmin());

        if ($object->delete())
        {
            if(!$request->isXmlHttpRequest())
            {
                $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
            }
        }
        if(!$request->isXmlHttpRequest())
        {
            $redirect_url = (isset($this->_partial) ? $_SERVER['DOCUMENT_URI'] . '?_spath=' : '') . $this->generateUrl('user');
            $this->redirect($redirect_url);
        }
        else
        {
            return sfView::NONE;
        }
    }

    protected function executeBatchDelete(sfWebRequest $request)
    {
        $ids = $request->getParameter('ids');

        $records = Doctrine_Query::create()
            ->from('User')
            ->whereIn('id', $ids)
            ->execute();

        foreach ($records as $record)
        {
            if($record->getUsername() !== 'root' && ($this->getUser()->getUsername() === 'root' || !$record->getIsSuperAdmin())){
                $record->delete();
            }
        }

        $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
        $redirect_url = (isset($this->_partial) ? $_SERVER['DOCUMENT_URI'] . '?_spath=' : '') . $this->generateUrl('user');

        $this->redirect($redirect_url);
    }
}
