actions.class.php 3.17 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?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());
  }
}