コード例 #1
0
ファイル: Client.php プロジェクト: photonparticle/dress
 private function loadProduct($id)
 {
     $response = [];
     $product = Model_Main::getProducts($id);
     //Breadcrumbs
     $response['all_categories'] = $this->categories['all'];
     if (!empty($product[$id]['main_category']) && $this->categories['all'][$product[$id]['main_category']]) {
         $response['breadcrumbs'] = self::generateCategoryBreadcrumbs($this->categories['all'][$product[$id]['main_category']]);
     }
     //Prepare product for response
     if (!empty($product[$id])) {
         $response['product'] = $product[$id];
         if (!empty($response['product']['page_title'])) {
             View::share('page_title', $response['product']['page_title']);
         } else {
             if (!empty($response['product']['title'])) {
                 View::share('page_title', $response['product']['title']);
             }
         }
         if (!empty($response['product']['meta_description'])) {
             View::share('page_meta_description', $response['product']['meta_description']);
         }
         if (!empty($response['product']['meta_keywords'])) {
             View::share('page_meta_keywords', $response['product']['meta_keywords']);
         }
         if (!empty($response['product']['discount_price'])) {
             //Calculate is discount active
             $now = time();
             if ($response['product']['discount_start'] == '0000.00.00 00:00:00' || strtotime($response['product']['discount_start']) <= $now) {
                 $allow_start = TRUE;
             } else {
                 $allow_start = FALSE;
             }
             if ($response['product']['discount_end'] == '0000.00.00 00:00:00' || strtotime($response['product']['discount_end']) <= $now) {
                 $allow_end = TRUE;
             } else {
                 $allow_end = FALSE;
             }
             if ($allow_start === TRUE && $allow_end === TRUE) {
                 $response['product']['active_discount'] = TRUE;
             }
         }
         if (!empty($response['product']['sizes']) && is_array($response['product']['sizes'] = json_decode($response['product']['sizes'], TRUE))) {
             foreach ($response['product']['sizes'] as $key => $size) {
                 if (empty($size['name']) || empty($size['quantity'])) {
                     if (isset($response['product']['sizes'][$key])) {
                         unset($response['product']['sizes'][$key]);
                     }
                 }
             }
         }
     }
     //Product discount percentage
     if (!empty($response['product']['active_discount'])) {
         $response['product']['discount'] = intval((floatval($response['product']['price']) - floatval($response['product']['discount_price'])) / floatval($response['product']['price']) * 100);
     }
     //Images
     if (!empty($response['product']['images'])) {
         $response['product']['images'] = json_decode($response['product']['images'], TRUE);
         if (is_array($response['product']['images'])) {
             $response['product_thumbs_path'] = Config::get('system_settings.product_public_path') . $id . '/' . Config::get('images.sm_icon_size') . '/';
             $response['images_path'] = Config::get('system_settings.product_public_path') . $id . '/' . Config::get('images.full_size') . '/';
             $response['md_path'] = Config::get('system_settings.product_public_path') . $id . '/' . Config::get('images.md_icon_size') . '/';
             uasort($response['product']['images'], function ($a, $b) {
                 if ($a == $b) {
                     return 0;
                 }
                 return $a < $b ? -1 : 1;
             });
         }
     }
     //Tags
     $response['product']['tags'] = Model_Main::getTags($id);
     //Manufacturer
     $response['product']['manufacturer'] = Model_Main::getManufacturer($id);
     //Material
     $response['product']['material'] = Model_Main::getMaterial($id);
     if (!empty($response['product']['material'][0])) {
         $response['product']['material'] = $response['product']['material'][0];
     }
     //Color
     $response['product']['related_colors'] = Model_Main::getColor($id);
     if (!empty($response['product']['related_colors'])) {
         $response['product']['related_colors'] = implode(', ', $response['product']['related_colors']);
     }
     //Related products
     if (!empty($response['product']['related_products']) && is_array(json_decode($response['product']['related_products'], TRUE))) {
         $response['carousel']['products'] = json_decode($response['product']['related_products'], TRUE);
     } else {
         if (!empty($response['product']['main_category'])) {
             $response['carousel']['products'] = Model_Main::getSimilarProducts($response['product']['main_category']);
         }
     }
     if (!empty($response['carousel']['products'])) {
         $response['products'] = $response['carousel']['products'];
     }
     //Get upcoming product
     $response['upcoming'] = Model_Client::getUpcomingProduct();
     if (!empty($response['upcoming']['product_id'])) {
         $response['products'][] = $response['upcoming']['product_id'];
         if (!empty($response['upcoming']['date'])) {
             $response['upcoming']['date'] = date('Y/m/d', strtotime($response['upcoming']['date']));
         }
     }
     $recent = Model_Main::getNewestProducts(3, $id, TRUE);
     if (!empty($recent)) {
         $response['recent'] = $recent;
         array_merge($response['products'], $recent);
     }
     $response['icon_size'] = Config::get('images.sm_icon_size');
     $response['thumbs_path'] = Config::get('system_settings.product_public_path');
     $response['carousel']['title'] = trans('client.similar_products');
     // Get products data
     $response['products'] = Model_Main::getProducts($response['products'], ['title', 'images', 'sizes', 'description']);
     // Send products to response
     $response['products'] = self::prepareProductsForResponse($response['products']);
     //Check dimensions table
     if (!empty($response['product']['dimensions_table'])) {
         $response['product']['dimensions_table'] = trim($response['product']['dimensions_table']);
     }
     return $response;
 }