public function indexAction($id)
 {
     $products = new \modules\commerce\models\Commerce_products();
     $categories = new \modules\commerce\models\Commerce_categories();
     $product_imgs = new \modules\commerce\models\Commerce_product_images();
     $product_imgs->commerce_product_id = $id;
     $info = $this->Database->query("SELECT commerce_products.*,`commerce_categories`.`title`,`commerce_categories`.`description` as category_desc, `commerce_brands`.`name` as brand, `commerce_brands`.`image` as brand_iamge " . "FROM `commerce_products` " . "JOIN `commerce_categories` ON `commerce_categories`.`commerce_category_id`=`commerce_products`.`commerce_category_id`" . "LEFT JOIN `commerce_brands` ON `commerce_brands`.`commerce_brand_id`=`commerce_products`.`commerce_brand_id`" . "WHERE commerce_products.commerce_product_id='{$id}'")->row();
     $main_category = $categories->getMainCategory($info->commerce_category_id);
     $related_products = $this->Database->query("SELECT commerce_products.*,`commerce_categories`.`title`,`commerce_categories`.`description` as category_desc " . "FROM `commerce_products` " . "JOIN `commerce_categories` ON `commerce_categories`.`commerce_category_id`=`commerce_products`.`commerce_category_id` " . "WHERE commerce_products.commerce_product_id != '{$id}' " . "AND commerce_categories.commerce_category_id = '" . $info->commerce_category_id . "' " . "ORDER BY rand() LIMIT 3")->result();
     return $this->render('commerce_product', ['item' => $info, 'categories' => $categories->get(), 'imgs' => $product_imgs->get(), 'product' => $products, 'main_cat' => $main_category, 'category_model' => $categories, 'product_model' => $products, 'related_products' => $related_products]);
 }
 public function manage_filesAction($id = false)
 {
     $this->layout = 'ajax';
     if ($id) {
         $model = new \modules\commerce\models\Commerce_product_images();
     }
     if ($_SERVER['REQUEST_METHOD'] == 'GET') {
         if (!$id) {
             return false;
         }
         $model->commerce_product_id = $id;
         foreach ($model->get() as $file) {
             $filename = $file->product_image;
             $file_id = $file->commerce_product_image_id;
             $files[] = ['name' => $filename, 'size' => filesize('./cdn/' . $this->_module . '/' . $filename), 'url' => Uri_helper::cdn($this->_module . '/' . $filename), 'primary' => $file->primary, 'thumbnailUrl' => Uri_helper::cdn($this->_module . '/' . $filename), 'deleteUrl' => Uri_helper::url('management/commerce_products/manage_files/' . $file_id) . '?file=' . $filename, 'deleteType' => 'DELETE'];
         }
         return json_encode(['files' => $files]);
     } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
         foreach ($this->uploadFiles($_FILES) as $file) {
             $files[] = ['name' => $file['file_name'], 'size' => $file['file_size'], 'url' => Uri_helper::cdn($this->_module . '/' . $file['file_name']), 'primary' => '', 'thumbnailUrl' => Uri_helper::cdn($this->_module . '/' . $file['file_name']), 'deleteUrl' => Uri_helper::url('management/commerce_products/manage_files/') . '?file=' . $file['file_name'], 'deleteType' => 'DELETE'];
         }
         return json_encode(['files' => $files]);
     } elseif ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
         if ($id) {
             $model->commerce_product_image_id = $id;
             $model->delete();
             echo '{"' . $this->input->get('file') . '":true}';
         } else {
             echo '{"' . $this->input->get('file') . '":true}';
         }
     }
 }