<?php

/**
 * user actions.
 *
 * @package katao
 * @subpackage user
 * @author Your name here
 * @version SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class userActions extends sfActions {
    public function executeIndex($request) {
        $this->user = sfContext::getInstance()->getUser()->getInstance();

        $this->members_count_by_node = stats::getMembersCountByNode();
        $this->invoices_sum_by_period_and_node = stats::getInvoicesSumByPeriodAndNode();

    	$this->active_period = KataoPeriodPeer::getActivePeriod();
    }

    public function executeLogin($request) {
        if ($this->getUser()->isAuthenticated()) {
            $this->redirect('@homepage');
        }

        $this->form = new LoginForm();
        if ($request->isMethod('post')) {
            $form_values = $request->getParameter('login');
            $this->form->bind($form_values);
            if ($this->form->isValid()) {
                $this->getUser()->signIn($form_values);
                if (false !== strpos($request->getReferer(), 'logout')) {
                    $this->redirect('@homepage');
                }
                $this->redirect($request->getReferer());
            }
        }
    }

    public function executeLogout() {
        $this->getUser()->signout();
        $this->redirect('@login');
    }

    public function executeProfile($request) {
        $this->katao_user =/*(KataoUser)*/ $this->getUser()->getInstance();
        if ($this->getUser()->isJustSupplier()) {
            $this->form = new KataoSupplierProfileForm($this->katao_user->getKataoSupplier());
            $this->history_order = $this->katao_user->getHistoryOrder();
        } else {
            wpFlashMessages::addError('Accès restreint.');
            $this->redirect('@homepage');
        }
    }

    public function executeProfileUpdate($request) {
        $this->forward404Unless($request->isMethod('post'));

        $this->katao_user =/*(KataoUser)*/ $this->getUser()->getInstance();
        if ($this->getUser()->isJustSupplier()) {
            $this->form = new KataoSupplierProfileForm($this->katao_user->getKataoSupplier());
            $this->history_order = $this->katao_user->getHistoryOrder();
        } else {
            wpFlashMessages::addError('Accès restreint.');
            $this->redirect('@homepage');
        }

        $this->form->bind($request->getParameter('katao_supplier'));
        if ($this->form->isValid()) {
            $katao_supplier = $this->form->save();
            $this->getUser()->setInstance(KataoUserPeer::retrieveByPK($this->katao_user->getId()));

            wpFlashMessages::addConfirmation('Votre profil a été mis à jour avec succès.');
            $this->redirect('@homepage');
        }

        $this->history_order = $this->katao_user->getHistoryOrder();
    }

    public function executeProfileGmap($request) {
        $this->katao_user =/*(KataoUser)*/ $this->getUser()->getInstance();
        if ($this->getUser()->isJustSupplier()) {
            $this->katao_member_or_supplier = $this->katao_user->getKataoSupplier();
        } else {
            wpFlashMessages::addError('Accès restreint.');
            $this->redirect('@homepage');
        }

        if ($request->isMethod('post')) {
            $this->katao_member_or_supplier->setGmapLng($request->getParameter('gmap_lng'));
            $this->katao_member_or_supplier->setGmapLat($request->getParameter('gmap_lat'));
            $this->katao_member_or_supplier->save();

            return $this->redirect('@profile');
        }
    }
}