PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/Theme.php

https://gitlab.com/staging06/myproject
PHP | 387 lines | 261 code | 55 blank | 71 comment | 38 complexity | 39cc3c107bfda53a81c99f2f4be7a0ea MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 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-2015 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 ThemeCore extends ObjectModel
  27. {
  28. public $name;
  29. public $directory;
  30. public $responsive;
  31. public $default_left_column;
  32. public $default_right_column;
  33. public $product_per_page;
  34. const CACHE_FILE_CUSTOMER_THEMES_LIST = '/config/xml/customer_themes_list.xml';
  35. const CACHE_FILE_MUST_HAVE_THEMES_LIST = '/config/xml/must_have_themes_list.xml';
  36. const UPLOADED_THEME_DIR_NAME = 'uploaded';
  37. /** @var int access rights of created folders (octal) */
  38. public static $access_rights = 0775;
  39. /**
  40. * @see ObjectModel::$definition
  41. */
  42. public static $definition = array(
  43. 'table' => 'theme',
  44. 'primary' => 'id_theme',
  45. 'fields' => array(
  46. 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 64, 'required' => true),
  47. 'directory' => array('type' => self::TYPE_STRING, 'validate' => 'isDirName', 'size' => 64, 'required' => true),
  48. 'responsive' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  49. 'default_left_column' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  50. 'default_right_column' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  51. 'product_per_page' => array('type' => self::TYPE_INT, 'validate' => 'isInt')
  52. ),
  53. );
  54. public static function getThemes()
  55. {
  56. $themes = new PrestaShopCollection('Theme');
  57. $themes->orderBy('name');
  58. return $themes;
  59. }
  60. public static function getAllThemes($excluded_ids = false)
  61. {
  62. $themes = new PrestaShopCollection('Theme');
  63. if (is_array($excluded_ids) && !empty($excluded_ids)) {
  64. $themes->where('id_theme', 'notin', $excluded_ids);
  65. }
  66. $themes->orderBy('name');
  67. return $themes;
  68. }
  69. /**
  70. * return an array of all available theme (installed or not)
  71. *
  72. * @param bool $installed_only
  73. * @return array string (directory)
  74. */
  75. public static function getAvailable($installed_only = true)
  76. {
  77. static $dirlist = array();
  78. $available_theme = array();
  79. if (empty($dirlist)) {
  80. $themes = scandir(_PS_ALL_THEMES_DIR_);
  81. foreach ($themes as $theme) {
  82. if (is_dir(_PS_ALL_THEMES_DIR_.DIRECTORY_SEPARATOR.$theme) && $theme[0] != '.') {
  83. $dirlist[] = $theme;
  84. }
  85. }
  86. }
  87. if ($installed_only) {
  88. $themes = Theme::getThemes();
  89. foreach ($themes as $theme_obj) {
  90. /** @var Theme $theme_obj */
  91. $themes_dir[] = $theme_obj->directory;
  92. }
  93. foreach ($dirlist as $theme) {
  94. if (false !== array_search($theme, $themes_dir)) {
  95. $available_theme[] = $theme;
  96. }
  97. }
  98. } else {
  99. $available_theme = $dirlist;
  100. }
  101. return $available_theme;
  102. }
  103. /**
  104. * check if a theme is used by a shop
  105. *
  106. * @return bool
  107. */
  108. public function isUsed()
  109. {
  110. return Db::getInstance()->getValue('SELECT count(*)
  111. FROM '._DB_PREFIX_.'shop WHERE id_theme = '.(int)$this->id);
  112. }
  113. /**
  114. * add only theme if the directory exists
  115. *
  116. * @param bool $null_values
  117. * @param bool $autodate
  118. * @return bool Insertion result
  119. */
  120. public function add($autodate = true, $null_values = false)
  121. {
  122. if (!is_dir(_PS_ALL_THEMES_DIR_.$this->directory)) {
  123. return false;
  124. }
  125. return parent::add($autodate, $null_values);
  126. }
  127. /**
  128. * Checks if theme exists (by folder) and returns Theme object.
  129. *
  130. * @param string $directory
  131. *
  132. * @return bool|Theme
  133. */
  134. public static function getByDirectory($directory)
  135. {
  136. if (is_string($directory) && strlen($directory) > 0 && file_exists(_PS_ALL_THEMES_DIR_.$directory) && is_dir(_PS_ALL_THEMES_DIR_.$directory)) {
  137. $id_theme = (int)Db::getInstance()->getValue('SELECT id_theme FROM '._DB_PREFIX_.'theme WHERE directory="'.pSQL($directory).'"');
  138. return $id_theme ? new Theme($id_theme) : false;
  139. }
  140. return false;
  141. }
  142. public static function getInstalledThemeDirectories()
  143. {
  144. $list = array();
  145. $tmp = Db::getInstance()->executeS('SELECT `directory` FROM '._DB_PREFIX_.'theme');
  146. foreach ($tmp as $t) {
  147. $list[] = $t['directory'];
  148. }
  149. return $list;
  150. }
  151. public static function getThemeInfo($id_theme)
  152. {
  153. $theme = new Theme((int)$id_theme);
  154. $theme_arr = array();
  155. if (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml')) {
  156. $config_file = _PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml';
  157. } elseif ($theme->name == 'default-bootstrap') {
  158. $config_file = _PS_ROOT_DIR_.'/config/xml/themes/default.xml';
  159. } else {
  160. $config_file = false;
  161. }
  162. if ($config_file) {
  163. $theme_arr['theme_id'] = (int)$theme->id;
  164. $xml_theme = @simplexml_load_file($config_file);
  165. if ($xml_theme !== false) {
  166. foreach ($xml_theme->attributes() as $key => $value) {
  167. $theme_arr['theme_'.$key] = (string)$value;
  168. }
  169. foreach ($xml_theme->author->attributes() as $key => $value) {
  170. $theme_arr['author_'.$key] = (string)$value;
  171. }
  172. if ($theme_arr['theme_name'] == 'default-bootstrap') {
  173. $theme_arr['tc'] = Module::isEnabled('themeconfigurator');
  174. }
  175. }
  176. } else {
  177. // If no xml we use data from database
  178. $theme_arr['theme_id'] = (int)$theme->id;
  179. $theme_arr['theme_name'] = $theme->name;
  180. $theme_arr['theme_directory'] = $theme->directory;
  181. }
  182. return $theme_arr;
  183. }
  184. public static function getNonInstalledTheme()
  185. {
  186. $installed_theme_directories = Theme::getInstalledThemeDirectories();
  187. $not_installed_theme = array();
  188. foreach (glob(_PS_ALL_THEMES_DIR_.'*', GLOB_ONLYDIR) as $theme_dir) {
  189. $dir = basename($theme_dir);
  190. $config_file = _PS_ALL_THEMES_DIR_.$dir.'/config.xml';
  191. if (!in_array($dir, $installed_theme_directories) && @filemtime($config_file)) {
  192. if ($xml_theme = @simplexml_load_file($config_file)) {
  193. $theme = array();
  194. foreach ($xml_theme->attributes() as $key => $value) {
  195. $theme[$key] = (string)$value;
  196. }
  197. if (!empty($theme)) {
  198. $not_installed_theme[] = $theme;
  199. }
  200. }
  201. }
  202. }
  203. return $not_installed_theme;
  204. }
  205. /**
  206. * update the table PREFIX_theme_meta for the current theme
  207. * @param array $metas
  208. * @param bool $full_update If true, all the meta of the theme will be deleted prior the insert, otherwise only the current $metas will be deleted
  209. *
  210. */
  211. public function updateMetas($metas, $full_update = false)
  212. {
  213. if ($full_update) {
  214. Db::getInstance()->delete('theme_meta', 'id_theme='.(int)$this->id);
  215. }
  216. $values = array();
  217. if ($this->id > 0) {
  218. foreach ($metas as $meta) {
  219. if (!$full_update) {
  220. Db::getInstance()->delete('theme_meta', 'id_theme='.(int)$this->id.' AND id_meta='.(int)$meta['id_meta']);
  221. }
  222. $values[] = array(
  223. 'id_theme' => (int)$this->id,
  224. 'id_meta' => (int)$meta['id_meta'],
  225. 'left_column' => (int)$meta['left'],
  226. 'right_column' => (int)$meta['right']
  227. );
  228. }
  229. Db::getInstance()->insert('theme_meta', $values);
  230. }
  231. }
  232. public function hasColumns($page)
  233. {
  234. return Db::getInstance()->getRow('
  235. SELECT IFNULL(left_column, default_left_column) as left_column, IFNULL(right_column, default_right_column) as right_column
  236. FROM '._DB_PREFIX_.'theme t
  237. LEFT JOIN '._DB_PREFIX_.'theme_meta tm ON (t.id_theme = tm.id_theme)
  238. LEFT JOIN '._DB_PREFIX_.'meta m ON (m.id_meta = tm.id_meta)
  239. WHERE t.id_theme ='.(int)$this->id.' AND m.page = "'.pSQL($page).'"');
  240. }
  241. public function hasColumnsSettings($page)
  242. {
  243. return (bool)Db::getInstance()->getValue('
  244. SELECT m.`id_meta`
  245. FROM '._DB_PREFIX_.'theme t
  246. LEFT JOIN '._DB_PREFIX_.'theme_meta tm ON (t.id_theme = tm.id_theme)
  247. LEFT JOIN '._DB_PREFIX_.'meta m ON (m.id_meta = tm.id_meta)
  248. WHERE t.id_theme ='.(int)$this->id.' AND m.page = "'.pSQL($page).'"');
  249. }
  250. public function hasLeftColumn($page = null)
  251. {
  252. return (bool)Db::getInstance()->getValue(
  253. 'SELECT IFNULL(
  254. (
  255. SELECT left_column
  256. FROM '._DB_PREFIX_.'theme t
  257. LEFT JOIN '._DB_PREFIX_.'theme_meta tm ON ( t.id_theme = tm.id_theme )
  258. LEFT JOIN '._DB_PREFIX_.'meta m ON ( m.id_meta = tm.id_meta )
  259. WHERE t.id_theme ='.(int)$this->id.'
  260. AND m.page = "'.pSQL($page).'" ) , default_left_column
  261. )
  262. FROM '._DB_PREFIX_.'theme
  263. WHERE id_theme ='.(int)$this->id
  264. );
  265. }
  266. public function hasRightColumn($page = null)
  267. {
  268. return (bool)Db::getInstance()->getValue(
  269. 'SELECT IFNULL(
  270. (
  271. SELECT right_column
  272. FROM '._DB_PREFIX_.'theme t
  273. LEFT JOIN '._DB_PREFIX_.'theme_meta tm ON ( t.id_theme = tm.id_theme )
  274. LEFT JOIN '._DB_PREFIX_.'meta m ON ( m.id_meta = tm.id_meta )
  275. WHERE t.id_theme ='.(int)$this->id.'
  276. AND m.page = "'.pSQL($page).'" ) , default_right_column
  277. )
  278. FROM '._DB_PREFIX_.'theme
  279. WHERE id_theme ='.(int)$this->id);
  280. }
  281. /**
  282. * @return array|bool
  283. */
  284. public function getMetas()
  285. {
  286. if (!Validate::isUnsignedId($this->id) || $this->id == 0) {
  287. return false;
  288. }
  289. return Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'theme_meta WHERE id_theme = '.(int)$this->id);
  290. }
  291. /**
  292. * @return bool
  293. */
  294. public function removeMetas()
  295. {
  296. if (!Validate::isUnsignedId($this->id) || $this->id == 0) {
  297. return false;
  298. }
  299. return Db::getInstance()->delete('theme_meta', 'id_theme = '.(int)$this->id);
  300. }
  301. public function toggleResponsive()
  302. {
  303. // Object must have a variable called 'responsive'
  304. if (!array_key_exists('responsive', $this)) {
  305. throw new PrestaShopException('property "responsive" is missing in object '.get_class($this));
  306. }
  307. // Update only responsive field
  308. $this->setFieldsToUpdate(array('responsive' => true));
  309. // Update active responsive on object
  310. $this->responsive = !(int)$this->responsive;
  311. // Change responsive to active/inactive
  312. return $this->update(false);
  313. }
  314. public function toggleDefaultLeftColumn()
  315. {
  316. if (!array_key_exists('default_left_column', $this)) {
  317. throw new PrestaShopException('property "default_left_column" is missing in object '.get_class($this));
  318. }
  319. $this->setFieldsToUpdate(array('default_left_column' => true));
  320. $this->default_left_column = !(int)$this->default_left_column;
  321. return $this->update(false);
  322. }
  323. public function toggleDefaultRightColumn()
  324. {
  325. if (!array_key_exists('default_right_column', $this)) {
  326. throw new PrestaShopException('property "default_right_column" is missing in object '.get_class($this));
  327. }
  328. $this->setFieldsToUpdate(array('default_right_column' => true));
  329. $this->default_right_column = !(int)$this->default_right_column;
  330. return $this->update(false);
  331. }
  332. }