<?php

/**
 * kataoProductCategory actions.
 *
 * @package www.katao.fr
 * @subpackage kataoProductCategory
 * @author Your name here
 * @version SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class kataoProductCategoryActions extends wpActions {
    public function executeIndex() {
        $criteria = new Criteria();

        $sort_method = $this->getCriteriaSortMethod();
        switch ($this->sort = $this->getRequestParameter('sort', 'order')) {
            case 'name':
                $criteria->$sort_method(KataoProductCategoryPeer::NAME);
                break;
            case 'accounting-code':
                $criteria->$sort_method(KataoProductCategoryPeer::ACCOUNTING_CODE_SUFFIX);
                break;
            default:
                $criteria->$sort_method(KataoProductCategoryPeer::SORT_ORDER);
        }

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

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

    public function executeShow($request) {
        $this->katao_product_category = KataoProductCategoryPeer::retrieveByPk($request->getParameter('id'));
        $this->redirectWithErrorUnless($this->katao_product_category, 'Rayon #' . $request->getParameter('id') . ' inconnu.');
    }

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

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

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

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

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

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

            wpFlashMessages::addConfirmation('Rayon "' . $katao_product_category->getName() . '" sauvé avec succès.');
            $this->redirect('kataoProductCategory/index');
        }

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

    public function executeDelete($request) {
        $this->redirectWithErrorUnless($katao_product_category = KataoProductCategoryPeer::retrieveByPk($request->getParameter('id')), 'Rayon #' . $request->getParameter('id') . ' inconnu.');
        $this->redirectWithErrorIf(!$katao_product_category->isDeletable());

        $katao_product_category->delete();

        wpFlashMessages::addConfirmation('Rayon "' . $katao_product_category->getName() . '" supprimé avec succès.');
        $this->redirect('kataoProductCategory/index');
    }

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

            wpPersistenceManager::cleanSelection('katao_product_category');
            wpFlashMessages::addConfirmation('Les rayons sélectionnés ont été supprimés avec succès.');
        } else {
            wpFlashMessages::addWarning('Merci de sélectionner au moins un rayon.');
        }

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