<?php

class ProfileController extends Fiktiv_Controller_Action
{
    public function  preDispatch() {
        if (!Zend_Auth::getInstance()->hasIdentity())
            $this->_redirect('/');
    }

    public function indexAction()
    {

        $form = $this->view->form = new Zend_Form();

        $form->addElement('text', 'email', array(
            'label'      => 'u:email',
            'validators' => array(new Zend_Validate_EmailAddress())
        ));

        $form->addElement('text', 'firstName', array(
            'label'      => 'u:firstname',
            'validators' => array(new Zend_Validate_Alpha())
        ));

        $form->addElement('text', 'lastName', array(
            'label'      => 'u:lastname',
            'validators' => array(new Zend_Validate_Alpha())
        ));

        $options = array(
            1   => 'Ingen avatar',
            2 => 'Egen avatar',
            3 => 'Gravatar'
        );
        $form->addElement('select', 'avatar', array(
            'label'      => 'u:avatar type',
            'validators' => array(new Zend_Validate_InArray($options)),
            'multiOptions' => $options
        ));

        $form->addElement('file', 'avatarImage', array(
            'label'      => 'Upload avatar'
        ));

        $form->addElement('password', 'password', array(
            'label'      => 'u:password',
            'validators' => array(new Zend_Validate_Alnum())
        ));

        $form->addElement('password', 'passwordConfirm', array(
            'label'      => 'u:confirm password',
            'validators' => array(new Zend_Validate_Alnum())
        ));

        $form->addElement('Submit', 'save', array(
            'label'     => 'u:save'
        ));

        $form->addDisplayGroup(
                array(
                    'email',
                    'firstName',
                    'lastName'
                ),
                'partOne',
                array(
                    'legend' => 'u:user information'
                )
        );

        $form->addDisplayGroup(
                array(
                    'avatar',
                    'avatarImage',
                ),
                'partTwo',
                array(
                    'legend' => 'u:avatar'
                )
        );

        $form->addDisplayGroup(
                array(
                    'password',
                    'passwordConfirm',
                    'save'
                ),
                'partThree',
                array(
                    'legend' => 'u:change password'
                )
        );


        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity();


        if ($this->_request->isPost()) {

            $users = new ModelUser();
            $userRow = $users->find($user->id)->current();

            $data = $this->_request->getPost();

            if ($data['password'] !== $data['passwordConfirm'] || empty($data['password'])) {

                if (!empty($data['password'])) {
                    $this->view->messages = 'passwords dont match';
                }
                
            } else {

                if ($users->setPassword($user->id, $data['password'])) {
                    $this->view->messages = 'Lösenordet är ändrat!';
                }
                
                unset($data['password']);
            }

            unset($data['password']);
            unset($data['passwordConfirm']);
            unset($data['save']);

            $userRow->setFromArray($data);
            $userRow->save();

            // Update auth session
            $user = (object) array_merge((array)$user, $userRow->toArray());
            unset($user->salt);
            unset($user->password);
            $auth->getStorage()->write($user);
        }

        $form->populate((array)$user);

    }
}