public function editAction()
 {
     $id = $this->dispatcher->getParam('id');
     $zone = Zones::findFirst($id);
     if ($zone && $id) {
         if ($this->request->isPost()) {
             if ($zone->save($this->request->getPost(), array('name'))) {
                 $this->flashSession->success("Зона отредактирована");
                 return $this->response->redirect(array('for' => 'controller', 'controller' => 'zones'));
             } else {
                 foreach ($zone->getMessages() as $message) {
                     $this->flashSession->error($message->getMessage());
                 }
             }
         }
         $this->view->zone = $zone;
         $this->view->title = "Редактирование зоны";
         \Phalcon\Tag::prependTitle("Редактирование зоны");
     } else {
         $this->dispatcher->forward(array("namespace" => 'App\\Controllers', "controller" => "error", "action" => "notFound"));
     }
 }
 public function editAction()
 {
     $this->assets->collection('bottom-js')->addJs('js/moment/moment.min.js')->addJs('js/moment/ru.js')->addJs('js/datetimepicker/js/bootstrap-datetimepicker.js');
     $this->assets->collection('css')->addCss('js/datetimepicker/css/bootstrap-datetimepicker.min.css');
     $id = $this->dispatcher->getParam('id');
     $banner = Banners::findFirst($id);
     if ($id && $banner) {
         if ($this->request->isPost()) {
             $old_banner_type = $banner->type;
             $old_banner_content = $banner->content;
             $data = $this->request->getPost();
             $data['target_blank'] = (bool) $this->request->get('target_blank') ? '1' : '0';
             if ($this->request->getPost('start_date')) {
                 $start_date = date_parse_from_format('d.m.Y H:i', $data['start_date']);
                 $data['start_date'] = mktime($start_date['hour'], $start_date['minute'], 0, $start_date['month'], $start_date['day'], $start_date['year']);
             }
             if ($this->request->getPost('end_date')) {
                 $end_date = date_parse_from_format('d.m.Y H:i', $data['end_date']);
                 $data['end_date'] = mktime($end_date['hour'], $end_date['minute'], 0, $end_date['month'], $end_date['day'], $end_date['year']);
             }
             if (($data['type'] == "image" || $data['type'] == "flash") && $data['source'] == "local") {
                 $data['content'] = Banners::findFirst(array('id = :id:', 'bind' => array('id' => $data['content'])))->content;
             }
             $this->db->begin();
             if ($banner->save($data, array('name', 'width', 'height', 'link', 'target_blank', 'priority', 'type', 'content', 'max_impressions', 'start_date', 'end_date', 'url_mask', 'advertiser_id')) == false) {
                 $this->db->rollback();
                 foreach ($banner->getMessages() as $message) {
                     $this->flashSession->error($message->getMessage());
                 }
             } else {
                 if (($this->request->getPost('type') == "image" || $this->request->getPost('type') == "flash") && $this->request->getPost('source') == "file") {
                     if ($this->request->hasFiles(true)) {
                         $file = $this->request->getUploadedFiles()[0];
                         $validation = new \Phalcon\Validation();
                         $validation->add('file', new UploadValid());
                         if ($this->request->getPost('type') == "image") {
                             $validation->add('file', new UploadType(array('allowed' => array('jpg', 'jpeg', 'png', 'gif'))));
                             $validation->add('file', new UploadImage());
                         } elseif ($this->request->getPost('type') == "flash") {
                             $validation->add('file', new UploadType(array('allowed' => array('swf'))));
                         }
                         $messages = $validation->validate($_FILES);
                         if (count($messages)) {
                             $this->db->rollback();
                             foreach ($validation->getMessages() as $message) {
                                 $this->flashSession->error($message->getMessage());
                             }
                         } else {
                             $extension = pathinfo($file->getName(), PATHINFO_EXTENSION);
                             $name = $banner->id . '_' . time() . '.' . $extension;
                             $file->moveTo(($this->request->getPost('type') == "image" ? $this->config->banners->imagePath : $this->config->banners->flashPath) . $name);
                             if ($banner->save(array('content' => $name)) == false) {
                                 $this->db->rollback();
                                 foreach ($banner->getMessages() as $message) {
                                     $this->flashSession->error($message->getMessage());
                                 }
                             } else {
                                 if (($old_banner_type == "image" || $old_banner_type == "flash") && !empty($old_banner_content)) {
                                     unlink(($old_banner_type == "image" ? $this->config->banners->imagePath : $this->config->banners->flashPath) . $old_banner_content);
                                 }
                                 $this->db->commit();
                             }
                         }
                     } else {
                         $this->db->rollback();
                         $this->flashSession->error("Необходимо указать файл");
                     }
                 } else {
                     $this->db->commit();
                 }
                 BannersZones::find(array("banner_id=:banner:", 'bind' => array('banner' => $banner->id)))->delete();
                 if ($this->request->getPost('zones')) {
                     foreach ($this->request->getPost('zones') as $zone) {
                         $zone = Zones::findFirst($zone);
                         if ($zone) {
                             $m = new BannersZones();
                             $m->banner_id = $banner->id;
                             $m->zone_id = $zone->id;
                             $m->create();
                         }
                     }
                 }
                 return $this->response->redirect(array('for' => 'controller', 'controller' => 'banners'));
             }
         }
         $this->view->checked_zones = $this->request->getPost('zones') ? $this->request->getPost('zones') : array_column($banner->getZones(array('columns' => array('id')))->toArray(), 'id');
         $this->view->banner = $banner;
         $this->view->pick("banners/edit");
         $this->view->title = "Редактирование баннера";
         \Phalcon\Tag::prependTitle("Редактирование баннера");
     } else {
         $this->dispatcher->forward(array("namespace" => 'App\\Controllers', "controller" => "error", "action" => "notFound"));
     }
 }