public function addofferformAction()
 {
     $this->view->disable();
     function valid_form_offer()
     {
         $sub_mess = '';
         if (!isset($_POST['model']) || strlen($_POST['model']) < 3) {
             $sub_mess['model'] = 'Не корректно заполнено поле Модель:';
         }
         if (!isset($_POST['cat_id'])) {
             $sub_mess['cat_id'] = 'Не корректно заполнено поле Класс:';
         }
         if (!isset($_POST['city']) || strlen($_POST['city']) < 3) {
             $sub_mess['city'] = 'Не корректно заполнено поле Город:';
         }
         if (!isset($_POST['price']) || strlen($_POST['price']) < 2) {
             $sub_mess['price'] = 'Не корректно заполнено поле Цена:';
         }
         if (!isset($_POST['text_offer']) || strlen($_POST['text_offer']) < 5) {
             $sub_mess['text_offer'] = 'Не корректно заполнено поле Характеристики техники: ';
         }
         if (is_array($sub_mess)) {
             echo json_encode($sub_mess);
             return false;
         } else {
             return true;
         }
     }
     if (valid_form_offer()) {
         //// Обработчик формы предложения
         $user_id = $this->session->get('user_id');
         $cat_id = $this->session->get('cat_id');
         $el = new Elements();
         $el->dir_valid($user_id);
         if ($this->request->hasPost('offer_id')) {
             $offer = Offers::findFirst($this->request->getPost('offer_id'));
         } else {
             $offer = new Offers();
         }
         $offer->name = $this->request->getPost('model');
         $md = new Model();
         $md->name = $this->request->getPost('model');
         $this->modelsCache->delete("md");
         $md->save();
         $offer->user_id = $user_id;
         if ($this->request->hasPost('sub_cat_id')) {
             $offer->category_id = $this->request->getPost('sub_cat_id');
         } else {
             if ($this->request->hasPost('cat_id')) {
                 $offer->category_id = $this->request->getPost('cat_id');
             }
         }
         $offer->creation_date = date("Y-m-d-H-i-s");
         $offer->text = $this->request->getPost('text_offer');
         if ($this->request->hasPost('public') && $this->request->getPost('public') == 'y') {
             $offer->status = 1;
         }
         $offer->save();
         if ($offer->save() == false) {
             foreach ($offer->getMessages() as $message) {
             }
         } else {
         }
         // Поля
         if ($this->request->hasPost('offer_id')) {
             foreach ($offer->dannoffers as $dann) {
                 $dann->delete();
             }
         }
         if ($this->request->hasPost('city')) {
             $dann = new DannOffers();
             $dann->dann = $this->request->getPost('city');
             $dann->field_type_id = 4;
             $dann->offers_id = $offer->id;
             $dann->active = 1;
             $dann->save();
         }
         if ($this->request->hasPost('price')) {
             $dann = new DannOffers();
             $dann->dann = $this->request->getPost('price');
             $dann->field_type_id = 5;
             $dann->offers_id = $offer->id;
             $dann->active = 1;
             $dann->save();
         }
         if ($this->request->hasPost('oper')) {
             $dann = new DannOffers();
             $dann->dann = $this->request->getPost('oper');
             $dann->field_type_id = 14;
             $dann->offers_id = $offer->id;
             $dann->active = 1;
             $dann->save();
         }
         if ($this->request->hasPost('gsm-act')) {
             $dann = new DannOffers();
             $dann->dann = $this->request->getPost('gsm-act');
             $dann->field_type_id = 7;
             $dann->offers_id = $offer->id;
             $dann->active = 1;
             $dann->save();
         }
         if ($this->request->hasPost('rad-dost')) {
             $dann = new DannOffers();
             $dann->dann = $this->request->getPost('rad-dost');
             $dann->field_type_id = 15;
             $dann->offers_id = $offer->id;
             $dann->active = 1;
             $dann->save();
         }
         if ($this->request->hasPost('dost-act')) {
             $dann = new DannOffers();
             $dann->dann = $this->request->getPost('dost-act');
             $dann->field_type_id = 6;
             $dann->offers_id = $offer->id;
             $dann->active = 1;
             $dann->save();
         }
         foreach ($_POST as $key => $fil) {
             if (stripos($key, 'fil_cat') !== false) {
                 $dann = new DannOffers();
                 $dann->dann = $this->request->getPost($key);
                 $dann->field_type_id = intval(str_replace('fil_cat-', '', $key));
                 $dann->offers_id = $offer->id;
                 $dann->active = 1;
                 $dann->save();
             }
         }
         /// Фото
         if ($this->request->hasFiles() == true) {
             $i = 0;
             // Выводим имя и размер файла
             foreach ($this->request->getUploadedFiles() as $file) {
                 $i++;
                 // Перемещаем в приложение
                 if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/public/upload/users/user-' . $user_id . '/offers/offer-' . $offer->id)) {
                 } else {
                     mkdir($_SERVER['DOCUMENT_ROOT'] . '/public/upload/users/user-' . $user_id . '/offers/offer-' . $offer->id);
                 }
                 $file->moveTo('upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/' . $file->getName());
                 $gd = new GdAdapter('upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/' . $file->getName());
                 $gd->resize(600, 430)->save('upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/image-big-' . $i . '.jpg');
                 $gd->resize(280, 201)->save('upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/image-medium-' . $i . '.jpg');
                 $gd->resize(120, 86)->save('upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/image-small-' . $i . '.jpg');
                 unlink('upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/' . $file->getName());
                 $image['image-big-' . $i] = 'upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/image-big-' . $i . '.jpg';
                 $image['image-medium-' . $i] = 'upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/image-medium-' . $i . '.jpg';
                 $image['image-small-' . $i] = 'upload/users/user-' . $user_id . '/offers/offer-' . $offer->id . '/image-small-' . $i . '.jpg';
             }
             $offer->image = serialize($image);
             $offer->save();
         }
         echo json_encode(array('success' => "Предложение " . $offer->name . " добавлено", 'offer_id' => $offer->id));
         ////Конец Обработчика формы предложения
     }
 }