示例#1
0
 public function orderAction()
 {
     // Check incoming data
     $name = Text::xssClean(Arr::get($this->post, 'name'));
     if (!$name) {
         $this->error('Вы не указали имя!');
     }
     $email = Text::xssClean(Arr::get($this->post, 'email'));
     if (!$email or !filter_var($email, FILTER_VALIDATE_EMAIL)) {
         $this->error('Вы указали неверный e-mail!');
     }
     $phone = Text::xssClean(Arr::get($this->post, 'phone'));
     if (!$phone or !preg_match('/\\(\\d{3}\\)\\s\\d{3}-\\d{2}-\\d{2}/', $phone, $matches)) {
         $this->error('Вы указали неверный телефон!');
     }
     $places = Text::xssClean(Arr::get($this->post, 'seats'));
     $places = array_filter(explode(',', $places));
     if (!$places or !is_array($places)) {
         $this->error('Вы не выбрали места!');
     }
     $message = nl2br(Text::xssClean(Arr::get($this->post, 'message', null)));
     $afishaId = (int) Text::xssClean(Arr::get($this->post, 'id'));
     // Get prices by afisha ID
     $prices = DB::select('id')->from('prices')->where('afisha_id', '=', $afishaId)->find_all();
     if (count($prices) == 0) {
         $this->error('Ошибка создания заказа (выборка цен)');
     }
     $pricesIds = array();
     foreach ($prices as $price) {
         $pricesIds[] = $price->id;
     }
     // Generate seats id from places list
     $seats = DB::select('id')->from('seats')->where('view_key', 'IN', $places)->where('price_id', 'IN', $pricesIds)->and_where_open()->where('status', '=', 1)->or_where_open()->where('status', '=', 2)->where('reserved_at', '<', time() - 60 * 60 * 24 * conf::get('reserved_days'))->or_where_close()->and_where_close()->find_all();
     if (count($seats) == 0) {
         $this->error('Ошибка создания заказа (выборка мест)');
     }
     $seatsId = array();
     foreach ($seats as $seat) {
         $seatsId[] = $seat->id;
     }
     $data = array('afisha_id' => $afishaId, 'name' => $name, 'email' => $email, 'phone' => $phone, 'message' => $message, 'seats_keys' => implode(',', $places), 'created_at' => time(), 'first_created_at' => time(), 'updated_at' => time(), 'ip' => System::getRealIP());
     $res = \Core\Common::insert('afisha_orders', $data)->execute();
     if (!$res) {
         $this->error('ошибка создания заказа');
     }
     // Update status
     $res2 = DB::update('seats')->set(array('status' => 2, 'reserved_at' => time()))->where('id', 'IN', $seatsId)->execute();
     $afisha = DB::select()->from('afisha')->where('id', '=', $afishaId)->find();
     $data['event_name'] = $afisha->name;
     // Send email messages for adimn and user
     Afisha\Models\Afisha::sendOrderMessageAdmin(array('id_order' => $res[0], 'order' => $data, 'order_text' => Arr::get($this->post, 'order')));
     Afisha\Models\Afisha::sendOrderMessageUser(array('id_order' => $res[0], 'order' => $data, 'order_text' => Arr::get($this->post, 'order')));
     // Save log
     $qName = 'Новый заказ';
     $url = '/backend/afisha_orders/edit/' . $res[0];
     Log::add($qName, $url, 8);
     $response = array();
     // Redirect to payment system
     if (Arr::get($this->post, 'action') == 'payment') {
         $response['redirect'] = \Core\HTML::link('payment/' . $res[0]);
     } else {
         $response['reload'] = true;
     }
     $response['response'] = 'Ваш заказ отправлен';
     return $this->success($response);
 }