public static function populateView()
 {
     $itemsAwaitingApproval = ViralList::where('status', 'awaiting_approval')->count();
     $itemsAwaitingChangesApproval = ViralListChanges::all()->count();
     View::share(array('itemsAwaitingApproval' => $itemsAwaitingApproval + $itemsAwaitingChangesApproval));
 }
 public function approveList()
 {
     $admin = App::make('loggedInAdmin');
     $listId = Input::get('list-id');
     $redirectUrl = Input::get('redirect-url');
     if (!$listId) {
         return 'List ID not passed';
     }
     try {
         $list = ViralList::findOrFail($listId);
         $pendingChanges = ViralListChanges::find($listId);
         if ($pendingChanges) {
             ListController::addListData($pendingChanges->content, $list);
             $pendingChanges->delete();
         }
         $list->markAsApproved();
         try {
             ListController::saveThumbnails($list);
             ListController::saveOgImage($list);
         } catch (Exception $e) {
             return Response::error("Error! List not saved! " . $e->getMessage());
         }
         $list->save();
         \Event::fire('list:approved-by-admin', $list);
         return Redirect::to(Helpers::getUrlWithQuery(array('status' => 'awaiting_approval'), route('adminViewLists')));
     } catch (ModelNotFoundException $e) {
         return Response::error("Error finding list with id " . $listId);
     }
 }
 public function createEdit()
 {
     //Creating or saving a list as draft - or updating an existing list.
     $listId = Input::get('listId', null);
     $listData = Input::get('list', '{}');
     $listData = json_decode($listData, true);
     $duplicateList = Input::get('create-from');
     $user = Auth::user();
     $isNewList = false;
     $validateRules = self::_getListFormRules();
     try {
         if ($duplicateList) {
             $duplicateListObject = ViralList::findOrFail($duplicateList);
             View::share(array('duplicateList' => $duplicateListObject));
         }
         if ($listId || !empty($listData['id'])) {
             //Editing a list
             $editListId = $listId ? $listId : $listData['id'];
             $list = ViralList::findOrFail($editListId);
             $pendingChanges = $list->pendingChanges;
             if ($pendingChanges) {
                 View::share('hasChangesPendingApproval', true);
                 self::addListData($pendingChanges->content, $list);
             }
             $this->_ensurePermission($list);
         } else {
             $list = new ViralList();
             $list->creator_user_id = $user->id;
             if ($duplicateList) {
                 $list->created_from_list_id = $duplicateList;
             }
             $isNewList = true;
         }
     } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
         if (Request::ajax()) {
             return Response::error($e->getMessage());
         } else {
             return Response::notFound();
         }
     } catch (PermissionDeniedException $e) {
         return Response::error($e->getMessage());
     }
     if (Request::ajax() && Request::isMethod('post')) {
         //Form submitted- Create/update the list
         $validator = Validator::make($listData, $validateRules);
         if ($validator->fails()) {
             //Validation failed - Respond with error
             return Response::error(__('someErrorsInInput'));
         }
         //Set status explicitly - Don't allow editor/admin to alter it via listData - Approval option has a separate controller action
         $this->_setApprovalStatusOnSave($list);
         $markedForApprovalAgain = false;
         //If the list is marked for approval again or if there is pending changes for approval already(regardless of current approval status)
         if (!self::isAutoApproveUpdatesEnabled() && $list->isApproved()) {
             $markedForApprovalAgain = true;
             if (!$pendingChanges) {
                 $pendingChanges = new ViralListChanges(['id' => $list->id]);
             }
             $pendingChanges->content = json_encode($listData);
             $pendingChanges->save();
         } else {
             self::addListData($listData, $list);
             try {
                 self::saveThumbnails($list);
                 self::saveOgImage($list);
             } catch (Exception $e) {
                 return Response::error("Error! List not saved! " . $e->getMessage());
             }
         }
         $list->save();
         //Add tags
         $list->retag($listData['tags']);
         return Response::json(array('success' => 1, 'list' => $list, 'markedForApprovalAgain' => $markedForApprovalAgain, 'redirect' => $isNewList ? self::_getJustSavedListEditurl($list) : null));
     } else {
         //Show list editor page
         $populateQuizData = Input::get('listData', '{}');
         $populateQuizData = json_decode($populateQuizData);
         if (!empty($duplicateQuizData)) {
             $populateQuizData = $duplicateQuizData;
         }
         if ($list->exists) {
             View::share('list', $list);
         }
         $justSavedTheNewList = Input::get(self::JUST_SAVED_LIST_PARAM, false);
         View::share('justSavedTheNewList', $justSavedTheNewList);
         return View::make('lists.create');
     }
 }