Page 785 Customer Picture, source in 29
Source29
src/Layers/W1/BaseApp/Sales/Customer/CustomerPicture.Page.al191 lines, Copyright (c) Microsoft Corporation. MIT
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace Microsoft.Sales.Customer;
using Microsoft.Integration.Entity;
using System.Device;
using System.IO;
/// <summary>
/// Manages the customer picture with options to take, import, export, and delete images.
/// </summary>
page 785 "Customer Picture"
{
Caption = 'Customer Picture';
DeleteAllowed = false;
InsertAllowed = false;
LinksAllowed = false;
PageType = CardPart;
SourceTable = Customer;
layout
{
area(content)
{
field(Image; Rec.Image)
{
ApplicationArea = All;
ShowCaption = false;
}
}
}
actions
{
area(processing)
{
action(TakePicture)
{
ApplicationArea = All;
Caption = 'Take';
Image = Camera;
ToolTip = 'Activate the camera on the device.';
Visible = CameraAvailable;
trigger OnAction()
begin
TakeNewPicture();
end;
}
action(ImportPicture)
{
ApplicationArea = All;
Caption = 'Import';
Image = Import;
ToolTip = 'Import a picture file.';
trigger OnAction()
var
FileManagement: Codeunit "File Management";
FileName: Text;
ClientFileName: Text;
begin
Rec.TestField("No.");
if Rec.Name = '' then
Error(MustSpecifyNameErr);
if Rec.Image.HasValue() then
if not Confirm(OverrideImageQst) then
exit;
FileName := FileManagement.UploadFile(SelectPictureTxt, ClientFileName);
if FileName = '' then
exit;
Clear(Rec.Image);
Rec.Image.ImportFile(FileName, ClientFileName);
Rec.SetForceUpdateContact(true);
if not Rec.Modify(true) then
Rec.Insert(true);
if FileManagement.DeleteServerFile(FileName) then;
end;
}
action(ExportFile)
{
ApplicationArea = All;
Caption = 'Export';
Enabled = DeleteExportEnabled;
Image = Export;
ToolTip = 'Export the picture to a file.';
trigger OnAction()
var
DummyPictureEntity: Record "Picture Entity";
FileManagement: Codeunit "File Management";
ToFile: Text;
ExportPath: Text;
begin
Rec.TestField("No.");
Rec.TestField(Name);
ToFile := DummyPictureEntity.GetDefaultMediaDescription(Rec);
ExportPath := TemporaryPath + Rec."No." + Format(Rec.Image.MediaId);
Rec.Image.ExportFile(ExportPath);
FileManagement.ExportImage(ExportPath, ToFile);
end;
}
action(DeletePicture)
{
ApplicationArea = All;
Caption = 'Delete';
Enabled = DeleteExportEnabled;
Image = Delete;
ToolTip = 'Delete the record.';
trigger OnAction()
begin
Rec.TestField("No.");
if not Confirm(DeleteImageQst) then
exit;
Clear(Rec.Image);
Rec.Modify(true);
end;
}
}
}
trigger OnAfterGetCurrRecord()
begin
SetEditableOnPictureActions();
end;
trigger OnOpenPage()
begin
CameraAvailable := Camera.IsAvailable();
end;
var
Camera: Codeunit Camera;
CameraAvailable: Boolean;
OverrideImageQst: Label 'The existing picture will be replaced. Do you want to continue?';
DeleteImageQst: Label 'Are you sure you want to delete the picture?';
SelectPictureTxt: Label 'Select a picture to upload';
DeleteExportEnabled: Boolean;
MustSpecifyNameErr: Label 'You must specify a customer name before you can import a picture.';
MimeTypeTok: Label 'image/jpeg', Locked = true;
/// <summary>
/// Captures a new picture using the device camera and saves it to the customer record.
/// </summary>
procedure TakeNewPicture()
var
PictureInstream: InStream;
PictureDescription: Text;
begin
Rec.Find();
Rec.TestField("No.");
Rec.TestField(Name);
if Rec.Image.HasValue() then
if not Confirm(OverrideImageQst) then
exit;
if Camera.GetPicture(PictureInstream, PictureDescription) then begin
Clear(Rec.Image);
Rec.Image.ImportStream(PictureInstream, PictureDescription, MimeTypeTok);
Rec.Modify(true)
end;
end;
local procedure SetEditableOnPictureActions()
begin
DeleteExportEnabled := Rec.Image.HasValue;
end;
/// <summary>
/// Checks whether a camera is available on the device.
/// </summary>
/// <returns>True if camera is available, otherwise false.</returns>
procedure IsCameraAvailable(): Boolean
begin
exit(Camera.IsAvailable());
end;
}