)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » ContactController (Ia.Statistics.Wa.Controllers)

Public general use code classes and xml files that we've compiled and used over the years:

    1: using Ia.Statistics.Wa.Models;
    2: using Microsoft.AspNetCore.Mvc;
    3: using System.Diagnostics;
    4: using System.Net;
    5:  
    6: namespace Ia.Statistics.Wa.Controllers
    7: {
    8:     ////////////////////////////////////////////////////////////////////////////
    9:  
   10:     /// <summary publish="true">
   11:     ///
   12:     /// </summary>
   13:     /// 
   14:     /// <remarks> 
   15:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   16:     ///
   17:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   18:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   19:     ///
   20:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   21:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   22:     /// 
   23:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   24:     /// 
   25:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   26:     /// </remarks> 
   27:     public class ContactController : Controller
   28:     {
   29:         private readonly ILogger<ContactController> _logger;
   30:  
   31:         /////////////////////////////////////////////////////////////////////////////////
   32:  
   33:         /// <summary>
   34:         ///
   35:         /// </summary>
   36:         public ContactController(ILogger<ContactController> logger)
   37:         {
   38:             _logger = logger;
   39:         }
   40:  
   41:         /////////////////////////////////////////////////////////////////////////////////
   42:  
   43:         /// <summary>
   44:         ///
   45:         /// </summary>
   46:         [HttpGet]
   47:         public IActionResult Index()
   48:         {
   49:             return View();
   50:         }
   51:  
   52:         /////////////////////////////////////////////////////////////////////////////////
   53:  
   54:         /// <summary>
   55:         ///
   56:         /// </summary>
   57:         [HttpPost]
   58:         public IActionResult Index(string nameTextInput, string emailTextInput, string phoneTextInput, string inquiryTextArea)
   59:         {
   60:             var result = ProcessEmail(nameTextInput, emailTextInput, phoneTextInput, inquiryTextArea);
   61:  
   62:             if (result.IsSuccessful)
   63:             {
   64:                 ViewBag.ResultMessage = result.Message;
   65:                 ViewBag.ResultClass = "success";
   66:             }
   67:             else
   68:             {
   69:                 ViewBag.ResultMessage = result.Message;
   70:                 ViewBag.ResultClass = "error";
   71:             }
   72:  
   73:             return View();
   74:         }
   75:  
   76:         /////////////////////////////////////////////////////////////////////////////////
   77:  
   78:         /// <summary>
   79:         ///
   80:         /// </summary>
   81:         protected Ia.Cl.Models.Result ProcessEmail(string nameTextInput, string emailTextInput, string phoneTextInput, string inquiryTextArea)
   82:         {
   83:             var result = new Ia.Cl.Models.Result();
   84:  
   85:             if (!string.IsNullOrEmpty(nameTextInput))
   86:             {
   87:                 if (!string.IsNullOrEmpty(emailTextInput))
   88:                 {
   89:                     if (Ia.Cl.Models.Default.IsEmail(emailTextInput))
   90:                     {
   91:                         if (!string.IsNullOrEmpty(inquiryTextArea))
   92:                         {
   93:                             if (Ia.Cl.Models.Google.Recaptcha.Passed(Request.Form["g-recaptcha-response"]))
   94:                             {
   95:                                 var name = WebUtility.HtmlEncode(nameTextInput);
   96:                                 var email = WebUtility.HtmlEncode(emailTextInput);
   97:                                 var phone = WebUtility.HtmlEncode(phoneTextInput);
   98:                                 var inquiry = WebUtility.HtmlEncode(inquiryTextArea);
   99:  
  100:                                 result = SendMail(name, email, phone, inquiry);
  101:                             }
  102:                             else
  103:                             {
  104:                                 result.AddError("Captcha had failed.");
  105:                             }
  106:                         }
  107:                         else
  108:                         {
  109:                             result.AddError("Please enter your inquiry.");
  110:                         }
  111:                     }
  112:                     else
  113:                     {
  114:                         result.AddError("Your email is not in valid email format.");
  115:                     }
  116:                 }
  117:                 else
  118:                 {
  119:                     result.AddError("Please enter your email.");
  120:                 }
  121:             }
  122:             else
  123:             {
  124:                 result.AddError("Please enter your name.");
  125:             }
  126:  
  127:             return result;
  128:         }
  129:  
  130:         /////////////////////////////////////////////////////////////////////////////////
  131:  
  132:         /// <summary>
  133:         ///
  134:         /// </summary>
  135:         private Ia.Cl.Models.Result SendMail(string name, string email, string phone, string inquiry)
  136:         {
  137:             var message = Ia.Statistics.Wa.Models.Ui.Default.MailTop();
  138:  
  139:             message += @"
  140: <p>A visitor to the website has an inquiry. Visitor information is below:</p>
  141:  
  142: <p>Visitor information:</p>
  143:  
  144: <table class=""form"">
  145: <tr><td>Name:</td><td>" + name + @"&nbsp;</td></tr>
  146: <tr><td>Email:</td><td>" + email + @"&nbsp;</td></tr>
  147: <tr><td>Phone:</td><td>" + phone + @"&nbsp;</td></tr>
  148: <tr><td></td><td></td></tr>
  149: <tr><td valign=""top"">Inquery:</td><td>" + inquiry + @"</td></tr>
  150: </table>";
  151:  
  152:             message += Ia.Statistics.Wa.Models.Ui.Default.MailBottom();
  153:  
  154:             var applicationContact = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ApplicationContact");
  155:             var applicationEmail = Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:ApplicationEmail");
  156:  
  157:             var result = Ia.Cl.Models.Smtp.SendHtml(applicationContact, applicationEmail, "A visitor to the website has an inquiry", message);//, out result);
  158:  
  159:             return result;
  160:         }
  161:  
  162:         /////////////////////////////////////////////////////////////////////////////////
  163:  
  164:         /// <summary>
  165:         ///
  166:         /// </summary>
  167:         [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  168:         public IActionResult Error()
  169:         {
  170:             return View(new Ia.Statistics.Wa.Models.ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  171:         }
  172:  
  173:         /////////////////////////////////////////////////////////////////////////////////
  174:         /////////////////////////////////////////////////////////////////////////////////
  175:     }
  176:  
  177:     /////////////////////////////////////////////////////////////////////////////////
  178:     /////////////////////////////////////////////////////////////////////////////////
  179: }