Author: sevein
Date: Sat Oct 30 02:50:20 2010
New Revision: 8705

Log:
Use DefaultEditAction class, $resource object and cosmetic changes.

Modified:
   trunk/apps/qubit/modules/user/actions/passwordEditAction.class.php
   trunk/apps/qubit/modules/user/templates/passwordEditSuccess.php

Modified: trunk/apps/qubit/modules/user/actions/passwordEditAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/user/actions/passwordEditAction.class.php  Sat Oct 
30 00:02:10 2010        (r8704)
+++ trunk/apps/qubit/modules/user/actions/passwordEditAction.class.php  Sat Oct 
30 02:50:20 2010        (r8705)
@@ -17,24 +17,16 @@
  * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-class UserPasswordEditAction extends sfAction
+class UserPasswordEditAction extends DefaultEditAction
 {
-  public function execute($request)
-  {
-    $this->form = new sfForm;
-    $this->user = new QubitUser;
+  // Arrays not allowed in class constants
+  public static
+    $NAMES = array(
+      'confirmPassword',
+      'password');
 
-    if (isset($this->request->id))
-    {
-      $this->user = QubitUser::getById($this->request->id);
-
-      if (!isset($this->user))
-      {
-        $this->forward404();
-      }
-    }
-
-    // Test password vs. confirm password
+  protected function earlyExecute()
+  {
     $this->form->getValidatorSchema()->setOption('allow_extra_fields', true);
     $this->form->getValidatorSchema()->setPostValidator(new 
sfValidatorSchemaCompare(
       'password', '==', 'confirmPassword',
@@ -42,50 +34,72 @@
       array('invalid' => 'Your password confirmation did not match you 
password.')
     ));
 
-    // password
-    $this->form->setDefault('password', null);
-    $this->form->setValidator('password', new sfValidatorString);
-    $this->form->setWidget('password', new sfWidgetFormInputPassword);
-
-    // confirm password
-    $this->form->setDefault('confirmPassword', null);
-    $this->form->setValidator('confirmPassword', new sfValidatorString);
-    $this->form->setWidget('confirmPassword', new sfWidgetFormInputPassword);
-
-    $this->isAdministrator = false;
-    if ($this->context->user->hasCredential('administrator'))
+    $this->resource = new QubitUser;
+    if (isset($this->getRoute()->resource))
     {
-      $this->isAdministrator = true;
+      $this->resource = $this->getRoute()->resource;
     }
 
-    //except for administrators, only allow users to reset their own password
-    if (!$this->isAdministrator)
+    // Except for administrators, only allow users to reset their own password
+    if (!$this->context->user->hasCredential('administrator'))
     {
-      if ($this->request->id != $this->context->user->getAttribute('user_id'))
+      if ($this->resource->id != $this->context->user->getAttribute('user_id'))
       {
         QubitAcl::forwardToSecureAction();
       }
     }
+  }
 
-    if ($request->isMethod('post'))
+  protected function addField($name)
+  {
+    switch ($name)
     {
-      $this->form->bind($request->getPostParameters());
+      case 'confirmPassword':
+      case 'password':
 
-      if ($this->form->isValid())
-      {
-        $this->processForm();
-        $this->redirect(array($this->user, 'module' => 'user'));
-      }
+        $this->form->setDefault($name, null);
+        $this->form->setValidator($name, new sfValidatorString);
+        $this->form->setWidget($name, new sfWidgetFormInputPassword);
+
+        break;
     }
   }
 
-  protected function processForm()
+  protected function processField($field)
   {
-    if (0 < strlen(trim($this->form->getValue('password'))))
+    switch ($name = $field->getName())
     {
-      $this->user->setPassword($this->form->getValue('password'));
+      case 'confirmPassword':
+        // Don't do anything for confirmPassword
+        break;
+
+      case 'password':
+
+        if (0 < strlen(trim($this->form->getValue('password'))))
+        {
+          $this->resource->setPassword($this->form->getValue('password'));
+        }
+
+        break;
     }
+  }
 
-    $this->user->save();
+  public function execute($request)
+  {
+    parent::execute($request);
+
+    if ($request->isMethod('post'))
+    {
+      $this->form->bind($request->getPostParameters());
+
+      if ($this->form->isValid())
+      {
+        $this->processForm();
+
+        $this->resource->save();
+
+        $this->redirect(array($this->resource, 'module' => 'user'));
+      }
+    }
   }
 }

Modified: trunk/apps/qubit/modules/user/templates/passwordEditSuccess.php
==============================================================================
--- trunk/apps/qubit/modules/user/templates/passwordEditSuccess.php     Sat Oct 
30 00:02:10 2010        (r8704)
+++ trunk/apps/qubit/modules/user/templates/passwordEditSuccess.php     Sat Oct 
30 02:50:20 2010        (r8705)
@@ -2,38 +2,58 @@
 
 <h1><?php echo __('Reset password'); ?></h1>
 
-<form method="post" action="<?php echo url_for(array('module' => 'user', 
'action' => 'passwordEdit')) ?>">
+<h1 class="label"><?php echo render_title($resource) ?></h1>
 
