Skip to content

Instantly share code, notes, and snippets.

@wbarcovsky
Created August 30, 2015 13:29
Show Gist options
  • Select an option

  • Save wbarcovsky/432ba9d8732e553e664b to your computer and use it in GitHub Desktop.

Select an option

Save wbarcovsky/432ba9d8732e553e664b to your computer and use it in GitHub Desktop.
<?php
namespace HLService\LightImport\Adapters {
use HLService\LightImport\Helpers\Status;
use HLService\LightImport\Helpers\File;
use PHPExcelReader\SpreadsheetReader;
class Sinerghenta
{
protected static $url = '{mail.nic.ru:143}';
protected static $login = 'synergenta@fondo.ru';
protected static $password = 'M7pm4awpW8yLM';
private $status;
private $data;
private $xlsData;
private $tempFolder;
public function __construct()
{
$this->status = new Status('Sinerghenta');
$this->data = new File('Sinerghenta');
$this->tempFolder = dirname(__DIR__) . '/temp/';
}
public function clearAdapterDir()
{
File::adapterDirClear($this->data->getAdapterDir());
}
public function getArchives($data)
{
// temp files names
$zipFile = $this->tempFolder . 'tmp.zip';
$xlsFile = $this->tempFolder . 'tmp.xls';
$imap = imap_open(self::$url, self::$login, self::$password);
$mc = imap_check($imap); // Get last message
$mc = $mc->Nmsgs;
$structure = imap_fetchstructure($imap, $mc);
$attachment = current($this->getAttachments($imap, $structure, $mc)); // Get 1st attachment in email
if (count($attachment) > 0) {
file_put_contents($zipFile, $attachment['attachment']);
$zip = new \ZipArchive();
if ($zip->open($zipFile) === true) {
$xlsData = $zip->getFromIndex(0); // Get 1st file from archive
$zip->close();
file_put_contents($xlsFile, $xlsData);
$this->xlsData = new SpreadsheetReader($xlsFile, false);
} else {
//TODO: Exception - attachment in wrong format or not founded (get data from prev. message?)
}
} else {
//TODO: Exception - attachment in wrong format or not founded (get data from prev. message?)
}
imap_close($imap);
}
private function getAttachments($connect, $structure, $messageCount)
{
$attachments = [];
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i ++) {
$attachments[$i] = [
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
];
if ($structure->parts[$i]->ifdparameters) {
foreach ($structure->parts[$i]->dparameters as $object) {
if (strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if ($structure->parts[$i]->ifparameters) {
foreach ($structure->parts[$i]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if ($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($connect, $messageCount, $i + 1);
if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
} elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
foreach ($attachments as $key => $attachment) {
if ( !$attachment['is_attachment']) {
unset($attachments[$key]);
}
}
return $attachments;
}
public function getCategories()
{
$categories = [];
$highestRow = $this->xlsData->sheets[0]['numRows'];
for ($row = 3; $row <= $highestRow; $row ++) {
$cell = $this->xlsData->sheets[0]['cells'][$row][3];
if ( !empty($cell)) {
$title = trim($cell);
$id = md5($title);
$categories[$id] = [
'title' => $title,
'keydata' => [
'id' => $id,
]
];
}
}
return $categories;
}
public function getProducts($category)
{
$highestRow = $this->xlsData->sheets[1]['numRows'];
$start = false;
$products = [];
for ($row = 5; $row < $highestRow; $row ++) {
$cell = $this->xlsData->sheets[1]['cells'][$row];
$title = explode('-', $cell[4]);
$title = trim($title[0]);
if (empty($cell[5])) { // Category line
if ($category['title'] != $title) {
if ($start) {
return $products;
}
} else {
$start = true; // Start to pick up products
}
} else { // Product line
if (!$start) {
continue;
}
$price = floatval($cell[7]) > 0 ? floatval($cell[7]) : floatval($cell[8]);
$products[] = [
'attachment_id' => $category['attachment_id'],
'shopcategory' => $category['attachment']['shopcategory'],
'prodart' => 'Sinerghenta-' . $cell[4],
'barcode' => '',
'model' => $cell[4],
'producer' => $category['title'],
'country' => '',
'images' => [],
'description' => $cell[5],
'stock' => 'onstock',
'price_in' => $price,
];
}
}
return $products;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment