Saving Preordernotification and test email done
This commit is contained in:
@@ -71,12 +71,12 @@ class PreordernotificationController extends mfBaseController {
|
||||
$filter['preordercampaign_id'] = $my_campaign_ids;
|
||||
}
|
||||
}
|
||||
|
||||
//var_dump($filter);exit;
|
||||
$pagination['maxItems'] = PreordernotificationModel::count($filter);
|
||||
$notifications = PreordernotificationModel::search($filter, $pagination);
|
||||
|
||||
$this->layout()->set("pagination", $pagination);
|
||||
$this->layout()->set("preorders", $notifications);
|
||||
$this->layout()->set("notifications", $notifications);
|
||||
|
||||
|
||||
}
|
||||
@@ -111,22 +111,271 @@ class PreordernotificationController extends mfBaseController {
|
||||
protected function editAction() {
|
||||
$id = $this->request->id;
|
||||
if(!is_numeric($id) || $id < 1) {
|
||||
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
|
||||
$this->redirect("Preordercampaign");
|
||||
$this->layout()->setFlash("Aussendung nicht gefunden", "error");
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
|
||||
$preorder = new Preorder($id);
|
||||
if(!$preorder->id) {
|
||||
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
|
||||
$this->redirect("Preordercampaign");
|
||||
$notification = new Preordernotification($id);
|
||||
if(!$notification->id) {
|
||||
$this->layout()->setFlash("Aussendung nicht gefunden", "error");
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
|
||||
$this->request->set("preordercampaign_id", $preorder->preordercampaign_id); // needed in addAction()
|
||||
$this->layout()->set("preorder", $preorder);
|
||||
//var_dump($notification->filter);exit;
|
||||
|
||||
//var_dump($preorder->building->street);exit;
|
||||
$this->request->set("preordercampaign_id", $notification->preordercampaign_id); // needed in addAction()
|
||||
$this->layout()->set("notification", $notification);
|
||||
|
||||
return $this->addAction();
|
||||
|
||||
}
|
||||
|
||||
protected function saveAction() {
|
||||
$r = $this->request;
|
||||
//var_dump($r->get(), $_FILES);exit;
|
||||
|
||||
/*
|
||||
* add or edit
|
||||
*/
|
||||
$id = $r->id;
|
||||
if(is_numeric($id) && $id > 0) {
|
||||
$mode = "edit";
|
||||
$notification = new Preordernotification($id);
|
||||
if(!$notification->id) {
|
||||
$this->layout()->setFlash("Aussendung nicht gefunden", "error");
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
} else {
|
||||
$id = false;
|
||||
$mode = "add";
|
||||
}
|
||||
|
||||
/*
|
||||
* check campaign and permissions
|
||||
*/
|
||||
|
||||
$campaign_id = $r->preordercampaign_id;
|
||||
if(!$campaign_id) {
|
||||
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
$campaign = new Preordercampaign($campaign_id);
|
||||
if(!$campaign->id) {
|
||||
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
|
||||
// check permission
|
||||
if(!$this->me->is("Admin")) {
|
||||
$my_campaign_ids = [];
|
||||
$my_networks = $this->me->myNetworks(["netowner", "salespartner"]);
|
||||
//var_dump($my_networks);exit;
|
||||
|
||||
foreach($my_networks as $network) {
|
||||
foreach(PreordercampaignModel::search(['network_id' => $network->id]) as $campaign) {
|
||||
if(!in_array($campaign->id, $my_campaign_ids)) $my_campaign_ids[] = $campaign->id;
|
||||
}
|
||||
}
|
||||
|
||||
if(!in_array($campaign_id, $my_campaign_ids)) {
|
||||
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* data colletion
|
||||
*/
|
||||
$filter = [];
|
||||
if(is_array($r->type) && count($r->type)) {
|
||||
$filter['type'] = [];
|
||||
foreach($r->type as $type) {
|
||||
switch($type) {
|
||||
case "interest":
|
||||
$filter["type"][] = "interest";
|
||||
break;
|
||||
case "provision":
|
||||
$filter["type"][] = "provision";
|
||||
break;
|
||||
case "order":
|
||||
$filter["type"][] = "order";
|
||||
break;
|
||||
case "reorder":
|
||||
$filter["type"][] = "reorder";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(is_array($r->connection_type) && count($r->connection_type)) {
|
||||
$filter['connection_type'] = [];
|
||||
foreach($r->connection_type as $type) {
|
||||
switch($type) {
|
||||
case "single-dwelling":
|
||||
$filter["connection_type"][] = "single-dwelling";
|
||||
break;
|
||||
case "multi-dwelling":
|
||||
$filter["connection_type"][] = "multi-dwelling";
|
||||
break;
|
||||
case "apartment-building":
|
||||
$filter["connection_type"][] = "apartment-building";
|
||||
break;
|
||||
case "apartment":
|
||||
$filter["connection_type"][] = "apartment";
|
||||
break;
|
||||
case "business":
|
||||
$filter["connection_type"][] = "business";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//var_dump($filter);
|
||||
$data = [];
|
||||
$data['preordercampaign_id'] = $campaign_id;
|
||||
$data['subject'] = $r->subject;
|
||||
$data['body_html'] = $r->body_html;
|
||||
$data['note'] = $r->note;
|
||||
|
||||
if($r->tosend_day) {
|
||||
$tosend_day = $r->tosend_day;
|
||||
$tosend_hour = $r->tosend_hour;
|
||||
if($tosend_hour < 0 || $tosend_hour > 23) {
|
||||
$tosend_hour = 0;
|
||||
}
|
||||
$data['tosend_date'] = self::dateToTimestamp($tosend_day." ".str_pad($tosend_hour, 2, "0", STR_PAD_LEFT).":00:00");
|
||||
}
|
||||
|
||||
$data['preorder_filter'] = json_encode($filter);
|
||||
|
||||
//var_dump($data);exit;
|
||||
|
||||
if($mode == "edit") {
|
||||
$notification->update($data);
|
||||
} else {
|
||||
$notification = PreordernotificationModel::create($data);
|
||||
}
|
||||
|
||||
$new_id = $notification->save();
|
||||
if(!$new_id) {
|
||||
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
||||
if($id) {
|
||||
$this->redirect("Preordernotification", "edit", ['id' => $id]);
|
||||
} else {
|
||||
$this->redirect("Preordernotification");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Attachment upload
|
||||
*/
|
||||
|
||||
if(array_key_exists("attachment", $_FILES)) {
|
||||
$files = $_FILES['attachment'];
|
||||
if(is_array($files) && count($files)) {
|
||||
$file_errors = 0;
|
||||
foreach($files['name'] as $i => $name) {
|
||||
if(!$name) continue;
|
||||
$upload_error = false;
|
||||
try {
|
||||
$upload = new mfUpload(['attachment', $i]);
|
||||
$upload->setSavepath(MFUPLOAD_FILE_SAVE_PATH . "/" . TT_PREORDERNOTIFICATION_FILE_UPLOAD_SUBFOLDER);
|
||||
} catch(Exception $e) {
|
||||
$this->layout()->setFlash("Dateiupload fehlgeschlagen: ".$e->getMessage(), "warn");
|
||||
$file_errors++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!$upload->getSize()) {
|
||||
$this->layout()->setFlash("Dateiupload fehlgeschlagen: Datei ist leer!", "warn");
|
||||
$upload_error = true;
|
||||
}
|
||||
|
||||
$mime = "";
|
||||
|
||||
if(!$upload_error) {
|
||||
try {
|
||||
$mime = $upload->getMimetype();
|
||||
$upload->save();
|
||||
} catch(Exception $e) {
|
||||
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
|
||||
$upload_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
if($upload_error) {
|
||||
$file_errors++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$file_data = [];
|
||||
$file_data['name'] = $upload->getOriginalFilename();
|
||||
$file_data['filename'] = $upload->getOriginalFilename();
|
||||
$file_data['subfolder'] = TT_PREORDERNOTIFICATION_FILE_UPLOAD_SUBFOLDER;
|
||||
$file_data['store_filename'] = $upload->getFilename();
|
||||
$file_data['orig_filename'] = $upload->getOriginalFilename();
|
||||
$file_data['mimetype'] = $mime;
|
||||
|
||||
$file = FileModel::create($file_data);
|
||||
$file_id = $file->save();
|
||||
if(!$file_id) {
|
||||
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
|
||||
unlink($upload->getSavepath()."/".$upload->getFilename());
|
||||
} else {
|
||||
$pnf = [];
|
||||
$pnf['preordernotification_id'] = $notification->id;
|
||||
$pnf['file_id'] = $file_id;
|
||||
$pnf['filename'] = $file->filename;
|
||||
|
||||
$notification_file = PreordernotificationFileModel::create($pnf);
|
||||
if(!$notification_file->save()) {
|
||||
$file->delete();
|
||||
unlink($upload->getSavepath()."/".$upload->getFilename());
|
||||
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* delete files
|
||||
*/
|
||||
|
||||
if(is_array($r->deletefile)) {
|
||||
foreach($r->deletefile as $pnf_id) {
|
||||
$pnf = new PreordernotificationFile($pnf_id);
|
||||
if($pnf->preordernotification_id != $notification->id) continue;
|
||||
$pnf->file->delete();
|
||||
$pnf->delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// get and save preorder count
|
||||
$preorder_filter = json_decode($notification->preorder_filter, true);
|
||||
$preorder_filter['preordercampaign_id'] = $notification->preordercampaign_id;
|
||||
$preorder_count = PreorderModel::count($preorder_filter);
|
||||
$notification->recipient_count = $preorder_count;
|
||||
$notification->save();
|
||||
|
||||
if($r->send_testmail && $r->testmail_to) {
|
||||
$preorder = PreorderModel::getFirst($preorder_filter);
|
||||
if(!$preorder) {
|
||||
$this->layout()->setFlash("Testmail konnte nicht versendet werden. Keine Empfänger gefunden", "warn");
|
||||
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
|
||||
}
|
||||
$notification->sendToPreorder($preorder, $r->testmail_to);
|
||||
$this->layout()->setFlash("Testmail versendet", "success");
|
||||
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("Erfolgreich gepeichert.", "success");
|
||||
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user