Skip to content

Commit

Permalink
Merge pull request #4879 from julienbourdeau/starter-theme/feat/image…
Browse files Browse the repository at this point in the history
…-types

[*] StarterTheme: define image type in theme.yml
  • Loading branch information
Maxime Biloé committed Feb 15, 2016
2 parents dcfae34 + 45e2dbc commit 8fc9449
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 27 deletions.
16 changes: 16 additions & 0 deletions install-dev/controllers/console/process.php
Expand Up @@ -142,6 +142,12 @@ public function process()
}
}

if (in_array('theme', $steps)) {
if (!$this->processInstallTheme()) {
$this->printErrors();
}
}

if ($this->datas->newsletter) {
$params = http_build_query(array(
'email' => $this->datas->admin_email,
Expand Down Expand Up @@ -274,4 +280,14 @@ public function processInstallAddonsModules()
{
return $this->model_install->installModulesAddons();
}

/**
* PROCESS : installTheme
* Install theme
*/
public function processInstallTheme()
{
$this->initializeContext();
return $this->model_install->installTheme();
}
}
22 changes: 0 additions & 22 deletions install-dev/data/xml/image_type.xml

This file was deleted.

6 changes: 6 additions & 0 deletions src/Core/Addon/Theme/ThemeChecker.php
Expand Up @@ -56,6 +56,12 @@ public function getRequiredProperties()
'author.name',
'meta.compatibility.from',
'meta.available_layouts',
'global_settings.image_types.cart_default',
'global_settings.image_types.small_default',
'global_settings.image_types.medium_default',
'global_settings.image_types.large_default',
'global_settings.image_types.home_default',
'global_settings.image_types.category_default',
];
}

Expand Down
12 changes: 11 additions & 1 deletion src/Core/Addon/Theme/ThemeManager.php
Expand Up @@ -27,6 +27,7 @@

