/**
  * Get the list of available options for use in the categories dropdown menu
  * @return array
  */
 private function getCategoriesDropdownHTML()
 {
     $list = array(0 => 'None (Top Level)');
     foreach ($this->categories->getFlattenedCategories() as $category) {
         $list[$category->id] = $category->getLevelIndicator('-') . ' ' . $category->name;
     }
     return $list;
 }
 /**
  * Edit the product
  * @param       string  $sku    The SKU Of The Product To Edit
  * @access      public
  * @return      View
  */
 public function getEdit($id = null)
 {
     $product = $this->products->with('categories')->with('attributeSet')->find($id);
     // Redirect all requests where the product doesn't exist back to the main products dashboard
     if (!$product) {
         return Redirect::to('manage/products');
     }
     // Get the top level categories only, we nest from the view itself
     $categories = $this->categories->getTopLevel();
     $attribute_sets = [0 => 'None'] + $this->attribute_sets->getAll()->lists('name', 'id');
     $collections = [0 => 'None'] + $this->collections->getAll()->lists('name', 'id');
     // Setup the old data so it's easy to find
     $mainImage = Input::old('mainImage', false);
     if ($mainImage === false and $product->getMainImage()) {
         $mainImage = $product->getMainImage()->id;
     }
     // Setup the old data so it's easy to find
     $thumbnailImage = Input::old('thumbnailImage', false);
     if ($thumbnailImage === false and $product->getThumbnailImage()) {
         $thumbnailImage = $product->getThumbnailImage()->id;
     }
     // We need to render the attribute views that we can edit for the product, lets see if our product actually has attributes first
     $attributeViews = [];
     if ($product->getAvailableAttributes()) {
         // Setup our old data
         $oldData = Input::old('attributes', []);
         // Loop through the attributes
         foreach ($product->getAvailableAttributes() as $attribute) {
             // Get the attribute object so we can access certain methods
             $attr = $this->attributes->find($attribute->id);
             if ($attr) {
                 // Old data should override stored data, lets ensure that happens here
                 $old = array_key_exists($attribute->id, $oldData) ? $oldData[$attribute->id] : null;
                 if ($old === null) {
                     $saved_value = $product->getAttrValue($attribute->id);
                     if (!$saved_value or $saved_value == '') {
                         $value = $attribute->default;
                     } else {
                         $value = $saved_value;
                     }
                 } else {
                     $value = $old;
                 }
                 // Render the resulting view into an array that we eventually render
                 $attributeViews[] = View::make('ProductCatalog::products.partials.attributes.' . $attr->type()->getViewName())->with('attribute', $attr)->with('value', $value);
             }
         }
     }
     // Make the end view, whoop!
     return View::make('ProductCatalog::products.edit')->with('product', $product)->with('attributeViews', $attributeViews)->with('mainImageId', $mainImage)->with('attribute_sets', $attribute_sets)->with('collections', $collections)->with('thumbnailImageId', $thumbnailImage)->with('categories', $categories);
 }
 /**
  * The function that actually goes and recursively collects all products from passed in category and children categories
  * @param  Category $category The category to recursively check
  * @return Array
  */
 private function grabAllProducts(Category $category)
 {
     $child_products = $products = array();
     if ($category->products()->count() > 0) {
         foreach ($category->products as $product) {
             $products[$product->id] = $product;
         }
     }
     if ($category->children()->count() > 0) {
         foreach ($category->children as $child) {
             $child_products[] = $this->grabAllProducts($child);
         }
     }
     return $products + $child_products;
 }