Клуб о поиске Яндекса

Формирование YML файла

teploe55555
23 марта 2014, 12:24

Добрый день Народ. Подскажите, вот есть магазин. С помощью PHP формируется YML фаил для яндекса. Проблема в том что параметр parentId как то не правильно присваевывается. 

 


<categories>
<category id="2">Главная</category>
<category id="86" parentId="1">Манекены, торсы</category>
<category id="97" parentId="2">Манекены женские</category>
<category id="98" parentId="2">Манекены мужские</category>
<category id="99" parentId="2">Манекены детские</category>
<category id="104" parentId="1">Стойки для очков</category>
</categories>



Вчем можеть быть проблема? Подскажите. 

Сам код фаила формирования.

<?php 

class YaMarket {

public $project;

public $catalogs;

private $yml_counter;

private $yml_offers;


function __construct($project) {

if (!is_object($project)) 

$project = mysql_fetch_object(do_mysql_query("SELECT * FROM `".T_YAMARKET_PROJECTS."` WHERE `id` = $project")


$this->project = $project;

$this->catalogs = empty($project->catalogs) ? array() : array_fill_keys(explode(':', $project->catalogs), 1);

}


public function yml_publish() {

$this->project->offer_sales_notes = (empty($this->project->offer_sales_notes) ? '' : '

<sales_notes>'.xmlspecialchars($this->project->offer_sales_notes).'</sales_notes>');


$file = $_SERVER['cache_path']['yml'].$this->project->id.'.xml';


$open = fopen($file, 'w');

fwrite($open, $this->yml_full()

fclose($open);

do_chmod($file);

}


public function fix_project() {

if (empty($this->catalogs)) 

return $this->save_catalogs(


$list = array(

$q = do_mysql_query("SELECT `id` FROM `".T_TREE."` WHERE `id` IN (".implode(', ', array_keys($this->catalogs)).") AND `suspend`");

while (list($id) = mysql_fetch_row($q)) 

$list[] = $id;


if ($list) 

$this->remove_catalogs($list);



if (empty($this->catalogs)) 

return $this->save_catalogs(


$fix_needed = false;


$parents = array(

$q = do_mysql_query("SELECT `type`, `parent` FROM `".T_TREE."` WHERE `id` IN (".implode(', ', array_keys($this->catalogs)).")");

while (list($type, $parent) = mysql_fetch_row($q)) {

if ($parent != $this->project->tree_id and !array_key_exists($parent, $this->catalogs)) {

$this->add_catalogs($parent);

$fix_needed = true;

}

}


if ($fix_needed) 

$this->fix_project(

else 

$this->save_catalogs(

}


public function add_catalogs($list) {

if (!is_array($list)) 

$list = array($list);


foreach ($list as $id) 

$this->catalogs[$id] = 1;

}


public function remove_catalogs($list) {

if (!is_array($list)) 

$list = array($list);


foreach ($list as $id) 

unset($this->catalogs[$id]


$imploded = implode(', ', $list);


do_mysql_query("DELETE FROM `".T_YAMARKET_GOODS."` WHERE `project_id` = ".$this->project->id." AND `tree_id` IN ($imploded)");


$q = do_mysql_query("SELECT `id` FROM `".T_TREE."` WHERE `parent` IN ($imploded)");

$list = array(

while (list($id) = mysql_fetch_row($q)) 

$list[] = $id;


if ($list) 

$this->remove_catalogs($list);

}




private function save_catalogs() {

global $db;


$db->update_row(T_YAMARKET_PROJECTS, array(

'catalogs' => implode(':', array_keys($this->catalogs))

), $this->project->id);

}


private function yml_full() {

global $config;


$this->fix_project(


$this->yml_counter = 1;

$this->yml_offers = '';


$r  = '<?xml version="1.0" encoding="UTF-8"?>';


$r .= '

<!DOCTYPE yml_catalog SYSTEM "shops.dtd">

<yml_catalog date="'.date('Y-m-d H:i').'">

<shop>';


if (!empty($config['site']['name'])) 

$r .= '

<name>'.xmlspecialchars(unsafe($config['site']['name'])).'</name>';


if (!empty($config['site']['company'])) 

$r .= '

<company>'.xmlspecialchars(unsafe($config['site']['company'])).'</company>';


$r .= '

<url>'.$config['site']['url'].'</url>

<currencies>

<currency id="RUR" rate="1"/>

</currencies>

<categories>';


$r .= $this->yml_catalogs($this->project->yml_parent);


$r .= '

</categories>';


if (!empty($config['goods']['delivery_price'])) 

$r .= '

<local_delivery_cost>'.$config['goods']['delivery_price'].'</local_delivery_cost>';


$r .= '

<offers>';


$r .= $this->yml_offers;


$r .= '

</offers>';


$r .= '

</shop>

</yml_catalog>';


return $r;

}


private function yml_catalogs($parent = 0, $yml_parent = 0) {

global $config;


$parent = (int) $parent;


$result = '';


$q = do_mysql_query("SELECT `id`, `name`, `parent` FROM `".T_TREE."` WHERE `parent` = $parent ORDER BY `position`, `id`");

while (list($id, $name, $parent) = mysql_fetch_row($q)) {

if (!array_key_exists($id, $this->catalogs)) 

continue;


$yml_id = $this->yml_counter++;


$this->yml_offers .= $goods = $this->yml_goods($id, $yml_id);


if ($subcats = $this->yml_catalogs($id, $yml_id) or $goods) 

$result .= '

<category id="'.$id.'"'.($yml_parent ? ' parentId="'.$yml_parent.'"' : '').'>'.xmlspecialchars(unsafe($name)).'</category>'.$subcats;

}


return $result;

}


private function yml_goods($tree_id, $yml_parent) {

global $config;


$result = '';


list($mode, $goods) = mysql_fetch_row(do_mysql_query("SELECT `mode`, `goods` FROM `".T_YAMARKET_GOODS."` WHERE `project_id` = ".$this->project->id." AND `tree_id` = $tree_id")


//$condition = "`tree_id` = $tree_id AND !`suspend` AND `price` AND `image`";

$condition = "`tree_id` = $tree_id AND !`suspend` AND `price` AND `image` AND `yamarket` != ''";


if ($mode == 1 and !$goods) 

$condition .= " AND FALSE";

else if ($mode and $goods) 

$condition .= " AND `id`".($mode == 2 ? " NOT" : '')." IN (".str_replace(':', ', ', $goods).")";


$q = do_mysql_query("SELECT `id`, `name`, `articule`, `price`, `developer`, `brand`, `yamarket` FROM `".T_GOODS."` WHERE $condition ORDER BY `position`, `id`");

while ($r = mysql_fetch_object($q)) 

$result .= '

<offer id="'.$r->id.'" available="true">

<url>'.$config['site']['url'].'/index.php?'.MSRL.'='.$tree_id.'&amp;detail='.$r->id.'</url>

<price>'.$r->price.'</price>

<currencyId>RUR</currencyId>

<categoryId>'.$yml_parent.'</categoryId>

<picture>'.$config['site']['url'].'/data/images/goods/src/'.$r->id.'.jpg</picture>

<delivery>true</delivery>

<name>'.xmlspecialchars(unsafe($r->name)).'</name>

<description>'.xmlspecialchars(unsafe($r->yamarket)).'</description>'.

$this->project->offer_sales_notes.'

</offer>';


return $result;

}

}

 

?>


 

 


1 комментарий
Подписаться на комментарии к посту

;-( - меня выводят эти смайлы

Еще было бы хорошо видеть комментарии.

Я правильно понял, что структура берется из базы, и родительские связи тоже? Может быть в базе есть категория с id=1, поэтому и появляется parentId=1