/**
  * Action - audio
  * upload/download/ordering audio
  * 
  * Access to the action is possible in the following paths:
  * - /blogmanager/audio
  *
  * @return void
  */
 public function audioAction()
 {
     $json = array();
     $filterSanitize = new Default_Form_Filter_Sanitize();
     //-----------------------
     // Получим обьект записи
     $request = $this->getRequest();
     $post_id = (int) $request->getPost('id');
     if (!$post_id) {
         $post_id = (int) $request->getParam('id');
     }
     $post = new Default_Model_DbTable_BlogPost($this->db);
     // Если конкретной записи нет, то перейдем к странице по умолчанию
     if (!$post->loadForUser($this->_identity->user_id, $post_id)) {
         $this->_redirect('/blogmanager');
     }
     // Определим тип операции над аудио: 'upload', 'reorder', 'delete'
     // Загрузка файла с помощью - FileUploader
     if (Default_Plugin_FileUploader::isFileUploader()) {
         // Получим вид загрузчика - Iframe или Xhr
         $fileUploader = Default_Plugin_FileUploader::isFileUploader();
         // list of valid extensions, ex. array("jpeg", "xml", "bmp")
         $allowedExtensions = explode(';', $request->getParam('allowedExtensions'));
         // max file size in bytes
         $sizeLimit = (int) $request->getParam('sizeLimit');
         // Получим обьект загрузчика файлов
         try {
             $uploader = new Default_Plugin_FileUploader($allowedExtensions, $sizeLimit);
             // Определим путь загрузки файлов
             $path = Default_Model_DbTable_BlogPostAudio::GetUploadPath();
             $path .= '/';
             //Загрузим файлы
             $result = $uploader->handleUpload($path);
         } catch (Exception $e) {
             $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка загрузки файла') . '</em>', Default_Plugin_SysBox::getMessageError($e)));
             if ($fileUploader == 'Iframe') {
                 $this->sendJson_Html($json);
             } else {
                 $this->sendJson($json);
             }
             return;
         }
         if (isset($result['success'])) {
             // OK
             // Создадим обьект изображения
             try {
                 $audio = new Default_Model_DbTable_BlogPostAudio($post->getDb());
                 $audio->post_id = $post->getId();
                 $filename = $path . $uploader->file->getName();
                 $audio->uploadFile($filename);
                 $audio->filename = basename($filename);
                 if (!$audio->save()) {
                     $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка при сохранении данных') . '</em>'));
                     if ($fileUploader == 'Iframe') {
                         $this->sendJson_Html($json);
                     } else {
                         $this->sendJson($json);
                     }
                     return;
                 }
                 $json['success'] = $result['success'];
                 $json['audio_id'] = $audio->getId();
                 $json['filename'] = $audio->filename;
                 $json['post_id'] = $audio->post_id;
                 $json['url_image'] = $this->getUrlRes('images/media/thumbs/file-mp3.png') . '?id=' . $audio->getId();
                 $json['form_action'] = $this->getUrl('audio', 'blogmanager');
             } catch (Exception $e) {
                 $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка загрузки файла') . '</em>', Default_Plugin_SysBox::getMessageError($e)));
                 if ($fileUploader == 'Iframe') {
                     $this->sendJson_Html($json);
                 } else {
                     $this->sendJson($json);
                 }
                 return;
             }
         } else {
             // Error
             $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка загрузки файла') . '</em>', $result['error']));
             if ($fileUploader == 'Iframe') {
                 $this->sendJson_Html($json);
             } else {
                 $this->sendJson($json);
             }
             return;
         }
     } else {
         if ($request->getPost('reorder')) {
             $order = $request->getPost('preview-audio');
             $post->setAudioOrder($order);
         } else {
             if ($request->getPost('delete')) {
                 $audio_id = (int) $request->getPost('image');
                 $audio = new Default_Model_DbTable_BlogPostAudio($this->db);
                 if ($audio->loadForPost($post->getId(), $audio_id)) {
                     $audio->delete();
                     // Определим кол. оставшихся изображений
                     $count_audios = count($post->audio) - 1;
                     $json = array('deleted' => true, 'image_id' => $audio_id, 'count_images' => $count_audios);
                 }
             } else {
                 if ($request->getPost('comment_update')) {
                     $audio_id = (int) $request->getPost('image');
                     $audio = new Default_Model_DbTable_BlogPostAudio($this->db);
                     if ($audio->loadForPost($post->getId(), $audio_id)) {
                         $comment = $request->getPost('comment');
                         $comment = $filterSanitize->filter($comment);
                         $arrComment = explode('#', $comment);
                         if (count($arrComment) > 1) {
                             $audio->name = $arrComment[0];
                             $audio->comment = $arrComment[1];
                         } else {
                             $audio->name = $arrComment[0];
                         }
                         if (!$audio->save()) {
                             $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка при сохранении данных') . '</em>'));
                             $this->sendJson($json);
                             return;
                         }
                         $json = array('commented' => true, 'title' => $audio->name, 'comment' => $audio->comment);
                     }
                 } else {
                     if ($request->getPost('download_images')) {
                         // Загрузим изображения в виде HTML на страницу
                         // Получим файлы музыки для статьи
                         $audios = Default_Model_DbTable_BlogPostAudio::GetAudio($this->db, array('post_id' => $post_id));
                         // Создадим обьект шаблона
                         $templater = Default_Plugin_SysBox::createViewSmarty();
                         //Установим параметры шаблона
                         $templater->audios = $audios;
                         $templater->post_id = $post_id;
                         // Получим результат шаблона
                         $html = $templater->render('blogmanager/lib/download-audio.tpl');
                         $json = array('downloaded' => true, 'html' => $html);
                     }
                 }
             }
         }
     }
     if ($this->_isAjaxRequest) {
         $this->sendJson($json);
     } else {
         $this->sendJson_Html($json);
     }
 }