module image

This commit is contained in:
Etienne Roudeix
2013-09-18 15:58:58 +02:00
parent 9b37bbb862
commit 45106f24bd
16 changed files with 307 additions and 11 deletions

View File

@@ -48,7 +48,7 @@ class Image extends BaseI18nLoop
/** /**
* @var array Possible image sources * @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 * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection

View 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);
}
}

View File

@@ -25,6 +25,12 @@
namespace Thelia\Module; namespace Thelia\Module;
use Symfony\Component\DependencyInjection\ContainerAware; 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 abstract class BaseModule extends ContainerAware
{ {
@@ -56,6 +62,86 @@ abstract class BaseModule extends ContainerAware
return $this->container; 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 install();
abstract public function destroy(); abstract public function destroy();

View 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
View 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;
}
}

View File

@@ -34,7 +34,7 @@ INSERT INTO `module` (`id`, `code`, `type`, `activate`, `position`, `full_namesp
(1, 'DebugBar', 1, 1, 1, 'DebugBar\\DebugBar', NOW(), NOW()), (1, 'DebugBar', 1, 1, 1, 'DebugBar\\DebugBar', NOW(), NOW()),
(2, 'Colissimo', 2, 1, 1, 'Colissimo\\Colissimo', NOW(), NOW()), (2, 'Colissimo', 2, 1, 1, 'Colissimo\\Colissimo', NOW(), NOW()),
(3, 'Cheque', 3, 1, 1, 'Cheque\\Cheque', 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 INSERT INTO `module_i18n` (`id`, `locale`, `title`, `description`, `chapo`, `postscriptum`) VALUES
('2', 'en_US', '72h delivery', NULL, NULL, NULL), ('2', 'en_US', '72h delivery', NULL, NULL, NULL),

View File

@@ -25,7 +25,6 @@ namespace Cheque;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\Country;
use Thelia\Module\BaseModule; use Thelia\Module\BaseModule;
use Thelia\Module\PaymentModuleInterface; use Thelia\Module\PaymentModuleInterface;
@@ -73,4 +72,9 @@ class Cheque extends BaseModule implements PaymentModuleInterface
// TODO: Implement destroy() method. // TODO: Implement destroy() method.
} }
public function getCode()
{
return 'Cheque';
}
} }

View File

@@ -81,4 +81,9 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
// TODO: Implement destroy() method. // TODO: Implement destroy() method.
} }
public function getCode()
{
return 'Colissimo';
}
} }

View File

@@ -41,4 +41,9 @@ class DebugBar extends BaseModule
{ {
// TODO: Implement destroy() method. // TODO: Implement destroy() method.
} }
public function getCode()
{
return 'DebugBar';
}
} }

View File

@@ -21,15 +21,15 @@
/* */ /* */
/*************************************************************************************/ /*************************************************************************************/
namespace Paypal; namespace FakeCB;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\Country; use Thelia\Model\Base\ModuleImageQuery;
use Thelia\Module\BaseModule; use Thelia\Module\BaseModule;
use Thelia\Module\PaymentModuleInterface; use Thelia\Module\PaymentModuleInterface;
class Paypal extends BaseModule implements PaymentModuleInterface class FakeCB extends BaseModule implements PaymentModuleInterface
{ {
protected $request; protected $request;
protected $dispatcher; protected $dispatcher;
@@ -59,13 +59,13 @@ class Paypal extends BaseModule implements PaymentModuleInterface
// TODO: Implement pay() method. // TODO: Implement pay() method.
} }
/**
* YOU HAVE TO IMPLEMENT HERE ABSTRACT METHODD FROM BaseModule Class
* Like install and destroy
*/
public function install() 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() public function destroy()
@@ -73,4 +73,9 @@ class Paypal extends BaseModule implements PaymentModuleInterface
// TODO: Implement destroy() method. // TODO: Implement destroy() method.
} }
public function getCode()
{
return 'FakeCB';
}
} }

View 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;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -12,6 +12,7 @@
<testsuites> <testsuites>
<testsuite name="Thelia"> <testsuite name="Thelia">
<directory>core/lib/Thelia/Tests</directory> <directory>core/lib/Thelia/Tests</directory>
<directory>local/modules/*/Tests</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <filter>