2020-04-16 19:41:37 +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 .
*
* /
2021-03-10 15:38:56 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using ASC.Api.CRM ;
2020-04-16 19:41:37 +00:00
using ASC.Common.Web ;
2021-03-10 15:38:56 +00:00
using ASC.CRM.ApiModels ;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core ;
2021-03-10 15:38:56 +00:00
using ASC.CRM.Core.Dao ;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core.Entities ;
using ASC.CRM.Core.Enums ;
using ASC.MessagingSystem ;
using ASC.Web.Api.Routing ;
2021-03-19 16:56:26 +00:00
using AutoMapper ;
2021-03-24 15:55:47 +00:00
using Microsoft.AspNetCore.Mvc ;
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public class CustomFieldsController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
private readonly MessageService _messageService ;
private readonly MessageTarget _messageTarget ;
2021-03-19 16:56:26 +00:00
2021-05-05 14:09:05 +00:00
public CustomFieldsController ( CrmSecurity crmSecurity ,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory ,
MessageTarget messageTarget ,
2021-03-19 16:56:26 +00:00
MessageService messageService ,
IMapper mapper )
: base ( daoFactory , crmSecurity , mapper )
2021-03-10 15:38:56 +00:00
{
2021-03-19 16:56:26 +00:00
_messageTarget = messageTarget ;
_messageService = messageService ;
2021-03-10 15:38:56 +00:00
}
2020-04-16 19:41:37 +00:00
/// <summary>
/// Returns the list of descriptions for all existing user fields
/// </summary>
/// <param name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Type</param>
/// <short>Get user field list</short>
/// <category>User fields</category>
///<returns>
/// User field list
/// </returns>
///<exception cref="ArgumentException"></exception>
2021-03-02 16:29:07 +00:00
[Read(@"{entityType:regex(contact|person|company|opportunity|case)}/customfield/definitions")]
2021-03-09 15:37:16 +00:00
public IEnumerable < CustomFieldDto > GetCustomFieldDefinitions ( string entityType )
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
return _mapper . Map < List < CustomField > , List < CustomFieldDto > > ( _daoFactory . GetCustomFieldDao ( ) . GetFieldsDescription ( ToEntityType ( entityType ) ) ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Returns the list of all user field values using the entity type and entity ID specified in the request
/// </summary>
/// <param name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Type</param>
/// <param name="entityid">ID</param>
/// <short>Get user field values</short>
/// <category>User fields</category>
/// <returns></returns>
2021-03-02 16:29:07 +00:00
[Read(@"{entityType:regex(contact|person|company|opportunity|case)}/{entityid:int}/customfield")]
2021-03-09 15:37:16 +00:00
public IEnumerable < CustomFieldBaseDto > GetCustomFieldForSubject ( string entityType , int entityid )
2020-04-16 19:41:37 +00:00
{
2021-03-22 11:56:42 +00:00
return _mapper . Map < List < CustomField > , List < CustomFieldDto > > ( _daoFactory . GetCustomFieldDao ( ) . GetEnityFields ( ToEntityType ( entityType ) , entityid , false ) ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Sets the new user field value using the entity type, ID, field ID and value specified in the request
/// </summary>
/// <param name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Type</param>
/// <param name="entityid">ID</param>
/// <param name="fieldid">Field ID</param>
/// <param name="fieldValue">Field Value</param>
/// <short>Set user field value</short>
/// <category>User fields</category>
/// <returns>
/// User field
/// </returns>
2021-03-02 16:29:07 +00:00
[Create(@"{entityType:regex(contact|person|company|opportunity|case)}/{entityid:int}/customfield/{fieldid:int}")]
2021-03-24 15:55:47 +00:00
public CustomFieldBaseDto SetEntityCustomFieldValue (
2021-06-04 14:38:42 +00:00
[FromRoute] string entityType ,
[FromRoute] int entityid ,
2021-03-24 15:55:47 +00:00
[FromRoute] int fieldid ,
[FromForm] string fieldValue )
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
var customField = _daoFactory . GetCustomFieldDao ( ) . GetFieldDescription ( fieldid ) ;
2020-04-16 19:41:37 +00:00
var entityTypeStr = ToEntityType ( entityType ) ;
customField . EntityID = entityid ;
customField . Value = fieldValue ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetCustomFieldDao ( ) . SetFieldValue ( entityTypeStr , entityid , fieldid , fieldValue ) ;
2020-04-16 19:41:37 +00:00
2021-03-22 11:56:42 +00:00
return _mapper . Map < CustomFieldBaseDto > ( customField ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Creates a new user field with the parameters (entity type, field title, type, etc.) specified in the request
/// </summary>
/// <param optional="false" name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Entity type</param>
/// <param optional="false" name="label">Field title</param>
/// <param name="fieldType"
/// remark="Allowed values: TextField, TextArea, SelectBox, CheckBox, Heading or Date">
/// User field value
/// </param>
/// <param optional="true" name="position">Field position</param>
/// <param optional="true" name="mask" remark="Sent in json format only" >Mask</param>
/// <short>Create user field</short>
/// <category>User fields</category>
/// <returns>
/// User field
/// </returns>
///<example>
/// <![CDATA[
///
/// Data transfer in application/json format:
///
/// 1) Creation of a user field of TextField type
///
/// data: {
/// entityType: "contact",
/// label: "Sample TextField",
/// fieldType: 0,
/// position: 0,
/// mask: {"size":"40"} - this is the text field size. All other values are ignored.
/// }
///
///
/// 2) Creation of a user field of TextArea type
///
/// data: {
/// entityType: "contact",
/// label: "Sample TextArea",
/// fieldType: 1,
/// position: 1,
/// mask: '{"rows":"2","cols":"30"}' - this is the TextArea size. All other values are ignored.
/// }
///
///
/// 3) Creation of a user field of SelectBox type
///
/// data: {
/// entityType: "contact",
/// label: "Sample SelectBox",
/// fieldType: 2,
/// position: 0,
/// mask: ["1","2","3"] - SelectBox values.
/// }
///
///
///
/// 4) Creation of a user field of CheckBox type
///
/// data: {
/// entityType: "contact",
/// label: "Sample CheckBox",
/// fieldType: 3,
/// position: 0,
/// mask: ""
/// }
///
///
///
/// 5) Creation of a user field of Heading type
///
/// data: {
/// entityType: "contact",
/// label: "Sample Heading",
/// fieldType: 4,
/// position: 0,
/// mask: ""
/// }
///
///
///
/// 6) Creation of a user field of Date type
///
/// data: {
/// entityType: "contact",
/// label: "Sample Date",
/// fieldType: 5,
/// position: 0,
/// mask: ""
/// }
///
///
/// ]]>
/// </example>
2021-03-02 16:29:07 +00:00
[Create(@"{entityType:regex(contact|person|company|opportunity|case)}/customfield")]
2021-03-24 15:55:47 +00:00
public CustomFieldDto CreateCustomFieldValue (
[FromRoute] string entityType ,
2021-06-04 14:38:42 +00:00
[FromForm] string label ,
2021-03-24 15:55:47 +00:00
[FromForm] int fieldType ,
2021-06-04 14:38:42 +00:00
[FromForm] int position ,
2021-03-24 15:55:47 +00:00
[FromForm] string mask )
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
if ( ! ( _crmSecurity . IsAdmin ) ) throw _crmSecurity . CreateSecurityException ( ) ;
2020-04-16 19:41:37 +00:00
var entityTypeObj = ToEntityType ( entityType ) ;
2021-03-19 16:56:26 +00:00
var fieldID = _daoFactory . GetCustomFieldDao ( ) . CreateField ( entityTypeObj , label , ( CustomFieldType ) fieldType , mask ) ;
var wrapper = _daoFactory . GetCustomFieldDao ( ) . GetFieldDescription ( fieldID ) ;
2020-04-16 19:41:37 +00:00
var messageAction = GetCustomFieldCreatedAction ( entityTypeObj ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( wrapper . ID ) , wrapper . Label ) ;
2020-04-16 19:41:37 +00:00
2021-03-22 11:56:42 +00:00
return _mapper . Map < CustomFieldDto > ( _daoFactory . GetCustomFieldDao ( ) . GetFieldDescription ( fieldID ) ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Updates the selected user field with the parameters (entity type, field title, type, etc.) specified in the request
/// </summary>
/// <param name="id">User field id</param>
/// <param name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Entity type</param>
/// <param optional="false" name="label">Field title</param>
/// <param name="fieldType"
/// remark="Allowed values: 0 (TextField),1 (TextArea),2 (SelectBox),3 (CheckBox),4 (Heading) or 5 (Date)">
/// User field value
/// </param>
/// <param optional="true" name="position">Field position</param>
/// <param optional="true" name="mask" remark="Sent in json format only" >Mask</param>
/// <short> Updates the selected user field</short>
/// <category>User fields</category>
/// <returns>
/// User field
/// </returns>
///<remarks>
/// <![CDATA[
/// You can update field if there is no related elements. If such elements exist there will be updated only label and mask, other parameters will be ignored.
/// ]]>
/// </remarks>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
2021-03-02 16:29:07 +00:00
[Update(@"{entityType:regex(contact|person|company|opportunity|case)}/customfield/{id:int}")]
2021-03-09 15:37:16 +00:00
public CustomFieldDto UpdateCustomFieldValue ( int id , string entityType , string label , int fieldType , int position , string mask )
2020-04-16 19:41:37 +00:00
{
if ( id < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _daoFactory . GetCustomFieldDao ( ) . IsExist ( id ) ) throw new ItemNotFoundException ( ) ;
2020-04-16 19:41:37 +00:00
var entityTypeObj = ToEntityType ( entityType ) ;
var customField = new CustomField
2021-03-02 16:29:07 +00:00
{
EntityType = entityTypeObj ,
2021-05-17 14:50:55 +00:00
Type = ( CustomFieldType ) fieldType ,
2021-03-02 16:29:07 +00:00
ID = id ,
Mask = mask ,
Label = label ,
2021-05-17 14:50:55 +00:00
SortOrder = position
2021-03-02 16:29:07 +00:00
} ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetCustomFieldDao ( ) . EditItem ( customField ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
customField = _daoFactory . GetCustomFieldDao ( ) . GetFieldDescription ( id ) ;
2020-04-16 19:41:37 +00:00
var messageAction = GetCustomFieldUpdatedAction ( entityTypeObj ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( customField . ID ) , customField . Label ) ;
2020-04-16 19:41:37 +00:00
2021-03-22 11:56:42 +00:00
return _mapper . Map < CustomFieldDto > ( customField ) ;
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Deletes the user field with the ID specified in the request
/// </summary>
/// <param name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Type</param>
/// <param name="fieldid">Field ID</param>
/// <short>Delete user field</short>
/// <category>User fields</category>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
/// <returns>
/// User field
/// </returns>
2021-03-02 16:29:07 +00:00
[Delete(@"{entityType:regex(contact|person|company|opportunity|case)}/customfield/{fieldid:int}")]
2021-03-09 15:37:16 +00:00
public CustomFieldDto DeleteCustomField ( string entityType , int fieldid )
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
if ( ! ( _crmSecurity . IsAdmin ) ) throw _crmSecurity . CreateSecurityException ( ) ;
2020-04-16 19:41:37 +00:00
if ( fieldid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var customField = _daoFactory . GetCustomFieldDao ( ) . GetFieldDescription ( fieldid ) ;
2020-04-16 19:41:37 +00:00
if ( customField = = null ) throw new ItemNotFoundException ( ) ;
2021-03-22 11:56:42 +00:00
var result = _mapper . Map < CustomFieldDto > ( customField ) ;
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetCustomFieldDao ( ) . DeleteField ( fieldid ) ;
2020-04-16 19:41:37 +00:00
var messageAction = GetCustomFieldDeletedAction ( ToEntityType ( entityType ) ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( customField . ID ) , result . Label ) ;
2020-04-16 19:41:37 +00:00
return result ;
}
/// <summary>
/// Updates user fields order
/// </summary>
/// <param name="fieldids">User field ID list</param>
/// <param name="entityType" remark="Allowed values: contact,person,company,opportunity,case">Entity type</param>
/// <category>User fields</category>
/// <returns>
/// User fields
/// </returns>
/// <exception cref="SecurityException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ItemNotFoundException"></exception>
2021-03-02 16:29:07 +00:00
[Update(@"{entityType:regex(contact|person|company|opportunity|case)}/customfield/reorder")]
2021-03-09 15:37:16 +00:00
public IEnumerable < CustomFieldBaseDto > UpdateCustomFieldsOrder ( IEnumerable < int > fieldids , string entityType )
2020-04-16 19:41:37 +00:00
{
if ( fieldids = = null ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! ( _crmSecurity . IsAdmin ) ) throw _crmSecurity . CreateSecurityException ( ) ;
2020-04-16 19:41:37 +00:00
var customFields = new List < CustomField > ( ) ;
foreach ( var id in fieldids )
{
2021-03-19 16:56:26 +00:00
if ( ! _daoFactory . GetCustomFieldDao ( ) . IsExist ( id ) ) throw new ItemNotFoundException ( ) ;
customFields . Add ( _daoFactory . GetCustomFieldDao ( ) . GetFieldDescription ( id ) ) ;
2020-04-16 19:41:37 +00:00
}
2021-03-19 16:56:26 +00:00
_daoFactory . GetCustomFieldDao ( ) . ReorderFields ( fieldids . ToArray ( ) ) ;
2020-04-16 19:41:37 +00:00
var messageAction = GetCustomFieldsUpdatedOrderAction ( ToEntityType ( entityType ) ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( messageAction , _messageTarget . Create ( fieldids ) , customFields . Select ( x = > x . Label ) ) ;
2020-04-16 19:41:37 +00:00
2021-03-22 11:56:42 +00:00
return _mapper . Map < List < CustomField > , List < CustomFieldDto > > ( customFields ) ;
2020-04-16 19:41:37 +00:00
}
2021-03-23 15:41:56 +00:00
2020-04-16 19:41:37 +00:00
private static MessageAction GetCustomFieldCreatedAction ( EntityType entityType )
{
switch ( entityType )
{
case EntityType . Contact :
return MessageAction . ContactUserFieldCreated ;
case EntityType . Person :
return MessageAction . PersonUserFieldCreated ;
case EntityType . Company :
return MessageAction . CompanyUserFieldCreated ;
case EntityType . Opportunity :
return MessageAction . OpportunityUserFieldCreated ;
case EntityType . Case :
return MessageAction . CaseUserFieldCreated ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
private static MessageAction GetCustomFieldUpdatedAction ( EntityType entityType )
{
switch ( entityType )
{
case EntityType . Contact :
return MessageAction . ContactUserFieldUpdated ;
case EntityType . Person :
return MessageAction . PersonUserFieldUpdated ;
case EntityType . Company :
return MessageAction . CompanyUserFieldUpdated ;
case EntityType . Opportunity :
return MessageAction . OpportunityUserFieldUpdated ;
case EntityType . Case :
return MessageAction . CaseUserFieldUpdated ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
private static MessageAction GetCustomFieldDeletedAction ( EntityType entityType )
{
switch ( entityType )
{
case EntityType . Contact :
return MessageAction . ContactUserFieldDeleted ;
case EntityType . Person :
return MessageAction . PersonUserFieldDeleted ;
case EntityType . Company :
return MessageAction . CompanyUserFieldDeleted ;
case EntityType . Opportunity :
return MessageAction . OpportunityUserFieldDeleted ;
case EntityType . Case :
return MessageAction . CaseUserFieldDeleted ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
private static MessageAction GetCustomFieldsUpdatedOrderAction ( EntityType entityType )
{
switch ( entityType )
{
case EntityType . Contact :
return MessageAction . ContactUserFieldsUpdatedOrder ;
case EntityType . Person :
return MessageAction . PersonUserFieldsUpdatedOrder ;
case EntityType . Company :
return MessageAction . CompanyUserFieldsUpdatedOrder ;
case EntityType . Opportunity :
return MessageAction . OpportunityUserFieldsUpdatedOrder ;
case EntityType . Case :
return MessageAction . CaseUserFieldsUpdatedOrder ;
default :
throw new ArgumentException ( "Invalid entityType: " + entityType ) ;
}
}
}
}