DocSpace-client/products/ASC.CRM/Server/Classes/Global.cs

273 lines
10 KiB
C#
Raw Normal View History

2020-03-02 15:38:31 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
2020-04-14 18:59:32 +00:00
using ASC.Common;
2020-04-02 19:42:03 +00:00
using ASC.Core;
using ASC.Core.Common.Settings;
2020-03-02 15:38:31 +00:00
using ASC.CRM.Core;
2020-04-02 19:42:03 +00:00
using ASC.CRM.Core.Dao;
2020-03-02 15:38:31 +00:00
using ASC.CRM.Core.Enums;
using ASC.CRM.Resources;
using ASC.Data.Storage;
using ASC.Web.Core;
using ASC.Web.Core.Files;
using ASC.Web.Studio.Core;
2020-04-02 19:42:03 +00:00
using Microsoft.Extensions.Configuration;
2020-03-02 15:38:31 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Text;
namespace ASC.Web.CRM.Classes
{
2021-03-13 12:22:14 +00:00
[Scope]
2020-03-02 15:38:31 +00:00
public class Global
{
2020-04-02 19:42:03 +00:00
public Global(StorageFactory storageFactory,
SecurityContext securityContext,
SetupInfo setupInfo,
FilesLinkUtility filesLinkUtility,
CRMSecurity cRMSecurity,
TenantManager tenantManager,
SettingsManager settingsManager,
2021-03-02 16:29:07 +00:00
IConfiguration configuration
2020-04-02 19:42:03 +00:00
)
2020-03-02 15:38:31 +00:00
{
2020-04-02 19:42:03 +00:00
StorageFactory = storageFactory;
FilesLinkUtility = filesLinkUtility;
SetupInfo = setupInfo;
SecurityContext = securityContext;
CRMSecurity = cRMSecurity;
TenantID = tenantManager.GetCurrentTenant().TenantId;
SettingsManager = settingsManager;
Configuration = configuration;
2020-03-02 15:38:31 +00:00
}
2020-04-02 19:42:03 +00:00
public IConfiguration Configuration { get; }
public SettingsManager SettingsManager { get; }
2020-03-02 15:38:31 +00:00
public static readonly int EntryCountOnPage = 25;
public static readonly int VisiblePageCount = 10;
public static readonly int MaxCustomFieldSize = 150;
public static readonly int MaxCustomFieldRows = 25;
public static readonly int MaxCustomFieldCols = 150;
public static readonly int DefaultCustomFieldSize = 40;
public static readonly int DefaultCustomFieldRows = 2;
public static readonly int DefaultCustomFieldCols = 40;
public static readonly int MaxHistoryEventCharacters = 65000;
public static readonly decimal MaxInvoiceItemPrice = (decimal) 99999999.99;
2020-04-02 19:42:03 +00:00
protected int TenantID { get; private set; }
public FilesLinkUtility FilesLinkUtility { get; }
public SetupInfo SetupInfo { get; }
public SecurityContext SecurityContext { get; }
2020-03-02 15:38:31 +00:00
2020-04-02 19:42:03 +00:00
public StorageFactory StorageFactory { get; }
public CRMSecurity CRMSecurity { get; }
2021-03-01 17:36:18 +00:00
2020-04-02 19:42:03 +00:00
public IDataStore GetStore()
2020-03-02 15:38:31 +00:00
{
2020-04-02 19:42:03 +00:00
return StorageFactory.GetStorage(TenantID.ToString(), "crm");
2020-03-02 15:38:31 +00:00
}
2020-04-02 19:42:03 +00:00
public IDataStore GetStoreTemplate()
2020-03-02 15:38:31 +00:00
{
return StorageFactory.GetStorage(String.Empty, "crm_template");
}
2020-04-02 19:42:03 +00:00
public bool CanCreateProjects()
2020-03-02 15:38:31 +00:00
{
try
{
var apiUrl = String.Format("{0}project/securityinfo.json", SetupInfo.WebApiBaseUrl);
2020-03-11 15:10:53 +00:00
var cacheKey = String.Format("{0}-{1}", SecurityContext.CurrentAccount.ID, apiUrl);
2020-03-02 15:38:31 +00:00
2020-04-02 19:42:03 +00:00
bool canCreateProject = false;
2020-03-02 15:38:31 +00:00
2020-04-02 19:42:03 +00:00
throw new NotImplementedException();
//if (HttpRuntime.Cache[cacheKey] != null)
// return Convert.ToBoolean(HttpRuntime.Cache[cacheKey]);
2020-03-02 15:38:31 +00:00
2020-04-02 19:42:03 +00:00
//var apiServer = new Api.ApiServer();
2020-03-02 15:38:31 +00:00
2020-04-02 19:42:03 +00:00
//var responseApi = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(apiServer.GetApiResponse(apiUrl, "GET"))))["response"];
2020-03-02 15:38:31 +00:00
2020-04-02 19:42:03 +00:00
//if (responseApi.HasValues)
// canCreateProject = Convert.ToBoolean(responseApi["canCreateProject"].Value<String>());
//else
// canCreateProject = false;
//HttpRuntime.Cache.Remove(cacheKey);
//HttpRuntime.Cache.Insert(cacheKey, canCreateProject, null, System.Web.Caching.Cache.NoAbsoluteExpiration,
// TimeSpan.FromMinutes(5));
2020-03-02 15:38:31 +00:00
return canCreateProject;
}
catch
{
return false;
}
}
2020-04-02 19:42:03 +00:00
public bool CanDownloadInvoices
2020-03-02 15:38:31 +00:00
{
get
{
2020-04-02 19:42:03 +00:00
var value = Configuration["crm:invoice:download:enable"];
2020-03-02 15:38:31 +00:00
if (string.IsNullOrEmpty(value)) return false;
2020-04-02 19:42:03 +00:00
bool canDownloadFiles = Convert.ToBoolean(value);
2020-03-02 15:38:31 +00:00
if (canDownloadFiles && string.IsNullOrEmpty(FilesLinkUtility.DocServiceConverterUrl))
{
canDownloadFiles = false;
}
return canDownloadFiles;
}
}
2020-04-02 19:42:03 +00:00
public bool CanCreateReports
2020-03-02 15:38:31 +00:00
{
get
{
return !string.IsNullOrEmpty(FilesLinkUtility.DocServiceDocbuilderUrl) && CRMSecurity.IsAdmin;
}
}
2020-04-02 19:42:03 +00:00
public void SaveDefaultCurrencySettings(CurrencyInfo currency)
2020-03-02 15:38:31 +00:00
{
2020-04-02 19:42:03 +00:00
var tenantSettings = SettingsManager.Load<CRMSettings>();
tenantSettings.DefaultCurrency = currency;
SettingsManager.Save<CRMSettings>(tenantSettings);
2020-03-02 15:38:31 +00:00
}
2021-03-01 17:36:18 +00:00
2021-03-02 16:29:07 +00:00
2020-03-02 15:38:31 +00:00
/// <summary>
/// The method to Decode your Base64 strings.
/// </summary>
/// <param name="encodedData">The String containing the characters to decode.</param>
/// <returns>A String containing the results of decoding the specified sequence of bytes.</returns>
public static string DecodeFrom64(string encodedData)
{
var encodedDataAsBytes = Convert.FromBase64String(encodedData);
return System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
}
/// <summary>
/// The method create a Base64 encoded string from a normal string.
/// </summary>
/// <param name="toEncode">The String containing the characters to encode.</param>
/// <returns>The Base64 encoded string.</returns>
public static string EncodeTo64(string toEncode)
{
var toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode);
return Convert.ToBase64String(toEncodeAsBytes);
}
public static byte[] ToByteArray(Stream inputStream)
{
var br = new MemoryStream();
var data = new byte[1024];
var readed = 0;
while ((readed = inputStream.Read(data, 0, data.Length)) > 0)
{
br.Write(data, 0, readed);
}
br.Close();
return br.ToArray();
}
2020-04-02 19:42:03 +00:00
2020-03-02 15:38:31 +00:00
public static string GetImgFormatName(ImageFormat format)
{
if (format.Equals(ImageFormat.Bmp)) return "bmp";
if (format.Equals(ImageFormat.Emf)) return "emf";
if (format.Equals(ImageFormat.Exif)) return "exif";
if (format.Equals(ImageFormat.Gif)) return "gif";
if (format.Equals(ImageFormat.Icon)) return "icon";
if (format.Equals(ImageFormat.Jpeg)) return "jpeg";
if (format.Equals(ImageFormat.MemoryBmp)) return "MemoryBMP";
if (format.Equals(ImageFormat.Png)) return "png";
if (format.Equals(ImageFormat.Tiff)) return "tiff";
if (format.Equals(ImageFormat.Wmf)) return "wmf";
return "jpg";
}
public static byte[] SaveToBytes(Image img)
{
return CommonPhotoManager.SaveToBytes(img, GetImgFormatName(img.RawFormat));
}
2020-04-02 19:42:03 +00:00
2020-03-02 15:38:31 +00:00
private static readonly string[] Formats = new[]
{
"o",
"yyyy'-'MM'-'dd'T'HH'-'mm'-'ss'.'fffK",
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK",
"yyyy-MM-ddTHH:mm:ss"
};
public static DateTime ApiDateTimeParse(string data)
{
if (string.IsNullOrEmpty(data)) throw new ArgumentNullException("data");
if (data.Length < 7) throw new ArgumentException(CRMErrorsResource.DateTimeFormatInvalid);
DateTime dateTime;
if (DateTime.TryParseExact(data, Formats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dateTime))
{
return new DateTime(dateTime.Ticks, DateTimeKind.Unspecified);
}
throw new ArgumentException(CRMErrorsResource.DateTimeFormatInvalid);
}
public static JObject JObjectParseWithDateAsString(string data)
{
JsonReader reader = new JsonTextReader(
new StringReader(data)
);
reader.DateParseHandling = DateParseHandling.None;
return JObject.Load(reader);
2020-04-02 19:42:03 +00:00
}
2020-03-02 15:38:31 +00:00
}
}