yavsc/web/Controllers/FrontOfficeController.cs

240 lines
6.3 KiB
C#
Raw Normal View History

2014-07-16 20:35:03 +02:00
using System;
using Yavsc;
using System.Web.Mvc;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using Yavsc.Controllers;
using System.Collections.Generic;
using Yavsc.Model;
2014-10-06 04:58:47 +02:00
using Yavsc.Model.WorkFlow;
using WorkFlowProvider;
using System.Web.Security;
using System.Threading;
2015-01-27 14:17:33 +01:00
using Yavsc.Model.FrontOffice;
2014-07-16 20:35:03 +02:00
namespace Yavsc.Controllers
{
/// <summary>
/// Front office controller.
/// Access granted to all
/// </summary>
2014-07-16 20:35:03 +02:00
public class FrontOfficeController : Controller
{
/// <summary>
/// The wfmgr.
/// </summary>
protected WorkFlowManager wfmgr = null;
2015-01-27 14:17:33 +01:00
/// <summary>
/// Initialize the specified requestContext.
/// </summary>
/// <param name="requestContext">Request context.</param>
protected override void Initialize (System.Web.Routing.RequestContext requestContext)
{
base.Initialize (requestContext);
wfmgr = new WorkFlowManager ();
}
2015-01-27 14:17:33 +01:00
/// <summary>
/// Index this instance.
/// </summary>
2015-01-02 04:52:48 +01:00
public ActionResult Index ()
{
return View ();
}
2015-01-27 14:17:33 +01:00
/// <summary>
/// Estimates this instance.
/// </summary>
[Authorize]
public ActionResult Estimates ()
{
string username = Membership.GetUser ().UserName;
2015-01-27 14:17:33 +01:00
return View (wfmgr.GetEstimates (username));
}
2015-01-27 14:17:33 +01:00
/// <summary>
/// Estimate the specified model and submit.
/// </summary>
/// <param name="model">Model.</param>
/// <param name="submit">Submit.</param>
2014-10-12 15:22:45 +02:00
[Authorize]
2015-01-27 14:17:33 +01:00
public ActionResult Estimate (Estimate model, string submit)
{
ViewData ["WebApiBase"] = "http://" + Request.Url.Authority + "/api";
ViewData ["WABASEWF"] = ViewData ["WebApiBase"] + "/WorkFlow";
if (submit == null) {
2014-10-12 15:22:45 +02:00
if (model.Id > 0) {
Estimate f = wfmgr.GetEstimate (model.Id);
2014-10-12 15:22:45 +02:00
if (f == null) {
ModelState.AddModelError ("Id", "Wrong Id");
return View (model);
}
model = f;
ModelState.Clear ();
string username = HttpContext.User.Identity.Name;
if (username != model.Responsible
&& username != model.Client
&& !Roles.IsUserInRole ("FrontOffice"))
throw new UnauthorizedAccessException ("You're not allowed to view this estimate");
}
} else {
string username = HttpContext.User.Identity.Name;
if (model.Id == 0) {
2015-01-27 14:17:33 +01:00
model.Responsible = username;
ModelState.Clear ();
}
if (ModelState.IsValid) {
if (username != model.Responsible
2015-01-27 14:17:33 +01:00
&& !Roles.IsUserInRole ("FrontOffice"))
throw new UnauthorizedAccessException ("You're not allowed to modify this estimate");
if (model.Id == 0)
model = wfmgr.CreateEstimate (
username,
model.Client, model.Title, model.Description);
2014-11-13 14:57:48 +01:00
else {
wfmgr.UpdateEstimate (model);
model = wfmgr.GetEstimate (model.Id);
2014-11-13 14:57:48 +01:00
}
}
}
2015-01-27 14:17:33 +01:00
return View (model);
}
2015-01-27 14:17:33 +01:00
/// <summary>
/// Catalog this instance.
/// </summary>
2015-01-27 14:17:33 +01:00
[AcceptVerbs ("GET")]
2014-07-16 20:35:03 +02:00
public ActionResult Catalog ()
{
return View (
CatalogManager.GetCatalog (Request.Url.AbsolutePath)
2014-07-16 20:35:03 +02:00
);
}
2014-11-13 14:57:48 +01:00
2014-07-16 20:35:03 +02:00
/// <summary>
/// Catalog this instance.
/// </summary>
2015-01-27 14:17:33 +01:00
[AcceptVerbs ("GET")]
2014-07-16 20:35:03 +02:00
public ActionResult Brand (string id)
{
Catalog c = CatalogManager.GetCatalog (Request.Url.AbsolutePath);
2014-07-16 20:35:03 +02:00
ViewData ["BrandName"] = id;
2015-01-27 14:17:33 +01:00
return View (c.GetBrand (id));
2014-07-16 20:35:03 +02:00
}
/// <summary>
/// get the product category
/// </summary>
/// <returns>The category object.</returns>
/// <param name="brandid">Brand id.</param>
/// <param name="pcid">Product category Id.</param>
2015-01-27 14:17:33 +01:00
[AcceptVerbs ("GET")]
public ActionResult ProductCategory (string brandid, string pcid)
2014-07-16 20:35:03 +02:00
{
ViewData ["BrandId"] = brandid;
ViewData ["ProductCategoryId"] = pcid;
2014-07-16 20:35:03 +02:00
return View (
CatalogManager.GetCatalog (Request.Url.AbsolutePath).GetBrand (brandid).GetProductCategory (pcid)
2014-07-16 20:35:03 +02:00
);
}
2015-01-27 14:17:33 +01:00
/// <summary>
/// Product the specified id, pc and pref.
/// </summary>
/// <param name="id">Identifier.</param>
/// <param name="pc">Pc.</param>
/// <param name="pref">Preference.</param>
2015-01-27 14:17:33 +01:00
[AcceptVerbs ("GET")]
2014-07-16 20:35:03 +02:00
public ActionResult Product (string id, string pc, string pref)
{
Product p = null;
ViewData ["BrandName"] = id;
ViewData ["ProdCatRef"] = pc;
ViewData ["ProdRef"] = pref;
Catalog cat = CatalogManager.GetCatalog (Request.Url.AbsolutePath);
2014-07-16 20:35:03 +02:00
if (cat == null) {
ViewData ["Message"] = "Catalog introuvable";
ViewData ["RefType"] = "Catalog";
return View ("ReferenceNotFound");
}
Brand b = cat.GetBrand (id);
if (b == null) {
ViewData ["RefType"] = "Brand";
return View ("ReferenceNotFound");
}
ProductCategory pcat = b.GetProductCategory (pc);
if (pcat == null) {
ViewData ["RefType"] = "ProductCategory";
return View ("ReferenceNotFound");
}
ViewData ["ProdCatName"] = pcat.Name;
p = pcat.GetProduct (pref);
2015-01-27 14:17:33 +01:00
if (p.CommandForm == null)
2014-07-16 20:35:03 +02:00
p.CommandForm = b.DefaultForm;
2015-01-27 14:17:33 +01:00
return View ((p is Service) ? "Service" : "Product", p);
2014-07-16 20:35:03 +02:00
}
2015-01-27 14:17:33 +01:00
/// <summary>
/// Command this instance.
/// </summary>
2015-01-27 14:17:33 +01:00
public ActionResult Command ()
2014-07-16 20:35:03 +02:00
{
return View ();
}
2015-01-27 14:17:33 +01:00
private Basket GetBasket ()
{
if (Session ["Basket"] == null)
Session ["Basket"] = new Basket();
return Session ["Basket"] as Basket;
}
/// <summary>
/// Command the specified collection.
/// </summary>
/// <param name="collection">Collection.</param>
2014-07-16 20:35:03 +02:00
[HttpPost]
[Authorize]
2015-01-27 14:17:33 +01:00
public ActionResult Command (FormCollection collection)
2014-07-16 20:35:03 +02:00
{
try {
// get files from the request
string fnre = "[A-Za-z0-9~\\-.]+";
HttpFileCollectionBase hfc = Request.Files;
2015-01-27 14:17:33 +01:00
foreach (String h in hfc.AllKeys) {
if (!Regex.Match (hfc [h].FileName, fnre).Success) {
2014-07-16 20:35:03 +02:00
ViewData ["Message"] = "File name refused";
2015-01-27 14:17:33 +01:00
ModelState.AddModelError (
2014-07-16 20:35:03 +02:00
h,
2015-01-27 14:17:33 +01:00
string.Format (
2014-07-16 20:35:03 +02:00
"The file name {0} dosn't match an acceptable file name {1}",
2015-01-27 14:17:33 +01:00
hfc [h].FileName, fnre));
return View (collection);
2014-07-16 20:35:03 +02:00
}
}
2015-01-27 14:17:33 +01:00
foreach (String h in hfc.AllKeys) {
2014-07-16 20:35:03 +02:00
// TODO Limit with hfc[h].ContentLength
2015-01-27 14:17:33 +01:00
hfc [h].SaveAs (Path.Combine (FileSystemController.BaseDir, hfc [h].FileName));
2014-07-16 20:35:03 +02:00
}
// Add specified product command to the basket,
2015-01-27 14:17:33 +01:00
GetBasket().Add (Commande.Create (collection));
ViewData ["Message"] = LocalizedText.Item_added_to_basket;
2014-07-16 20:35:03 +02:00
return View (collection);
2015-01-27 14:17:33 +01:00
} catch (Exception e) {
ViewData ["Message"] = "Exception:" + e.Message;
2014-07-16 20:35:03 +02:00
return View (collection);
}
}
2015-01-27 14:17:33 +01:00
ActionResult YourCommands ()
{
return View (GetBasket());
}
2014-07-16 20:35:03 +02:00
}
}