public function index()
 {
     $this->wf->ProfileIn(__METHOD__);
     $this->wg->Out->setPageTitle(wfMsg('managewikiahome'));
     if (!$this->checkAccess()) {
         $this->forward('ManageWikiaHome', 'onWrongRights');
         return false;
     }
     $this->setVal('slotsInTotal', WikiaHomePageHelper::SLOTS_IN_TOTAL);
     //wikis with visualization selectbox
     $visualizationLang = $this->wg->request->getVal(self::WHST_VISUALIZATION_LANG_VAR_NAME, $this->wg->contLang->getCode());
     $visualizationLang = substr(strtolower($visualizationLang), 0, 2);
     $this->visualizationLang = $visualizationLang;
     $this->visualizationWikisData = $this->helper->getVisualizationWikisData();
     $this->currentPage = $this->request->getVal('page', 1);
     $this->corpWikiId = $this->visualizationWikisData[$this->visualizationLang]['wikiId'];
     //verticals slots' configuration
     /* @var $this->helper WikiaHomePageHelper */
     $videoGamesAmount = $this->request->getVal('video-games-amount', $this->helper->getNumberOfVideoGamesSlots($this->visualizationLang));
     $entertainmentAmount = $this->request->getVal('entertainment-amount', $this->helper->getNumberOfEntertainmentSlots($this->visualizationLang));
     $lifestyleAmount = $this->request->getVal('lifestyle-amount', $this->helper->getNumberOfLifestyleSlots($this->visualizationLang));
     $hotWikisAmount = $this->request->getVal('hot-wikis-amount', $this->helper->getNumberOfHotWikiSlots($this->visualizationLang));
     $newWikisAmount = $this->request->getVal('new-wikis-amount', $this->helper->getNumberOfNewWikiSlots($this->visualizationLang));
     if ($this->request->wasPosted()) {
         //todo: separate post request handling
         //todo: move validation from saveSlotsConfigInWikiFactory() to helper
         $total = intval($videoGamesAmount) + intval($entertainmentAmount) + intval($lifestyleAmount);
         if ($total !== WikiaHomePageHelper::SLOTS_IN_TOTAL) {
             $this->setVal('errorMsg', wfMsg('manage-wikia-home-error-invalid-total-no-of-slots', array($total, WikiaHomePageHelper::SLOTS_IN_TOTAL)));
         } elseif ($this->isAnySlotNumberNegative($videoGamesAmount, $entertainmentAmount, $lifestyleAmount)) {
             $this->setVal('errorMsg', wfMsg('manage-wikia-home-error-negative-slots-number-not-allowed'));
         } else {
             $this->saveSlotsConfigInWikiFactory($this->corpWikiId, $visualizationLang, array(WikiaHomePageHelper::VIDEO_GAMES_SLOTS_VAR_NAME => $videoGamesAmount, WikiaHomePageHelper::ENTERTAINMENT_SLOTS_VAR_NAME => $entertainmentAmount, WikiaHomePageHelper::LIFESTYLE_SLOTS_VAR_NAME => $lifestyleAmount));
         }
     }
     $this->setVal('videoGamesAmount', $videoGamesAmount);
     $this->setVal('entertainmentAmount', $entertainmentAmount);
     $this->setVal('lifestyleAmount', $lifestyleAmount);
     $this->setVal('hotWikisAmount', $hotWikisAmount);
     $this->setVal('newWikisAmount', $newWikisAmount);
     $this->response->addAsset('/extensions/wikia/SpecialManageWikiaHome/css/ManageWikiaHome.scss');
     $this->response->addAsset('/extensions/wikia/SpecialManageWikiaHome/js/ManageWikiaHome.js');
     F::build('JSMessages')->enqueuePackage('ManageWikiaHome', JSMessages::EXTERNAL);
     $this->wf->ProfileOut(__METHOD__);
 }
 public function index()
 {
     wfProfileIn(__METHOD__);
     $this->wg->Out->setPageTitle(wfMsg('managewikiahome'));
     if (!$this->checkAccess()) {
         wfProfileOut(__METHOD__);
         $this->forward('ManageWikiaHome', 'onWrongRights');
         return false;
     }
     $this->setVal('slotsInTotal', WikiaHomePageHelper::SLOTS_IN_TOTAL);
     $this->infoMsg = FlashMessages::pop();
     //wikis with visualization selectbox
     $visualizationLang = $this->wg->request->getVal(self::WHST_VISUALIZATION_LANG_VAR_NAME, $this->wg->contLang->getCode());
     $visualizationLang = substr(strtolower($visualizationLang), 0, 2);
     $this->visualizationLang = $visualizationLang;
     $this->visualizationWikisData = $this->helper->getVisualizationWikisData();
     $this->currentPage = $this->request->getVal('page', 1);
     $this->corpWikiId = $this->visualizationWikisData[$this->visualizationLang]['wikiId'];
     $this->filterOptions = array_merge($this->initFilterOptions(), $this->request->getParams());
     $this->exportListAsCSVUri = $this->getExportListAsCSVUri();
     //verticals slots' configuration
     /* @var $this->helper WikiaHomePageHelper */
     $videoGamesAmount = $this->request->getVal('video-games-amount', $this->helper->getNumberOfVideoGamesSlots($this->visualizationLang));
     $entertainmentAmount = $this->request->getVal('entertainment-amount', $this->helper->getNumberOfEntertainmentSlots($this->visualizationLang));
     $lifestyleAmount = $this->request->getVal('lifestyle-amount', $this->helper->getNumberOfLifestyleSlots($this->visualizationLang));
     $this->form = new CollectionsForm();
     $this->statsForm = new StatsForm();
     $this->hubsForm = new HubsSlotsForm();
     $savedSlotsValues = $this->helper->getHubSlotsFromWF($this->corpWikiId, $visualizationLang);
     $homePageSlotsValues = $this->prepareSlots($savedSlotsValues);
     $collectionsModel = $this->getWikiaCollectionsModel();
     $this->collectionsList = $collectionsModel->getList($this->visualizationLang);
     $collectionValues = $this->prepareArrayFieldsToShow($this->collectionsList);
     $wikisPerCollection = $this->getWikisPerCollection($this->collectionsList);
     $statsValues = $this->app->sendRequest('WikiaStatsController', 'getWikiaStatsFromWF')->getData();
     if ($this->request->wasPosted()) {
         if ($this->request->getVal('wikis-in-slots', false)) {
             //todo: separate post request handling
             //todo: move validation from saveSlotsConfigInWikiFactory() to helper
             $total = intval($videoGamesAmount) + intval($entertainmentAmount) + intval($lifestyleAmount);
             if ($total !== WikiaHomePageHelper::SLOTS_IN_TOTAL) {
                 $this->errorMsg = wfMessage('manage-wikia-home-error-invalid-total-no-of-slots')->params(array($total, WikiaHomePageHelper::SLOTS_IN_TOTAL))->text();
             } elseif ($this->isAnySlotNumberNegative($videoGamesAmount, $entertainmentAmount, $lifestyleAmount)) {
                 $this->errorMsg = wfMessage('manage-wikia-home-error-negative-slots-number-not-allowed')->text();
             } else {
                 $this->saveSlotsConfigInWikiFactory($this->corpWikiId, $visualizationLang, array(WikiaHomePageHelper::VIDEO_GAMES_SLOTS_VAR_NAME => $videoGamesAmount, WikiaHomePageHelper::ENTERTAINMENT_SLOTS_VAR_NAME => $entertainmentAmount, WikiaHomePageHelper::LIFESTYLE_SLOTS_VAR_NAME => $lifestyleAmount));
             }
         } elseif ($this->request->getVal('collections', false)) {
             $collectionValues = $this->request->getParams();
             $collectionValues = $this->form->filterData($collectionValues);
             $isValid = $this->form->validate($collectionValues);
             if ($isValid) {
                 $collectionSavedValues = $this->prepareArrayFieldsForSave($collectionValues);
                 $collectionsModel->saveAll($this->visualizationLang, $collectionSavedValues);
                 FlashMessages::put(wfMessage('manage-wikia-home-collections-success')->text());
                 $this->response->redirect($_SERVER['REQUEST_URI']);
             } else {
                 $this->errorMsg = wfMessage('manage-wikia-home-collections-failure')->text();
             }
         } elseif ($this->request->getVal('stats', false)) {
             $statsValues = $this->request->getParams();
             $statsValues = $this->statsForm->filterData($statsValues);
             $isValid = $this->statsForm->validate($statsValues);
             if ($isValid) {
                 $isAllowed = true;
                 try {
                     $this->app->sendRequest('WikiaStatsController', 'saveWikiaStatsInWF', array('statsValues' => $statsValues));
                 } catch (PermissionsException $ex) {
                     $isAllowed = false;
                 }
                 if ($isAllowed) {
                     FlashMessages::put(wfMessage('manage-wikia-home-stats-success')->text());
                     $this->response->redirect($_SERVER['REQUEST_URI']);
                 } else {
                     $this->errorMsg = wfMessage('manage-wikia-home-stats-permissions-error')->text();
                 }
             } else {
                 $this->errorMsg = wfMessage('manage-wikia-home-stats-failure')->text();
             }
         } elseif ($this->request->getVal('hubs-slots', false)) {
             $homePageSlotsValues = $this->request->getParams();
             $homePageSlotsValues = $this->hubsForm->filterData($homePageSlotsValues);
             $homePageSlotsValues = $this->filterUrls($homePageSlotsValues);
             $hubSavedSlotsValues = $homePageSlotsValues['hub_slot'];
             $marketingSlotsValues = $this->getMarketingSlotsValues($homePageSlotsValues);
             $marketingSavedSlotsValues = $this->prepareArrayFieldsForSave($marketingSlotsValues);
             $isValid = $this->hubsForm->validate($homePageSlotsValues);
             $isValid &= $this->validateMarketingSlots($marketingSavedSlotsValues);
             if ($isValid) {
                 $savedSlotsValues = ['hub_slot' => $hubSavedSlotsValues, 'marketing_slot' => $marketingSavedSlotsValues];
                 $status = $this->helper->saveHubSlotsToWF($savedSlotsValues, $this->corpWikiId, $this->visualizationLang, 'wgWikiaHomePageHubsSlotsV2');
                 if ($status) {
                     $this->infoMsg = wfMessage('manage-wikia-home-hubs-slot-success')->text();
                 } else {
                     $this->errorMsg = wfMessage('manage-wikia-home-hubs-slot-error')->text();
                 }
             }
         }
     }
     $this->form->setFieldsValues($collectionValues);
     $this->statsForm->setFieldsValues($statsValues);
     $this->verticals = $this->helper->getWikiVerticals();
     $this->marketingImages = [];
     if (array_key_exists('marketing_slots', $savedSlotsValues)) {
         $this->marketingImages = $this->prepareMarketingSlotImages($savedSlotsValues['marketing_slot']);
     }
     $this->prepareHubsForm($homePageSlotsValues);
     $this->setVal('videoGamesAmount', $videoGamesAmount);
     $this->setVal('entertainmentAmount', $entertainmentAmount);
     $this->setVal('lifestyleAmount', $lifestyleAmount);
     $this->setVal('wikisPerCollection', $wikisPerCollection);
     $this->response->addAsset('/extensions/wikia/SpecialManageWikiaHome/css/ManageWikiaHome.scss');
     $this->response->addAsset('manage_wikia_home_js');
     JSMessages::enqueuePackage('ManageWikiaHome', JSMessages::EXTERNAL);
     $this->wg->Out->addJsConfigVars(['wgWikisPerCollection' => $wikisPerCollection, 'wgSlotsInTotal' => WikiaHomePageHelper::SLOTS_IN_TOTAL]);
     wfProfileOut(__METHOD__);
 }