Last active
July 25, 2018 03:05
-
-
Save pavel-one/105279f079b49f2165eaa05d28b412a6 to your computer and use it in GitHub Desktop.
Example import product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pdo = $modx->getService('pdoFetch'); | |
//$filds = 'id, pagetitle, longtitle, description, alias, introtext, content, parent, article, price, old_price, weight, image, made_in' | |
$pdo->setConfig(array( | |
'parents' => 0, | |
'limit' => 0, | |
'depth' => 999, | |
'return' => 'data', | |
'includeTVs' => 'productCertificates, productDescription, productCharacteristics, productRecommendations, out_of_stock', | |
'leftJoin' => [ | |
'Data' => [ | |
'class' => 'msProductData', | |
'on' => 'modResource.id = Data.id' | |
], | |
'Vendor' => [ | |
'class' => 'msVendor', | |
'on' => 'Data.vendor = Vendor.id' | |
] | |
], | |
'select' => [ | |
'modResource' => 'id,pagetitle,longtitle,description,alias,introtext,content,parent', | |
'Data' => 'article, price, old_price, weight, image, made_in, vendor', | |
'Vendor' => 'name as vendorname' | |
], | |
'where' => array( | |
'class_key' => 'msProduct', | |
//'available' => '1', | |
) | |
)); | |
$arr = $pdo->run(); | |
$outArray = array( | |
array( | |
'id', | |
'pagetitle', | |
'longtitle', | |
'description', | |
'alias', | |
'introtext', | |
'content', | |
'parent', | |
'article', | |
'price', | |
'old_price', | |
'weight', | |
'image', | |
'made_in', | |
'vendorname', | |
'productFiles', | |
'productDescriptionBlocks', | |
'productCharacter', | |
'productInstruction', | |
'outOfStock' | |
) | |
); | |
foreach ($arr as $item) { | |
//описание | |
$newProdDsc = array(); | |
if ($item['productDescription']) { | |
foreach($item['productDescription'] as $prodDsc) { | |
$newProdDsc[] = array( | |
'MIGX_id' => $prodDsc['MIGX_id'], | |
'title' => '', | |
'text' => $prodDsc['content'], | |
'images' => json_decode($prodDsc['images'], 1), | |
); | |
} | |
} | |
$item['productDescription'] = $newProdDsc; | |
//характеристика | |
$newChar = ''; | |
if ($item['productCharacteristics']) { | |
foreach ($item['productCharacteristics'] as $char) { | |
$newChar .= $char['content'].'<br />'; | |
} | |
} | |
$item['productCharacteristics'] = $newChar; | |
//рекомендации | |
$item['productRecommendations'] = getRecomend($item['productRecommendations']); | |
if (!$item['out_of_stock']) { | |
$item['out_of_stock'] = 0; | |
} | |
$outArray[] = array( | |
'id' => $item['id'], | |
'pagetitle' => $item['pagetitle'], | |
'longtitle' => $item['longtitle'], | |
'description' => $item['description'], | |
'alias' => $item['alias'], | |
'introtext' => $item['introtext'], | |
'content' => $item['content'], | |
'parent' => getParents($item['id']), | |
'article' => $item['article'], | |
'price' => $item['price'], | |
'old_price' => $item['old_price'], | |
'weight' => $item['weight'], | |
'image' => 'paveltemp/'.str_replace('/assets/images/', '', $item['image']), | |
'made_in' => $item['made_in'], | |
'vendorname' => $item['vendorname'], | |
'productFiles' => json_encode($item['productCertificates']), | |
'productDescriptionBlocks' => json_encode($item['productDescription']), | |
'productCharacter' => $item['productCharacteristics'], | |
'productInstruction' => $item['productRecommendations'], | |
'outOfStock' => $item['out_of_stock'], | |
); | |
} | |
//echo '<pre>'; | |
function getParents($id) { | |
global $modx; | |
$out = ''; | |
$arr = array(); | |
for ($i=0; $i<99; $i++) { | |
$parId = $modx->getObject('modResource', $id)->get('parent'); | |
$parentRes = $modx->getObject('modResource', $parId); | |
if (!$parentRes) { | |
$out = 'Fake'; | |
break; | |
} | |
$parentId = $parentRes->get('id'); | |
if ($parentId != 3) { | |
$pagetitile = $parentRes->get('pagetitle'); | |
$arr[] = $pagetitile; | |
//echo $pagetitile; | |
$id = $parentId; | |
} else { | |
//print_r($arr); | |
$arr = array_reverse($arr); | |
$out = implode('|', $arr); | |
break; | |
} | |
} | |
return $out; | |
} | |
function getRecomend($current) { | |
if (!$current) { | |
return ''; | |
} | |
//$out = array(); | |
$outText = ''; | |
foreach ($current as $item) { | |
if ($item['content']) { | |
$outText .= $item['content'] . '<br />'; | |
} | |
if ($item['images']) { | |
$item['images'] = json_decode($item['images'], 1); | |
$outText .= '<div class="imageList">'; | |
foreach ($item['images'] as $image) { | |
$outText .= '<img src="'.$image['image'].'" />'; | |
} | |
$outText .= '</div>'; | |
} | |
} | |
return $outText; | |
} | |
//print_r($outArray); | |
$file = fopen(MODX_CORE_PATH.'import.csv', "w"); | |
foreach ($outArray as $item) { | |
fputcsv($file, $item, ';'); | |
} | |
/*Активируем почтовый сервис MODX*/ | |
$modx->getService('mail', 'mail.modPHPMailer'); | |
$modx->mail->set(modMail::MAIL_FROM, $modx->getOption('emailsender')); | |
$modx->mail->set(modMail::MAIL_FROM_NAME, $modx->getOption('site_name')); | |
/*Адрес получателя нашего письма*/ | |
$modx->mail->address('to', '[email protected]'); | |
/*Заголовок сообщения*/ | |
$modx->mail->set(modMail::MAIL_SUBJECT, 'Импорт завершен'); | |
/*Подставляем чанк с телом письма (предварительно его нужно создать)*/ | |
$modx->mail->set(modMail::MAIL_BODY, 'Можно выкачать базу!'); | |
/*Отправляем*/ | |
$modx->mail->setHTML(true); | |
if (!$modx->mail->send()) { | |
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo); | |
} | |
$modx->mail->reset(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment