Beispiel #1
0
    /**
     * Build a categories tree
     *
     * @param array $indexedCategories Array with categories where product is indexed (in order to check checkbox)
     * @param array $categories Categories to list
     * @param array $current Current category
     * @param integer $id_category Current category id
     */
    public static function recurseCategoryForInclude($id_obj, $indexedCategories, $categories, $current, $id_category = 1, $id_category_default = null, $has_suite = array())
    {
        global $done;
        static $irow;
        $content = '';
        if (!isset($done[$current['infos']['id_parent']])) {
            $done[$current['infos']['id_parent']] = 0;
        }
        $done[$current['infos']['id_parent']] += 1;
        $todo = count($categories[$current['infos']['id_parent']]);
        $doneC = $done[$current['infos']['id_parent']];
        $level = $current['infos']['level_depth'] + 1;
        $content .= '
		<tr class="' . ($irow++ % 2 ? 'alt_row' : '') . '">
			<td>
				<input type="checkbox" name="categoryBox[]" class="categoryBox' . ($id_category_default == $id_category ? ' id_category_default' : '') . '" id="categoryBox_' . $id_category . '" value="' . $id_category . '"' . (in_array($id_category, $indexedCategories) || (int) Tools::getValue('id_category') == $id_category && !(int) $id_obj ? ' checked="checked"' : '') . ' />
			</td>
			<td>
				' . $id_category . '
			</td>
			<td>';
        for ($i = 2; $i < $level; $i++) {
            $content .= '<img src="../img/admin/lvl_' . $has_suite[$i - 2] . '.gif" alt="" />';
        }
        $content .= '<img src="../img/admin/' . ($level == 1 ? 'lv1.gif' : 'lv2_' . ($todo == $doneC ? 'f' : 'b') . '.gif') . '" alt="" /> &nbsp;
			<label for="categoryBox_' . $id_category . '" class="t">' . stripslashes($current['infos']['name']) . '</label></td>
		</tr>';
        if ($level > 1) {
            $has_suite[] = $todo == $doneC ? 0 : 1;
        }
        if (isset($categories[$id_category])) {
            foreach ($categories[$id_category] as $key => $row) {
                if ($key != 'infos') {
                    $content .= AdminProductsController::recurseCategoryForInclude($id_obj, $indexedCategories, $categories, $categories[$id_category][$key], $key, $id_category_default, $has_suite);
                }
            }
        }
        return $content;
    }
 public function ajaxProcessaddProductImage()
 {
     self::$currentIndex = 'index.php?tab=AdminProducts';
     $product = new Product((int) Tools::getValue('id_product'));
     $legends = Tools::getValue('legend');
     if (!is_array($legends)) {
         $legends = (array) $legends;
     }
     if (!Validate::isLoadedObject($product)) {
         $files = array();
         $files[0]['error'] = Tools::displayError('Cannot add image because product creation failed.');
     }
     $image_uploader = new HelperImageUploader('file');
     $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size);
     $files = $image_uploader->process();
     foreach ($files as &$file) {
         $image = new Image();
         $image->id_product = (int) $product->id;
         $image->position = Image::getHighestPosition($product->id) + 1;
         foreach ($legends as $key => $legend) {
             if (!empty($legend)) {
                 $image->legend[(int) $key] = $legend;
             }
         }
         if (!Image::getCover($image->id_product)) {
             $image->cover = 1;
         } else {
             $image->cover = 0;
         }
         if (($validate = $image->validateFieldsLang(false, true)) !== true) {
             $file['error'] = Tools::displayError($validate);
         }
         if (isset($file['error']) && (!is_numeric($file['error']) || $file['error'] != 0)) {
             continue;
         }
         if (!$image->add()) {
             $file['error'] = Tools::displayError('Error while creating additional image');
         } else {
             if (!($new_path = $image->getPathForCreation())) {
                 $file['error'] = Tools::displayError('An error occurred during new folder creation');
                 continue;
             }
             $error = 0;
             if (!ImageManager::resize($file['save_path'], $new_path . '.' . $image->image_format, null, null, 'jpg', false, $error)) {
                 switch ($error) {
                     case ImageManager::ERROR_FILE_NOT_EXIST:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file does not exist anymore.');
                         break;
                     case ImageManager::ERROR_FILE_WIDTH:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file width is 0px.');
                         break;
                     case ImageManager::ERROR_MEMORY_LIMIT:
                         $file['error'] = Tools::displayError('An error occurred while copying image, check your memory limit.');
                         break;
                     default:
                         $file['error'] = Tools::displayError('An error occurred while copying image.');
                         break;
                 }
                 continue;
             } else {
                 $imagesTypes = ImageType::getImagesTypes('products');
                 foreach ($imagesTypes as $imageType) {
                     if (!ImageManager::resize($file['save_path'], $new_path . '-' . stripslashes($imageType['name']) . '.' . $image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) {
                         $file['error'] = Tools::displayError('An error occurred while copying image:') . ' ' . stripslashes($imageType['name']);
                         continue;
                     }
                 }
             }
             unlink($file['save_path']);
             unset($file['save_path']);
             Hook::exec('actionWatermark', array('id_image' => $image->id, 'id_product' => $product->id));
             if (!$image->update()) {
                 $file['error'] = Tools::displayError('Error while updating status');
                 continue;
             }
             $shops = Shop::getContextListShopID();
             $image->associateTo($shops);
             $json_shops = array();
             foreach ($shops as $id_shop) {
                 $json_shops[$id_shop] = true;
             }
             $file['status'] = 'ok';
             $file['id'] = $image->id;
             $file['position'] = $image->position;
             $file['cover'] = $image->cover;
             $file['legend'] = $image->legend;
             $file['path'] = $image->getExistingImgPath();
             $file['shops'] = $json_shops;
             @unlink(_PS_TMP_IMG_DIR_ . 'product_' . (int) $product->id . '.jpg');
             @unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $product->id . '_' . $this->context->shop->id . '.jpg');
         }
     }
     die(json_encode(array($image_uploader->getName() => $files)));
 }