<?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 processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
      try
      {
        $user = $form->save();
        
        
        
        $old = $user->getModified(false, true);
        $new = $user->getModified(true, true);

        if($user->getEmail() && (count(array_diff($old, $new)) > 0 || $request->hasParameter('_send_password')))
        {
          $mailer = $this->getMailer();
          $message = $mailer->compose(
            sfConfig::get('app_email_sender', 'noreply@'.$request->getHost()),
            array($user->getEmail()), 
            'Напоминание пароля ' . $request->getHost()
          );
          $message->setContentType('text/plain; charset=UTF-8');
          $message->setBody($this->getPartial('user/mail_update', array('user'=> $user, 'host'=> $request->getHost())));
          $result = $mailer->send($message);
        }

        $user->setIsSuperAdmin(false);
        $user->save();
      } 
      catch (Doctrine_Validator_Exception $e)
      {
        $errorStack = $form->getObject()->getErrorStack();
        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors)
        {
          $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');
        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }
      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $user)));
      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');
        $this->redirect('@user_new');
      }
      elseif ($request->hasParameter('_save_and_list'))
      {
        $this->redirect('@user');
      }
      else
      {
        if(!$request->isXmlHttpRequest())
        {
          $this->getUser()->setFlash('notice', $notice);
          $this->redirect(array('sf_route' => 'user_edit', 'sf_subject' => $user));
        }
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
      $this->getResponse()->addHttpMeta('Sf-Form-Error', '1', true);
    }
  }
  public function executeEdit(sfWebRequest $request)
  {
      $this->user = $this->getRoute()->getObject();
      $this->form = $this->configuration->getForm($this->user);
      $this->history = Doctrine::getTable('ClientOrder')->findByUserId($this->user->getId());
  }
}
