<?php
class actionParsingrssSaveNew extends cmsAction{
public function run(){
$h1 = $_SESSION['article']['h1'];
$src = $_SESSION['article']['img']['src'];
$text = $_SESSION['article']['p'];
$file_name = $h[count($h) - 1];
//=====================================================
$_POST['photo'] = $src;
$this->cms_uploader->enableRemoteUpload()->setAllowedMime([
'image/jpeg',
'image/png',
'image/gif',
'image/webp'
]);
$result = $this->cms_uploader->upload('photo');
//print_r($result);
//die();
if ($result['success']) {
try {
$image = new cmsImages($result['path']);
} catch (Exception $exc) {
$result['success'] = false;
$result['error'] = LANG_UPLOAD_ERR_MIME;
}
}
if (!$result['success']) {
if (!empty($result['path'])) { files_delete_file($result['path'], 2);
}
$src = null;
} else {
$sizes = ['content_item', 'content_list', 'content_list_small', 'small'];
$result['paths'] = [];
$file_context = [
'target_controller' => 'content',
'target_subject' => 'news',
'target_id' => null
];
$presets = $this->model_images->orderByList([
['by' => 'is_square', 'to' => 'asc'],
['by' => 'width', 'to' => 'desc']
])->getPresets();
foreach ($presets as $p) {
if (!in_array($p['name'], $sizes, true)) { continue;
}
$resized_path = $image->resizeByPreset($p);
if (!$resized_path) {
continue;
}
$result['paths'][$p['name']] = $resized_path;
//print_r($resized_path);
//die();
if ($file_context['target_controller']) {
$this->controller_images->registerUploadFile($file_context);
}
$this->controller_images->registerFile(['path' => $resized_path]);
}
//Удаляем или регистрируем оригинал
if (!in_array('original', $sizes, true)) {
files_delete_file($result['path'], 2);
} else {
if ($file_context['target_controller']) {
$this->registerUploadFile($file_context);
}
$this->registerFile(['path' => $result['url']]);
}
$src = $result['paths'];
}
$content = cmsCore::getController('content');
$content->request->set('csrf_token', cmsForm::getCSRFToken());
$content->request->set('photo', $src);
$content->request->set('category_id', 3);
$content->request->set('is_private', 0);
$content->request->set('date_pub', $today);
$content->request->set('title', $h1);
$content->request->set('teaser', $h1);
$content->request->set('submit', 'Сохранить');
$content->request->set('ctype_name', 'news');
$content->request->set('is_comments_on', 1);
$content->request->set('is_pub', 1);
$content->request->set('content', $text);
//$content->request->set('back', $_SERVER['HTTP_REFERER']);
$content->runAction('item_add');