Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Общие вопросы > Класс, генерирующий XML документ для экспорта в Яндекс.Маркет


Автор: MuToGeN 1.6.2005, 18:57
Вот, насал недавно. КОму надо, пользуйтесь...
Код

class yml{
    var $catalogDate;
    var $useCurrentCatalogDate;
    var $shopName;
    var $companyName;
    var $shopUrl;
    var $currencies;
    var $categories;
    var $offers;
    function yml(){
  $this->useCurrentCatalogDate=true;
  $this->currencies=array();
  array_push($this->currencies,array('id'=>'RUR','rate'=>'1'));
  $this->categories=array();
  $this->offers=array();
    }
    function addCurrency($id,$rate,$plus=''){
  array_push($this->currencies,array(
    'id'=>$id,
    'rate'=>$rate,
    'plus'=>$plus));
    }
    function addCategory($id,$parent,$name){
  array_push($this->categories,
  array('id'=>$id,'parent'=>$parent,'name'=>$name));
    }
    function addOffer($id,$url,$price,$currencyId,$categoryId,$picture,$ordering,$name,
    $vendorCode,$description){
  array_push($this->offers,array(
    'id'=>$id,
    'url'=>$url,
    'price'=>$price,
    'currencyId'=>$currencyId,
    'categoryId'=>$categoryId,
    'picture'=>$picture,
    'ordering'=>$ordering,
    'name'=>$name,
    'vendorCode'=>$vendorCode,
    'description'=>$description));
    }
    function setCatalogDate($date){
  $this->catalogDate=$date;
  $this->useCurrentCatalogDate=false;
    }
    function getCatalogDate(){
  if($this->useCurrentCatalogDate){
    return date('Y-m-d H:i');
  }else{
    return $this->catalogDate;
  }
    }
    function getCurrencies(){
  foreach($this->currencies as $currency){
    $r.="    <currency id=\"".$this->ymlTextConv($currency['id']).
    "\" rate=\"".$this->ymlTextConv($currency['rate'])."\"";
    if($currency['plus']!=''){
    $r.=" plus=\"".$this->ymlTextConv($currency['plus'])."\"";
    }
    $r.="/>\n";
  }
  return $r;
    }
    function getCategories(){
  foreach($this->categories as $category){
    $r.="    <category id=\"".$this->ymlTextConv($category['id'])."\"";
    if($category['parent']!=''){
    $r.=" parentId=\"".$this->ymlTextConv($category['parent'])."\"";
    }
    $r.=">".$this->ymlTextConv($category['name'])."</category>\n";
  }
  return $r;
    }
    function getOffers(){
  foreach($this->offers as $offer){
    $r.="    <offer id=\"".$this->ymlTextConv($offer['id'])."\">\n".
    "    <url>".$this->ymlTextConv($offer['url'])."</url>\n".
    "    <price>".$this->ymlTextConv($offer['price'])."</price>\n".
    "    <currencyId>".$this->ymlTextConv($offer['currencyId'])."</currencyId>\n".
    "    <categoryId>".$this->ymlTextConv($offer['categoryId'])."</categoryId>\n".
    "    <picture>".$this->ymlTextConv($offer['picture'])."</picture>\n".
    "    <orderingTime>\n".
    "     <ordering>".$this->ymlTextConv($offer['ordering'])."</ordering>\n".
    "    </orderingTime>\n".
    "    <name>".$this->ymlTextConv($offer['name'])."</name>\n".
    "    <vendorCode>".$this->ymlTextConv($offer['vendorCode'])."</vendorCode>\n".
    "    <description>".$this->ymlTextConv($offer['description'])."</description>\n".
    "    </offer>\n";
  }
  return $r;
    }
    function ymlTextConv($text){
  $text=str_replace('&','&amp;',$text);
  $text=str_replace('"','&quot;',$text);
  $text=str_replace('>','&gt;',$text);
  $text=str_replace('<','&lt;',$text);
  $text=str_replace("'",'&apos;',$text);
  return $text;
    }
    function getYml(){
  $date=$this->ymlTextConv($this->getCatalogDate());
  $shopName=$this->ymlTextConv($this->shopName);
  $companyName=$this->ymlTextConv($this->companyName);
  $shopUrl=$this->ymlTextConv($this->shopUrl);

  return "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n".
  "<!DOCTYPE yml_catalog SYSTEM \"shops.dtd\">\n".
  "<yml_catalog date=\"$date\">\n".
  "    <shop>\n".
  "  <name>$shopName</name>\n".
  "  <company>$companyName</company>\n".
  "  <url>$shopUrl</url>\n".
  "  <currencies>\n".
  $this->getCurrencies().
  "  </currencies>\n".
  "  <categories>\n".
  $this->getCategories().
  "  </categories>\n".
  "  <offers>\n".
  $this->getOffers().
  "  </offers>\n".
  "    </shop>\n".
  "</yml_catalog>\n";
    }
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)