406 lines
12 KiB
PHP
406 lines
12 KiB
PHP
<?php
|
|
class stSetupDefaultData
|
|
{
|
|
const STEPS = [
|
|
0 => 'loadFixtures',
|
|
1 => 'changeTheme',
|
|
2 => 'changeCurrency',
|
|
3 => 'changeLanguage',
|
|
4 => 'changeTax',
|
|
5 => 'postInstall',
|
|
6 => 'clearCache',
|
|
];
|
|
|
|
protected $message;
|
|
|
|
|
|
|
|
public function getTitle()
|
|
{
|
|
return sfContext::getInstance()->getI18N()->__('Zapisywanie domyślnych ustawień:', null, 'stSetup');
|
|
}
|
|
|
|
public static function getSteps()
|
|
{
|
|
return count(self::STEPS);
|
|
}
|
|
|
|
public function step($step)
|
|
{
|
|
$settings = $this->loadSettings();
|
|
|
|
$this->callStep($step, $settings);
|
|
|
|
return $step + 1;
|
|
}
|
|
|
|
public function close()
|
|
{
|
|
sfLoader::loadHelpers(['Helper', 'stProgressBar']);
|
|
|
|
$this->message = progress_bar('stCleanInstallerDownload', 'stCleanInstallerDownload', 'step', stCleanInstallerDownload::getSteps());
|
|
$this->message .= progress_bar('stCleanInstallerSrc', 'stCleanInstallerSrc', 'step', stCleanInstallerSrc::getSteps());
|
|
}
|
|
|
|
public function getMessage()
|
|
{
|
|
return $this->message;
|
|
}
|
|
|
|
protected function callStep(int $step, array $settings)
|
|
{
|
|
$method = self::STEPS[$step];
|
|
|
|
call_user_func([$this, $method], $settings);
|
|
}
|
|
|
|
protected function loadSettings(): array
|
|
{
|
|
$settingsFile = sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'db' . DIRECTORY_SEPARATOR . '.settings.reg';
|
|
$settings = sfYaml::load($settingsFile);
|
|
|
|
if ($settings['language'] === false)
|
|
{
|
|
$settings['language'] = 'no';
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
protected function loadFixtures(array $settings)
|
|
{
|
|
$this->setDefaultOptLanguage('pl_PL');
|
|
|
|
sfLoader::loadPluginConfig();
|
|
|
|
if (stInstallMethod::getWebInstall())
|
|
{
|
|
if (!$this->loadSqlFixtures($settings['fixtures_name']))
|
|
{
|
|
$this->runTask('installer-load-data ' . SF_ENVIRONMENT);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$task = $settings['load_fixtures'] ? 'st-propel-load-default-data ' . $settings['fixtures_name'] : 'installer-load-data ' . SF_ENVIRONMENT;
|
|
$this->runTask($task);
|
|
}
|
|
}
|
|
|
|
protected function changeTheme(array $settings)
|
|
{
|
|
$this->initializeDatabase();
|
|
|
|
stTheme::setActiveTheme($settings['theme']);
|
|
}
|
|
|
|
protected function changeCurrency(array $settings)
|
|
{
|
|
$this->initializeDatabase();
|
|
|
|
$c = new Criteria();
|
|
$c->add(CurrencyPeer::SHORTCUT, $settings['currency']);
|
|
$currency = CurrencyPeer::doSelectOne($c);
|
|
|
|
if (is_object($currency))
|
|
{
|
|
$config = stConfig::getInstance(sfContext::getInstance(), 'stCurrencyPlugin');
|
|
$config->set('default_currency', $currency->getShortcut());
|
|
$config->save(true);
|
|
|
|
$exchange = $currency->getExchange();
|
|
|
|
$currency->setActive(1);
|
|
$currency->setMain(1);
|
|
$currency->setExchange(1);
|
|
$currency->save();
|
|
}
|
|
|
|
$c = new Criteria();
|
|
$criterion = $c->getNewCriterion(CurrencyPeer::MAIN, 0);
|
|
$criterion1 = $c->getNewCriterion(CurrencyPeer::MAIN, null, Criteria::ISNULL);
|
|
$criterion->addOr($criterion1);
|
|
$c->add($criterion);
|
|
$currencies = CurrencyPeer::doSelect($c);
|
|
|
|
foreach ($currencies as $currency)
|
|
{
|
|
$currency->setExchange($currency->getExchange() / $exchange);
|
|
$currency->save();
|
|
}
|
|
}
|
|
|
|
protected function changeCountry(array $settings)
|
|
{
|
|
$this->initializeDatabase();
|
|
|
|
$c = new Criteria();
|
|
$c->add(CountriesPeer::ISO_A2, $settings['delivery_country']);
|
|
$country = CountriesPeer::doSelectOne($c);
|
|
|
|
if (is_object($country))
|
|
{
|
|
$country->setIsDefault(1);
|
|
$country->save();
|
|
}
|
|
|
|
$connection = Propel::getConnection();
|
|
$statementTable = $connection->prepareStatement('TRUNCATE `st_countries_area`');
|
|
$statementTable->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
$statementTable = $connection->prepareStatement('TRUNCATE `st_countries_area_has_countries`');
|
|
$statementTable->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
$area = new CountriesArea();
|
|
$area->setName($country->getName());
|
|
$area->setIsActive(1);
|
|
$area->save();
|
|
|
|
$areaHasCountry = new CountriesAreaHasCountries();
|
|
$areaHasCountry->setCountriesId($country->getId());
|
|
$areaHasCountry->setCountriesAreaId($area->getId());
|
|
$areaHasCountry->save();
|
|
}
|
|
|
|
protected function changePanelLanguage(array $settings)
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(LanguagePeer::LANGUAGE, $settings['language_panel']);
|
|
$language = LanguagePeer::doSelectOne($c);
|
|
|
|
if (null !== $language)
|
|
{
|
|
$language->setIsDefaultPanel(true);
|
|
$language->save();
|
|
|
|
$config = stConfig::getInstance(sfContext::getInstance(), 'stLanguagePlugin');
|
|
$config->set('admin_language', $language->getOriginalLanguage());
|
|
$config->save(true);
|
|
|
|
$this->setDefaultOptLanguage($language->getOriginalLanguage());
|
|
|
|
$this->updateOptI18nFields($language->getOriginalLanguage());
|
|
}
|
|
|
|
if (stSoteshopVersion::getVersion() == stSoteshopVersion::ST_SOTESHOP_VERSION_INTERNATIONAL)
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(LanguagePeer::LANGUAGE, 'pl_PL');
|
|
$language = LanguagePeer::doSelectOne($c);
|
|
|
|
if (is_object($language))
|
|
{
|
|
$language->setIsTranslatePanel(false);
|
|
$language->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function changeLanguage(array $settings)
|
|
{
|
|
$this->initializeDatabase();
|
|
|
|
$this->changePanelLanguage($settings);
|
|
|
|
$c = new Criteria();
|
|
$c->add(LanguagePeer::LANGUAGE, $settings['language']);
|
|
$language = LanguagePeer::doSelectOne($c);
|
|
|
|
if (is_object($language))
|
|
{
|
|
$sc = new Criteria();
|
|
$sc->add(LanguagePeer::ACTIVE, true);
|
|
$uc = new Criteria();
|
|
$uc->add(LanguagePeer::ACTIVE, false);
|
|
BasePeer::doUpdate($sc, $uc, Propel::getConnection());
|
|
|
|
$language->setIsDefault(true);
|
|
$language->setActive(true);
|
|
$language->save();
|
|
|
|
stLockSimpleCache::updateLockCache();
|
|
}
|
|
}
|
|
|
|
protected function changeTax(array $settings)
|
|
{
|
|
$country = $settings['delivery_country'];
|
|
|
|
if ($country == 'PL')
|
|
{
|
|
return true;
|
|
}
|
|
|
|
$taxFile = sfConfig::get('sf_plugins_dir') . DIRECTORY_SEPARATOR . 'stTaxPlugin' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'countries_tax_list.yml';
|
|
|
|
if (file_exists($taxFile))
|
|
{
|
|
|
|
$databaseManager = new sfDatabaseManager();
|
|
$databaseManager->initialize();
|
|
|
|
$connection = Propel::getConnection();
|
|
$statementTable = $connection->prepareStatement('TRUNCATE `st_tax`');
|
|
$statementTable->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
$taxes = sfYaml::load($taxFile);
|
|
|
|
if (!isset($taxes[$country]))
|
|
{
|
|
$country = 'default';
|
|
}
|
|
|
|
foreach ($taxes[$country] as $name => $params)
|
|
{
|
|
$tax = new Tax();
|
|
$tax->setVat($params['vat']);
|
|
$tax->setVatName($params['vat_name']);
|
|
|
|
if ($name == 'Tax_1')
|
|
{
|
|
$tax->setIsDefault(true);
|
|
}
|
|
|
|
$tax->save();
|
|
}
|
|
|
|
$statementTable = $connection->prepareStatement('UPDATE st_product SET `tax_id` = 1, `opt_vat` = ' . $taxes[$country]['Tax_1']['vat'] . ', `price` = (`opt_price_brutto`/(1+(' . $taxes[$country]['Tax_1']['vat'] . '/100)))');
|
|
$statementTable->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
$statementTable = $connection->prepareStatement('UPDATE st_delivery SET `tax_id` = 1');
|
|
$statementTable->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
unset($taxes);
|
|
|
|
$databaseManager->shutdown();
|
|
}
|
|
}
|
|
|
|
protected function postInstall(array $settings)
|
|
{
|
|
$shopInfoConfig = stConfig::getInstance('stShopInfoBackend');
|
|
$shopInfoConfig->set('country', $settings['company_country']);
|
|
$shopInfoConfig->save();
|
|
|
|
$files = glob(sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'install-post' . DIRECTORY_SEPARATOR . '*');
|
|
|
|
foreach ($files as $file)
|
|
{
|
|
file_put_contents(sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'post-install.log', 'Load script file: ' . basename($file) . "\n", FILE_APPEND);
|
|
include_once($file);
|
|
}
|
|
}
|
|
|
|
protected function loadSqlFixtures($fixturesName)
|
|
{
|
|
$sqlFile = sfConfig::get('sf_root_dir') . '/install/sql/' . $fixturesName . '.sql';
|
|
|
|
if (file_exists($sqlFile))
|
|
{
|
|
|
|
$databaseManager = new sfDatabaseManager();
|
|
$databaseManager->initialize();
|
|
$con = Propel::getConnection();
|
|
|
|
$queries = preg_split('/\n/', file_get_contents($sqlFile));
|
|
|
|
foreach ($queries as $query)
|
|
{
|
|
if (empty($query)) continue;
|
|
|
|
if (preg_match('/^\/\*\!/', $query))
|
|
{
|
|
preg_match('/^\/\*\![0-9]+\s{1}(.*)\*\/\;$/', $query, $matches);
|
|
if (isset($matches[1]) && !empty($matches[1]))
|
|
{
|
|
$query = trim($matches[1]) . ';';
|
|
}
|
|
}
|
|
|
|
$con->executeQuery($query);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
protected function updateOptI18nFields(string $culture)
|
|
{
|
|
$con = Propel::getConnection();
|
|
$statement = $con->prepareStatement('SHOW TABLES LIKE "%_i18n"');
|
|
$resultset = $statement->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
while ($resultset->next())
|
|
{
|
|
$i18nFields = [];
|
|
$optFields = [];
|
|
$setOptFieldStatements = [];
|
|
$tableNameI18n = $resultset->getString(1);
|
|
$tableName = substr($tableNameI18n, 0, -5);
|
|
|
|
$statementTable = $con->prepareStatement("SHOW COLUMNS FROM $tableNameI18n WHERE Field <> 'id' AND Field <> 'culture'");
|
|
$resultsetTable = $statementTable->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
while ($resultsetTable->next())
|
|
{
|
|
$i18nFields[] = $resultsetTable->getString(1);
|
|
}
|
|
|
|
$query = "UPDATE $tableName, $tableNameI18n SET ";
|
|
$updateI18FieldList = 'opt_' . implode("', 'opt_", $i18nFields);
|
|
|
|
$ps = $con->prepareStatement("SHOW COLUMNS FROM $tableName WHERE Field IN ('$updateI18FieldList')");
|
|
$result = $ps->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
|
|
while ($result->next())
|
|
{
|
|
$optFields[] = $result->getString(1);
|
|
}
|
|
|
|
if (!empty($optFields))
|
|
{
|
|
foreach ($optFields as $optField)
|
|
{
|
|
$i18nField = substr($optField, 4);
|
|
$setOptFieldStatements[] = "$tableName.$optField = $tableNameI18n.$i18nField";
|
|
}
|
|
|
|
$setOptFieldStatement = implode(', ', $setOptFieldStatements);
|
|
$query = "UPDATE $tableName, $tableNameI18n SET $setOptFieldStatement WHERE {$tableName}.id = {$tableNameI18n}.id AND {$tableNameI18n}.culture = '$culture'";
|
|
|
|
$statementUpdate = $con->prepareStatement($query);
|
|
$statementUpdate->executeQuery(ResultSet::FETCHMODE_NUM);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function setDefaultOptLanguage(string $language)
|
|
{
|
|
$config = stConfig::getInstance(sfContext::getInstance(), 'stLanguagePlugin');
|
|
$config->set('default_opt_language', $language);
|
|
$config->save(true);
|
|
}
|
|
|
|
protected function clearCache()
|
|
{
|
|
$this->runTask('cc');
|
|
}
|
|
|
|
protected function runTask(string $task)
|
|
{
|
|
$pakeweb = new stPakeWeb();
|
|
|
|
if (!$pakeweb->run($task))
|
|
{
|
|
throw new Exception($pakeweb->error);
|
|
}
|
|
}
|
|
|
|
protected function initializeDatabase()
|
|
{
|
|
$databaseManager = new sfDatabaseManager();
|
|
$databaseManager->initialize();
|
|
}
|
|
}
|