<?php

/**
 * kataoNode actions.
 *
 * @package www.katao.fr
 * @subpackage kataoNode
 * @author Your name here
 * @version SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class kataoNodeActions extends wpActions {
    public function executeIndex() {
        $criteria = new Criteria();
        $criteria->addJoin(KataoNodePeer::RESPONSIBLE, KataoUserPeer::ID);
        $criteria->addJoin(KataoUserPeer::KATAO_MEMBER_ID, KataoMemberPeer::ID);

        $sort_method = $this->getCriteriaSortMethod();
        switch ($this->sort = $this->getRequestParameter('sort', 'city')) {
            case 'delegate':
                $criteria->$sort_method(KataoMemberPeer::LAST_NAME);
                $criteria->$sort_method(KataoMemberPeer::FIRST_NAME);
                break;
            case 'date':
                $criteria->$sort_method(KataoNodePeer::BEGIN_AT);
                break;
            default:
                $criteria->$sort_method(KataoNodePeer::CITY);
        }

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

        $this->katao_nodeList = $pager->getResults();

        $this->managed_node_ids = $this->getUser()->getInstance()->getManagedNodeIds();
    }

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

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

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

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

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

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

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

            wpFlashMessages::addConfirmation('Noeud "' . $katao_node->getCity() . '" sauvé avec succès.');
            $this->redirect('kataoNode/index');
        }

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

    public function executeDelete($request) {
        $this->redirectWithErrorUnless($katao_node = KataoNodePeer::retrieveByPk($request->getParameter('id')), 'Noeud #' . $request->getParameter('id') . ' inconnu.');
        $this->redirectWithErrorIf(!$katao_node->isDeletable() || $katao_node->getIsDefault());

        $katao_node->delete();

        wpFlashMessages::addConfirmation('Noeud "' . $katao_node->getCity() . '" supprimé avec succès.');
        $this->redirect('kataoNode/index');
    }

    public function executeDeleteMultiple($request) {
        if (!wpPersistenceManager::isEmpty('katao_node')) {
            foreach (KataoNodePeer::retrieveByPKs(array_keys(wpPersistenceManager::getSelectedItems('katao_node'))) as/*(KataoNode)*/ $katao_node) {
                if ($katao_node->isDeletable() && !$katao_node->getIsDefault()) {
                    $katao_node->delete();
                }
            }

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

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

    public function executeManageProducts($request) {
        $this->katao_node = KataoNodePeer::retrieveByPk($request->getParameter('id'));
        $this->redirectWithErrorUnless($this->katao_node, 'Noeud #' . $request->getParameter('id') . ' inconnu.');

        $managed_node_ids = $this->getUser()->getInstance()->getManagedNodeIds();
        $this->redirectWithErrorIf(empty($managed_node_ids[$this->katao_node->getId()]));
    }

    public function executeManageProductsByPeriod($request) {
        $this->katao_node = KataoNodePeer::retrieveByPk($request->getParameter('node'));
        $this->redirectWithErrorUnless($this->katao_node, 'Noeud #' . $request->getParameter('node') . ' inconnu.');

        $managed_node_ids = $this->getUser()->getInstance()->getManagedNodeIds();
        $this->redirectWithErrorIf(empty($managed_node_ids[$this->katao_node->getId()]));

        $this->katao_period = KataoPeriodPeer::retrieveByPk($request->getParameter('period'));
        $this->redirectWithErrorUnless($this->katao_period, 'Période #' . $request->getParameter('period') . ' inconnue.');

        if ($request->isMethod('post')) {
            $this->katao_node->updateAvailableProducts($this->katao_period->getId(), !empty($_POST['suppliers_with_all_products'])?$_POST['suppliers_with_all_products']:'', !empty($_POST['products'])?$_POST['products']:array());

            wpFlashMessages::addConfirmation('Gestion des produits pour le noeud "' . $this->katao_node->getCity() . '" et pour la période "' . $this->katao_period->getName() . '" effectuée avec succès.');
            wpFlashMessages::addWarning('Attention : si vous avez enlevé des produits, merci de bien vouloir prévenir les adhérents qui auraient pu voir ces produits.');
            return $this->redirect('@katao_node_manage_products?id=' . $this->katao_node->getId());
        }

        $this->katao_suppliers = KataoSupplierPeer::getAllToSortArray($this->katao_period->getId(), $this->katao_node->getId());
        $this->products = KataoProductPeer::getSelectedProducts(array_keys($this->katao_suppliers['some_product']), $this->katao_period->getId(), $this->katao_node->getId());
    }

    public function executeRefreshProducts($request) {
        return $this->renderText('(' . Utils::array_to_json_string(KataoProductPeer::getSelectedProducts(explode(',', $request->getParameter('suppliers_ids')), $request->getParameter('period'), $request->getParameter('node'))) . ')');
    }
}