Views: 10032
Last Modified: 18.09.2014
Infoblock copy is not provided for in Bitrix Framework as a standard option, although sometimes it may become necessary, and it can be done. Automation of this process will be a good example of using infoblock API.
The Use of XML Import
Infoblocks can be copied using the XML import/export function:
- Download the necessary infoblock by exporting in XML.
- Open the XML file for editing and carefully adjust the infoblock ID where necessary. In the beginning of the XML, the ID node may be replaced with the Title node:
<?xml version="1.0" encoding="UTF-8"?>
<CommerceInformation SchemaVersion="2.021" CreationDate="2010-03-20T09:55:13">
<Metadata>
<Id>2
<Title>Notebooks
<Properties>
<Property>
<Id>CML2_CODE
<Title>Symbol code
Find the following code after the description of the infoblock and its properties:
<Catalog>
<Id>2Ид>
<MetadataId>2
<Title>Notebooks
Establish data in accordance with the amendments made above in the nodes ID, MetadataId, and Title.
Copy Automation
Use the script provided below to import metadata from the information block created earlier when generating a new one:
Metadata copy setting is made using three fields:
Script code:
CModule::IncludeModule("iblock");
if(intval($_REQUEST["IBLOCK_ID_FIELDS"])>0){
$bError = false;
$IBLOCK_ID = intval($_REQUEST["IBLOCK_ID_FIELDS"]);
$ib = new CIBlock;
$arFields = CIBlock::GetArrayByID($IBLOCK_ID);
$arFields["GROUP_ID"] = CIBlock::GetGroupPermissions($IBLOCK_ID);
$arFields["NAME"] = $arFields["NAME"]."_new";
unset($arFields["ID"]);
if($_REQUEST["IBLOCK_TYPE_ID"]!="empty")
$arFields["IBLOCK_TYPE_ID"]=$_REQUEST["IBLOCK_TYPE_ID"];
$ID = $ib->Add($arFields);
if(intval($ID)<=0)
$bError = true;
if($_REQUEST["IBLOCK_ID_PROPS"]!="empty")
$iblock_prop=intval($_REQUEST["IBLOCK_ID_PROPS"]);
else
$iblock_prop=$IBLOCK_ID;
$iblock_prop_new = $ID;
$ibp = new CIBlockProperty;
$properties = CIBlockProperty::GetList(Array("sort"=>"asc", "name"=>"asc"), Array("ACTIVE"=>"Y", "IBLOCK_ID"=>$iblock_prop));
while ($prop_fields = $properties->GetNext()){
if($prop_fields["PROPERTY_TYPE"] == "L"){
$property_enums = CIBlockPropertyEnum::GetList(Array("DEF"=>"DESC", "SORT"=>"ASC"),
Array("IBLOCK_ID"=>$iblock_prop, "CODE"=>$prop_fields["CODE"]));
while($enum_fields = $property_enums->GetNext()){
$prop_fields["VALUES"][] = Array(
"VALUE" => $enum_fields["VALUE"],
"DEF" => $enum_fields["DEF"],
"SORT" => $enum_fields["SORT"]
);
}
}
$prop_fields["IBLOCK_ID"]=$iblock_prop_new;
unset($prop_fields["ID"]);
foreach($prop_fields as $k=>$v){
if(!is_array($v))$prop_fields[$k]=trim($v);
if($k{0}=='~') unset($prop_fields[$k]);
}
$PropID = $ibp->Add($prop_fields);
if(intval($PropID)<=0)
$bError = true;
}
if(!$bError && $IBLOCK_ID>0)
LocalRedirect($APPLICATION->GetCurPageParam("success=Y",array("success","IBLOCK_ID_FIELDS")));
else
LocalRedirect($APPLICATION->GetCurPageParam("error=Y",array("success","IBLOCK_ID_FIELDS")));
}
$str .='<form action='.$APPLICATION->GetCurPageParam().' method="post">[table]';
if($_REQUEST["success"]=="Y") $str .='[tr][td]<font color="green">IB is copied successfully</font>[b][/td][/tr]';
elseif($_REQUEST["error"]=="Y") $str .='[tr][td]<font color="red">Error</font><br/>[/td][/tr]';
$str .='[tr][td]Copy of IB metadata to a new IB:[/b]<br/>[/td][/tr]';
$res = CIBlock::GetList(Array(),Array(),true);
while($ar_res = $res->Fetch())
$arRes[]=$ar_res;
$str .='[tr][td]Copy IB:<br><select name="IBLOCK_ID_FIELDS">';
foreach($arRes as $vRes)
$str .= '<option value='.$vRes['ID'].'>'.$vRes['NAME'].' ['.$vRes["ID"].']</option>';
$str .='</select>[/td]';
$str .='[td]Copy to new IB properties of another IB: *<br><select name="IBLOCK_ID_PROPS">';
$str .='<option value="empty">';
foreach($arRes as $vRes)
$str .= '<option value='.$vRes['ID'].'>'.$vRes['NAME'].' ['.$vRes["ID"].']</option>';
$str .='</select>[/td][/tr]';
$str .='[tr][td]Copy IB to type:<br><select name="IBLOCK_TYPE_ID">';
$str .='<option value="empty">';
$db_iblock_type = CIBlockType::GetList();
while($ar_iblock_type = $db_iblock_type->Fetch()){
if($arIBType = CIBlockType::GetByIDLang($ar_iblock_type["ID"], LANG))
$str .= '<option value='.$ar_iblock_type["ID"].'>'.htmlspecialcharsex($arIBType["NAME"])."</option>";
}
$str .='</select>[/td][/tr]';
$str .='[tr][td]<br/>if the value is not specified, IB metadata of the "Properties" section will be collected from the IB of the 1st field[/td][/tr]';
$str .='[tr][td]<input type="submit" value="copy">[/td][/tr]';
$str .='[/table]</form>';
echo $str;
The script can prove to be of invaluable help, for example, when copying IB without using XML export/import mechanisms of information blocks.
This tool is recommended for infoblocks where there are many list-based properties or generally a big number of properties that require a detailed setup.
The script must be located in the website root.