<?php
function soyshop_item_block_plugin($html, $page){

	$obj = $page->create("soyshop_item_block_plugin", "HTMLTemplatePage", array(
		"arguments" => array("soyshop_item_block_plugin", $html)
	));

	//件数
	preg_match('/block:id=\"item_list\".*count=\"(\d.*?)\"/', $html, $tmp);
	$limit = (isset($tmp[1]) && is_numeric($tmp[1]) && (int)$tmp[1] > 0) ? (int)$tmp[1] : 10;

	preg_match('/block:id=\"item_list\".*category=\"(\d.*?)\"/', $html, $tmp);
	if(isset($tmp[1]) && is_numeric($tmp[1]) && (int)$tmp[1] > 0){
		$categoryId = (int)$tmp[1];
	}else{
		$categoryId = null;
	}

	//カスタムサーチフィールド
	preg_match('/block:id=\"item_list\".*csf:column=\"(.*?)\"/', $html, $tmp);
	if(isset($tmp[1]) && strlen($tmp[1])){
		$csfFieldId = trim($tmp[1]);
		preg_match('/block:id=\"item_list\".*csf:value=\"(.*?)\"/', $html, $tmp);
		$csfValue = (isset($tmp[1]) && strlen($tmp[1])) ? trim($tmp[1]) : null;

		$logic = SOY2Logic::createInstance("module.plugins.custom_search_field.logic.SearchLogic");

		if(isset($categoryId)) $_GET["c_search"]["parent_and_child_category"] = $categoryId;	//親子を両方調べる
		$_GET["c_search"][$csfFieldId] = $csfValue;
		$items = $logic->search(new SOYShop_SearchPage(), 1, $limit);

	//カテゴリで検索
	}else{
		list($items, $total) = SOY2Logic::createInstance("logic.shop.item.SearchItemUtil")->getByCategoryIds($categoryId, 0, $limit);
	}

	SOY2::import("base.site.classes.SOYShop_ItemListComponent");
	$obj->createAdd("item_list", "SOYShop_ItemListComponent", array(
		"list" => $items,
		"soy2prefix" => "block"
	));

	$obj->display();
}
