Esempio n. 1
0
 public function startImport()
 {
     global $cookie;
     @ini_set('max_execution_time', 0);
     ini_set('memory_limit', '256M');
     self::$_exportStartTime = time();
     $this->_header = self::$_serviceData['header'] == 1;
     self::$_sef = (int) Configuration::get('PS_REWRITING_SETTINGS');
     self::$_exportStore = (int) self::$_serviceData['id_store'];
     self::$_exportShop = (int) self::$_serviceData['id_shop'];
     self::$_exportGroup = (int) self::$_serviceData['id_group'];
     self::$_exportLanguage = (int) self::$_serviceData['id_lang'];
     self::$_carrierId = (int) self::$_serviceData['id_carrier'];
     self::$_carrierTax = self::getShippingTax(self::$_carrierId);
     $carrier = new Carrier((int) self::$_carrierId);
     self::$_carrierMethod = $carrier->getShippingMethod();
     self::$_exportCountry = (int) self::$_serviceData['id_country'];
     self::$_exportCurrency = (int) self::getCurrencyByCountry(self::$_exportCountry);
     self::$_exportState = (int) self::$_serviceData['id_state'];
     self::$_exportCondition = self::$_serviceData['condition'];
     self::$_zone = Country::getIdZone(self::$_exportCountry);
     self::$_billingMode = (int) Configuration::get('PS_SHIPPING_METHOD');
     self::$_shippingCarrierData = self::getCarrierShippingRanges();
     self::$_existingPictures = self::getExistingPictures();
     self::$_existingCategories = self::getExistingCategories(self::$_exportLanguage);
     self::$_specificPrices = self::getSpecificPrices(self::$_exportShop);
     self::$_productPriorities = self::getProductsPriorities();
     self::$_taxRates = self::getTaxes();
     self::$_exportCategories = MoussiqFreeService::getCategories($this->service_id);
     self::$_productCategories = self::getProductCategories();
     self::$_exportLanguageObj = new Language(self::$_exportLanguage);
     if (self::$_exportCurrency == 0) {
         self::$_exportCurrency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
     }
     if (sizeof(self::$_productCategories)) {
         $chunk_size = 3000;
         $current_size = 0;
         $this->beforeImport(self::$_serviceData['template'], array());
         do {
             $products = self::getProducts(self::$_exportLanguage, $current_size, $chunk_size, 'date_add', 'ASC', false, self::$_serviceData['export_inactive'] == true ? false : true, self::$_exportCountry, self::$_exportShop, self::$_exportCondition);
             $current_size += $chunk_size;
             $fileName = $this->filename;
             $fileDir = dirname(__FILE__) . '/../export/';
             if (!self::checkDir($fileDir)) {
                 $this->_errors[] = Tools::displayError('The directory is not writeable');
                 return false;
             }
             foreach ($products as $product) {
                 if (array_key_exists($product['id_product'], self::$_productCategories)) {
                     $product['categories'] = self::$_productCategories[$product['id_product']];
                     $product['reduction'] = self::getProductSpecificPrice($product['id_product'], self::$_exportStore, Configuration::get('PS_CURRENCY_DEFAULT'), self::$_exportCountry, self::$_exportGroup);
                     $product['quantity'] = (int) StockAvailable::getQuantityAvailableByProduct($product['id_product'], null, self::$_exportShop);
                     //to fix, get cover id_image
                     $product['id_image'] = self::getProductCoverWs($product['id_product']);
                     $product['id_product_image'] = $product['id_product'];
                     $features = self::collectFeatures(self::$_exportLanguage, $product['id_product']);
                     if (is_array($features)) {
                         foreach ($features as $id_feature => $feature) {
                             $product['ft' . $id_feature] = trim($feature);
                         }
                     }
                     $this->addProductLine($product, self::$_serviceData['template']);
                 }
             }
         } while ($chunk_size == count($products));
         $this->postProcess();
         return $this->saveFile();
     }
 }
    public function displayForm($isMainTab = true)
    {
        global $cookie, $currentIndex;
        $currentIndex = $_SERVER['SCRIPT_NAME'] . (($controller = Tools::getValue('controller')) ? '?controller=' . $controller : '');
        parent::displayForm($isMainTab);
        $obj = $this->loadObject(true);
        $url = $currentIndex . '&token=' . Tools::getAdminTokenLite('AdminMoussiqFree');
        $fields = $this->getReadableFields($this->getDbFieldNames(array('product', 'product_lang')));
        $countries = Country::getCountries((int) $cookie->id_lang);
        $carriers = Carrier::getCarriers((int) $cookie->id_lang, true);
        $languages = Language::getLanguages();
        $customerGroups = Group::getGroups((int) $cookie->id_lang);
        $engines = $this->getExportEngines();
        $stores = Db::getInstance()->ExecuteS('SELECT `id_store`, `name` FROM `' . _DB_PREFIX_ . 'store`');
        $shops = Db::getInstance()->ExecuteS('SELECT `id_shop`, `name` FROM `' . _DB_PREFIX_ . 'shop`');
        $conditions = array(array('condition' => 'new:used:refurbished', 'name' => $this->l('New') . ', ' . $this->l('Used') . ', ' . $this->l('Refurbished')), array('condition' => 'new', 'name' => $this->l('New')), array('condition' => 'used', 'name' => $this->l('Used')), array('condition' => 'refurbished', 'name' => $this->l('Refurbished')), array('condition' => 'new:used', 'name' => $this->l('New') . ', ' . $this->l('Used')), array('condition' => 'new:refurbished', 'name' => $this->l('New') . ', ' . $this->l('Refurbished')), array('condition' => 'used:refurbished', 'name' => $this->l('Used') . ', ' . $this->l('Refurbished')));
        $headerSet = self::getSettingsValue(Tools::getValue('header', null), $obj->header);
        $export_engine = self::getSettingsValue(Tools::getValue('export_engine', null), $obj->export_engine);
        $export_inactiveSet = self::getSettingsValue(Tools::getValue('export_inactive', null), $obj->export_inactive);
        $enclosureSet = self::getSettingsValue(Tools::getValue('enclosure', null), $obj->enclosure);
        $carrierSet = self::getSettingsValue(Tools::getValue('id_carrier', null), $obj->id_carrier);
        $countrySet = self::getSettingsValue(Tools::getValue('id_country', null), $obj->id_country);
        $stateSet = self::getSettingsValue(Tools::getValue('id_state', null), $obj->id_state);
        $conditionSet = self::getSettingsStringValue(Tools::getValue('condition', null), $obj->condition);
        $langSet = self::getSettingsValue(Tools::getValue('id_lang', null), $obj->id_lang);
        $groupSet = self::getSettingsValue(Tools::getValue('id_group', null), $obj->id_group);
        $storeSet = self::getSettingsValue(Tools::getValue('id_store', null), $obj->id_store);
        $shopSet = self::getSettingsValue(Tools::getValue('id_shop', null), $obj->id_shop);
        $psVersion = explode('.', _PS_VERSION_);
        $reductionAllowed = (int) $psVersion[0] == 1 && (int) $psVersion[1] < 4;
        echo '
        <link type="text/css" rel="stylesheet" href="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/css/style.css" />
        <script src="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/js/jquery-ui.min.js"></script>
        <script src="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/js/json2.js"></script>
        <script type="text/javascript">
            var fNameLabel            = "' . $this->l('Field Name') . '";
            var fBeforeLabel          = "' . $this->l('Before Value') . '";
            var fAfterLabel           = "' . $this->l('After Value') . '";
            var fValueLabel           = "' . $this->l('Value') . '";
            var fWithTaxLabel         = "' . $this->l('Apply Tax') . '";
            var fWithShippingLabel    = "' . $this->l('Add Shipping Price') . '";
            var fWithReductionLabel   = "' . $this->l('Include Price Reduction') . '";
            var fUrlsOfAllPictures   = "' . $this->l('Urls of all pictures') . '";
            var FControlDelete        = "' . $this->l('Delete this Field') . '";
            var FControlEdit          = "' . $this->l('Show Field\'s Settings') . '";
            var FCloseSettings        = "' . $this->l('Hide this Field\'s settings') . '";
            var fCodFee               = "' . $this->l('COD Fee') . '";
			var showReduction         = ' . ($reductionAllowed ? 'true' : 'false') . ';
			var serviceCountrySelected = ' . ($countrySet ? (int) $countrySet : (int) Configuration::get('EXPORTFREE_COUNTRY')) . ';
			var serviceStateSelected = ' . ($stateSet ? (int) $stateSet : (int) Configuration::get('EXPORTFREE_STATE')) . ';
			
			function processCheckBoxes(currentValue) {
				$("input[name^=categoryBox]").attr("checked", currentValue);
			}
			
			$(document).ready(function(){
				initStateSelect(\'select[name=id_country]\', \'id_state\', serviceStateSelected, serviceCountrySelected);
			});
        </script>
        <script src="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/js/helper.js"></script>
        <script src="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/js/exporter.js"></script>
        <script src="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/js/init.js"></script>
        <script src="' . __PS_BASE_URI__ . 'modules/' . $this->module . '/js/timepicker.js"></script>';
        if ($template = Tools::getValue('tplFile', false)) {
            echo '
			<div class="confirmation conf">
				' . $this->l('You can now download your template file:') . '<a href="' . _MODULE_DIR_ . 'moussiqfree/download.php?key=' . md5(_COOKIE_KEY_) . '&template=' . $template . '">' . $template . '</a>
			</div>';
        }
        echo '
        <p style="margin-bottom: 1em;">
            <a href="' . $url . '"><img src="../img/admin/arrow2.gif" /> ' . $this->l('Back to services list') . '</a>
        </p>
        <form action="' . $_SERVER['REQUEST_URI'] . '" method="post" id="mousiqueMainFrm">
            ' . ($obj->id ? '<input type="hidden" name="' . $this->identifier . '" value="' . $obj->id . '" />' : '') . '
			
            <fieldset style="margin-bottom: 1em;">
                <legend>
                    <img src="../modules/' . $this->module . '/images/' . ($obj->id ? 'edit' : 'add') . '.png" /> ' . $this->l('Service name') . '
                </legend>
				
                <label>' . $this->l('Name') . '</label>
                <div class="margin-form">
                    <input type="text" name="name" value="' . $this->getFieldValue($obj, 'name') . '" /><sup>*</sup>
                    <p class="clear">' . $this->l('Service name') . '</p>
                </div>
			</fieldset>

            <span id="toggleMousiqueSettings">
                <img src="../img/admin/arrow.gif" /> 
                ' . $this->l('Click here to display template settings') . '
            </span>
            <fieldset style="margin-bottom: 1em;" id="mousiqueSettings">
                <legend>
                    <img src="../modules/' . $this->module . '/images/' . ($obj->id ? 'edit' : 'add') . '.png" /> ' . ($obj->id ? $this->l('Update existing service') : $this->l('Add new service')) . '
                </legend>

                <label>' . $this->l('Status:') . ' </label>
                <div class="margin-form">
                    <input type="radio" name="status" id="status_on" value="1" ' . ($this->getFieldValue($obj, 'status') ? 'checked="checked" ' : '') . '/>
                    <label class="t" for="status_on"> <img src="../img/admin/enabled.gif" alt="' . $this->l('Enabled') . '" title="' . $this->l('Enabled') . '" /></label>
                    <input type="radio" name="status" id="status_off" value="0" ' . (!$this->getFieldValue($obj, 'status') ? 'checked="checked" ' : '') . '/>
                    <label class="t" for="status_off"> <img src="../img/admin/disabled.gif" alt="' . $this->l('Disabled') . '" title="' . $this->l('Disabled') . '" /></label>
                    <p>' . $this->l('Is this service active? (If set to "off" module will not generate csv for it)') . '</p>
                </div>
				
				<label>' . $this->l('Categories:') . '</label>
				<div class="margin-form">
					<table cellspacing="0" cellpadding="0" class="table" style="width: 600px;">
						<tr>
							<th>
								<input type="checkbox" name="checkme" class="noborder" onclick="processCheckBoxes(this.checked)" />
							</th>
							<th>' . $this->l('ID') . '</th>
							<th>' . $this->l('Name') . '</th>
						</tr>';
        $categories = Category::getCategories((int) $cookie->id_lang, false);
        $indexedCategories = $obj->id ? MoussiqFreeService::getCategories($obj->id) : array();
        $this->recurseCategoryForInclude((int) Tools::getValue($this->identifier), $indexedCategories, $categories, $categories[0][1], 1, $obj->id);
        echo '
					</table>
				</div>';
        echo '                
                <label>' . $this->l('Inactive products policy') . '</label>
                <div class="margin-form">
                    <select name="export_inactive">
                        <option value="-1"' . (!$export_inactiveSet ? ' selected="selected"' : '') . '>' . $this->l('Use default settings') . '</option>
                        <option value="1"' . ($export_inactiveSet === 1 ? ' selected="selected"' : '') . '>' . $this->l('Export') . '</option>
                        <option value="0"' . ($export_inactiveSet === 0 ? ' selected="selected"' : '') . '>' . $this->l('Do not Export') . '</option>
                    </select>
                    <p class="clear">' . $this->l('Choose if you want to export inactive products for this service (overrides default settings).') . '</p>
                </div>

                <label>' . $this->l('Export products in condition(s)') . '</label>
                <div class="margin-form">
                    <select name="condition">
                        <option value="-1" ' . (!$conditionSet ? ' selected="selected"' : '') . '>' . $this->l('Use default') . '</option>';
        foreach ($conditions as $condition) {
            echo '<option value="' . $condition['condition'] . '"' . ($conditionSet === $condition['condition'] ? ' selected="selected"' : '') . '>' . $condition['name'] . '</option>';
        }
        echo '
                    </select>
                    <p class="clear">' . $this->l('Choose if you want to export products in some conditions for this service (overrides default settings).') . '</p>
                </div>
                
                <label>' . $this->l('Language') . '</label>
                <div class="margin-form">
                    <select name="id_lang">
                        <option value="-1" ' . (!$langSet ? ' selected="selected"' : '') . '>' . $this->l('Use default settings') . '</option>';
        foreach ($languages as $language) {
            echo '<option value="' . $language['id_lang'] . '"' . ($langSet === intval($language['id_lang']) ? ' selected="selected"' : '') . '>' . $language['name'] . '</option>';
        }
        echo '
                    </select>
                    <p class="clear">' . $this->l('In what language would you like products to be exported') . '</p>
                </div>
				
                <label>' . $this->l('Customer Group') . '</label>
                <div class="margin-form">
                    <select name="id_group">
                        <option value="-1" ' . (!$groupSet ? ' selected="selected"' : '') . '>' . $this->l('Use default') . '</option>';
        foreach ($customerGroups as $customerGroup) {
            echo '<option value="' . $customerGroup['id_group'] . '"' . ($groupSet === intval($customerGroup['id_group']) ? ' selected="selected"' : '') . '>' . $customerGroup['name'] . '</option>';
        }
        echo '
                    </select>
                    <p class="clear">' . $this->l('In what customer groups would you like products to be exported') . '</p>
                </div>
                
                <label>' . $this->l('Delimiter') . '</label>
                <div class="margin-form">
                    <input type="text" size="3" name="delimiter" value="' . $this->getFieldValue($obj, 'delimiter') . '" />
                    <p class="clear">' . $this->l('Specify field delimiter for this service. Leave blank to use default delimiter.') . '</p>
                </div>
                
                <label>' . $this->l('Enclosure') . '</label>
                <div class="margin-form">
                    <select name="enclosure">
                        <option value="-1"' . (!$enclosureSet ? ' selected="selected"' : '') . '>' . $this->l('Use default settings') . '</option>
                        <option value="1"' . ($enclosureSet === 1 ? ' selected="selected"' : '') . '>' . $this->l('Double Quote') . '</option>
                        <option value="2"' . ($enclosureSet === 2 ? ' selected="selected"' : '') . '>' . $this->l('Single Quote') . '</option>
                    </select>
                    <p class="clear">' . $this->l('Field enclosure for CSV data (eg.: "My Product" - double quotes here is an enclosure character)') . '</p>
                </div>
                
                <label>' . $this->l('Generate Header') . '</label>
                <div class="margin-form">
                    <select name="header">
                        <option value="-1"' . (!$headerSet ? ' selected="selected"' : '') . '>' . $this->l('Use default settings') . '</option>
                        <option value="1"' . ($headerSet === 1 ? ' selected="selected"' : '') . '>' . $this->l('Yes') . '</option>
                        <option value="0"' . ($headerSet === 0 ? ' selected="selected"' : '') . '>' . $this->l('No') . '</option>
                    </select>
                    <p class="clear">' . $this->l('Csv header is the first line that does not include product information, but contains field names, like "Product name", "Product link", etc.') . '</p>
                </div>
                
                <label>' . $this->l('Country') . '</label>
                <div class="margin-form">
                    <select name="id_country">
                        <option value="-1" ' . (!$countrySet ? ' selected="selected"' : '') . '>' . $this->l('Use default') . '</option>';
        foreach ($countries as $country) {
            echo '<option value="' . $country['id_country'] . '"' . ($countrySet === (int) $country['id_country'] ? ' selected="selected"' : '') . '>' . $country['name'] . '</option>';
        }
        echo '
                    </select>
                    <p class="clear">' . $this->l('This setting overrides default zone selected in module settings') . '</p>
                </div>
                
                <label>' . $this->l('Carrier') . '</label>
                <div class="margin-form">
                    <select name="id_carrier">
                        <option value="-1" ' . (!$carrierSet ? ' selected="selected"' : '') . '>' . $this->l('Use default') . '</option>';
        foreach ($carriers as $carrier) {
            echo '<option value="' . $carrier['id_carrier'] . '"' . ($carrierSet === (int) $carrier['id_carrier'] ? ' selected="selected"' : '') . '>' . $carrier['name'] . '</option>';
        }
        echo '
                    </select>
                    <p class="clear">' . $this->l('This setting overrides default carrier selected in module settings') . '</p>
                </div>';
        if ($engines && sizeof($engines)) {
            echo '
                <label>' . $this->l('Export engine') . '</label>
                <div class="margin-form">
                    <select name="export_engine">
			';
            foreach ($engines as $engine => $engineName) {
                echo '
						<option value="' . $engine . '"' . ($export_engine == $engine ? ' selected="selected"' : '') . '>' . $engineName . '</option>';
            }
            echo '
					</select>
					<p class="clear">' . $this->l('An export engine to be used for this service.') . '</p>
				</div>';
        }
        if ($stores && sizeof($stores)) {
            echo '
                <label>' . $this->l('Export Store') . '</label>
                <div class="margin-form">
                    <select name="id_store">
			';
            foreach ($stores as $store) {
                echo '
						<option value="' . $store['id_store'] . '"' . ($storeSet == $store['id_store'] ? ' selected="selected"' : '') . '>' . $store['name'] . '</option>';
            }
            echo '
					</select>
					<p class="clear">' . $this->l('Export product from this store.') . '</p>
				</div>';
        }
        if (Shop::isFeatureActive() && $shops && sizeof($shops)) {
            echo '
                <label>' . $this->l('Export shop') . '</label>
                <div class="margin-form">
                    <select name="id_shop">
			';
            foreach ($shops as $shop) {
                echo '
						<option value="' . $shop['id_shop'] . '"' . ($shopSet == $shop['id_shop'] ? ' selected="selected"' : '') . '>' . $shop['name'] . '</option>';
            }
            echo '
					</select>
					<p class="clear">' . $this->l('Export product from this shop.') . '</p>
				</div>';
        }
        echo '
            </fieldset>
            
            <fieldset class="fields availFields">
                <legend>
                    <img src="../modules/' . $this->module . '/images/fields.png" />
                    ' . $this->l('Available fields') . '
                </legend>
                <ul class="fields">';
        foreach ($fields as $field => $fieldData) {
            echo '
                        <li class="' . $field . ' ' . $fieldData['class'] . '">
                            <span class="fieldLegendWrapper">
                                <i class="fieldLegend"></i>
                            </span>
                            <span class="fieldFancyName">' . $fieldData['name'] . '</span>
                        </li>';
        }
        echo '
                </ul>
            </fieldset>
            
            <fieldset class="fields targetFields">
                <legend>
                    <img src="../modules/' . $this->module . '/images/add.png" />
                    ' . $this->l('Export fields') . '
                </legend>
                
                <span class="purgeFields">' . $this->l('Remove all fields') . '</span>
                
                <ul id="export">';
        $template = Tools::getValue('template', base64_decode($this->getFieldValue($obj, 'template')));
        //print_r($template);
        if (stristr($template, '\\"')) {
            unset($template);
            // old type of templates
            //echo "pos = ".stristr($template, '\"').'===';
            $template = Tools::getValue('template', stripslashes(base64_decode($this->getFieldValue($obj, 'template'))));
            //print_r($template);
            //echo '***1***';
            $template_str = stripcslashes(Tools::getValue('template', base64_decode($obj->template)));
        } else {
            unset($template);
            // new type of templates(supporting \n )
            $template = Tools::getValue('template', base64_decode($this->getFieldValue($obj, 'template')));
            //print_r($template);
            //echo '---2---';
            $template_str = addcslashes(Tools::getValue('template', base64_decode($obj->template)), "\\");
        }
        if ($template != '') {
            $template = Tools::jsonDecode($template);
            foreach ($template->fields as $field) {
                $fieldFancyName = stripslashes($field->field);
                if (isset($fields[stripslashes($field->field)])) {
                    $fieldFancyName = $fields[stripslashes($field->field)]['name'];
                    $fieldClass = $fields[stripslashes($field->field)]['class'];
                }
                echo '
                        <li class="' . stripslashes($field->field) . ' ' . $fieldClass . '">
							<span class="fieldLegendWrapper">
								<i class="fieldLegend"></i>
							</span>
                            <span class="fieldFancyName">' . $fieldFancyName . '</span>
                        </li>';
            }
        }
        echo '
                </ul>
                <textarea name="template" id="template">' . $template_str . '</textarea>
            </fieldset>
            <div class="clear">&nbsp;</div>

            <fieldset>
                <legend>
                    <img src="../modules/' . $this->module . '/images/save.png" />
                    ' . $this->l('Save settings') . '
                </legend>
                
				<div class="margin-form">
					<input type="submit" value="' . $this->l('   Save   ') . '" name="submitAdd' . $this->table . '" class="button" />
				</div>';
        if ($obj->id) {
            echo '
				<div class="margin-form">
					<input type="submit" value="' . $this->l('Export this template') . '" class="button" name="createTemplate" />
				</div>';
        }
        echo '
                <p style="margin-top: 1em;">
                    
                <p>
            </fieldset>
        </form>
        <p style="margin-top: 1em;">
            <a href="' . $url . '"><img src="../img/admin/arrow2.gif" /> ' . $this->l('Back to services list') . '</a>
        </p>';
    }