module image
This commit is contained in:
@@ -48,7 +48,7 @@ class Image extends BaseI18nLoop
|
||||
/**
|
||||
* @var array Possible image sources
|
||||
*/
|
||||
protected $possible_sources = array('category', 'product', 'folder', 'content');
|
||||
protected $possible_sources = array('category', 'product', 'folder', 'content', 'module');
|
||||
|
||||
/**
|
||||
* @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
|
||||
|
||||
39
core/lib/Thelia/Exception/ModuleException.php
Executable file
39
core/lib/Thelia/Exception/ModuleException.php
Executable file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Exception;
|
||||
|
||||
class ModuleException extends \RuntimeException
|
||||
{
|
||||
const UNKNOWN_EXCEPTION = 0;
|
||||
|
||||
const CODE_NOT_FOUND = 404;
|
||||
|
||||
public function __construct($message, $code = null, $previous = null)
|
||||
{
|
||||
if ($code === null) {
|
||||
$code = self::UNKNOWN_EXCEPTION;
|
||||
}
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,12 @@
|
||||
namespace Thelia\Module;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerAware;
|
||||
use Thelia\Model\Map\ModuleImageTableMap;
|
||||
use Thelia\Tools\Image;
|
||||
use Thelia\Exception\ModuleException;
|
||||
use Thelia\Model\Module;
|
||||
use Thelia\Model\ModuleImage;
|
||||
use Thelia\Model\ModuleQuery;
|
||||
|
||||
abstract class BaseModule extends ContainerAware
|
||||
{
|
||||
@@ -56,6 +62,86 @@ abstract class BaseModule extends ContainerAware
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
public function deployImageFolder(Module $module, $folderPath)
|
||||
{
|
||||
try {
|
||||
$directoryBrowser = new \DirectoryIterator($folderPath);
|
||||
} catch(\UnexpectedValueException $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$con = \Propel\Runtime\Propel::getConnection(
|
||||
ModuleImageTableMap::DATABASE_NAME
|
||||
);
|
||||
|
||||
/* browse the directory */
|
||||
$imagePosition = 1;
|
||||
foreach($directoryBrowser as $directoryContent) {
|
||||
/* is it a file ? */
|
||||
if ($directoryContent->isFile()) {
|
||||
|
||||
$fileName = $directoryContent->getFilename();
|
||||
$filePath = $directoryContent->getPathName();
|
||||
|
||||
/* is it a picture ? */
|
||||
if( Image::isImage($filePath) ) {
|
||||
|
||||
$con->beginTransaction();
|
||||
|
||||
$image = new ModuleImage();
|
||||
$image->setModuleId($module->getId());
|
||||
$image->setPosition($imagePosition);
|
||||
$image->save();
|
||||
|
||||
$imageDirectory = sprintf("%s/../../../../local/media/images/module", __DIR__);
|
||||
$imageFileName = sprintf("%s-%d-%s", $module->getCode(), $image->getId(), $fileName);
|
||||
|
||||
$increment = 0;
|
||||
while(file_exists($imageDirectory . '/' . $imageFileName)) {
|
||||
$imageFileName = sprintf("module/%s-%d-%d-%s", $module->getCode(), $image->getId(), $increment, $fileName);
|
||||
$increment++;
|
||||
}
|
||||
|
||||
$imagePath = sprintf('%s/%s', $imageDirectory, $imageFileName);
|
||||
|
||||
if (! is_dir($imageDirectory)) {
|
||||
if(! mkdir($imageDirectory, 0777, true)) {
|
||||
$con->rollBack();
|
||||
throw new ModuleException(sprintf("Cannot create directory : %s", $imageDirectory), ModuleException::CODE_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
if(! copy($filePath, $imagePath)) {
|
||||
$con->rollBack();
|
||||
throw new ModuleException(sprintf("Cannot copy file : %s to : %s", $filePath, $imagePath), ModuleException::CODE_NOT_FOUND);
|
||||
}
|
||||
|
||||
$image->setFile($imageFileName);
|
||||
$image->save();
|
||||
|
||||
$con->commit();
|
||||
$imagePosition++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ChildModule
|
||||
* @throws \Thelia\Exception\ModuleException
|
||||
*/
|
||||
public function getModuleModel()
|
||||
{
|
||||
$moduleModel = ModuleQuery::create()->findOneByCode($this->getCode());
|
||||
|
||||
if(null === $moduleModel) {
|
||||
throw new ModuleException(sprintf("Module Code `%s` not found", $this->getCode()), ModuleException::CODE_NOT_FOUND);
|
||||
}
|
||||
|
||||
return $moduleModel;
|
||||
}
|
||||
|
||||
abstract public function getCode();
|
||||
abstract public function install();
|
||||
abstract public function destroy();
|
||||
|
||||
|
||||
53
core/lib/Thelia/Tests/Module/BaseModuleTestor.php
Normal file
53
core/lib/Thelia/Tests/Module/BaseModuleTestor.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tests\Module;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
abstract class BaseModuleTestor extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $instance;
|
||||
|
||||
abstract public function getTestedClassName();
|
||||
abstract public function getTestedInstance();
|
||||
|
||||
/*protected function getMethod($name)
|
||||
{
|
||||
$class = new \ReflectionClass($this->getTestedClassName());
|
||||
$method = $class->getMethod($name);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method;
|
||||
}*/
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->instance = $this->getTestedInstance();
|
||||
}
|
||||
}
|
||||
|
||||
44
core/lib/Thelia/Tools/Image.php
Executable file
44
core/lib/Thelia/Tools/Image.php
Executable file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Tools;
|
||||
|
||||
class Image
|
||||
{
|
||||
static public function isImage($filePath, $allowedImageTypes = null)
|
||||
{
|
||||
$imageFile = getimagesize($filePath);
|
||||
$imageType = $imageFile[2];
|
||||
|
||||
if(!is_array($allowedImageTypes) && $imageType != IMAGETYPE_UNKNOWN) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(in_array($imageType , $allowedImageTypes))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ INSERT INTO `module` (`id`, `code`, `type`, `activate`, `position`, `full_namesp
|
||||
(1, 'DebugBar', 1, 1, 1, 'DebugBar\\DebugBar', NOW(), NOW()),
|
||||
(2, 'Colissimo', 2, 1, 1, 'Colissimo\\Colissimo', NOW(), NOW()),
|
||||
(3, 'Cheque', 3, 1, 1, 'Cheque\\Cheque', NOW(), NOW()),
|
||||
(4, 'Paypal', 3, 1, 2, 'Paypal\\Paypal', NOW(), NOW());
|
||||
(4, 'FakeCB', 3, 1, 2, 'FakeCB\\FakeCB', NOW(), NOW());
|
||||
|
||||
INSERT INTO `module_i18n` (`id`, `locale`, `title`, `description`, `chapo`, `postscriptum`) VALUES
|
||||
('2', 'en_US', '72h delivery', NULL, NULL, NULL),
|
||||
|
||||
@@ -25,7 +25,6 @@ namespace Cheque;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Thelia\Model\Country;
|
||||
use Thelia\Module\BaseModule;
|
||||
use Thelia\Module\PaymentModuleInterface;
|
||||
|
||||
@@ -73,4 +72,9 @@ class Cheque extends BaseModule implements PaymentModuleInterface
|
||||
// TODO: Implement destroy() method.
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
{
|
||||
return 'Cheque';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -81,4 +81,9 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
|
||||
// TODO: Implement destroy() method.
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
{
|
||||
return 'Colissimo';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,4 +41,9 @@ class DebugBar extends BaseModule
|
||||
{
|
||||
// TODO: Implement destroy() method.
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
{
|
||||
return 'DebugBar';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,15 @@
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Paypal;
|
||||
namespace FakeCB;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Thelia\Model\Country;
|
||||
use Thelia\Model\Base\ModuleImageQuery;
|
||||
use Thelia\Module\BaseModule;
|
||||
use Thelia\Module\PaymentModuleInterface;
|
||||
|
||||
class Paypal extends BaseModule implements PaymentModuleInterface
|
||||
class FakeCB extends BaseModule implements PaymentModuleInterface
|
||||
{
|
||||
protected $request;
|
||||
protected $dispatcher;
|
||||
@@ -59,13 +59,13 @@ class Paypal extends BaseModule implements PaymentModuleInterface
|
||||
// TODO: Implement pay() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* YOU HAVE TO IMPLEMENT HERE ABSTRACT METHODD FROM BaseModule Class
|
||||
* Like install and destroy
|
||||
*/
|
||||
public function install()
|
||||
{
|
||||
// TODO: Implement install() method.
|
||||
/* insert the images from image folder if first module activation */
|
||||
$module = $this->getModuleModel();
|
||||
if(ModuleImageQuery::create()->filterByModule($module)->count() == 0) {
|
||||
$this->deployImageFolder($module, sprintf('%s/images', __DIR__));
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
@@ -73,4 +73,9 @@ class Paypal extends BaseModule implements PaymentModuleInterface
|
||||
// TODO: Implement destroy() method.
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
{
|
||||
return 'FakeCB';
|
||||
}
|
||||
|
||||
}
|
||||
54
local/modules/FakeCB/Tests/FakeCBTest.php
Executable file
54
local/modules/FakeCB/Tests/FakeCBTest.php
Executable file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace FakeCB\Tests;
|
||||
|
||||
use FakeCB\FakeCB;
|
||||
use Thelia\Tests\Module\BaseModuleTestor;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class FakeCBTest extends BaseModuleTestor
|
||||
{
|
||||
public function getTestedClassName()
|
||||
{
|
||||
return 'FakeCB\FakeCB';
|
||||
}
|
||||
|
||||
public function getTestedInstance()
|
||||
{
|
||||
return new FakeCB();
|
||||
}
|
||||
|
||||
public function testInstall()
|
||||
{
|
||||
$fakeCB = new FakeCB();
|
||||
|
||||
$fakeCB->install();
|
||||
|
||||
$out = true;
|
||||
}
|
||||
}
|
||||
BIN
local/modules/FakeCB/images/mastercard.png
Normal file
BIN
local/modules/FakeCB/images/mastercard.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.6 KiB |
BIN
local/modules/FakeCB/images/visa.png
Normal file
BIN
local/modules/FakeCB/images/visa.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
@@ -12,6 +12,7 @@
|
||||
<testsuites>
|
||||
<testsuite name="Thelia">
|
||||
<directory>core/lib/Thelia/Tests</directory>
|
||||
<directory>local/modules/*/Tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
|
||||
Reference in New Issue
Block a user