Controller Action Response

Posted Over 8 years ago. Visible to the public.

json response

    public function completeAction()
    {
        $orderId = $this->getRequest()->getParam('order_id', false);
        $info['order_id'] = 0;
        if ($orderId) {
          $order = Mage::getModel('patron/order')->load($orderId);
          if ($info['order_id'] = $order->getId()) {
              $info['ostatus'] = $order->getOstatus();
              $info['lat'] = $order->getDeliveredLat();
              $info['lng'] = $order->getDeliveredLng();
          }
        }        
        $this->getResponse()->setHeader('Content-Type', 'application/json', true)->setBody(json_encode($info));
    }

with error messages:

    public function validateAction()
    {
        $response = new Varien_Object();
        $response->setError(false);

        $attributeCode  = $this->getRequest()->getParam('attribute_code');
        $attributeId    = $this->getRequest()->getParam('attribute_id');
        $attribute = Mage::getModel('catalog/resource_eav_attribute')
            ->loadByCode($this->_entityTypeId, $attributeCode);

        if ($attribute->getId() && !$attributeId) {
            Mage::getSingleton('adminhtml/session')->addError(
                Mage::helper('catalog')->__('Attribute with the same code already exists'));
            $this->_initLayoutMessages('adminhtml/session');
            $response->setError(true);
            $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
        }

        $this->getResponse()->setBody($response->toJson());
    }

mass action:

    public function massCloseLadingAction()
    {
        $itemIds = $this->getRequest()->getParam('order');
        $totalActed = 0;

        try {
            foreach ($itemIds as $itemId) {
                $item = Mage::getModel('patron/order')->load($itemId);
                if ($item->getId()) {
                    $lot = Mage::getModel('ordermanager/orderLot')->load($item->getOrderLotId());
                    if ($lot->getId() && !$lot->getLadingDuration()) {
                        $lot->closeLading();
                        $totalActed++;
                    }                    
                }
            }
            if ($totalActed > 0) {
                $this->_getSession()->addSuccess($this->__('Lading window of %d lot(s) have been closed.', $totalActed));
            } else {
                $this->_getSession()->addNotice($this->__('No lading wondow wss closed.'));
            }
        } catch (Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        }
        $block = $this->getLayout()
                  ->createBlock('core/messages')
                  ->setMessages($this->_getSession()->getMessages(true));
        $result['message'] = $block->getGroupedHtml();          
        $result['totalActed'] = $totalActed;        
        $this->getResponse()->setHeader('Content-Type', 'application/json', true)->setBody(json_encode($result));
    }

text

    public function refreshAction()
    {       
        $block = $this->getLayout()
                  ->createBlock('patron/adminhtml_order_grid')
                  ->setIsAjaxRefresh(true);
        $html = trim($block->toHtml());
        /**
         * we are uisng periodic ajax update on existing container orderGrid,
         * so we first remove the constructed container, 
         * next in order for the js to run properly, we need to remove the var          
         */                 
        if (strpos($html, '<div id=') === 0) {
            $html = substr($html, strpos($html, '>') + 1); //start pos <div id="orderGrid">
            $pos = strrpos($html, '</div>'); //last occurrance of </div>
            $html1 = substr($html, 0, $pos); //html part
            $html2 = substr($html, $pos + 6); //js part
            $html2 = str_replace('var ', '', $html2); //remove all var           
            $html = $html1.$html2;
        }

        $this->getResponse()->setHeader('Content-Type', 'text/html', true)->setBody($html);      
    }
kiatng
Last edit
Over 3 years ago
kiatng
Posted by kiatng to OpenMage (2015-08-05 09:00)