-<?php echo object_input_hidden_tag($user, 'getId') ?>
+<?php echo $form->renderGlobalErrors() ?>
 
-<div class="formHeader">
-  <?php echo __('Reset password: %1%', array('%1%' => $user)) ?>
-</div>
+<?php echo $form->renderFormTag(url_for(array($resource, 'module' => 'user', 
'action' => 'passwordEdit')), array('id' => 'editForm')) ?>
 
-<fieldset>
-<div>
-  <?php $settings = json_encode(array('password' => array('strengthTitle' => 
'Password strength:', 'hasWeaknesses' => 'To make your password stronger:', 
'tooShort' => 'Make it at least six characters', 'addLowerCase' => 'Add 
lowercase letters', 'addUpperCase' => 'Add uppercase letters', 'addNumbers' => 
'Add numbers', 'addPunctuation' => 'Add punctuation', 'sameAsUsername' => 'Make 
it different from your username', 'confirmSuccess' => 'yes', 'confirmFailure' 
=> 'no', 'confirmTitle' => 'Passwords match:', 'username' => ''))) ?>
+  <?php $settings = json_encode(array(
+    'password' => array(
+      'strengthTitle' => __('Password strength:'),
+      'hasWeaknesses' => __('To make your password stronger:'),
+      'tooShort' => __('Make it at least six characters'),
+      'addLowerCase' => __('Add lowercase letters'),
+      'addUpperCase' => __('Add uppercase letters'),
+      'addNumbers' => __('Add numbers'),
+      'addPunctuation' => __('Add punctuation'),
+      'sameAsUsername' => __('Make it different from your username'),
+      'confirmSuccess' => __('yes'),
+      'confirmFailure' => __('no'),
+      'confirmTitle' => __('Passwords match:'),
+      'username' => ''))) ?>
   <?php echo javascript_tag(<<<EOF
 jQuery.extend(Drupal.settings, $settings);
 EOF
 ) ?>
 
-  <?php echo $form->renderGlobalErrors() ?>
+  <?php echo $form->password->renderError() ?>
 
-  <?php echo $form->password->label(__('Change 
password'))->renderRow(array('class' => 'password-field')) ?>
-  <br style="clear: both"/>
-  <?php echo $form->confirmPassword->label(__('Confirm 
password'))->renderRow(array('class' => 'password-confirm')) ?>
-</div>
-</fieldset>
+  <div class="form-item password-parent">
+    <?php echo $form->password
+      ->label(__('New password'))
+      ->renderLabel() ?>
+    <?php echo $form->password->render(array('class' => 'password-field')) ?>
+  </div>
+
+  <div class="form-item confirm-parent">
+    <?php echo $form->confirmPassword
+      ->label(__('Confirm password'))
+      ->renderLabel() ?>
+    <?php echo $form->confirmPassword->render(array('class' => 
'password-confirm')) ?>
+  </div>
 
   <div class="actions section">
+
     <h2 class="element-invisible"><?php echo __('Actions') ?></h2>
+
     <div class="content">
       <ul class="clearfix links">
-        <li><?php echo link_to(__('Cancel'), array('module' => 'user', 'id' => 
$user->id)) ?></li>
+        <li><?php echo link_to(__('Cancel'), array($resource, 'module' => 
'user')) ?></li>
         <li><input class="form-submit" type="submit" value="<?php echo 
__('Save') ?>"/></li>
       </ul>
     </div>
+
   </div>
 
 </form>

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to