public function action_update_files($id = false) { if (!is_numeric($id)) { \Response::redirect('admin/news/list'); } // Get news item to edit if (!($item = Model_News::find_one_by_id($id))) { \Response::redirect('admin/news/list'); } \View::set_global('title', 'Edit News Files'); if (\Input::post()) { if (\Input::post('file_upload', false)) { // Upload files and display errors if there are any $file = $this->upload_file(); if (!$file['exists'] && \Config::get('details.image.required', false) && empty($item->images)) { // No previous images and image is not selected and it is required \Messages::error('<strong>There was an error while trying to upload content file</strong>'); \Messages::error('You have to select image'); } elseif ($file['errors']) { \Messages::error('<strong>There was an error while trying to upload content file</strong>'); foreach ($file['errors'] as $error) { \Messages::error($error); } } if ($file['is_valid'] && !(!$file['exists'] && \Config::get('details.file.required', false) && empty($item->files))) { /** FILES **/ // Get all alt texts to update if there is no image change foreach (\Arr::filter_prefixed(\Input::post(), 'title_') as $file_id => $title) { if (strpos($file_id, 'new_') === false) { $item_files[$file_id] = array('id' => $file_id, 'data' => array('title' => \Input::post('title_' . $file_id, ''))); } } // Save files if new ones are submitted if (isset($this->_file_data)) { foreach ($this->_file_data as $file_data) { $cover_count = count($item->files); if (strpos($file_data['field'], 'new_') === false) { // Update existing file if (str_replace('file_', '', $file_data['field']) != 0) { $file_id = (int) str_replace('file_', '', $file_data['field']); $cover_count--; $item_files[$file_id] = array('id' => $file_id, 'data' => array('content_id' => $item->id, 'file' => $file_data['saved_as'], 'title' => \Input::post('title_' . $file_id, ''))); $this->delete_file(\Input::post('file_db_' . $file_id, '')); } } else { // Save new file $file_tmp = str_replace('file_new_', '', $file_data['field']); $item_files[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'file' => $file_data['saved_as'], 'title' => \Input::post('title_new_' . $file_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); } } } Model_News::bind_files($item_files); \Messages::success('News documents successfully updated.'); \Response::redirect(\Uri::admin('current')); /** END OF FILES **/ } else { // Delete uploaded files if there is news saving error if (isset($this->_file_data)) { foreach ($this->_file_data as $file_data) { $this->delete_file($file_data['saved_as']); } } \Messages::error('<strong>There was an error while trying to update news documents</strong>'); } } if (\Input::post('video_upload', false)) { // Upload image and display errors if there are any $image = $this->upload_image('video'); if ($image['errors']) { \Messages::error('<strong>There was an error while trying to upload content image</strong>'); foreach ($image['errors'] as $error) { \Messages::error($error); } } if ($image['is_valid'] && !(!$image['exists'] && \Config::get('details.video.required', false) && empty($item->videos))) { /** VIDEOS **/ // Get all video urls to update if there is no image change foreach (\Arr::filter_prefixed(\Input::post(), 'video_url_') as $video_id => $url) { $not_updated[] = $video_id; if (strpos($video_id, 'new_') === false) { $video_url = \Input::post('video_url_' . $video_id, false); if ($video_url) { // Check video $youtube = \App\Youtube::forge(); $video = $youtube->parse(\Input::post('video_url_' . $video_id, ''))->get(); if ($video) { // Update existing videos $item_videos[$video_id] = array('id' => $video_id, 'data' => array('content_id' => $item->id, 'url' => \Input::post('video_url_' . $video_id, $video['link']) ?: $video['link'], 'title' => \Input::post('video_title_' . $video_id, $video['title']) ?: $video['title'])); // Remove video ID as it is updated correctly array_pop($not_updated); // Delete image and use default one if (\Input::post('video_delete_image_' . $video_id, false)) { $item_videos[$video_id]['data']['thumbnail'] = ''; $this->delete_image(\Input::post('video_file_db_' . $video_id, ''), 'video'); } } else { \Messages::error('"' . \Input::post('video_url_' . $video_id, '') . '" is invalid video URL. Video not updated.'); } } else { \Messages::error('Video URL can\'t be empty. Video not updated.'); } } else { $video_url = \Input::post('video_url_' . $video_id, false); if ($video_url) { // Check video $youtube = \App\Youtube::forge(); $video = $youtube->parse(\Input::post('video_url_' . $video_id, ''))->get(); if ($video) { // Add new videos $item_videos[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'url' => \Input::post('video_url_' . $video_id, $video['link']) ?: $video['link'], 'title' => \Input::post('video_title_' . $video_id, $video['title']) ?: $video['title'], 'sort' => count($item->videos) + 1)); // Remove video ID as it is updated correctly array_pop($not_updated); } else { \Messages::error('"' . \Input::post('video_url_' . $video_id, '') . '" is invalid video URL. Video not updated.'); } } } } // Save images if new files are submitted if (isset($this->_image_data)) { foreach ($this->_image_data as $image_data) { if (!in_array(str_replace('video_file_', '', $image_data['field']), $not_updated)) { if (strpos($image_data['field'], 'new_') === false) { // Update existing image if (str_replace('video_file_', '', $image_data['field']) != 0) { $video_id = (int) str_replace('video_file_', '', $image_data['field']); $item_videos[$video_id]['data']['thumbnail'] = $image_data['saved_as']; $this->delete_image(\Input::post('video_file_db_' . $video_id, ''), 'video'); } } else { // Save new image $image_tmp = str_replace('video_file_new_', '', $image_data['field']); $item_videos[0]['data']['thumbnail'] = $image_data['saved_as']; } } else { // That video was not updated so we remove its image if there is one $this->delete_image($image_data['saved_as'], 'video'); } } } Model_News::bind_videos($item_videos); \Messages::success('News videos successfully updated.'); \Response::redirect(\Uri::admin('current')); /** END OF VIDEOS **/ } else { // Delete uploaded images if there is news saving error if (isset($this->_image_data)) { foreach ($this->_image_data as $image_data) { $this->delete_image($image_data['saved_as'], 'video'); } } \Messages::error('<strong>There was an error while trying to update news videos</strong>'); } } } \Theme::instance()->set_partial('content', $this->view_dir . 'update_files')->set('news', Model_News::find_one_by_id($id)); }