PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/DDB/Areas/Administration/Controllers/AdminController.cs

https://github.com/sixtythreebits/ddb
C# | 846 lines | 691 code | 148 blank | 7 comment | 168 complexity | d42bda56a4ea08b0e449d52c78dc92ac MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Core;
  7. using DevExpress.Web.Mvc;
  8. using DevExpress.Web.ASPxGridView;
  9. using System.Web.SessionState;
  10. using DDB.Areas.Administration.Models.Helper;
  11. using System.IO;
  12. using System.Net;
  13. using System.Xml.Linq;
  14. using System.Web.Configuration;
  15. namespace DDB.Areas.Administration.Controllers
  16. {
  17. [Authorize]
  18. public class AdminController : Controller
  19. {
  20. public AdminController()
  21. {
  22. ModelBinders.Binders.DefaultBinder = new DevExpressEditorsBinder();
  23. }
  24. //
  25. // GET: /Administration/Admin/
  26. public ActionResult Index()
  27. {
  28. return RedirectToAction("FrontPage", "Admin");
  29. }
  30. public ActionResult ListProjects()
  31. {
  32. Project _Project = new Project();
  33. var model = _Project.ListProjects(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  34. return View(model);
  35. }
  36. public ActionResult ListProjectEdit()
  37. {
  38. Project _Project = new Project();
  39. var model = _Project.ListProjects(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  40. return PartialView("ListProjects", model);
  41. }
  42. [HttpPost]
  43. public ActionResult ListProjectUpdate(Project _Project)
  44. {
  45. Project tmpProject = new Project(); // Old project
  46. if (ModelState.IsValid)
  47. {
  48. if (_Project.Slug.Contains(" "))
  49. {
  50. ModelState.AddModelError("Slug", "Slug contains empty space.");
  51. }
  52. else if (_Project.IsSlugUnique(_Project.Slug) == false && tmpProject.GetProjectByID(_Project.ID.Value).Slug != _Project.Slug)
  53. {
  54. ModelState.AddModelError("Slug", "Slug already exist.");
  55. }
  56. else
  57. {
  58. _Project.TSP_Projects(1, _Project.ID, _Project.Slug, null, _Project.Title, null, null, null, null, _Project.IsPublished, _Project.SortIndex,null,null);
  59. if (_Project.IsError)
  60. {
  61. ViewData["EditError"] = Resources.Fail;
  62. return PartialView("ListProjects", new Project().ListProjects(null));
  63. }
  64. }
  65. }
  66. else
  67. {
  68. ViewData["EditError"] = "Please, correct all errors.";
  69. }
  70. var model = _Project.ListProjects(null);
  71. return PartialView("ListProjects", model);
  72. }
  73. [HttpPost]
  74. public ActionResult ListProjectAdd(Project _Project)
  75. {
  76. if (ModelState.IsValid)
  77. {
  78. if (_Project.IsSlugUnique(_Project.Slug) == false)
  79. {
  80. ModelState.AddModelError("Slug", "Slug already exist.");
  81. }
  82. else if (_Project.Slug.Contains(" "))
  83. {
  84. ModelState.AddModelError("Slug", "Slug contains empty space.");
  85. }
  86. else
  87. {
  88. _Project.TSP_Projects(0, null, _Project.Slug, null, _Project.Title, null, null, null, null, _Project.IsPublished, _Project.SortIndex,null,null);
  89. if (_Project.IsError)
  90. {
  91. ViewData["EditError"] = Resources.Fail;
  92. return PartialView("ListProjects", new Project().ListProjects(null));
  93. }
  94. }
  95. }
  96. else
  97. {
  98. ViewData["EditError"] = "Please, correct all errors.";
  99. }
  100. var model = _Project.ListProjects(null);
  101. return PartialView("ListProjects", model);
  102. }
  103. public ActionResult ListProjectDelete(Project _Project)
  104. {
  105. if (_Project.ID >= 0)
  106. {
  107. DeleteProjectPictures(_Project);
  108. _Project.TSP_Projects(2, _Project.ID, null, null, null, null, null, null, null, null, null, null, null);
  109. if (_Project.IsError)
  110. {
  111. ViewData["EditError"] = Resources.Fail;
  112. return PartialView("ListProjects", new Project().ListProjects(null));
  113. }
  114. }
  115. var model = _Project.ListProjects(null);
  116. return PartialView("ListProjects", model);
  117. }
  118. private void DeleteProjectPictures(Project _Project)
  119. {
  120. var path = WebConfigurationManager.AppSettings["UploadPath"];
  121. var imgName = _Project.ListProjects(null).Where(p => p.ID == _Project.ID).Select(p => p.SmallImgName).FirstOrDefault();
  122. string delPath = Server.MapPath(path + "/" + imgName);
  123. if (System.IO.File.Exists(delPath))
  124. {
  125. System.IO.File.Delete(delPath);
  126. }
  127. ProjectAssets _ProjectAssets = new ProjectAssets();
  128. List<ProjectAssets> mediaImages = _ProjectAssets.ListProjectAssets(null).Where(p => p.ProjectID == _Project.ID).ToList();
  129. foreach (var item in mediaImages)
  130. {
  131. if (item.CodeVal == 2)
  132. {
  133. delPath = Server.MapPath(path + "/" + item);
  134. if (System.IO.File.Exists(delPath))
  135. {
  136. System.IO.File.Delete(delPath);
  137. }
  138. }
  139. }
  140. }
  141. [HttpGet]
  142. public ActionResult ListProjectDetails(string Slug)
  143. {
  144. if(Slug == null)
  145. {
  146. return RedirectToAction("ListProjects", "Admin");
  147. }
  148. Project _Project = new Project();
  149. _Project = _Project.ListProjects(null).SingleOrDefault(p => p.Slug == Slug);
  150. if (_Project == null)
  151. return HttpNotFound();
  152. ProjectAssets _ProjectAsset = new ProjectAssets();
  153. ViewBag.MediaItems = _ProjectAsset.ListProjectAssets(_Project.ID);
  154. Dictionary _Dictionary = new Dictionary();
  155. ViewBag.CategoryItems = _Dictionary.ListDictionary(1, 1);
  156. ProjectCategory _ProjectCategory = new ProjectCategory();
  157. ViewBag.SelectedCategoryItems = _ProjectCategory.ListProjectCategories(_Project.ID.Value).Select(x => x.CategoryID.Value).ToList();
  158. return View(_Project);
  159. }
  160. [HttpPost]
  161. [ValidateInput(false)]
  162. public ActionResult ListProjectDetails(Project _Project)
  163. {
  164. ProjectAssets _ProjectAsset = new ProjectAssets();
  165. ViewBag.MediaItems = _ProjectAsset.ListProjectAssets(_Project.ID);
  166. var allow = new string[] { ".jpg", ".gif", ".png" };
  167. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  168. var guid = Guid.NewGuid().ToString();
  169. string ext = null;
  170. if (_Project.SmallImg == null && _Project.SmallImgName == null)
  171. {
  172. ModelState.AddModelError("SmallImg", "Image is required");
  173. }
  174. else if (_Project.SmallImg != null)
  175. {
  176. ext = Path.GetExtension(_Project.SmallImg.FileName).ToLower();
  177. if (!allow.Contains(ext))
  178. {
  179. ModelState.AddModelError("SmallImg", "unsupportied file extension: " + ext);
  180. }
  181. }
  182. if (_Project.ProjectCategories == null || !_Project.ProjectCategories.Any() )
  183. {
  184. ModelState.AddModelError("ProjectCategories", "Category is required");
  185. }
  186. if (_Project.BackgroundColor == null)
  187. {
  188. ModelState.AddModelError("BackgroundColor", "Theme is required");
  189. }
  190. if (ModelState.IsValid)
  191. {
  192. if (_Project.SmallImg != null)
  193. {
  194. ext = Path.GetExtension(_Project.SmallImg.FileName).ToLower();
  195. if (_Project.SmallImgName != null)
  196. {
  197. string delPath = path + "/" + _Project.SmallImgName;
  198. if (System.IO.File.Exists(delPath))
  199. {
  200. System.IO.File.Delete(delPath);
  201. }
  202. }
  203. path = Path.Combine(path, guid + ext);
  204. _Project.SmallImg.SaveAs(path);
  205. _Project.SmallImgName = guid + ext;
  206. }
  207. XElement X = new XElement("data",
  208. new XElement("items",
  209. _Project.ID == null ? null : new XElement("id", _Project.ID),
  210. _Project.Slug == null ? null : new XElement("slug", _Project.Slug),
  211. _Project.SmallImgName == null ? null : new XElement("small_img", _Project.SmallImgName),
  212. _Project.Title == null ? null : new XElement("title", _Project.Title),
  213. _Project.SmallText == null ? null : new XElement("small_text", _Project.SmallText),
  214. _Project.Summary == null ? null : new XElement("summary", _Project.Summary),
  215. _Project.Credits == null ? null : new XElement("credits", _Project.Credits),
  216. _Project.BackgroundColor == null ? null : new XElement("background_color", _Project.BackgroundColor),
  217. _Project.IsPublished == null ? null : new XElement("is_published", _Project.IsPublished),
  218. _Project.SortIndex == null ? null : new XElement("sort_index", _Project.SortIndex),
  219. _Project.Keywords == null ? null : new XElement("keywords", _Project.Keywords),
  220. _Project.Description == null ? null : new XElement("description", _Project.Description),
  221. new XElement("category",
  222. _Project.ProjectCategories.Select(c => new XElement("category_id", c))
  223. )
  224. )
  225. );
  226. _Project.TX_Projects(0, X);
  227. }
  228. Dictionary _Dictionary = new Dictionary();
  229. ViewBag.CategoryItems = _Dictionary.ListDictionary(1, 1);
  230. ViewBag.CodeVals = _Dictionary.ListDictionary(1, 2);
  231. List<int> EmptyCategories = new List<int>();
  232. ViewBag.SelectedCategoryItems = _Project.ProjectCategories ?? EmptyCategories;
  233. if (_Project.IsError)
  234. {
  235. ViewBag.TSP_Error = Resources.Fail;
  236. }
  237. return View(_Project);
  238. }
  239. [HttpPost]
  240. public JsonResult EditPicture(HttpPostedFileBase File)
  241. {
  242. return Json( "http://localhost:1325/Areas/Administration/Content/images/1/img2.jpg" );
  243. }
  244. [HttpPost]
  245. public ActionResult AddPicture(ProjectAssets _ProjectAsset)
  246. {
  247. var allow = new string[] { ".jpg", ".gif", ".png" };
  248. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  249. var guid = Guid.NewGuid().ToString();
  250. Dictionary _Dictionary = new Dictionary();
  251. if (_ProjectAsset.imgFile != null)
  252. {
  253. string ext = Path.GetExtension(_ProjectAsset.imgFile.FileName).ToLower();
  254. if (!allow.Contains(ext))
  255. {
  256. return Json(new { errorMsg = "unsupportied file extension: " + ext });
  257. }
  258. else
  259. {
  260. path = Path.Combine(path, guid + ext);
  261. _ProjectAsset.imgFile.SaveAs(path);
  262. _ProjectAsset.Media = guid + ext;
  263. _ProjectAsset.TSP_ProjectAssets(0, _ProjectAsset.ID, _ProjectAsset.ProjectID, _Dictionary.GetDictionaryIDByCodeVal(2, _ProjectAsset.CodeVal), _ProjectAsset.GetNewPosition(_ProjectAsset.ProjectID), null, null, _ProjectAsset.Media);
  264. if (_ProjectAsset.IsError)
  265. {
  266. return Json(new { errorMsg = Resources.Fail });
  267. }
  268. return PartialView("_ListProjectDetailsPicture", new ProjectAssets { Media = _ProjectAsset.Media, ID = _ProjectAsset.ID });
  269. }
  270. }
  271. return Json(new {errorMsg = "There was some problem with file uploading !"});
  272. }
  273. [HttpPost]
  274. public JsonResult Sort(int[] ID, int[] Position, int ProjectID)
  275. {
  276. var _ProjectAsset = new ProjectAssets();
  277. for (int k = 0; k < ID.Length; k++)
  278. {
  279. _ProjectAsset.TSP_ProjectAssets(1, ID[k], ProjectID, null, Position[k], null, null, null);
  280. if (_ProjectAsset.IsError)
  281. {
  282. return Json(new { errorMsg = Resources.Fail });
  283. }
  284. }
  285. return null;
  286. }
  287. [HttpPost]
  288. [ValidateInput(false)]
  289. public ActionResult AddVideo(ProjectAssets _ProjectAsset)
  290. {
  291. Dictionary _Dictionary = new Dictionary();
  292. _ProjectAsset.TSP_ProjectAssets(0, _ProjectAsset.ID, _ProjectAsset.ProjectID, _Dictionary.GetDictionaryIDByCodeVal(2, _ProjectAsset.CodeVal), _ProjectAsset.GetNewPosition(_ProjectAsset.ProjectID), null, null, _ProjectAsset.Media);
  293. if (_ProjectAsset.IsError)
  294. {
  295. return Json(new { errorMsg = Resources.Fail });
  296. }
  297. return PartialView("_ListProjectDetailsVideo", new ProjectAssets { ID = _ProjectAsset.ID, Position = _ProjectAsset.Position, Media = _ProjectAsset.Media });
  298. }
  299. [HttpPost]
  300. [ValidateInput(false)]
  301. public ActionResult EditVideo(ProjectAssets _ProjectAsset)
  302. {
  303. _ProjectAsset.TSP_ProjectAssets(1, _ProjectAsset.ID, null, null, null, null, null, WebUtility.HtmlDecode(_ProjectAsset.Media));
  304. if (_ProjectAsset.IsError)
  305. {
  306. return Json(new { errorMsg = Resources.Fail });
  307. }
  308. return PartialView("_ListProjectDetailsVideo", new ProjectAssets { ID = _ProjectAsset.ID, Position = _ProjectAsset.Position, Media = _ProjectAsset.Media });
  309. }
  310. [HttpPost]
  311. [ValidateInput(false)]
  312. public ActionResult AddText(ProjectAssets _ProjectAsset)
  313. {
  314. Dictionary _Dictionary = new Dictionary();
  315. _ProjectAsset.TSP_ProjectAssets(0, _ProjectAsset.ID, _ProjectAsset.ProjectID, _Dictionary.GetDictionaryIDByCodeVal(2, _ProjectAsset.CodeVal), _ProjectAsset.GetNewPosition(_ProjectAsset.ProjectID), _ProjectAsset.Text, null, null);
  316. if (_ProjectAsset.IsError)
  317. {
  318. return Json(new { errorMsg = Resources.Fail });
  319. }
  320. return PartialView("_ListProjectDetailsText", new ProjectAssets { Text = _ProjectAsset.Text, ID = _ProjectAsset.ID });
  321. }
  322. [HttpPost]
  323. [ValidateInput(false)]
  324. public ActionResult EditText(ProjectAssets _ProjectAsset)
  325. {
  326. _ProjectAsset.TSP_ProjectAssets(1, _ProjectAsset.ID, null, null, _ProjectAsset.Position, WebUtility.HtmlDecode(_ProjectAsset.Text), null, null);
  327. if (_ProjectAsset.IsError)
  328. {
  329. return Json(new { errorMsg = Resources.Fail });
  330. }
  331. return PartialView("_ListProjectDetailsText", new ProjectAssets { Text = _ProjectAsset.Text, ID = _ProjectAsset.ID, Position = _ProjectAsset.Position });
  332. }
  333. [HttpPost]
  334. public JsonResult Delete(ProjectAssets _ProjectAsset)
  335. {
  336. var _Media = _ProjectAsset.ListProjectAssets(_ProjectAsset.ProjectID).Where(p => p.CodeVal == 2).Select(x => x.Media).FirstOrDefault();
  337. if (_Media != null)
  338. {
  339. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  340. string delPath = path + "/" + _Media.ToString();
  341. if (System.IO.File.Exists(delPath))
  342. {
  343. System.IO.File.Delete(delPath);
  344. }
  345. }
  346. _ProjectAsset.TSP_ProjectAssets(2, _ProjectAsset.ID, null, null, null, null, null, null);
  347. if (_ProjectAsset.IsError)
  348. {
  349. return Json(new { errorMsg = Resources.Fail });
  350. }
  351. return Json(new { ID = _ProjectAsset.ID });
  352. }
  353. //========================= People ============================//
  354. public ActionResult ListPeoples()
  355. {
  356. People _People = new People();
  357. var model = _People.ListPeoples(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  358. return View(model);
  359. }
  360. [HttpPost]
  361. public ActionResult ListPeopleEdit()
  362. {
  363. People _People = new People();
  364. var model = _People.ListPeoples(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  365. return PartialView("ListPeoples", model);
  366. }
  367. [HttpPost]
  368. public ActionResult ListPeopleUpdate(People _People)
  369. {
  370. if (ModelState.IsValid)
  371. {
  372. _People.TSP_Peoples(1, _People.ID, _People.PeopleName, _People.Position, _People.ImgName,
  373. _People.MusicName, _People.SortIndex, _People.Theme, _People.IsPublished, _People.Slug);
  374. }
  375. else
  376. {
  377. ViewData["EditError"] = "Please, correct all errors.";
  378. }
  379. var model = _People.ListPeoples(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  380. if(_People.IsError)
  381. {
  382. ViewData["EditError"] = Resources.Fail;
  383. }
  384. return PartialView("ListPeoples", model);
  385. }
  386. [HttpPost]
  387. public ActionResult ListPeopleAdd(People _People)
  388. {
  389. if (ModelState.IsValid)
  390. {
  391. _People.TSP_Peoples(0, _People.ID, _People.PeopleName, _People.Position, _People.ImgName,
  392. _People.MusicName, _People.SortIndex, _People.Theme, _People.IsPublished, _People.Slug);
  393. }
  394. else
  395. {
  396. ViewData["EditError"] = "Please, correct all errors.";
  397. }
  398. var model = _People.ListPeoples(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  399. if (_People.IsError)
  400. {
  401. ViewData["EditError"] = Resources.Fail;
  402. }
  403. return PartialView("ListPeoples", model);
  404. }
  405. [HttpPost]
  406. public ActionResult ListPeopleDelete(People _People)
  407. {
  408. if (_People.ID > 0)
  409. {
  410. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  411. var imgName = _People.ListPeoples(null).Where(p => p.ID == _People.ID).Select(p => p.ImgName).FirstOrDefault();
  412. string delPath = path + "/" + imgName;
  413. if (System.IO.File.Exists(delPath))
  414. {
  415. System.IO.File.Delete(delPath);
  416. }
  417. var musicName = _People.ListPeoples(null).Where(p => p.ID == _People.ID).Select(p => p.MusicName).FirstOrDefault();
  418. delPath = path + "/" + musicName;
  419. if (System.IO.File.Exists(delPath))
  420. {
  421. System.IO.File.Delete(delPath);
  422. }
  423. _People.TSP_Peoples(2, _People.ID, null, null, null, null, null, null, null, null);
  424. }
  425. var model = _People.ListPeoples(null).OrderBy(p => p.SortIndex).OrderByDescending(p => p.CRTime).ToList();
  426. if (_People.IsError)
  427. {
  428. ViewData["EditError"] = Resources.Fail;
  429. }
  430. return PartialView("ListPeoples", model);
  431. }
  432. [HttpGet]
  433. public ActionResult ListPeopleDetails(int? ID)
  434. {
  435. if(ID == null)
  436. {
  437. return RedirectToAction("ListPeoples", "Admin");
  438. }
  439. else if (ID > 0)
  440. {
  441. People _People = new People();
  442. _People = _People.ListPeoples(null).SingleOrDefault(p => p.ID == ID);
  443. if(_People == null)
  444. return HttpNotFound();
  445. Dictionary _Dictionary = new Dictionary();
  446. ViewBag.CategoryItems = _Dictionary.ListDictionary(1, 3);
  447. PeopleCategory _PeopleCategory = new PeopleCategory();
  448. ViewBag.SelectedCategoryItems = _PeopleCategory.ListPeopleCategories(_People.ID.Value).Select(p => p.CategoryID.Value).ToList();
  449. return View(_People);
  450. }
  451. return HttpNotFound();
  452. }
  453. [HttpPost]
  454. public ActionResult ListPeopleDetails(People _People)
  455. {
  456. var AllowIMG = new string[] { ".jpg", ".gif", ".png" };
  457. var AllowMusic = new string[] { ".mp3" };
  458. if (_People.ImgFile == null && _People.ImgName == null)
  459. {
  460. ModelState.AddModelError("ImgFile", "Image is required");
  461. }
  462. if (_People.MusicFile == null && _People.MusicName == null)
  463. {
  464. ModelState.AddModelError("MusicFile", "Music is required");
  465. }
  466. if (_People.Theme == null)
  467. {
  468. ModelState.AddModelError("Theme", "Theme is required");
  469. }
  470. if (_People.PeopleCategories == null)
  471. {
  472. ModelState.AddModelError("PeopleCategories", "Category is required");
  473. }
  474. if (_People.ImgFile != null)
  475. {
  476. string ext = Path.GetExtension(_People.ImgFile.FileName).ToLower();
  477. if (!AllowIMG.Contains(ext))
  478. {
  479. ModelState.AddModelError("ImgFile", "Unsupportied file extension: " + ext);
  480. }
  481. else if (_People.ImgFile.ContentLength > 2 * 1024 * 1024 * 8)
  482. {
  483. ModelState.AddModelError("ImgFile", "Size exceeds 2mb");
  484. }
  485. }
  486. if (_People.MusicFile != null)
  487. {
  488. string ext = Path.GetExtension(_People.MusicFile.FileName).ToLower();
  489. if (!AllowMusic.Contains(ext))
  490. {
  491. ModelState.AddModelError("MusicFile", "Unsupportied file extension: " + ext);
  492. }
  493. else if (_People.MusicFile.ContentLength > 10 * 1024 * 1024 * 8) // 10mb
  494. {
  495. ModelState.AddModelError("MusicFile", "Size exceeds 10mb");
  496. }
  497. }
  498. if(ModelState.IsValid)
  499. {
  500. //Upload Picture
  501. if (_People.ImgFile != null)
  502. {
  503. var guid = Guid.NewGuid().ToString();
  504. string ext = Path.GetExtension(_People.ImgFile.FileName).ToLower();
  505. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  506. if (_People.ImgName != null)
  507. {
  508. string delPath = path + "/" + _People.ImgName;
  509. if (System.IO.File.Exists(delPath))
  510. {
  511. System.IO.File.Delete(delPath);
  512. }
  513. }
  514. path = Path.Combine(path, guid + ext);
  515. _People.ImgFile.SaveAs(path);
  516. _People.ImgName = guid + ext;
  517. }
  518. //Upload Music
  519. if (_People.MusicFile != null)
  520. {
  521. var guid = Guid.NewGuid().ToString();
  522. string ext = Path.GetExtension(_People.MusicFile.FileName).ToLower();
  523. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  524. if (_People.MusicName != null)
  525. {
  526. string delPath = path + "/" + _People.MusicName;
  527. if (System.IO.File.Exists(delPath))
  528. {
  529. System.IO.File.Delete(delPath);
  530. }
  531. }
  532. path = Path.Combine(path, guid + ext);
  533. _People.MusicFile.SaveAs(path);
  534. _People.MusicName = guid + ext;
  535. }
  536. XElement X = new XElement("data",
  537. new XElement("items",
  538. _People.ID == null ? null : new XElement("id", _People.ID),
  539. _People.PeopleName == null ? null : new XElement("people_name", _People.PeopleName),
  540. _People.Position == null ? null : new XElement("position", _People.Position),
  541. _People.ImgName == null ? null : new XElement("img_name", _People.ImgName),
  542. _People.MusicName == null ? null : new XElement("music_name", _People.MusicName),
  543. _People.SortIndex == null ? null : new XElement("sort_index", _People.SortIndex),
  544. _People.Theme == null ? null : new XElement("theme", _People.Theme),
  545. _People.IsPublished == null ? null : new XElement("is_published", _People.IsPublished),
  546. _People.Slug == null ? null : new XElement("slug", _People.Slug),
  547. new XElement("category",
  548. _People.PeopleCategories.Select(c => new XElement("category_id", c))
  549. )
  550. )
  551. );
  552. _People.TX_Peoples(0, X);
  553. }
  554. Dictionary _Dictionary = new Dictionary();
  555. ViewBag.CategoryItems = _Dictionary.ListDictionary(1, 3);
  556. PeopleCategory _PeopleCategory = new PeopleCategory();
  557. List<int> EmptyCategories = new List<int>();
  558. ViewBag.SelectedCategoryItems = _People.PeopleCategories != null ? _People.PeopleCategories : EmptyCategories;
  559. if(_People.IsError)
  560. {
  561. ViewBag.TSP_Error = Resources.Fail;
  562. }
  563. return View(_People);
  564. }
  565. //=====================================refactor==============================
  566. public ActionResult ClientLogos()
  567. {
  568. var model = new Client().ListClients();
  569. return View(model);
  570. }
  571. [HttpGet]
  572. public ActionResult AddClientLogo()
  573. {
  574. var model = new Client();
  575. return View(model);
  576. }
  577. [HttpPost]
  578. public ActionResult AddClientLogo(Client Client)
  579. {
  580. if (Client.LogoFile == null)
  581. {
  582. ModelState.AddModelError("LogoFile", "Image is required");
  583. }
  584. if (Client.LogoFile != null)
  585. {
  586. var allow = new string[] { ".jpg", ".gif", ".png" };
  587. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  588. var guid = Guid.NewGuid().ToString();
  589. string ext = Path.GetExtension(Client.LogoFile.FileName).ToLower();
  590. if (!allow.Contains(ext))
  591. {
  592. ModelState.AddModelError("LogoFile", "Unsupportied file extension: " + ext);
  593. }
  594. else if (Client.LogoFile.ContentLength > 10 * 1024 * 1024 * 8)
  595. {
  596. ModelState.AddModelError("LogoFile", "Size exceeds 10mb");
  597. }
  598. else
  599. {
  600. path = Path.Combine(path, guid + ext);
  601. Client.LogoFile.SaveAs(path);
  602. Client.LogoName = guid + ext;
  603. }
  604. }
  605. if(ModelState.IsValid)
  606. {
  607. Client.TSP_Clients(0, Client.ID, Client.LogoName, null, Client.Link);
  608. if (Client.IsError)
  609. {
  610. ViewBag.TSP_Error = Resources.Fail;
  611. return View(Client);
  612. }
  613. return RedirectToAction("ClientLogos", "Admin");
  614. }
  615. return View(Client);
  616. }
  617. [HttpGet]
  618. public ActionResult EditClientLogo(int? ID)
  619. {
  620. if(ID == null)
  621. {
  622. return RedirectToAction("ClientLogos", "Admin");
  623. }
  624. else if (ID > 0)
  625. {
  626. var model = new Client().ListClients().FirstOrDefault(c => c.ID == ID);
  627. if(model == null)
  628. return HttpNotFound();
  629. return View(model);
  630. }
  631. return HttpNotFound();
  632. }
  633. [HttpPost]
  634. public ActionResult EditClientLogo(Client Client)
  635. {
  636. if (Client.LogoFile == null && Client.LogoName == null)
  637. {
  638. ModelState.AddModelError("LogoFile", "Image is required");
  639. }
  640. if (Client.LogoFile != null)
  641. {
  642. var allow = new string[] { ".jpg", ".gif", ".png" };
  643. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  644. var guid = Guid.NewGuid().ToString();
  645. string ext = Path.GetExtension(Client.LogoFile.FileName).ToLower();
  646. if (!allow.Contains(ext))
  647. {
  648. ModelState.AddModelError("LogoFile", "Unsupportied file extension: " + ext);
  649. }
  650. else if (Client.LogoFile.ContentLength > 10 * 1024 * 1024 * 8)
  651. {
  652. ModelState.AddModelError("LogoFile", "Size exceeds 10mb");
  653. }
  654. else
  655. {
  656. if (Client.LogoName != null)
  657. {
  658. string delPath = path + "/" + Client.LogoName;
  659. if (System.IO.File.Exists(delPath))
  660. {
  661. System.IO.File.Delete(delPath);
  662. }
  663. }
  664. path = Path.Combine(path, guid + ext);
  665. Client.LogoFile.SaveAs(path);
  666. Client.LogoName = guid + ext;
  667. }
  668. }
  669. if (ModelState.IsValid)
  670. {
  671. Client.TSP_Clients(1, Client.ID, Client.LogoName, null, Client.Link);
  672. if (Client.IsError)
  673. {
  674. ViewBag.TSP_Error = Resources.Fail;
  675. return View(Client);
  676. }
  677. }
  678. return View(Client);
  679. }
  680. public ActionResult DeleteClientLogo(int ID)
  681. {
  682. Client Client = new Client().ListClients().FirstOrDefault(c => c.ID == ID);
  683. var path = Server.MapPath(WebConfigurationManager.AppSettings["UploadPath"]);
  684. string delPath = path + "/" + Client.LogoName;
  685. if (System.IO.File.Exists(delPath))
  686. {
  687. System.IO.File.Delete(delPath);
  688. }
  689. Client.TSP_Clients(2, ID, null, null, null);
  690. if (Client.IsError)
  691. {
  692. TempData["TSP_Error"] = Resources.Fail;
  693. }
  694. return RedirectToAction("ClientLogos", "Admin");
  695. }
  696. //=======================================================================================
  697. }
  698. }