Preordernotification: Can attach Borderpoint image from Mapbox API now

This commit is contained in:
Frank Schubert
2024-11-28 17:00:22 +01:00
parent 2cf306c5c3
commit d8c826a261
10 changed files with 411 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/header.php"); ?>
<?php //var_dump($notification->filter);exit;?>
<!-- start page title -->
<div class="row">
@@ -94,6 +95,18 @@
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="status_id">Bestellstatus</label>
<div class="col-lg-10">
<select class="form-control" name="status_id[]" id="status_id" multiple="multiple">
<?php foreach(PreorderstatusModel::getAll() as $status): ?>
<option value="<?=$status->id?>" <?=($notification && array_key_exists("status_id", $notification->filter) && is_array($notification->filter["status_id"]) && in_array($status->id, $notification->filter["status_id"])) ? "selected='selected'" : ""?>><?=$status->code?> - <?=$status->name?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<!--div class="form-group row">
<label class="col-lg-2 col-form-label" for="product_id">Vertriebspartner</label>
@@ -130,6 +143,16 @@
</div>
</div>
<h4 class="mt-4">Übergabepunkt Bilddatei</h4>
<div class="form-group row">
<div class="col-lg-10">
<select class="form-control" name="attach_borderpoint" id="attach_borderpoint">
<option value="0" <?=(!$notification->attach_borderpoint) ? "selected='selected'" : ""?>>Nicht anhängen</option>
<option value="1" <?=($notification->attach_borderpoint) ? "selected='selected'" : ""?>>Anhängen</option>
</select>
</div>
</div>
<?php if(is_array($notification->files) && count($notification->files)): ?>
<div class="row">
<div class="col-4">
@@ -349,6 +372,11 @@
allowClear: true,
placeholder: ""
});
$("#status_id").select2({
allowClear: true,
placeholder: "",
closeOnSelect: false
});
});
function deleteFile(id) {

View File

@@ -847,6 +847,86 @@ class Preorder extends mfBaseModel {
return $a;
}
public function getBorderpointImageFile() {
if(!$this->adb_hausnummer_id) return false;
$border_lat = $this->getProperty("adb_hausnummer")->borderpoint_lat;
$border_long = $this->getProperty("adb_hausnummer")->borderpoint_long;
if(!$border_lat || !$border_long ) {
return false;
}
$filename = "borderpoint_map_h".$this->adb_hausnummer_id."_{$border_lat}_{$border_long}";
$bpi_file = PreorderFile::getFirst(["preorder_id" => $this->id, "filename" => $filename]);
if($bpi_file) {
return $bpi_file;
}
// get new Borderpoint Image from Mapbox API
$params = [
"gps_lat" => $border_lat,
"gps_long" => $border_long,
"zoom" => 19,
"size_x" => 640,
"size_y" => 640,
"style" => "satellite-streets-v12",
"pin" => [
"gps_lat" => $border_lat,
"gps_long" => $border_long,
"size" => "l",
"color" => "ee9900",
"icon" => "embassy"
],
"access_token" => TT_MAPBOX_TILE_API_TOKEN
];
$image_content = Mapbox_StaticImageApi::getImageFileContent($params);
if(!$image_content) {
return false;
}
$fs_filename = "$filename.jpg";
if(!file_put_contents(MFUPLOAD_FILE_SAVE_PATH."/preorder-borderpoint/$fs_filename", $image_content)) {
$this->log->error(__METHOD__.": Error saving Borderpoint Static Map Image File");
return false;
}
$file = FileModel::create([
"name" => "borderpoint_map",
"description" => $this->adb_hausnummer_id,
"filename" => "$filename.jpg",
"orig_filename" => "$filename.jpg",
"store_filename" => $fs_filename,
"subfolder" => "preorder-borderpoint",
]);
if(!$file->save()) {
$this->log->error(__METHOD__.": Error saving File Object");
return false;
}
$file->mimetype = $file->getMimetype();
$file->save();
$pf = PreorderFile::create([
"preorder_id" => $this->id,
"file_id" => $file->id,
"filename" => $filename
]);
if(!$pf->save()) {
$this->log->error(__METHOD__.": Error saving PreorderFile Object");
return false;
}
return $pf;
}
public function getApiArray($options = []) {
if(!$this->id) {
return false;

View File

@@ -506,6 +506,8 @@ class PreorderModel {
$status_code = $filter['status_code'];
if(is_numeric($status_code)) {
$where .= " AND tt_preorderstatus.code=$status_code";
} elseif(is_array($status_code)) {
$where .= " AND tt_preorderstatus.code IN (".implode(",", $status_code).")";
}
}

View File

@@ -0,0 +1,178 @@
<?php
class PreorderFile extends mfBaseModel {
private $file;
private $preorder;
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name . "_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-" . $this->$idfield);
if($this->$name === null) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-" . $this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new PreorderFile();
$table_fields = [
"preorder_id", "file_id", "filename",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("PreorderFile", "*", "1 = 1 ORDER BY preorder_id,file_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderFile($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("PreorderFile", "*", "$where ORDER BY preorder_id,file_id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderFile($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM `PreorderFile`
WHERE $where
";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit=false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT PreorderFile.* FROM `PreorderFile`
WHERE $where
ORDER BY preorder_id,file_id
";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderFile($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("id", $filter)) {
$id = $db->escape($filter['id']);
if($id) {
$where .= " AND PreorderFile.`id` = '$id'";
}
}
if(array_key_exists("preorder_id", $filter)) {
$preorder_id = $filter['preorder_id'];
if(is_numeric($preorder_id)) {
$where .= " AND preorder_id=$preorder_id";
} elseif(is_array($preorder_id) && count($preorder_id)) {
$where .= " AND PreorderFile.preorder_id IN (". implode(",", $preorder_id).")";
}
}
if(array_key_exists("file_id", $filter)) {
$file_id = $filter['file_id'];
if(is_numeric($file_id)) {
$where .= " AND PreorderFile.file_id=$file_id";
} elseif(is_array($file_id) && count($file_id)) {
$where .= " AND PreorderFile.file_id IN (". implode(",", $file_id).")";
}
}
if(array_key_exists("filename", $filter)) {
$filename = FronkDB::singleton()->escape($filter['filename']);
if($filename) {
$where .= " AND filename LIKE '$filename'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -32,6 +32,15 @@ class Preordernotification extends mfBaseModel {
$reply_to = $this->sender_replyto;
}
$borderpoint_file = false;
if($this->attach_borderpoint) {
$borderpoint_file = $preorder->getBorderpointImageFile();
if(!$borderpoint_file) return false;
if(!file_exists($borderpoint_file->file->getFullPath())) return false;
}
$to = $preorder->email;
if($email_to) {
@@ -88,6 +97,9 @@ class Preordernotification extends mfBaseModel {
$email->addAttachment($file["path"], null, $file["filename"], $file['mimetype']);
}
}
if($this->attach_borderpoint && $borderpoint_file) {
$email->addAttachment($borderpoint_file->file->getFullPath(), null, $borderpoint_file->file->filename, $borderpoint_file->file->mimetype);
}
$email->send();
$this->log->info(__CLASS__."::sendToPreorder(): Sending Preordernotification for Preorder id ".$preorder->id);

View File

@@ -293,6 +293,15 @@ class PreordernotificationController extends mfBaseController {
}
}
if(is_array($r->status_id) && count($r->status_id)) {
$filter["status_id"] = [];
foreach($r->status_id as $status_id) {
$test_status = new Preorderstatus($status_id);
if(!$test_status || !$test_status->id) continue;
$filter["status_id"][] = $status_id;
}
}
//var_dump($filter);
$data = [];
$data['preordercampaign_id'] = $campaign_id;
@@ -301,6 +310,7 @@ class PreordernotificationController extends mfBaseController {
$data['sender_replyto'] = $r->sender_replyto;
$data['subject'] = $r->subject;
$data['body_html'] = $r->body_html;
$data["attach_borderpoint"] = $r->attach_borderpoint;
$data['note'] = $r->note;
if($r->tosend_day) {

View File

@@ -48,7 +48,7 @@ class PreordernotificationFileModel {
}
public static function getFirst() {
public static function getFirst($filter = []) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
@@ -97,7 +97,7 @@ class PreordernotificationFileModel {
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class PreordernotificationAddAttachBorderpointAndCreatePreorderFile extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("Preordernotification");
$table->addColumn("attach_borderpoint", "integer", ["null" => false, "default" => 0, "limit" => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY, "after" => "preordercampaign_id"]);
$table->update();
$pf = $this->table("PreorderFile");
$pf->addColumn("preorder_id", "integer", ["null" => false]);
$pf->addColumn("file_id", "integer", ["null" => false]);
$pf->addColumn("filename", "string", ["null" => false]);
$pf->addColumn("create_by", "integer", ["null" => false]);
$pf->addColumn("edit_by", "integer", ["null" => false]);
$pf->addColumn("create", "integer", ["null" => false]);
$pf->addColumn("edit", "integer", ["null" => false]);
$pf->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("PreorderFile")->drop()->save();
$table = $this->table("Preordernotification");
$table->removeColumn("attach_borderpoint");
$table->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
class Mapbox_StaticImageApi {
public static function getImageFileContent(Array $params) : ?string {
$access_token = $params["access_token"];
$gps_lat = $params["gps_lat"];
$gps_long = $params["gps_long"];
$zoom = $params["zoom"];
$size_x = $params["size_x"];
$size_y = $params["size_y"];
$style = $params["style"];
$pin = $params["pin"];
$pin_part = "";
// https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v12/static/pin-l-embassy+ee9900(15.4200370610,46.9599897293)/15.4200370610,46.9599897293,19/640x640?access_token=pk.eyJ1Ijoic2tuZXR3b3J4IiwiYSI6ImNqdWw3eXoyMzBieWU0M284OTU2eWNxMTMifQ.GrgBYKMXVt3TiwU53OBllQ
$url = "https://api.mapbox.com/styles/v1/mapbox/$style/static";
if(is_array($pin) && $pin["gps_lat"] && $pin["gps_long"] && $pin["size"]) {
$pin_gps_lat = $pin["gps_lat"];
$pin_gps_long = $pin["gps_long"];
$pin_size = $pin["size"];
$pin_color = $pin["color"];
$pin_icon = $pin["icon"];
$pin_part .= "/pin-$pin_size";
if($pin_icon) $pin_part .= "-$pin_icon";
$pin_part .= "+$pin_color($pin_gps_long,$pin_gps_lat)";
}
$url .= "$pin_part/$gps_long,$gps_lat,$zoom/{$size_x}x{$size_y}?access_token=$access_token";
$ctx_opts = [
'http' => [
'method' => 'GET',
'header' => 'accept: application/json'
]
];
$ctx = stream_context_create($ctx_opts);
$response = file_get_contents($url, false, $ctx);
if($response === false) {
return null;
}
return $response;
}
}

View File

@@ -30,6 +30,8 @@ foreach(PreordernotificationModel::search(["send_finish" => null, "send_lock" =>
$notification->send_lock = ($mypid) ? $mypid : 1;
$notification->save();
$preorders = $notification->getPreorders();
foreach($notification->getPreorders() as $preorder) {
$notification->sendToPreorder($preorder);
}