/** * Convert an entity into an XML string * * @param array $row The result row that we are trying to format * @param string $class The class name of the entity * @param SimpleXMLElement $xml The resultset xml * * @return SimpleXMLElement */ private static function _formatAsXml($row, $class, SimpleXMLElement &$xml) { DaoMap::loadMap($class); // Populate the focus object $newRow = $xml->addChild('result'); $newRow->addAttribute('class', $class); //add id element $idCol = $newRow->addChild('id'); $idCol->addAttribute('value', $row['id']); foreach (DaoMap::$map[strtolower($class)] as $field => $properties) { //ignore metadata if (trim($field) === '_') { continue; } $newCol = $newRow->addChild($field); //if it's just a private data for this entity class if (!isset($properties['rel'])) { $newCol->addAttribute('value', $row[$field]); } else { if ($properties['rel'] === DaoMap::MANY_TO_ONE || $properties['rel'] === DaoMap::ONE_TO_ONE) { $newCol->addAttribute('ref', $properties['rel']); $newCol->addAttribute('childClass', DaoMap::$map[strtolower($fClass)][$field]['class']); $newCol->addAttribute('childId', $row[$field . 'Id']); } else { if ($properties['rel'] === DaoMap::ONE_TO_MANY || $properties['rel'] === DaoMap::MANY_TO_MANY) { $newCol->addAttribute('ref', $properties['rel']); $newCol->addAttribute('childrenCount', 0); } else { continue; } } } } return $xml; }
/** * removing all information for that type * * @param int $typeId The type id * * @return InfoEntityAbstract */ public function removeInfo($typeId) { DaoMap::loadMap($this); if (!isset(DaoMap::$map[strtolower(get_class($this))]['infos']) || ($class = trim(DaoMap::$map[strtolower(get_class($this))]['infos']['class'])) === '') { throw new EntityException('You can NOT get information from a entity' . get_class($this) . ', setup the relationship first!'); } $class::updateByCriteria('active = 0', 'typeId = ? and ' . strtolower(get_class($this)) . 'Id = ?', array($typeId, $this->getId())); unset($this->_cache[$typeId]); return $this; }
/** * getting the Json array from all the private memebers of the entity * * @param bool $reset Forcing the function to fetch data from the database again * * @return array The associative arary for json */ public function getJson($extra = array(), $reset = false) { if (!$this->isJsonLoaded($reset)) { $array = array('id' => trim($this->getId())); DaoMap::loadMap(get_class($this)); foreach (DaoMap::$map[strtolower(get_class($this))] as $field => $fieldMap) { if ($field === '_' || isset($fieldMap['rel'])) { continue; } $getterMethod = 'get' . ucfirst($field); if (!method_exists($this, $getterMethod)) { continue; } $value = $this->{$getterMethod}(); switch (trim($fieldMap['type'])) { case 'bool': $array[$field] = trim($value) === '1' ? true : false; break; default: $array[$field] = trim($value); break; } } $this->_jsonArray = array_merge($array, $extra); } return $this->_jsonArray; }
/** * Generating the m_m join table * * @param string $rightClass The other join class * * @throws DaoException * @return string */ private function _getTableForMTM($rightClass) { $leftClass = $this->_focus; DaoMap::loadMap($leftClass); foreach (DaoMap::$map[strtolower($leftClass)] as $field => $properties) { if (isset($properties['rel']) && $properties['rel'] == DaoMap::MANY_TO_MANY) { if (isset($properties['class']) && $properties['class'] == $rightClass) { if (isset($properties['side']) && $properties['side'] == DaoMap::RIGHT_SIDE) { return '`' . strtolower($leftClass) . '_' . strtolower($rightClass) . '`'; } } } } throw new DaoException('Many-to-many relationship not found'); }
/** * import all products * * @return CatelogConnector */ public function importProducts() { if (!($systemSetting = SystemSettings::getByType(SystemSettings::TYPE_LAST_NEW_PRODUCT_PULL)) instanceof SystemSettings) { throw new Exception('cannot get LAST_NEW_PRODUCT_PULL in system setting'); } $fromDate = $systemSetting->getValue(); $products = $this->getProductList($fromDate); if (count($products) === 0) { echo 'nothing from magento. exitting' . "\n"; return $this; } try { $transStarted = false; try { Dao::beginTransaction(); } catch (Exception $e) { $transStarted = true; } foreach ($products as $pro) { $mageId = trim($pro->product_id); $sku = trim($pro->sku); $pro = $this->getProductInfo($sku, $this->getInfoAttributes()); $created_at = trim($pro->created_at); $updated_at = trim($pro->updated_at); $product_id = trim($pro->product_id); if (is_null($pro) || !isset($pro->additional_attributes)) { continue; } // handle extra long sku from magento, exceeding mysql sku length limit DaoMap::loadMap('Product'); $skuSizeLimit = DaoMap::$map['product']['sku']['size']; if (strlen($sku) > $skuSizeLimit) { echo 'Product ' . $sku . '(id=' . $product->getId() . ', magento Product Creation Time=' . trim($pro->created_at) . ') magento sku length exceed system sku length limit of' . $skuSizeLimit . ', skipped' . "\n"; continue; } $additionAttrs = $this->_getAttributeFromAdditionAttr($pro->additional_attributes); $name = trim($additionAttrs['name']); $short_description = trim($additionAttrs['short_description']); $description = trim($additionAttrs['description']); $weight = trim($additionAttrs['weight']); $statusId = trim($additionAttrs['status']); $price = trim($additionAttrs['price']); $specialPrice = isset($additionAttrs['special_price']) ? trim($additionAttrs['special_price']) : ''; $specialPrice_From = isset($additionAttrs['special_from_date']) ? trim($additionAttrs['special_from_date']) : null; $specialPrice_To = isset($additionAttrs['special_to_date']) ? trim($additionAttrs['special_to_date']) : null; if (!($product = Product::getBySku($sku)) instanceof Product) { $product = Product::create($sku, $name); Log::logging(0, get_class($this), 'Found New Product from Magento with sku="' . trim($sku) . '" and name="' . $name . '", created_at="' . $created_at, self::LOG_TYPE, '', __FUNCTION__); echo 'Found New Product from Magento with sku="' . trim($sku) . '" and name="' . $name . '", created_at="' . $created_at . ', updated_at' . $updated_at . "\n"; } elseif (Product::getBySku($sku) instanceof Product) { $product = Product::getBySku($sku); echo 'Found Existing Product from Magento with sku="' . trim($sku) . '" and name="' . $name . '", created_at="' . $created_at . ', updated_at' . $updated_at . '"' . "\n"; echo "\t" . 'Name: "' . $name . '"' . "\n"; echo "\t" . 'MageId: "' . $mageId . '"' . "\n"; echo "\t" . 'Short Description: "' . $short_description . '"' . "\n"; echo "\t" . 'Full Description: "' . $description . '"' . "\n"; echo "\t" . 'Status: "' . ProductStatus::get($statusId) . '"' . "\n"; echo "\t" . 'Manufacturer: id=' . $this->getManufacturerName(trim($additionAttrs['manufacturer']))->getId() . ', name="' . $this->getManufacturerName(trim($additionAttrs['manufacturer']))->getName() . '"' . "\n"; echo "\t" . 'Price: "' . $price . '"' . "\n"; echo "\t" . 'Weight: "' . $weight . '"' . "\n"; } $asset = ($assetId = trim($product->getFullDescAssetId())) === '' || !($asset = Asset::getAsset($assetId)) instanceof Asset ? Asset::registerAsset('full_desc_' . $sku, $description, Asset::TYPE_PRODUCT_DEC) : $asset; $product->setName($name)->setMageId($mageId)->setShortDescription($short_description)->setFullDescAssetId(trim($asset->getAssetId()))->setIsFromB2B(true)->setStatus(ProductStatus::get($statusId))->setSellOnWeb(true)->setManufacturer($this->getManufacturerName(trim($additionAttrs['manufacturer'])))->save()->clearAllPrice()->addPrice(ProductPriceType::get(ProductPriceType::ID_RRP), $price)->addInfo(ProductInfoType::ID_WEIGHT, $weight); if ($specialPrice !== '') { $product->addPrice(ProductPriceType::get(ProductPriceType::ID_CASUAL_SPECIAL), $specialPrice, $specialPrice_From, $specialPrice_To); } if (isset($additionAttrs['supplier']) && ($supplierName = trim($additionAttrs['supplier'])) !== '') { $product->addSupplier(Supplier::create($supplierName, $supplierName, true)); } if (isset($pro->categories) && count($pro->categories) > 0) { $product->clearAllCategory(); foreach ($pro->category_ids as $cateMageId) { if (!($category = ProductCategory::getByMageId($cateMageId)) instanceof ProductCategory) { continue; } $product->addCategory($category); } } } $systemSetting->setValue($updated_at)->save(); if ($transStarted === false) { Dao::commitTransaction(); } } catch (Exception $ex) { if ($transStarted === false) { Dao::rollbackTransaction(); } throw $ex; } return $this; }
function processFile($filename, $clientScript) { //read the file $contents = file($filename); DaoMap::loadMap('Product'); $skuSizeLimit = DaoMap::$map['product']['sku']['size']; foreach ($contents as $line) { $pro = json_decode(trim($line), true); if (strlen($pro['sku']) > $skuSizeLimit) { continue; } updateProduct($pro, $clientScript, $filename, $line); } }