public function testJsonEncodeDecode() { $data = array('one' => 1, 'two' => 'two'); $jsonData = '{"one":1,"two":"two"}'; $this->assertEquals($jsonData, $this->_helper->jsonEncode($data)); $this->assertEquals($data, $this->_helper->jsonDecode($jsonData)); }
public function jsonEncode($valueToEncode, $cycleCheck = false, $options = array()) { if ('saveBilling' == Mage::app()->getRequest()->getActionName()) { if ('shipping_method' == $valueToEncode['goto_section']) { // will check if we have required attributes on shipping info step $collection = Mage::getModel('eav/entity_attribute')->getCollection(); $collection->addFieldToFilter('is_visible_on_front', 1); $collection->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('order')->getTypeId()); $collection->addFieldToFilter('checkout_step', 3); $collection->addFieldToFilter('is_required', 1); $collection->load(); $forceShipping = false; if ($collection->getSize() > 0) { foreach ($collection as $attribute) { $currentStore = Mage::app()->getStore()->getId(); $storeIds = explode(',', $attribute->getData('store_ids')); if (!in_array($currentStore, $storeIds) && !in_array(0, $storeIds)) { continue; } $forceShipping = true; } } if ($forceShipping) { $valueToEncode['goto_section'] = 'shipping'; unset($valueToEncode['update_section']); } } } return parent::jsonEncode($valueToEncode, $cycleCheck, $options); }
/** * Format error data according to required format. * * @param string $errorMessage * @param string $trace * @param string $format * @param int $httpCode * @return array */ protected function _formatError($errorMessage, $trace, $httpCode, $format) { $errorData = array(); $message = array('code' => $httpCode, 'message' => $errorMessage); if ($this->_app->isDeveloperMode()) { $message['trace'] = $trace; } $errorData['messages']['error'][] = $message; switch ($format) { case self::DATA_FORMAT_JSON: $errorData = $this->_coreHelper->jsonEncode($errorData); break; case self::DATA_FORMAT_XML: $errorData = '<?xml version="1.0"?>' . '<error>' . '<messages>' . '<error>' . '<data_item>' . '<code>' . $httpCode . '</code>' . '<message>' . $errorMessage . '</message>' . ($this->_app->isDeveloperMode() ? '<trace><![CDATA[' . $trace . ']]></trace>' : '') . '</data_item>' . '</error>' . '</messages>' . '</error>'; break; } return $errorData; }
/** * Duplicating downloadable product data * * @param Varien_Event_Observer $observer * @return Mage_Downloadable_Model_Observer */ public function duplicateProduct($observer) { $currentProduct = $observer->getCurrentProduct(); $newProduct = $observer->getNewProduct(); if ($currentProduct->getTypeId() !== Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) { //do nothing if not downloadable return $this; } $downloadableData = array(); $type = $currentProduct->getTypeInstance(); foreach ($type->getLinks($currentProduct) as $link) { $linkData = $link->getData(); $downloadableData['link'][] = array('is_delete' => false, 'link_id' => null, 'title' => $linkData['title'], 'is_shareable' => $linkData['is_shareable'], 'sample' => array('type' => $linkData['sample_type'], 'url' => $linkData['sample_url'], 'file' => $this->_helper->jsonEncode(array(array('file' => $linkData['sample_file'], 'name' => $linkData['sample_file'], 'size' => 0, 'status' => null)))), 'file' => $this->_helper->jsonEncode(array(array('file' => $linkData['link_file'], 'name' => $linkData['link_file'], 'size' => 0, 'status' => null))), 'type' => $linkData['link_type'], 'link_url' => $linkData['link_url'], 'sort_order' => $linkData['sort_order'], 'number_of_downloads' => $linkData['number_of_downloads'], 'price' => $linkData['price']); } foreach ($type->getSamples($currentProduct) as $sample) { $sampleData = $sample->getData(); $downloadableData['sample'][] = array('is_delete' => false, 'sample_id' => null, 'title' => $sampleData['title'], 'type' => $sampleData['sample_type'], 'file' => $this->_helper->jsonEncode(array(array('file' => $sampleData['sample_file'], 'name' => $sampleData['sample_file'], 'size' => 0, 'status' => null))), 'sample_url' => $sampleData['sample_url'], 'sort_order' => $sampleData['sort_order']); } $newProduct->setDownloadableData($downloadableData); return $this; }
/** * Save import rows bunch. * * @param string $entity * @param string $behavior * @param array $data * @return int */ public function saveBunch($entity, $behavior, array $data) { return $this->_getWriteAdapter()->insert($this->getMainTable(), array('behavior' => $behavior, 'entity' => $entity, 'data' => $this->_jsonHelper->jsonEncode($data))); }
/** * Return json-encoded list of existing behaviors * * @return string */ public function getUniqueBehaviors() { $importModel = $this->_importModel; $uniqueBehaviors = $importModel::getUniqueEntityBehaviors(); return $this->_coreHelper->jsonEncode(array_keys($uniqueBehaviors)); }
/** * Get js for image uploader * * @return string */ protected function _getJs() { return "<script>/* <![CDATA[ */" . "jQuery(function(){" . "BaseImageUploader({$this->_helperData->jsonEncode($this->getHtmlId())}, " . "{$this->_helperData->jsonEncode($this->_maxFileSize)});" . " });" . "/*]]>*/</script>"; }
/** * Validate data rows and save bunches to DB * * @return Mage_ImportExport_Model_Import_EntityAbstract */ protected function _saveValidatedBunches() { $source = $this->getSource(); $processedDataSize = 0; $bunchRows = array(); $startNewBunch = false; $nextRowBackup = array(); $source->rewind(); $this->_dataSourceModel->cleanBunches(); while ($source->valid() || $bunchRows) { if ($startNewBunch || !$source->valid()) { $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows); $bunchRows = $nextRowBackup; $processedDataSize = strlen(serialize($bunchRows)); $startNewBunch = false; $nextRowBackup = array(); } if ($source->valid()) { // errors limit check if ($this->_errorsCount >= $this->_errorsLimit) { return $this; } $rowData = $source->current(); // add row to bunch for save if ($this->validateRow($rowData, $source->key())) { $rowData = $this->_prepareRowForDb($rowData); $rowSize = strlen($this->_jsonHelper->jsonEncode($rowData)); $isBunchSizeExceeded = $this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize; if ($processedDataSize + $rowSize >= $this->_maxDataSize || $isBunchSizeExceeded) { $startNewBunch = true; $nextRowBackup = array($source->key() => $rowData); } else { $bunchRows[$source->key()] = $rowData; $processedDataSize += $rowSize; } } $this->_processedRowsCount++; $source->next(); } } return $this; }
/** * Convert data to JSON. * * @param array|object $data * @return string */ public function render($data) { return $this->_helper->jsonEncode($data); }