PricespercustomerController.php 7.21 KB
<?php

/**
 * @package magento
 * @subpackage AncPricepercustomer
 *
 * @author netz.coop code[at]netz.coop
 *
 * @license http://www.gnu.org/licenses/gpl-3.0.de.html GNU GENERAL PUBLIC LICENSE VERSION 3
 * @copyright (C) 2015 netz.coop eG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class Anc_Pricespercustomer_Adminhtml_PricespercustomerController extends Mage_Adminhtml_Controller_Action {

    public function listAction() {
        $rightsBlock = $this->getLayout()->createBlock('pricespercustomer/adminhtml_ncpriceslists');
        $this->loadLayout()
                ->_addContent($rightsBlock)
                ->renderLayout();
    }

    private static $customerid;

    protected function _initAction() {
        $this->loadLayout();

        return $this;
    }

    public function indexAction() {
        $this->_initAction();
        $this->renderLayout();

        return $this;
    }

    public function newAction() {
        $id = $this->getRequest()->getParam('id', null);
        $model = Mage::getModel('anc_pricespercustomer/entity');


        if ($id) {
            $model->load((int) $id);
            if ($model->getId()) {
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
                if ($data) {
                    $model->setData($data);
                }
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricespercustomer')->__('item does not exist'));
                $this->_redirect('*/*/');
            }
        }
        Mage::register('pricespercustomer_data', $model);
        $this->_title($this->__('price'))->_title($this->__('Edit'));
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
        $this->renderLayout();
    }

    public function editAction2() {
        $id = $this->getRequest()->getParam('id', null);
        $registry = Mage::getModel('anc_pricespercustomer/entity');
        if ($id) {
            $registry->load((int) $id);
            if ($registry->getId()) {
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
                if ($data) {
                    $registry->setData($data)->setId($id);
                }
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricespercustomer')->__('item does not exist'));
                $this->_redirect('*/*/');
            }
        }
        Mage::register('pricespercustomer_data', $registry);

        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
        $this->renderLayout();
    }

    public function editAction() {
        $id = $this->getRequest()->getParam('id', null);
        $model = Mage::getModel('anc_pricespercustomer/entity');


        if ($id) {
            $model->load((int) $id);
            if ($model->getId()) {
                $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
                if ($data) {
                    $model->setData($data);
                }
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricespercustomer')->__('Item Does niot exist'));
                $this->_redirect('*/*/');
            }
        }
        Mage::register('pricespercustomer_data', $model);
        $this->_title($this->__('price'))->_title($this->__('Edit'));
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
        $this->renderLayout();
    }

    public function saveAction() {

        if ($data = $this->getRequest()->getPost()) {
            $model = Mage::getModel('anc_pricespercustomer/entity');
            $id = $this->getRequest()->getParam('id');
            if ($key = 'entity_id' && !$value) {
                unset($data[$key]);
            }
            foreach ($data as $key => $value) {
                if (is_array($value)) {
                    $data[$key] = implode(',', $this->getRequest()->getParam($key));
                }
                if ($key == 'customer_id') {
                    $customerid = $data['customer_id'];
                    self::$customerid = $customerid;
                }
            }
            if ($id) {
                $model->load($id);
            }

            $model->setData($data);
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            try {
                if ($id) {
                    $model->setId($id);
                }
                $model->save();
                if (!$model->getId()) {
                    Mage::throwException(Mage::helper('pricespercustomer')->__('error saving price'));
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('pricespercustomer')->__('Price was successfully saved.'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    if ($customerid) {
                        $this->_redirect('/customer/edit/id/' . $customerid . '/key/' . Mage::getSingleton('adminhtml/url')->getSecretKey('adminhtml_customer', 'edit') . '/');
                    } else {
                        $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    }
                }
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());

                if ($model && $model->getId()) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                } else {
                    $this->_redirect('*/*/');
                }
            }
            return;
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricespercustomer')->__('no data found to save'));
        $this->_redirect('*/*/');
    }

    public function deleteAction() {

        try {
            $id = $this->getRequest()->getParam('id', null);

            $model = Mage::getModel('anc_pricespercustomer/entity')->load($id);
            $model->delete();
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('pricespercustomer')->__('The price has been deleted.'));
        } catch (Exception $ex) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }

        $this->_redirect('/customer/index/key/' . Mage::getSingleton('adminhtml/url')->getSecretKey('adminhtml_customer', 'index') . '/');
        return;
    }

}