host = $_SERVER['HTTP_ORIGIN']; $this->post = $post; $this->post['project_id'] = $this->project_model->getProjectId($this->host)->id; if(!$this->post['project_id']){ throw new \Exception("Project {$this->host} not found"); } else { print_r($this->post); } } private function addOneCustomer($template ){ $model = new \customersEmailList(); if($model->save($this->post)){ $this->MyMailer->OnlineEvent->SendForSelect($template, array($this->post)); }; } private function insertEventData(){ $data = $this->model->findFirst("name = '{$this->post['event']}' AND email_type = 'event_online' AND project_id = {$this->post['project_id']} "); if($data instanceof \eventEmail){ return $data; } else { throw new \Exception("EventData for event {$this->post['event']} in project {$this->host} not found"); } } private function insertItemData(){ if(isset($this->post['item_data']) && !empty($this->post['item_data'])){ return json_decode($this->post['item_data']); } else { return array(); } } private function prepareEventData($item_data, $template){ if($template->text_type=='static' ){ if( $item_data ){ $template->text = $this->MyMailer->OnlineEvent->itemSet( $template->toArray(), $item_data ); } $template->text = $this->MyMailer->OnlineEvent->dataSet( $template->text, $this->post ); print $template->text; } else { $template->text = $this->MyMailer->OnlineEvent->itemDynamicSet( $template->toArray(), $item_data ); unset($this->post['item_data']); $template->text = $this->MyMailer->OnlineEvent->dataSet( $template->text, $this->post ); print $template->text; } } /** * @param $data * @param $template * @throws \Exception */ private function sendDelivery($data, $template){ if($this->post['email']){ $customers = $this->customers_model->findFirst("email = '{$this->post['email']}'"); if(isset($this->post['event']) && !empty($this->post['event'])) { if(!$customers && $this->post['event'] == 'registration'){ $this->addOneCustomer($template); $customers = $this->customers_model->findFirst("email = '{$this->post['email']}'"); $this->event_info->event_id = $data->id; $this->event_info->customer_id = $customers->id; $this->event_info->project_id = $this->post['project_id']; $this->event_info->save(); $this->addLetersList($this->post['project_id']); } else { $this->event_info->event_id = $data->id; $this->event_info->customer_id = $customers->id; $this->event_info->project_id = $this->post['project_id']; if($data->unique) { $unique = $this->event_info->findFirst("event_id = {$data->id} AND customer_id = {$customers->id} AND project_id = {$this->post['project_id']}"); if($unique) { throw new \Exception(); } } $this->MyMailer->OnlineEvent->SendDelivery($this->delivery_model,$template->toArray(),$data->toArray(),array($customers->toArray())); $this->event_info->save(); } } else { throw new \Exception(); } } else { throw new \Exception("email missing"); } } private function getCustomer(){ $user = $this->customers_model->findFirst("email = '{$this->post['email']}' AND project_id = {$this->post['project_id']} "); if($user instanceof \customersEmailList){ return $user; } else { $user['email'] = $this->post['email']; $user['name'] = isset($this->post['name']) ? $this->post['name']:''; $user['project_id'] = $this->post['project_id']; $user['customer_id'] = isset($this->post['customer_id']) ? $this->post['customer_id']:''; $user['gender'] = isset($this->post['gender']) ? $this->post['gender']:''; $user['birthday'] = isset($this->post['birthday']) ? $this->post['birthday']:''; $this->customers_model->save($user); return $this->customers_model->findFirst("email = '{$this->post['email']}' AND project_id = {$this->post['project_id']} "); } } private function saveSpyData(){ if(isset($this->post['action'])){ $data['project_id'] = $this->post['project_id']; $data['customer_id'] = $this->customer->id; $data['action'] = $this->post['action']; $data['date'] = date("Y-m-d H:i:s"); $data['target_id'] = isset($this->post['item_id']) ? $this->post['item_id']:null; if($data['target_id']){ $this->spy_model = \spyEvent::findFirst("customer_id = '{$this->customer->id}' AND project_id = {$this->post['project_id']} AND target_id = {$data['target_id']}"); if( $this->spy_model instanceof \spyEvent){ $this->spy_model->save($data); }else{ $this->spy_model = new \spyEvent(); $this->spy_model->save( $data ); } }else{ $this->spy_model = new \spyEvent(); $this->spy_model->save( $data ); } return $this->spy_model->id; }else{ throw new \Exception("action missing"); } } private function getSpyData(){ if(isset($this->post['action'])){ $data['project_id'] = $this->post['project_id']; $data['customer_id'] = $this->customer->id; $data['target_id'] = isset($this->post['item_id']) ? $this->post['item_id']:null; $this->spy_model = \spyEvent::findFirst("customer_id = '{$this->customer->id}' AND project_id = {$this->post['project_id']} AND target_id = {$data['target_id']}"); if( $this->spy_model instanceof \spyEvent){ $id = $this->spy_model->id; $this->spy_model->delete(); return $id; }else{ throw new \Exception("spy event not found"); } }else{ throw new \Exception("action missing"); } } private function finishSpyOrder(){ if(isset($this->post['action'])){ $data['project_id'] = $this->post['project_id']; $data['customer_id'] = $this->customer->id; $data['action'] = $this->post['action']; $data['date'] = date("Y-m-d H:i:s"); $data['target_id'] = isset($this->post['item_id']) ? $this->post['item_id']:null; $this->spy_model = \spyEvent::query() ->where("project_id = :project_id:") ->andWhere("action ='order_add'") ->andWhere("customer_id =:customer_id:") ->bind(array("project_id" => $this->post['project_id'], "customer_id"=>$this->customer->id)) ->execute(); foreach($this->spy_model as $model){ $model->update(["action" =>'finish']); } $events_info = \eventInfo::query() ->where("project_id = :project_id:") ->andWhere("event_trigger ='order_time_after'") ->andWhere("customer_id =:customer_id:") ->bind(array("project_id" => $this->post['project_id'], "customer_id"=>$this->customer->id)) ->execute(); foreach($events_info as $event_info ){ $event_info->delete(); } }else{ throw new \Exception("action missing"); } } private function saveSpyOrder($spyID,$status){ if(isset($this->post['item_id'])){ $data['item_name'] = isset($this->post['item_name']) ? $this->post['item_name']:null; $data['item_id'] = $this->post['item_id']; $data['item_url'] = isset($this->post['item_url']) ? $this->post['item_url']:null; $data['item_image'] = isset($this->post['item_image']) ? $this->post['item_image']:null; $data['quantity'] = isset($this->post['quantity']) ? $this->post['quantity']:null; $data['price'] = isset($this->post['price']) ? $this->post['price']:null; $data['spy_event_id'] = $spyID; $data['status'] = $status; if($status == 'added'){ $model = \spyStore::findFirst("item_id='{$data['item_id']}' AND spy_event_id = {$spyID}"); if($model instanceof \spyStore){ $model->save( $data ); } else { $model = new \spyStore(); $model->save( $data ); } } else if($status == 'deleted'){ $model = \spyStore::findFirst("item_id='{$data['item_id']}' AND spy_event_id = {$spyID}"); if($model instanceof \spyStore){ $model->delete(); } else { throw new \Exception("there is no item with id {$data['item_id']}"); } } }else{ throw new \Exception("missing item id"); } } private function getEventDate($first_delivery,$time){ $now = date("Y-m-d"); $EmailDate = new \DateTime($first_delivery); $EmailDate->modify($time); return $EmailDate->format('Y-m-d'); } function getNextDate($day) { $ret = false; for ($i=1; $i<=7; $i++) { if (date('w',strtotime('+'.$i.' day'))==$day) { $ret = date('Y-m-d',strtotime('+'.$i.' day')); break; } } return $ret; } private function addLetersList($project_id){ $data = \seriesOfLetters::find("project_id ={$project_id} "); foreach($data as $row){ $letters = explode(',',$row->letters); $days = count($letters); for($i=0; $i<$days; $i++){ $now = date('N'); $row->first_delivery = $this->getNextDate($row->first_delivery); $date = $this->getEventDate($row->first_delivery, "+". ($i)*$row->period_val ." days"); $event = new \eventInfo(); $event->customer_id = $this->customers_model->id; $event->event_id = $letters[$i]; $event->next_delivery = $date; $event->project_id = $project_id; $event->event_trigger = 'series_of_letter'; $event->save(); } } } private function subscribeNewCustomer(){ $user['email'] = $this->post['email']; $user['name'] = isset($this->post['name']) ? $this->post['name']:''; $user['project_id'] = $this->post['project_id']; $customer = $this->customers_model->findFirst("email = '{$this->post['email']}'"); if($customer instanceof \customersEmailList){ } else { $this->customers_model->save($user); $this->addLetersList($this->post['project_id']); } } public function eventMailerAction() { header('Access-Control-Allow-Origin: *'); $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT); session_write_close(); try{ /* * $this->project_model = new \projects(); * $this->customers_model = new \customersEmailList(); */ $this->project_model = new \projects(); $this->customers_model = new \customersEmailList(); $this->insertPost($this->request->get()); if(isset($this->post['event_type'])){ switch ($this->post['event_type']) { case 'add_subscribe': $this->subscribeNewCustomer(); break; case 'online_event': $this->model = new \eventEmail(); $this->delivery_model = new \delivery(); $this->temp_model = new \emailTemplates(); $this->event_info = new \eventInfo(); $data = $this->insertEventData(); $template = $data->emailTemplates; $item_data = $this->insertItemData(); $this->MyMailer->OnlineEvent->setFromName($this->project_model->getProjectId($this->host)->name); $this->prepareEventData($item_data, $template); $template->text = $this->UTMParser->parse($data->toArray(), $template->toArray()); $this->sendDelivery( $data, $template); break; case 'spy_event': $this->customer = $this->getCustomer(); switch ($this->post['action']) { case 'order_add': $spyID = $this->saveSpyData(); $this->saveSpyOrder($spyID, 'added'); break; case 'order_delete': $spyID = $this->getSpyData(); $this->saveSpyOrder($spyID, 'deleted'); break; case 'order_finish': $spyID = $this->saveSpyData(); $this->finishSpyOrder($spyID, 'finish'); break; default: throw new \Exception("Unknown event action {$this->post['action']}"); } break; default: throw new \Exception("Unknown event type"); } } else { throw new \Exception("Unknown event type"); } } catch(\Exception $e){ die($e); } } }