<?php
/*
 * Developer : John D'Alto (jdalto@yahoo.com)
 * Date : 9/1/2012
 * All code (c)2012 JLS Consulting, all rights reserved
 */

namespace Dorcas\TabithaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
//use Dorcas\TabithaBundle\Entity\Client;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
//use Dorcas\TabithaBundle\Form\Type\ClientInformationType;
use Dorcas\TabithaBundle\Form\Type\UsersType;
use Dorcas\TabithaBundle\Entity\Users;


class ProfileController extends Controller
{
    /**
     * Show a user entry
     */
    public function showAction()
    {

    return $this->render('DorcasTabithaBundle:Profile:profileform.html.twig');
    }
    
     /**
     * Used by Profile SHOW to edit the users profile
     */
        public function updateAction()
        {   
            $usr = $this->get('security.context')->getToken()->getUser();
            $id = $usr->getId();
            $request = $this->get('request');

            $em = $this->getDoctrine()->getEntityManager();
            $user = $em->getRepository('DorcasTabithaBundle:Users')->find($id);
            $oldpassword = $user->getPassword();
            $oldsalt = $user->getSalt();
            $form = $this->createForm(new UsersType(), $user);

            if ($request->getMethod() == 'POST') {
                $form->bindRequest($request);

                
                // Check to see if there is only one validation error
                // and if it is coming from the password field
                $validator = $this->get('validator');
                $errors = $validator->validate($user);
                $error = "";

                // Get the property path if there is only one error left
                if (count($errors) == 1) 
                {
                    $error = $errors[0]->getPropertyPath();
                }

                if ($form->isValid() || $error == 'password') {
               // Make sure that a password was entered and save to database
                    if($user->getPassword()){
                        $factory = $this->get('security.encoder_factory');
                        $encoder = $factory->getEncoder($user);
                        $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
                        $user->setPassword($password);
                    }
                    else {
                        // save the object with the original password to the database
                        $user->setPassword($oldpassword);
                        $user->setSalt($oldsalt);
                    }
                    $em->persist($user);
                        $em->flush();
                        $this->get('session')->setFlash('notice', 'Your edit has succeeded!');
                        $validstatus = true;
                        
                        return $this->render('DorcasTabithaBundle:Profile:profileform.html.twig', array(
                    'user' => $user,
                    'valid' => $validstatus,
                    'form' => $form->createView(),
                    ));
                }
                        
                    return $this->render('DorcasTabithaBundle:Profile:profileform.html.twig', array(
                    'user'      => $user,
                    'form' => $form->createView(),
                    ));
            }
            return $this->render('DorcasTabithaBundle:Profile:profileformedit.html.twig', array(
            'user'      => $user,
            'form' => $form->createView(),
            ));
        }
    
}
