<?php

/**
 * kataoMemberAdhesion actions.
 *
 * @package    www.katao.fr
 * @subpackage kataoMemberAdhesion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class kataoMemberAdhesionActions extends sfActions
{
	/**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
	public function executeAdhesionDelete($request)
	{
		// gestion-des-adherentes/delete/userid/ahdesionid
		$this->forward404Unless($adhesion = KataoMemberAdhesionPeer::retrieveByPk($request->getParameter('id')));

		$c = new Criteria();
		$c->add(KataoMemberAdhesionPeer::ID, $request->getParameter('id'));
		$c->add(KataoMemberAdhesionPeer::KATAO_USER_ID, $request->getParameter('userid'));

		$adhesion = KataoMemberAdhesionPeer::doSelectOne($c);
		$adhesion->delete();

		wpFlashMessages::addConfirmation('Adhésion du "' . $adhesion->getYear('Y') . '" supprimé avec succès.');

		$this->redirect('@katao_member_show?id='.$request->getParameter('userid'));
		//$this->redirect('@katao_member_adhesion/id?='.$this->getParameter('id'));
	}

	public function executeAdhesionCreate($request){
		$this->form = new KataoMemberAdhesionForm();

		$userid = $request->getParameter('userid');
		$this->form->setDefault('katao_user_id', $userid);

		var_dump($this->form->getValue('katao_user_id'));

		if($request->isMethod('post'))
		{
			$this->form->bind($request->getParameter('form_subscription'));

			if($this->form->isValid())
			{
				$id = $request->getParameter('id');

				$save = $this->form->save();
				wpFlashMessages::addConfirmation('Adhésion ajouté avec succès.');
				$this->redirect('@katao_member_show?id='.$save->getKataoUserId());
			}
		}

		$this->setTemplate('adhesionCreate');
	}

	public function executeAdhesionUpdate($request){
		$adhesion = KataoMemberAdhesionPeer::retrieveByPk($request->getParameter('id'));

		$this->form = new KataoMemberAdhesionForm($adhesion);

		$this->id = $request->getParameter('id');
		$this->userid = $request->getParameter('userid');

		$this->form->setDefault('katao_user_id', $request->getParameter('userid'));
	//	var_dump($this->form->getKatao_user_id());

		if($request->isMethod('post'))
		{
			$this->form->bind($request->getParameter('form_subscription'));

			$userid = $request->getParameter('userid');

			if($this->form->isValid())
			{
				//$save = $this->form->save();
				$adhesion = $this->form->save();

				wpFlashMessages::addConfirmation('Adhésion modifié avec succès.');
				$this->redirect('@katao_member_show?id='.$this->userid);
			}
		}

		$this->setTemplate('adhesionUpdate');
	}

	public function showAllAdhesions($request){
		$c = new Criteria();

		$sql = "SELECT count(*) as cnt, year, amount, FROM 'katao_member_adhesion' group by year, amount";
	}

}
