/**
  * From AdminImportController
  */
 protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products')
 {
     $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
     $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
     switch ($entity) {
         case 'products':
             $image_obj = new Image($id_image);
             $path = $image_obj->getPathForCreation();
             break;
         case 'categories':
             $path = _PS_CAT_IMG_DIR_ . (int) $id_entity;
             break;
         default:
             break;
     }
     $url = str_replace(' ', '%20', trim($url));
     if (version_compare(_PS_VERSION_, '1.5', '<')) {
         include_once 'class/ImageManager.php';
         $imgSg = new ImageManagerCore();
         if (!$imgSg->checkImageMemoryLimit($url)) {
             return false;
         }
         if (@copy($url, $tmpfile)) {
             $imgSg->resize($tmpfile, $path . '.jpg');
             $images_types = ImageType::getImagesTypes($entity);
             foreach ($images_types as $image_type) {
                 $imgSg->resize($tmpfile, $path . '-' . Tools::stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
             }
         } elseif ($content = Tools::file_get_contents($url)) {
             $fp = fopen($tmpfile, "w");
             fwrite($fp, $content);
             fclose($fp);
             $imgSg->resize($tmpfile, $path . '.jpg');
             $images_types = ImageType::getImagesTypes($entity);
             foreach ($images_types as $image_type) {
                 $imgSg->resize($tmpfile, $path . '-' . Tools::stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
             }
         } else {
             unlink($tmpfile);
             return false;
         }
     } else {
         if (!ImageManager::checkImageMemoryLimit($url)) {
             return false;
         }
         if (@copy($url, $tmpfile)) {
             ImageManager::resize($tmpfile, $path . '.jpg');
             $images_types = ImageType::getImagesTypes($entity);
             foreach ($images_types as $image_type) {
                 ImageManager::resize($tmpfile, $path . '-' . Tools::stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
             }
             if (in_array($image_type['id_image_type'], $watermark_types)) {
                 Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
             }
         } elseif ($content = Tools::file_get_contents($url)) {
             $fp = fopen($tmpfile, "w");
             fwrite($fp, $content);
             fclose($fp);
             ImageManager::resize($tmpfile, $path . '.jpg');
             $images_types = ImageType::getImagesTypes($entity);
             foreach ($images_types as $image_type) {
                 ImageManager::resize($tmpfile, $path . '-' . Tools::stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
             }
             if (in_array($image_type['id_image_type'], $watermark_types)) {
                 Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
             }
         } else {
             unlink($tmpfile);
             return false;
         }
     }
     unlink($tmpfile);
     return true;
 }
 /**
  * render content info
  */
 public function getContent()
 {
     $output = '';
     $this->_html .= $this->headerHTML();
     $this->_html .= '<h2>' . $this->displayName . '.</h2>';
     /* update tree megamenu positions */
     if (Tools::getValue('doupdatepos') && Tools::isSubmit('updatePosition')) {
         $list = Tools::getValue('list');
         $root = 1;
         $child = array();
         foreach ($list as $id => $parent_id) {
             if ($parent_id <= 0) {
                 $parent_id = $root;
             }
             $child[$parent_id][] = $id;
         }
         $res = true;
         foreach ($child as $id_parent => $menus) {
             $i = 0;
             foreach ($menus as $id_sbmegamenu) {
                 $sql = 'UPDATE `' . _DB_PREFIX_ . 'sbmegamenu` SET `position` = ' . (int) $i . ', id_parent = ' . (int) $id_parent . ' 
                     WHERE `id_sbmegamenu` = ' . (int) $id_sbmegamenu;
                 $res &= Db::getInstance()->execute($sql);
                 $i++;
             }
         }
         $this->clearCache();
         die($this->l('Update Positions Done'));
     }
     if (Tools::getValue('typesub') && Tools::isSubmit('updatetypesub')) {
         $type = Tools::getValue('type');
         Configuration::updateValue('LEO_MENUSIDEBAR_TYPE', $type);
         $this->clearCache();
         die($this->l('Update Type Sub  Done'));
     }
     /* delete megamenu item */
     if (Tools::getValue('dodel')) {
         $obj = new Sbmegamenu((int) Tools::getValue('id_sbmegamenu'));
         $res = $obj->delete();
         $this->clearCache();
         Tools::redirectAdmin(AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules'));
     }
     if (Tools::isSubmit('save' . $this->name) && Tools::isSubmit('active')) {
         //if( Tools::getValue('type') == 'url' && !Tools::getValue('url')){
         // $errors[] = $this->l('Account details are required.');
         //}
         //			if (!isset($errors) AND !count($errors)){
         if ($id_sbmegamenu = Tools::getValue('id_sbmegamenu')) {
             $megamenu = new Sbmegamenu((int) $id_sbmegamenu);
         } else {
             $megamenu = new Sbmegamenu();
         }
         $keys = LeoSbmegamenuHelper::getConfigKey(false);
         $post = LeoSbmegamenuHelper::getPost($keys, false);
         $keys = LeoSbmegamenuHelper::getConfigKey(true);
         $post += LeoSbmegamenuHelper::getPost($keys, true);
         $megamenu->copyFromPost($post);
         $megamenu->id_shop = $this->context->shop->id;
         $url_default = '';
         foreach ($megamenu->url as $menu_url) {
             if ($menu_url) {
                 $url_default = $menu_url;
                 break;
             }
         }
         if ($url_default) {
             foreach ($megamenu->url as &$menu_url) {
                 if (!$menu_url) {
                     $menu_url = $url_default;
                 }
             }
         }
         if ($megamenu->type && $megamenu->type != 'html' && Tools::getValue($megamenu->type . '_type')) {
             $megamenu->item = Tools::getValue($megamenu->type . '_type');
         }
         $errors = array();
         if ($megamenu->validateFields(false) && $megamenu->validateFieldsLang(false)) {
             # Auto set position
             $megamenu->position = Sbmegamenu::getLastPosition((int) $megamenu->id_parent);
             $megamenu->save();
             $megamenu->cleanPositions($megamenu->id_parent);
             if (isset($_FILES['image']) && isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name'])) {
                 $this->checkFolderIcon();
                 if (ImageManagerCore::validateUpload($_FILES['image'])) {
                     return false;
                 } elseif (!($tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['image']['tmp_name'], $tmp_name)) {
                     return false;
                 } elseif (!ImageManager::resize($tmp_name, $this->img_path . $_FILES['image']['name'])) {
                     return false;
                 }
                 unlink($tmp_name);
                 $megamenu->image = $_FILES['image']['name'];
                 $megamenu->save();
             } else {
                 if (Tools::getIsset('delete_icon')) {
                     if ($megamenu->image) {
                         unlink($this->img_path . $megamenu->image);
                         $megamenu->image = '';
                         $megamenu->save();
                     }
                 }
             }
             Tools::redirectAdmin(AdminController::$currentIndex . '&configure=leomenusidebar&save' . $this->name . '&token=' . Tools::getValue('token') . '&id_sbmegamenu=' . $megamenu->id);
         } else {
             $errors[] = $this->l('An error occurred while attempting to save.');
         }
         //			}
         if (isset($errors) && count($errors)) {
             $output .= $this->displayError(implode('<br />', $errors));
         } else {
             $this->clearCache();
             $output .= $this->displayConfirmation($this->l('Settings updated.'));
         }
     }
     return $output . $this->displayForm();
 }