public ActionResult SMSAdd(ListSMSModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new DataSourceResult {
                    Errors = ModelState.SerializeErrors()
                }));
            }

            var storeId            = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var sendInBlueSettings = _settingService.LoadSetting <SendInBlueSettings>(storeId);

            var message = _messageTemplateService.GetMessageTemplateById(model.MessageId);

            if (message != null)
            {
                _genericAttributeService.SaveAttribute(message, "UseSMS", model.SMSActive);
                _genericAttributeService.SaveAttribute(message, "SMSText", model.Text);
                _genericAttributeService.SaveAttribute(message, "PhoneTypeId", model.PhoneTypeId);
            }

            //update list of the message templates which are sending in SMS
            if (!sendInBlueSettings.SMSMessageTemplatesIds.Contains(model.MessageId))
            {
                sendInBlueSettings.SMSMessageTemplatesIds.Add(model.MessageId);
                _settingService.SaveSetting(sendInBlueSettings, x => x.SMSMessageTemplatesIds, storeId, false);
                _settingService.ClearCache();
            }

            return(new NullJsonResult());
        }
        public ActionResult SMSList(ListSMSModel model)
        {
            var storeId            = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var sendInBlueSettings = _settingService.LoadSetting <SendInBlueSettings>(storeId);

            //get message templates which are sending in SMS
            var messageTemplates = _messageTemplateService.GetAllMessageTemplates(storeId)
                                   .Where(x => sendInBlueSettings.SMSMessageTemplatesIds.Contains(x.Id)).ToList();
            var gridModel = new DataSourceResult
            {
                Data = messageTemplates.Select(x =>
                {
                    var phoneTypeID = x.GetAttribute <int>("PhoneTypeId", _genericAttributeService);
                    var sms         = new ListSMSModel
                    {
                        Id        = x.Id,
                        MessageId = x.Id,
                        Name      = storeId > 0 ? x.Name : string.Format("{0} {1}", x.Name, !x.LimitedToStores ? string.Empty :
                                                                         _storeService.GetAllStores().Where(s => !x.LimitedToStores || _storeMappingService.GetStoresIdsWithAccess(x).Contains(s.Id))
                                                                         .Aggregate("-", (current, next) => string.Format("{0} {1}, ", current, next.Name)).TrimEnd(' ', ',')),
                        SMSActive   = x.GetAttribute <bool>("UseSMS", _genericAttributeService),
                        PhoneTypeId = phoneTypeID,
                        Text        = x.GetAttribute <string>("SMSText", _genericAttributeService)
                    };

                    //choose phone number for the sending SMS
                    //currently supported: "my phone" (filled on the configuration page), customer phone, phone of the billing address
                    switch (phoneTypeID)
                    {
                    case 0:
                        sms.PhoneType = _localizationService.GetResource("Plugins.Misc.SendInBlue.MyPhone");
                        break;

                    case 1:
                        sms.PhoneType = _localizationService.GetResource("Plugins.Misc.SendInBlue.CustomerPhone");
                        break;

                    case 2:
                        sms.PhoneType = _localizationService.GetResource("Plugins.Misc.SendInBlue.BillingAddressPhone");
                        break;

                    default:
                        break;
                    }

                    return(sms);
                }),
                Total = messageTemplates.Count
            };

            return(Json(gridModel));
        }
        public ActionResult SMSDelete(ListSMSModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new DataSourceResult {
                    Errors = ModelState.SerializeErrors()
                }));
            }

            var storeId            = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var sendInBlueSettings = _settingService.LoadSetting <SendInBlueSettings>(storeId);

            //delete generic attributes
            var message = _messageTemplateService.GetMessageTemplateById(model.MessageId);

            if (message != null)
            {
                var attributes   = _genericAttributeService.GetAttributesForEntity(message.Id, "MessageTemplate");
                var smsAttribute = attributes.FirstOrDefault(x => x.Key == "UseSMS");
                if (smsAttribute != null)
                {
                    _genericAttributeService.DeleteAttribute(smsAttribute);
                }
                smsAttribute = attributes.FirstOrDefault(x => x.Key == "SMSText");
                if (smsAttribute != null)
                {
                    _genericAttributeService.DeleteAttribute(smsAttribute);
                }
                smsAttribute = attributes.FirstOrDefault(x => x.Key == "PhoneTypeId");
                if (smsAttribute != null)
                {
                    _genericAttributeService.DeleteAttribute(smsAttribute);
                }
            }

            //update list of the message templates which are sending in SMS
            if (sendInBlueSettings.SMSMessageTemplatesIds.Contains(model.MessageId))
            {
                sendInBlueSettings.SMSMessageTemplatesIds.Remove(model.MessageId);
                _settingService.SaveSetting(sendInBlueSettings, x => x.SMSMessageTemplatesIds, storeId, false);
                _settingService.ClearCache();
            }

            return(new NullJsonResult());
        }
        public ActionResult SMSUpdate(ListSMSModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new DataSourceResult {
                    Errors = ModelState.SerializeErrors()
                }));
            }

            SMSAdd(model);
            if (model.Id != model.MessageId)
            {
                SMSDelete(new ListSMSModel {
                    MessageId = model.Id
                });
            }

            return(new NullJsonResult());
        }