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