PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/install.ps/models/install.php

https://bitbucket.org/enurkov/prestashop
PHP | 693 lines | 509 code | 80 blank | 104 comment | 67 complexity | b41f9ccbb1f8ead6dba7fca4834d7fcd MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class InstallModelInstall extends InstallAbstractModel
  27. {
  28. const SETTINGS_FILE = 'config/settings.inc.php';
  29. /**
  30. * @var FileLogger
  31. */
  32. public $logger;
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. $this->logger = new FileLogger();
  37. if (is_writable(_PS_ROOT_DIR_.'/log/'))
  38. $this->logger->setFilename(_PS_ROOT_DIR_.'/log/'.@date('Ymd').'_installation.log');
  39. }
  40. public function setError($errors)
  41. {
  42. if (!is_array($errors))
  43. $errors = array($errors);
  44. parent::setError($errors);
  45. foreach ($errors as $error)
  46. $this->logger->logError($error);
  47. }
  48. /**
  49. * Generate settings file
  50. */
  51. public function generateSettingsFile($database_server, $database_login, $database_password, $database_name, $database_prefix, $database_engine)
  52. {
  53. // Check permissions for settings file
  54. if (file_exists(_PS_ROOT_DIR_.'/'.self::SETTINGS_FILE) && !is_writable(_PS_ROOT_DIR_.'/'.self::SETTINGS_FILE))
  55. {
  56. $this->setError($this->language->l('%s file is not writable (check permissions)', self::SETTINGS_FILE));
  57. return false;
  58. }
  59. elseif (!file_exists(_PS_ROOT_DIR_.'/'.self::SETTINGS_FILE) && !is_writable(_PS_ROOT_DIR_.'/'.dirname(self::SETTINGS_FILE)))
  60. {
  61. $this->setError($this->language->l('%s folder is not writable (check permissions)', dirname(self::SETTINGS_FILE)));
  62. return false;
  63. }
  64. // Generate settings content and write file
  65. $settings_constants = array(
  66. '_DB_SERVER_' => $database_server,
  67. '_DB_NAME_' => $database_name,
  68. '_DB_USER_' => $database_login,
  69. '_DB_PASSWD_' => $database_password,
  70. '_DB_PREFIX_' => $database_prefix,
  71. '_MYSQL_ENGINE_' => $database_engine,
  72. '_PS_CACHING_SYSTEM_' => 'CacheMemcache',
  73. '_PS_CACHE_ENABLED_' => '0',
  74. '_MEDIA_SERVER_1_' => '',
  75. '_MEDIA_SERVER_2_' => '',
  76. '_MEDIA_SERVER_3_' => '',
  77. '_COOKIE_KEY_' => Tools::passwdGen(56),
  78. '_COOKIE_IV_' => Tools::passwdGen(8),
  79. '_PS_CREATION_DATE_' => date('Y-m-d'),
  80. '_PS_VERSION_' => _PS_INSTALL_VERSION_,
  81. );
  82. // If mcrypt is activated, add Rijndael 128 configuration
  83. if (function_exists('mcrypt_encrypt'))
  84. {
  85. $settings_constants['_RIJNDAEL_KEY_'] = Tools::passwdGen(mcrypt_get_key_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  86. $settings_constants['_RIJNDAEL_IV_'] = base64_encode(mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND));
  87. }
  88. $settings_content = "<?php\n";
  89. foreach ($settings_constants as $constant => $value)
  90. $settings_content .= "define('$constant', '".str_replace('\'', '\\\'', $value)."');\n";
  91. if (!file_put_contents(_PS_ROOT_DIR_.'/'.self::SETTINGS_FILE, $settings_content))
  92. {
  93. $this->setError($this->language->l('Cannot write settings file'));
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * PROCESS : installDatabase
  100. * Generate settings file and create database structure
  101. */
  102. public function installDatabase($clear_database = false)
  103. {
  104. // Clear database (only tables with same prefix)
  105. require_once _PS_ROOT_DIR_.'/'.self::SETTINGS_FILE;
  106. if ($clear_database)
  107. $this->clearDatabase();
  108. // Install database structure
  109. $sql_loader = new InstallSqlLoader();
  110. $sql_loader->setMetaData(array(
  111. 'PREFIX_' => _DB_PREFIX_,
  112. 'ENGINE_TYPE' => _MYSQL_ENGINE_,
  113. ));
  114. try
  115. {
  116. $sql_loader->parse_file(_PS_INSTALL_DATA_PATH_.'db_structure.sql');
  117. }
  118. catch (PrestashopInstallerException $e)
  119. {
  120. $this->setError($this->language->l('Database structure file not found'));
  121. return false;
  122. }
  123. if ($errors = $sql_loader->getErrors())
  124. {
  125. foreach ($errors as $error)
  126. $this->setError($this->language->l('SQL error on query <i>%s</i>', $error['error']));
  127. return false;
  128. }
  129. return true;
  130. }
  131. /**
  132. * Clear database (only tables with same prefix)
  133. *
  134. * @param bool $truncate If true truncate the table, if false drop the table
  135. */
  136. public function clearDatabase($truncate = false)
  137. {
  138. foreach (Db::getInstance()->executeS('SHOW TABLES') as $row)
  139. {
  140. $table = current($row);
  141. if (!_DB_PREFIX_ || preg_match('#^'._DB_PREFIX_.'#i', $table))
  142. Db::getInstance()->execute((($truncate) ? 'TRUNCATE' : 'DROP TABLE').' `'.$table.'`');
  143. }
  144. }
  145. /**
  146. * PROCESS : installDefaultData
  147. * Create default shop and languages
  148. */
  149. public function installDefaultData($shop_name, $clear_database = false)
  150. {
  151. if ($clear_database)
  152. $this->clearDatabase(true);
  153. // Install first shop
  154. if (!$this->createShop($shop_name))
  155. return false;
  156. // Install languages
  157. try
  158. {
  159. $languages = $this->installLanguages();
  160. }
  161. catch (PrestashopInstallerException $e)
  162. {
  163. $this->setError($e->getMessage());
  164. return false;
  165. }
  166. $flip_languages = array_flip($languages);
  167. $id_lang = (!empty($flip_languages[$this->language->getLanguageIso()])) ? $flip_languages[$this->language->getLanguageIso()] : 1;
  168. Configuration::updateGlobalValue('PS_LANG_DEFAULT', $id_lang);
  169. return true;
  170. }
  171. /**
  172. * PROCESS : populateDatabase
  173. * Populate database with default data
  174. */
  175. public function populateDatabase($entity = null)
  176. {
  177. $languages = array();
  178. foreach (Language::getLanguages(false) as $lang)
  179. $languages[$lang['id_lang']] = $lang['iso_code'];
  180. // Install XML data (data/xml/ folder)
  181. $xml_loader = new InstallXmlLoader();
  182. $xml_loader->setLanguages($languages);
  183. if (isset($this->xml_loader_ids) && $this->xml_loader_ids)
  184. $xml_loader->setIds($this->xml_loader_ids);
  185. if ($entity)
  186. $xml_loader->populateEntity($entity);
  187. else
  188. $xml_loader->populateFromXmlFiles();
  189. if ($errors = $xml_loader->getErrors())
  190. {
  191. $this->setError($errors);
  192. return false;
  193. }
  194. // IDS from xmlLoader are stored in order to use them for fixtures
  195. $this->xml_loader_ids = $xml_loader->getIds();
  196. unset($xml_loader);
  197. // Install custom SQL data (db_data.sql file)
  198. if (file_exists(_PS_INSTALL_DATA_PATH_.'db_data.sql'))
  199. {
  200. $sql_loader = new InstallSqlLoader();
  201. $sql_loader->setMetaData(array(
  202. 'PREFIX_' => _DB_PREFIX_,
  203. 'ENGINE_TYPE' => _MYSQL_ENGINE_,
  204. ));
  205. $sql_loader->parse_file(_PS_INSTALL_DATA_PATH_.'db_data.sql', false);
  206. if ($errors = $sql_loader->getErrors())
  207. {
  208. $this->setError($errors);
  209. return false;
  210. }
  211. }
  212. // Copy language default images (we do this action after database in populated because we need image types information)
  213. foreach ($languages as $iso)
  214. $this->copyLanguageImages($iso);
  215. return true;
  216. }
  217. public function createShop($shop_name)
  218. {
  219. // Create default group shop
  220. $shop_group = new ShopGroup();
  221. $shop_group->name = 'Default';
  222. $shop_group->active = true;
  223. if (!$shop_group->add())
  224. {
  225. $this->setError($this->language->l('Cannot create group shop').' / '.Db::getInstance()->getMsgError());
  226. return false;
  227. }
  228. // Create default shop
  229. $shop = new Shop();
  230. $shop->active = true;
  231. $shop->id_shop_group = $shop_group->id;
  232. $shop->id_category = 2;
  233. $shop->id_theme = 1;
  234. $shop->name = $shop_name;
  235. if (!$shop->add())
  236. {
  237. $this->setError($this->language->l('Cannot create shop').' / '.Db::getInstance()->getMsgError());
  238. return false;
  239. }
  240. Context::getContext()->shop = $shop;
  241. // Create default shop URL
  242. $shop_url = new ShopUrl();
  243. $shop_url->domain = Tools::getHttpHost();
  244. $shop_url->domain_ssl = Tools::getHttpHost();
  245. $shop_url->physical_uri = __PS_BASE_URI__;
  246. $shop_url->id_shop = $shop->id;
  247. $shop_url->main = true;
  248. $shop_url->active = true;
  249. if (!$shop_url->add())
  250. {
  251. $this->setError($this->language->l('Cannot create shop URL').' / '.Db::getInstance()->getMsgError());
  252. return false;
  253. }
  254. return true;
  255. }
  256. /**
  257. * Install languages
  258. *
  259. * @return array Association between ID and iso array(id_lang => iso, ...)
  260. */
  261. public function installLanguages()
  262. {
  263. $languages = array();
  264. foreach ($this->language->getIsoList() as $iso)
  265. {
  266. if (!file_exists(_PS_INSTALL_LANGS_PATH_.$iso.'/language.xml'))
  267. throw new PrestashopInstallerException($this->language->l('File "language.xml" not found for language iso "%s"', $iso));
  268. if (!$xml = @simplexml_load_file(_PS_INSTALL_LANGS_PATH_.$iso.'/language.xml'))
  269. throw new PrestashopInstallerException($this->language->l('File "language.xml" not valid for language iso "%s"', $iso));
  270. // Add language in database
  271. $language = new Language();
  272. $language->iso_code = $iso;
  273. $language->active = ($iso == $this->language->getLanguageIso()) ? true : false;
  274. $language->name = ($xml->name) ? (string)$xml->name : 'Unknown (Unknown)';
  275. $language->language_code = ($xml->language_code) ? (string)$xml->language_code : $iso;
  276. $language->date_format_lite = ($xml->date_format_lite) ? (string)$xml->date_format_lite : 'm/j/Y';
  277. $language->date_format_full = ($xml->date_format_full) ? (string)$xml->date_format_full : 'm/j/Y H:i:s';
  278. $language->is_rtl = ($xml->is_rtl && ($xml->is_rtl == 'true' || $xml->is_rtl == '1')) ? 1 : 0;
  279. if (!$language->add())
  280. throw new PrestashopInstallerException($this->language->l('Cannot install language "%s"', ($xml->name) ? $xml->name : $iso));
  281. $languages[$language->id] = $iso;
  282. // Copy language flag
  283. if (is_writable(_PS_IMG_DIR_.'l/'))
  284. if (!copy(_PS_INSTALL_LANGS_PATH_.$iso.'/flag.jpg', _PS_IMG_DIR_.'l/'.$language->id.'.jpg'))
  285. throw new PrestashopInstallerException($this->language->l('Cannot copy flag language "%s"', _PS_INSTALL_LANGS_PATH_.$iso.'/flag.jpg => '._PS_IMG_DIR_.'l/'.$language->id.'.jpg'));
  286. }
  287. return $languages;
  288. }
  289. public function copyLanguageImages($iso)
  290. {
  291. $img_path = _PS_INSTALL_LANGS_PATH_.$iso.'/img/';
  292. if (!is_dir($img_path))
  293. return;
  294. $list = array(
  295. 'products' => _PS_PROD_IMG_DIR_,
  296. 'categories', _PS_CAT_IMG_DIR_,
  297. 'manufacturers' => _PS_MANU_IMG_DIR_,
  298. 'suppliers' => _PS_SUPP_IMG_DIR_,
  299. 'scenes' => _PS_SCENE_IMG_DIR_,
  300. 'stores' => _PS_STORE_IMG_DIR_,
  301. null => _PS_IMG_DIR_.'l/', // Little trick to copy images in img/l/ path with all types
  302. );
  303. foreach ($list as $cat => $dst_path)
  304. {
  305. if (!is_writable($dst_path))
  306. continue;
  307. copy($img_path.$iso.'.jpg', $dst_path.$iso.'.jpg');
  308. $types = ImageType::getImagesTypes($cat);
  309. foreach ($types as $type)
  310. {
  311. if (file_exists($img_path.$iso.'-default-'.$type['name'].'.jpg'))
  312. copy($img_path.$iso.'-default-'.$type['name'].'.jpg', $dst_path.$iso.'-default-'.$type['name'].'.jpg');
  313. else
  314. ImageManager::resize($img_path.$iso.'.jpg', $dst_path.$iso.'-default-'.$type['name'].'.jpg', $type['width'], $type['height']);
  315. }
  316. }
  317. }
  318. /**
  319. * PROCESS : configureShop
  320. * Set default shop configuration
  321. */
  322. public function configureShop(array $data = array())
  323. {
  324. // Clear smarty cache
  325. $this->clearSmartyCache();
  326. //clear image cache in tmp folder
  327. Tools::deleteDirectory(_PS_TMP_IMG_DIR_, false);
  328. $default_data = array(
  329. 'shop_name' => 'My Shop',
  330. 'shop_activity' => '',
  331. 'shop_country' => 'us',
  332. 'shop_timezone' => 'US/Eastern',
  333. 'use_smtp' => false,
  334. 'smtp_server' => '',
  335. 'smtp_login' => '',
  336. 'smtp_password' => '',
  337. 'smtp_encryption' =>'off',
  338. 'smtp_port' => 25,
  339. );
  340. foreach ($default_data as $k => $v)
  341. if (!isset($data[$k]))
  342. $data[$k] = $v;
  343. Context::getContext()->shop = new Shop(1);
  344. Configuration::loadConfiguration();
  345. $id_country = Country::getByIso($data['shop_country']);
  346. // Set default configuration
  347. Configuration::updateGlobalValue('PS_SHOP_DOMAIN', Tools::getHttpHost());
  348. Configuration::updateGlobalValue('PS_SHOP_DOMAIN_SSL', Tools::getHttpHost());
  349. Configuration::updateGlobalValue('PS_INSTALL_VERSION', _PS_INSTALL_VERSION_);
  350. Configuration::updateGlobalValue('PS_LOCALE_LANGUAGE', $this->language->getLanguageIso());
  351. Configuration::updateGlobalValue('PS_SHOP_NAME', $data['shop_name']);
  352. Configuration::updateGlobalValue('PS_SHOP_ACTIVITY', $data['shop_activity']);
  353. Configuration::updateGlobalValue('PS_COUNTRY_DEFAULT', $id_country);
  354. Configuration::updateGlobalValue('PS_LOCALE_COUNTRY', $data['shop_country']);
  355. Configuration::updateGlobalValue('PS_TIMEZONE', $data['shop_timezone']);
  356. Configuration::updateGlobalValue('PS_CONFIGURATION_AGREMENT', (int)$data['configuration_agrement']);
  357. // Set mails configuration
  358. Configuration::updateGlobalValue('PS_MAIL_METHOD', ($data['use_smtp']) ? 2 : 1);
  359. Configuration::updateGlobalValue('PS_MAIL_SERVER', $data['smtp_server']);
  360. Configuration::updateGlobalValue('PS_MAIL_USER', $data['smtp_login']);
  361. Configuration::updateGlobalValue('PS_MAIL_PASSWD', $data['smtp_password']);
  362. Configuration::updateGlobalValue('PS_MAIL_SMTP_ENCRYPTION', $data['smtp_encryption']);
  363. Configuration::updateGlobalValue('PS_MAIL_SMTP_PORT', $data['smtp_port']);
  364. // Activate rijndael 128 encrypt algorihtm if mcrypt is activated
  365. Configuration::updateGlobalValue('PS_CIPHER_ALGORITHM', function_exists('mcrypt_encrypt') ? 1 : 0);
  366. // Set logo configuration
  367. if (file_exists(_PS_IMG_DIR_.'logo.jpg'))
  368. {
  369. list($width, $height) = getimagesize(_PS_IMG_DIR_.'logo.jpg');
  370. Configuration::updateGlobalValue('SHOP_LOGO_WIDTH', round($width));
  371. Configuration::updateGlobalValue('SHOP_LOGO_HEIGHT', round($height));
  372. }
  373. // Active only the country selected by the merchant
  374. Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'country SET active = 0 WHERE id_country != '.(int)$id_country);
  375. // Set localization configuration
  376. $version = str_replace('.', '', _PS_VERSION_);
  377. $version = substr($version, 0, 2);
  378. $localization_file_content = @Tools::file_get_contents('http://api.prestashop.com/localization/'.$version.'/'.$data['shop_country'].'.xml');
  379. if (!@simplexml_load_string($localization_file_content))
  380. $localization_file_content = false;
  381. if (!$localization_file_content)
  382. {
  383. $localization_file = _PS_ROOT_DIR_.'/localization/default.xml';
  384. if (file_exists(_PS_ROOT_DIR_.'/localization/'.$data['shop_country'].'.xml'))
  385. $localization_file = _PS_ROOT_DIR_.'/localization/'.$data['shop_country'].'.xml';
  386. $localization_file_content = file_get_contents($localization_file);
  387. }
  388. $locale = new LocalizationPackCore();
  389. $locale->loadLocalisationPack($localization_file_content, '', true);
  390. // Create default employee
  391. if (isset($data['admin_firstname']) && isset($data['admin_lastname']) && isset($data['admin_password']) && isset($data['admin_email']))
  392. {
  393. $employee = new Employee();
  394. $employee->firstname = Tools::ucfirst($data['admin_firstname']);
  395. $employee->lastname = Tools::ucfirst($data['admin_lastname']);
  396. $employee->email = $data['admin_email'];
  397. $employee->passwd = md5(_COOKIE_KEY_.$data['admin_password']);
  398. $employee->last_passwd_gen = date('Y-m-d h:i:s', strtotime('-360 minutes'));
  399. $employee->bo_theme = 'default';
  400. $employee->active = true;
  401. $employee->id_profile = 1;
  402. $employee->id_lang = Configuration::get('PS_LANG_DEFAULT');
  403. $employee->bo_show_screencast = 1;
  404. if (!$employee->add())
  405. {
  406. $this->setError($this->language->l('Cannot create admin account'));
  407. return false;
  408. }
  409. }
  410. else
  411. {
  412. $this->setError($this->language->l('Cannot create admin account'));
  413. return false;
  414. }
  415. // Update default contact
  416. if (isset($data['admin_email']))
  417. {
  418. Configuration::updateGlobalValue('PS_SHOP_EMAIL', $data['admin_email']);
  419. $contacts = new Collection('Contact');
  420. foreach ($contacts as $contact)
  421. {
  422. $contact->email = $data['admin_email'];
  423. $contact->update();
  424. }
  425. }
  426. return true;
  427. }
  428. /**
  429. * Clear smarty cache folders
  430. */
  431. public function clearSmartyCache()
  432. {
  433. foreach (array(_PS_CACHE_DIR_.'smarty/cache', _PS_CACHE_DIR_.'smarty/compile') as $dir)
  434. if (file_exists($dir))
  435. foreach (scandir($dir) as $file)
  436. if ($file[0] != '.' && $file != 'index.php')
  437. @unlink($dir.$file);
  438. }
  439. public function getModulesList()
  440. {
  441. // @todo REMOVE DEV MODE
  442. $modules = array();
  443. if (false)
  444. {
  445. foreach (scandir(_PS_MODULE_DIR_) as $module)
  446. if ($module[0] != '.' && is_dir(_PS_MODULE_DIR_.$module) && file_exists(_PS_MODULE_DIR_.$module.'/'.$module.'.php'))
  447. $modules[] = $module;
  448. }
  449. else
  450. {
  451. // @todo THIS CODE NEED TO BE REMOVED WHEN MODULES API IS COMMITED
  452. $modules = array(
  453. 'bankwire',
  454. 'blockadvertising',
  455. 'blockbestsellers',
  456. 'blockcart',
  457. 'blockcategories',
  458. 'blockcms',
  459. 'blockcontact',
  460. 'blockcontactinfos',
  461. 'blockcurrencies',
  462. 'blockcustomerprivacy',
  463. 'blocklanguages',
  464. 'blockmanufacturer',
  465. 'blockmyaccount',
  466. 'blockmyaccountfooter',
  467. 'blocknewproducts',
  468. 'blocknewsletter',
  469. 'blockpaymentlogo',
  470. 'blockpermanentlinks',
  471. 'blockreinsurance',
  472. 'blocksearch',
  473. 'blocksharefb',
  474. 'blocksocial',
  475. 'blockspecials',
  476. 'blockstore',
  477. 'blocksupplier',
  478. 'blocktags',
  479. 'blocktopmenu',
  480. 'blockuserinfo',
  481. 'blockviewed',
  482. 'cheque',
  483. 'favoriteproducts',
  484. 'feeder',
  485. 'graphartichow',
  486. 'graphgooglechart',
  487. 'graphvisifire',
  488. 'graphxmlswfcharts',
  489. 'gridhtml',
  490. 'gsitemap',
  491. 'homefeatured',
  492. 'homeslider',
  493. 'moneybookers',
  494. 'pagesnotfound',
  495. 'sekeywords',
  496. 'statsbestcategories',
  497. 'statsbestcustomers',
  498. 'statsbestproducts',
  499. 'statsbestsuppliers',
  500. 'statsbestvouchers',
  501. 'statscarrier',
  502. 'statscatalog',
  503. 'statscheckup',
  504. 'statsdata',
  505. 'statsequipment',
  506. 'statsforecast',
  507. 'statslive',
  508. 'statsnewsletter',
  509. 'statsorigin',
  510. 'statspersonalinfos',
  511. 'statsproduct',
  512. 'statsregistrations',
  513. 'statssales',
  514. 'statssearch',
  515. 'statsstock',
  516. 'statsvisits',
  517. 'themeinstallator',
  518. );
  519. }
  520. return $modules;
  521. }
  522. /**
  523. * PROCESS : installModules
  524. * Install all modules in ~/modules/ directory
  525. */
  526. public function installModules($module = null)
  527. {
  528. $modules = $module ? array($module) : $this->getModulesList();
  529. $errors = array();
  530. foreach ($modules as $module_name)
  531. {
  532. if (!file_exists(_PS_MODULE_DIR_.$module_name.'/'.$module_name.'.php'))
  533. continue;
  534. $module = Module::getInstanceByName($module_name);
  535. if (!$module->install())
  536. $errors[] = $this->language->l('Cannot install module "%s"', $module_name);
  537. }
  538. if ($errors)
  539. {
  540. $this->setError($errors);
  541. return false;
  542. }
  543. return true;
  544. }
  545. /**
  546. * PROCESS : installFixtures
  547. * Install fixtures (E.g. demo products)
  548. */
  549. public function installFixtures($entity = null)
  550. {
  551. // Load class (use fixture class if one exists, or use InstallXmlLoader)
  552. if (file_exists(_PS_INSTALL_FIXTURES_PATH_.'apple/install.php'))
  553. {
  554. require_once _PS_INSTALL_FIXTURES_PATH_.'apple/install.php';
  555. $class = 'InstallFixtures'.Tools::toCamelCase('apple');
  556. if (!class_exists($class, false))
  557. {
  558. $this->setError($this->language->l('Fixtures class "%s" not found', $class));
  559. return false;
  560. }
  561. $xml_loader = new $class();
  562. if (!$xml_loader instanceof InstallXmlLoader)
  563. {
  564. $this->setError($this->language->l('"%s" must be an instane of "InstallXmlLoader"', $class));
  565. return false;
  566. }
  567. }
  568. else
  569. $xml_loader = new InstallXmlLoader();
  570. // Install XML data (data/xml/ folder)
  571. $xml_loader->setFixturesPath();
  572. if (isset($this->xml_loader_ids) && $this->xml_loader_ids)
  573. $xml_loader->setIds($this->xml_loader_ids);
  574. $languages = array();
  575. foreach (Language::getLanguages(false) as $lang)
  576. $languages[$lang['id_lang']] = $lang['iso_code'];
  577. $xml_loader->setLanguages($languages);
  578. if ($entity)
  579. $xml_loader->populateEntity($entity);
  580. else
  581. $xml_loader->populateFromXmlFiles();
  582. if ($errors = $xml_loader->getErrors())
  583. {
  584. $this->setError($errors);
  585. return false;
  586. }
  587. // IDS from xmlLoader are stored in order to use them for fixtures
  588. $this->xml_loader_ids = $xml_loader->getIds();
  589. unset($xml_loader);
  590. // Index products in search tables
  591. Search::indexation(true);
  592. return true;
  593. }
  594. /**
  595. * PROCESS : installTheme
  596. * Install theme
  597. */
  598. public function installTheme()
  599. {
  600. // @todo do a real install of the theme
  601. $sql_loader = new InstallSqlLoader();
  602. $sql_loader->setMetaData(array(
  603. 'PREFIX_' => _DB_PREFIX_,
  604. 'ENGINE_TYPE' => _MYSQL_ENGINE_,
  605. ));
  606. $sql_loader->parse_file(_PS_INSTALL_DATA_PATH_.'theme.sql', false);
  607. if ($errors = $sql_loader->getErrors())
  608. {
  609. $this->setError($errors);
  610. return false;
  611. }
  612. }
  613. }