public function migration() {
$products = unserialize(file_get_contents(__DIR__ . '/../../external/data.txt'));;
echo "<pre>";
$t = 0;
$parentID = 4731;
foreach($products as $menu1 => $category1) {
$parentID1 = $this->createCatalog($menu1, $parentID);
echo "$menu1:\n";
foreach ($category1 as $menu2 => $category2) {
$parentID2 = $this->createCatalog($menu2, $parentID1);
echo "\t$menu2\n";;
foreach($category2 as $menu3 => $category3) {
$parentID3 = $this->createCatalog($menu3, $parentID2);
echo "\t\t$menu3\n";;
foreach($category3 as $menu4 => $category4) {
if (isset($category4['price'])) {
echo "\t\t\t *". $category4['name'] ."\n";
if (!is_int($this->createProduct($category4, $parentID3))) {
die('wrong a');
}
$t++;
if ($t > 30) die('okok');
}else {
continue;
$parentID4 = $this->createCatalog($menu4, $parentID3);
echo "\t\t\t$menu4\n";
foreach($category4 as $menu5 => $category5) {
echo "\t\t\t\t *". $category5['name'] ."\n";
if (!is_int($this->createProduct($category5, $parentID4))) {
die('wrong');
}
if ($t > 30) die('okok');
}
}
}
}
}
}
}
private function createCatalog($title, $pid) {
$do = new Catalog();
$do->Title = $title;
$do->MenuTitle = $title;
$do->ParentID = $pid;
$parentID = $do->write();
// $do->doPublish();
return $parentID;
}
private function createProduct($data, $pid) {
if ($pid <= 0) die('pidpid');
$do = new Product();
$do->ParentID = $pid;
$do->Title = $data['name'];
$do->MenuTitle = $data['name'];
$do->Content = $data['description'];
$do->Price = $data['price']['litais'];
if ($data['images']) {
foreach($data['images'] as $i => $image) {
if ($i > 4) {echo $i."more than\n"; break; }
if (file_exists(ASSETS_PATH . '/images/' . $image . '.jpg')) {
$img = new Image();
$img->setFilename(ASSETS_DIR . '/images/' . $image . '.jpg');
$img->Title = $data['name'];
$imgID = $img->write();
$img->CMSThumbnail();
$img->updateFilesystem();
$imgStr = 'Photo' . ($i+1) . 'ID';
$do->{$imgStr} = $imgID;
}else {
echo 'shit';
echo ASSETS_PATH . '/images/' . $image . '.jpg';
}
}
}
return $do->write();
// $do->doPublish();
//print_r($data);
//exit;
}