132 lines
3.8 KiB
PHP
132 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace KupShop\RewriteBundle\Admin;
|
|
|
|
use KupShop\KupShopBundle\Context\DomainContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\RewriteBundle\Util\Rewrite;
|
|
|
|
class RewriteImport extends \Frame
|
|
{
|
|
protected $template = 'board/rewriteImport.tpl';
|
|
protected $tableName = 'rewrite';
|
|
|
|
private $importErrors;
|
|
private $rewrite;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->rewrite = ServiceContainer::getService(Rewrite::class);
|
|
}
|
|
|
|
public function get_vars()
|
|
{
|
|
$vars = parent::get_vars();
|
|
|
|
if ($this->importErrors) {
|
|
$vars['body']['data']['import']['errors'] = $this->importErrors;
|
|
}
|
|
|
|
return $vars;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
if (getVal('acn') == 'importUrls') {
|
|
$errors = $this->importAction();
|
|
if (!empty($errors) && (!empty($errors['textarea']) || !empty($errors['file']))) {
|
|
$this->importErrors = $errors;
|
|
} else {
|
|
$this->returnOK();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getData()
|
|
{
|
|
$data = getVal('data', null, []);
|
|
|
|
if (!empty($data)) {
|
|
$data['ID'] = getVal('ID');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function importAction()
|
|
{
|
|
$data = $this->getData();
|
|
if (isset($data['import']['urls'])) {
|
|
$errorsFile = [];
|
|
// window import
|
|
$urls = $data['import']['urls'];
|
|
$fp = fopen('php://temp', 'r+');
|
|
fputs($fp, $urls);
|
|
rewind($fp);
|
|
$errors = $this->importUrls($fp);
|
|
fclose($fp);
|
|
|
|
// file import
|
|
if ($file = getVal('file_urls', $_FILES)) {
|
|
if (!empty($file['tmp_name'])) {
|
|
$fp = fopen($file['tmp_name'], 'r');
|
|
$errorsFile = $this->importUrls($fp);
|
|
fclose($fp);
|
|
}
|
|
}
|
|
|
|
return ['textarea' => $errors, 'file' => $errorsFile];
|
|
}
|
|
|
|
return ['textarea' => [], 'file' => []];
|
|
}
|
|
|
|
public function importUrls($fp)
|
|
{
|
|
$errors = [];
|
|
$lineNo = 0;
|
|
$domains = ServiceContainer::getService(DomainContext::class)->getSupported();
|
|
|
|
$keepUrlsDomain = ($this->getData()['import']['keep_domain'] ?? 'N') === 'Y';
|
|
|
|
while ($line = fgets($fp)) {
|
|
$lineNo++;
|
|
$line = trim($line);
|
|
if (!$line) {
|
|
continue;
|
|
}
|
|
|
|
$url_default = ['host' => '', 'path' => ''];
|
|
$data = preg_split('/[\s,]+/', $line);
|
|
if (isset($data[0]) && isset($data[1])) {
|
|
$parts0 = array_merge($url_default, parse_url($data[0]));
|
|
$parts1 = array_merge($url_default, parse_url($data[1]));
|
|
|
|
$oldUrl = $parts0['path'];
|
|
$oldUrl .= !empty($parts0['query']) ? ('?'.$parts0['query']) : '';
|
|
|
|
// nekdy proste potrebuju fakt zachovat domenu na obou stranach (stara i nova url), protoze treba pro CZ i EN meli stejnou URL
|
|
// takze potrebuju domena.cz/neco -> domena.cz/neco-noveho a domena.com/neco -> domena.en/something-new atd...
|
|
if ($keepUrlsDomain) {
|
|
$oldUrl = $data[0];
|
|
$newUrl = $data[1];
|
|
} elseif ($parts0['host'] == $parts1['host'] || in_array($parts1['host'], $domains)) {
|
|
$newUrl = $parts1['path'];
|
|
} else {
|
|
$newUrl = $data[1];
|
|
}
|
|
} else {
|
|
$errors[$lineNo] = $line;
|
|
continue;
|
|
}
|
|
|
|
$urlType = $this->rewrite->getTypeByUrl($newUrl);
|
|
$this->rewrite->addRewrite($oldUrl, $urlType['type'], $urlType['related']);
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
}
|
|
|
|
return RewriteImport::class;
|