use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShop\PrestaShop\Core\Module\HookConfigurator;
use PrestaShop\PrestaShop\Core\Image\ImageTypeRepository;
use PrestaShop\PrestaShop\Core\Addon\Theme\ThemeChecker;
use PrestaShop\PrestaShop\Core\Addon\AddonManagerInterface;
use PrestaShop\PrestaShop\Core\Addon\AddonListFilter;
Expand Down Expand Up @@ -57,7 +58,8 @@ public function __construct(
Employee $employee,
Filesystem $fs,
Finder $finder,
HookConfigurator $hookConfigurator)
HookConfigurator $hookConfigurator,
ImageTypeRepository $imageTypeRepository)
{
$this->shop = $shop;
$this->configurator = $configurator;
Expand All @@ -66,6 +68,7 @@ public function __construct(
$this->fs = $fs;
$this->finder = $finder;
$this->hookConfigurator = $hookConfigurator;
$this->imageTypeRepository = $imageTypeRepository;
}

/**
Expand Down Expand Up @@ -147,6 +150,7 @@ public function enable($name)
->doApplyConfiguration($theme->get('global_settings.configuration', []))
->doDisableModules($theme->get('global_settings.modules.to_disable', []))
->doEnableModules($theme->get('global_settings.modules.to_enable', []))
->doApplyImageTypes($theme->get('global_settings.image_types'))
->doHookModules($theme->get('global_settings.hooks.modules_to_hook'));

$theme->onEnable();
Expand Down Expand Up @@ -266,6 +270,12 @@ private function doHookModules(array $hooks)
return $this;
}

private function doApplyImageTypes(array $types)
{
$this->imageTypeRepository->setTypes($types);
return $this;
}

private function getThemesOnDisk()
{
$suffix = 'preview.png';
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Addon/Theme/ThemeManagerBuilder.php
Expand Up @@ -28,6 +28,7 @@

use PrestaShop\PrestaShop\Core\Module\HookConfigurator;
use PrestaShop\PrestaShop\Core\Module\HookRepository;
use PrestaShop\PrestaShop\Core\Image\ImageTypeRepository;
use PrestaShop\PrestaShop\Adapter\Hook\HookInformationProvider;
use PrestaShop\PrestaShop\Core\Addon\Theme\ThemeManager;
use PrestaShop\PrestaShop\Core\Addon\Theme\ThemeChecker;
Expand Down Expand Up @@ -56,6 +57,10 @@ public function __construct(Context $context, Db $db)
$context->shop,
$db
)
),
new ImageTypeRepository(
$context->shop,
$db
)
);
}
Expand Down
81 changes: 81 additions & 0 deletions src/Core/Image/ImageTypeRepository.php
@@ -0,0 +1,81 @@
<?php

namespace PrestaShop\PrestaShop\Core\Image;

use Db;
use Exception;
use Shop;

class ImageTypeRepository
{
private $shop;
private $db;
private $db_prefix;

public function __construct(
Shop $shop,
Db $db
) {
$this->shop = $shop;
$this->db = $db;
$this->db_prefix = $db->getPrefix();
}

public function setTypes(array $types)
{
$this->removeAllTypes();
foreach ($types as $name => $data) {
$this->createType(
$name,
$data['width'],
$data['height'],
$data['scope']
);
}
return $this;
}

public function createType($name, $width, $height, array $scope)
{
$data = [
'name' => $name,
'width' => $width,
'height' => $height,
];

foreach ($this->getScopeList() as $scope_item) {
if (in_array($scope_item, $scope)) {
$data[$scope_item] = 1;
} else {
$data[$scope_item] = 0;
}
}

$this->db->insert('image_type', $data);

return $this->getIdByName($name);
}

public function getScopeList()
{
return ['products', 'categories', 'manufacturers', 'suppliers', 'stores'];
}

public function getIdByName($name)
{
$escaped_name = $this->db->escape($name);

$id_image_type = $this->db->getValue(
"SELECT id_image_type FROM {$this->db_prefix}image_type WHERE name = '$escaped_name'"
);

return (int)$id_image_type;
}

protected function removeAllTypes()
{
Db::getInstance()->execute(
"TRUNCATE TABLE {$this->db_prefix}image_type"
);
}
}
13 changes: 13 additions & 0 deletions themes/StarterTheme/config/theme.dist.yml
Expand Up @@ -83,6 +83,19 @@ global_settings:
- category
- index

image_types:
# When theme will be enabled, all image types will be removed
# Template must declare their image type.
product_listing:
width: 220
height: 220
scope: [products, categories, manufacturers, suppliers]
large_banner:
width: 960
height: 400
scope: [categories]


theme_settings:
# All the settings below can be changed through
# an interface in the theme's administration panel,
Expand Down
31 changes: 27 additions & 4 deletions themes/StarterTheme/config/theme.yml
Expand Up @@ -22,17 +22,14 @@ meta:
global_settings:
configuration:
PS_QUICK_VIEW: false

hooks:
custom_hooks:
- name: displayFooterBefore
title: displayFooterBefore
description: Add a widget area above the footer
modules_to_hook:
displayNav:
# displayHeaderTop will have exactly the following
# modules hooked to it, in the specified order.
# Each module in this list will be unhooked
# from all other display hooks it is hooked to.
- blocklanguages
- blockcurrencies
- blockuserinfo
Expand All @@ -44,5 +41,31 @@ global_settings:
- blocknewsletter
- blockcontact

image_types:
cart_default:
width: 80
height: 80
scope: [products]
small_default:
width: 125
height: 125
scope: [products, categories, manufacturers, suppliers]
medium_default:
width: 300
height: 300
scope: [products, categories, manufacturers, suppliers]
large_default:
width: 500
height: 500
scope: [products]
home_default:
width: 250
height: 250
scope: [products]
category_default:
width: 960
height: 350
scope: [categories]

theme_settings:
default_layout: layout-full-width

0 comments on commit 8fc9449

Please sign in to comment.