nelsoft-easyshop / easyshop-admin

Administrator Application for Easyshop.ph
admin.easyshop.ph
0 stars 0 forks source link

CMS problem when a node is empty #330

Closed samgavinio closed 9 years ago

samgavinio commented 9 years ago

This whole part will fail if one of the nodes is empty:

  public function getHomeContent()
    {

        foreach($this->map->categoryNavigation->otherCategories as $map) {
            $otherCategories[] = $map;
        }        

        foreach($this->map->categoryNavigation->category as $map) {
            $categoryNavigation[] = $map;
        }         

        $adminEntity = App::make('AdminMemberRepository');          
        $categoryRepository = App::make('CategoryRepository');          

        foreach ($categoryRepository->getParentCategories() as $value) {
            $categoryLists[] = array("slug" => $value->slug, "name" => $value->name);
        }

        foreach ($categoryRepository->getChildCategories() as $value) {
            $childCategoryLists[] = array("slug" => $value->slug, "name" => $value->name);
        }               

        foreach($this->sliderMap->sliderSection->slide as $slides) {
            $sliders[] = $slides;
        }

        foreach($this->map->adSection as $ads) {
            $adsSection[] = $ads;
        }     

        $productEntity = App::make('ProductRepository');
        foreach($this->map->sellerSection->productPanel as $productPanel)
        {
            $product[] = $productEntity->getProductBySlug($productPanel->slug);   
        }

        $index = 0;
        foreach($this->map->categorySection as $categoryPanel)
        {
            foreach($categoryPanel->productPanel as $productPanel)
            {
                $categoryProductPanel[] = $productEntity->getProductBySlug($productPanel->slug);
            }
            $categorySection[] = $categoryPanel;   
            $categoryProductPanelList[] = array_flatten(array($index => $categoryProductPanel ));
            $index++;
            $categoryProductPanel = array();
        }

        foreach($this->map->menu->newArrivals[0] as $arrivals)
        {
            $newArrivals[] = $arrivals;
        }

        foreach($this->map->menu->topProducts as $tProducts)
        {
            $topProducts[] = $tProducts;
        }   

        foreach($this->map->menu->topSellers as $tSellers)
        {
            $topSellers[] = $tSellers;
        }           
        $brandRepository = App::make("BrandRepository");

        foreach($this->map->brandSection->brandId as $brands) 
        {
            $brandsLists[] = $brandRepository->getBrandById($brands);                  

        } 

        return View::make('pages.cms-newhome')
                    ->with('userid', Auth::id())
                    ->with('allBrandsLists', $brandRepository->getAllBrands())
                    ->with('brandsLists', $brandsLists)
                    ->with('otherCategories', $otherCategories)
                    ->with('categorySection', $categorySection)
                    ->with('categoryLists', $categoryLists)
                    ->with('categoryProductPanelList', $categoryProductPanelList)
                    ->with('productList', array_flatten($product))
                    ->with('childCategoryLists', $childCategoryLists)
                    ->with('templateLists', array_flatten($this->getTemplates($this->sliderMap->sliderTemplate)))
                    ->with('categoryNavigation', $categoryNavigation)
                    ->with('password', $adminEntity->getAdminMemberById(Auth::id()))
                    ->with('sliderSection', $sliders)
                    ->with('topProducts', $topProducts)
                    ->with('topSellers', $topSellers)
                    ->with('newArrivals', $newArrivals)
                    ->with('adSection', array_flatten($adsSection))
                    ->with('newHomeCmsLink', $this->XMLService->getNewHomeCmsLink())
                    ->with('easyShopLink',$this->XMLService->GetEasyShopLink());                                                   
    }

Say for example: $this->map->categoryNavigation->otherCategories is an empty array (not outside the realm of impossibility). In this case otherCategories will not even be declared and hence with('otherCategories', $otherCategories) in the View::make chain will obviously fail. This applies to each of the for loops.

The code is very fragile. Even if just one of the for loops does not iterate at least once then the whole CMS will no longer work. The fix of course is rather simple, define the variables before hand so you're sure that the View::make chain will not fail like so:

$otherCategories = [];
foreach($this->map->categoryNavigation->otherCategories as $map) {
            $otherCategories[] = $map;
 }        
inonbaguio commented 9 years ago

Noted @samgavinio. On the front-end side, all of our delete functions that were integrated in our cms app, have their own counter/constraint to prohibit this case from happening. I'll just apply it on the back-end. Thanks for the look on this. :+1: