<?php

/**
 * kataoProductFamily actions.
 *
 * @package www.katao.fr
 * @subpackage kataoProductFamily
 * @author Your name here
 * @version SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class kataoProductFamilyActions extends wpActions {
    public function executeIndex() {
        $criteria = new Criteria();
        $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);

        $sort_method = $this->getCriteriaSortMethod();
        switch ($this->sort = $this->getRequestParameter('sort', 'name')) {
            case 'category':
                $criteria->$sort_method(KataoProductCategoryPeer::NAME);
                break;
            default:
                $criteria->$sort_method(KataoProductFamilyPeer::NAME);
        }

        if ('' != $this->filter_product_category = $this->getUser()->getAttribute('katao_product_family_filter_product_category')) {
            $criteria->add(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, $this->filter_product_category);
        }

        $pager = new sfPropelPager('KataoProductFamily', sfConfig::get('app_pager'));
        $pager->setPeerMethod('doSelectJoinKataoProductCategory');
        $pager->setCriteria($criteria);
        $pager->setPage($this->getRequestParameter('page', 1));
        $pager->init();
        $this->pager = $pager;

        $this->katao_product_familyList = $pager->getResults();
    }

    public function executeFilter($request) {
        $this->getUser()->setAttribute('katao_product_family_filter_product_category', $request->getParameter('filter_product_category'));
        return $this->redirect('kataoProductFamily/index');
    }

    public function executeFilterReset($request) {
        $this->getUser()->setAttribute('katao_product_family_filter_product_category', '');
        return $this->redirect('kataoProductFamily/index');
    }

    public function executeShow($request) {
        $this->katao_product_family = KataoProductFamilyPeer::retrieveByPk($request->getParameter('id'));
        $this->redirectWithErrorUnless($this->katao_product_family, 'Famille #' . $request->getParameter('id') . ' inconnue.');
    }

    public function executeCreate() {
        $this->form = new KataoProductFamilyForm();

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

    public function executeEdit($request) {
        $this->form = new KataoProductFamilyForm(KataoProductFamilyPeer::retrieveByPk($request->getParameter('id')));
    }

    public function executeUpdate($request) {
        $this->redirectWithErrorUnless($request->isMethod('post'));

        $this->form = new KataoProductFamilyForm(KataoProductFamilyPeer::retrieveByPk($request->getParameter('id')));

        $this->form->bind($request->getParameter('katao_product_family'));
        if ($this->form->isValid()) {
            $katao_product_family = $this->form->save();

            wpFlashMessages::addConfirmation('Famille "' . $katao_product_family->getName() . '" sauvée avec succès.');
            $this->redirect('kataoProductFamily/index');
        }

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

    public function executeDelete($request) {
        $this->redirectWithErrorUnless($katao_product_family = KataoProductFamilyPeer::retrieveByPk($request->getParameter('id')), 'Famille #' . $request->getParameter('id') . ' inconnue.');
        $this->redirectWithErrorIf(!$katao_product_family->isDeletable());

        $katao_product_family->delete();

        wpFlashMessages::addConfirmation('Famille "' . $katao_product_family->getName() . '" supprimée avec succès.');
        $this->redirect('kataoProductFamily/index');
    }

    public function executeDeleteMultiple($request) {
        if (!wpPersistenceManager::isEmpty('katao_product_family')) {
            foreach (KataoProductFamilyPeer::retrieveByPKs(array_keys(wpPersistenceManager::getSelectedItems('katao_product_family'))) as/*(KataoProductFamily)*/ $katao_product_family) {
                if ($katao_product_family->isDeletable()) {
                    $katao_product_family->delete();
                }
            }

            wpPersistenceManager::cleanSelection('katao_product_family');
            wpFlashMessages::addConfirmation('Les familles sélectionnées ont été supprimées avec succès.');
        } else {
            wpFlashMessages::addWarning('Merci de sélectionner au moins une famille.');
        }

        $this->redirect('kataoProductFamily/index');
    }
}