Location: PHPKode > scripts > Yandex.Market YML > yandex-market-yml/yml_class.php
<?php
class yml{
	var $filename;
	var $yxml;
	var $categories;
	var $offers;
	var $name;
	var $company;
	var $url;
	var $phone;
	var $deliveryIncluded;
	var $currency;
	var $error;
	
	// Ñîáèðàåì âñå äàííûå â îäíó XML ñòðóêòóðó
	function process(){
		$this->yxml .= "<?xml version=\"1.0\" encoding=\"windows-1251\"?>
<!DOCTYPE yml_catalog SYSTEM \"shops.dtd\">
<yml_catalog date=\"".date("Y-m-d H:i",time())."\">
	<shop>";
		$this->yxml .= "
		<name>".$this->replaceSymbols($this->name)."</name>";
		$this->yxml .= "
		<company>".$this->replaceSymbols($this->company)."</company>";
		if (isset($this->phone) && $this->phone!=""){
		$this->yxml .= "
		<phone>".$this->replaceSymbols($this->phone)."</phone>";
		}
		if (isset($this->deliveryIncluded) && $this->deliveryIncluded!=""){
		$this->yxml .= "
		<deliveryIncluded>".$this->replaceSymbols($this->deliveryIncluded)."</deliveryIncluded>";
		}
		$this->yxml .= "
		<url>".$this->url."</url>";
		$this->yxml .= "
		<currencies>".$this->currency."
		</currencies>";
		$this->yxml .= "
		<categories>".$this->categories."
		</categories>";
		$this->yxml .= "
		<offers>".$this->offers."
		</offers>";
		$this->yxml .= "
	</shop>
</yml_catalog>";
		$this->dataCheck;
		if ($this->error==""){
			$this->saveYmlFile($this->yxml);
		} else {
			return $this->error;
		}
	}
	
	// çàìåíÿåì çàïÿòûå íà òî÷êè
	function replaceComma($value){
		return str_replace(",",".",$value);
	}
	
	// çàìåíÿåì ñëóæåáíûå ñèìâîëû
	function replaceSymbols($value){
		$value = str_replace("&","&amp;",$value);
		$value = str_replace("\"","&quot;",$value);
		$value = str_replace(">","&gt;",$value);
		$value = str_replace("<","&lt;",$value);
		$value = str_replace("'","&apos;",$value);
		return $value;
	}
	
	// Ïîëó÷àåì âàëþòû èç áàçû äàííûõ è ñîáèðàåì â îäíó XML ñòðóêòóðó
	function saveÑurrency($currency_id,$currency_rate, $currency_plus){
		if (isset($currency_plus) && $currency_plus!="" && $currency_plus!="0") {
			$currency_plus = " plus=\"".$this->replaceComma($currency_plus)."\"";
		} else {
			$currency_plus = "";
		}
		if (isset($currency_rate) && $currency_rate!=""){
			$this->currency .= "
			<currency id=\"".$currency_id."\" rate=\"".$this->replaceComma($currency_rate)."\"".$currency_plus."/>";
		} else {
			$this->currency .= "
			<currency id=\"".$currency_id."\" rate=\"CBRF\"".$currency_plus."/>";
		}
	}
	
	// Ïîëó÷àåì êàòåãîðèè èç áàçû äàííûõ è ñîáèðàåì â îäíó XML ñòðóêòóðó
	function saveCategory($category_id,$category_name,$category_parent){
		if (isset($category_parent) && $category_parent!=0) {
			$category_parent=" parentId=\"".$category_parent."\"";
		} else {
			$category_parent="";
		}
		$this->categories .= "
			<category id=\"".$category_id."\"".$category_parent.">".$this->replaceSymbols($category_name)."</category>";
	}
	
	// Ïîëó÷àåì ïðåäëîæåíèå èç áàçû äàííûõ è ñîáèðàåì â îäíó XML ñòðóêòóðó
	function saveOffer($offer){
		foreach ($offer AS $key=>$value){
			if ($key=="id" || $key=="type" || $key=="bid" || $key=="cbid" || $key=="available"){
				$offer_attrs .= " ".$key."=\"".$value."\"";
			} else {
				if($key=="price") $value=$this->replaceComma($value);
				$offer_elements .= "
				<".$key.">".$this->replaceSymbols($value)."</".$key.">";
			}
		}
		$this->offers .= "
			<offer".$offer_attrs.">".$offer_elements."
			</offer>";
	}
	
	// Ñîõðàíÿåì XML äàííûå â ôàéëå ñ èìåíåì $filename
	function saveYmlFile($datas){
		$file = fopen($this->filename,"w");
		fwrite($file,$datas);
		fclose($file);
	}
	
	// Ïðîâåðêà äàííûõ ïåðåä çàïèñüþ â ôàéë
	function dataCheck(){
		if (!isset($this->filename) || $this->filename=="") $this->error .= "Íå óêàçàííî èìÿ ôàéëà äëÿ çàïèñè YML<br>";
		if (!isset($this->categories) || $this->categories=="") $this->error .= "Îøèáêà çàïèñè êàòåãîðèé<br>";
		if (!isset($this->offers) || $this->offers=="") $this->error .= "Îøèáêà çàïèñè ïðåäëîæåíèé<br>";
		if (!isset($this->name) || $this->name=="") $this->error .= "Îøèáêà çàïèñè íàçâàíèÿ ìàãàçèíà<br>";
		if (!isset($this->company) || $this->company=="") $this->error .= "Îøèáêà çàïèñè íàçâàíèÿ êîìïàíèè<br>";
		if (!isset($this->url) || $this->url=="") $this->error .= "Îøèáêà çàïèñè URL ñàéòà<br>";
		if (!isset($this->currency) || $this->currency=="") $this->error .= "Îøèáêà çàïèñè âàëþò<br>";
	}
}
?>
Return current item: Yandex.Market YML