djfhdfjkghdkghrghthieruytiotlkr
djfhdfjkghdkghrghthieruytiotlkr
Изображение отсутствует
public function store()
{
if (!$this->request->is('post')) {
return redirect()->back()->with('error', 'Неверный запрос.');
}
$validation = \Config\Services::validation();
$validation->setRules([
'title' => 'required|min_length[5]|max_length[255]',
'content' => 'required|min_length[10]',
'category_id' => 'permit_empty|numeric',
'short_description' => 'permit_empty|max_length[500]',
]);
if (!$validation->withRequest($this->request)->run()) {
return redirect()->back()
->withInput()
->with('errors', $validation->getErrors());
}
try {
$userId = (int)$this->session->get('user_id');
// Определяем действие
$action = $this->request->getPost('action');
// Определяем статус в зависимости от действия
if ($action === 'submit') {
$status = 'pending'; // Отправляем на модерацию
$successMessage = 'Статья отправлена на модерацию';
} else {
$status = 'draft'; // Сохраняем как черновик
$successMessage = 'Статья успешно сохранена как черновик';
}
// Генерация slug
$slug = url_title($this->request->getPost('title'), '-', true);
$originalSlug = $slug;
$counter = 1;
while ($this->articleModel->where('slug', $slug)->first()) {
$slug = $originalSlug . '-' . $counter;
$counter++;
if ($counter > 100) {
throw new \RuntimeException('Не удалось сгенерировать уникальный slug');
}
}
$data = [
'user_id' => $userId,
'title' => $this->request->getPost('title'),
'slug' => $slug,
'content' => $this->request->getPost('content'),
'short_description' => $this->request->getPost('short_description'),
'category_id' => $this->request->getPost('category_id') ?: null,
'meta_title' => $this->request->getPost('meta_title'),
'meta_description' => $this->request->getPost('meta_description'),
'meta_keywords' => $this->request->getPost('meta_keywords'),
'status' => $status, // Используем определенный статус
];
// Обработка загрузки изображения
$image = $this->request->getFile('featured_image');
if ($image && $image->isValid() && !$image->hasMoved()) {
$newName = $image->getRandomName();
$image->move(ROOTPATH . 'public/uploads/articles', $newName);
$data['featured_image'] = 'uploads/articles/' . $newName;
}
if ($this->articleModel->save($data)) {
$this->session->setFlashdata('success', $successMessage);
log_message('info', "User {$userId} created article: " . $data['title'] . " with status: {$status}");
return redirect()->to('/dashboard/articles')
->with('success', $successMessage);
}
throw new \RuntimeException('Ошибка при создании статьи');
} catch (\RuntimeException $e) {
log_message('error', 'Error creating article: ' . $e->getMessage());
return redirect()->back()
->withInput()
->with('error', $e->getMessage());
} catch (\Exception $e) {
log_message('error', 'Exception creating article: ' . $e->getMessage());
return redirect()->back()
->withInput()
->with('error', 'Ошибка при создании статьи: ' . $e->getMessage());
}
}
Комментарии (0)
Пока нет комментариев. Будьте первым!