diff --git a/core/lib/Thelia/Core/Template/Loop/Image.php b/core/lib/Thelia/Core/Template/Loop/Image.php index 8d0c99400..6a3dbfee1 100755 --- a/core/lib/Thelia/Core/Template/Loop/Image.php +++ b/core/lib/Thelia/Core/Template/Loop/Image.php @@ -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 diff --git a/core/lib/Thelia/Exception/ModuleException.php b/core/lib/Thelia/Exception/ModuleException.php new file mode 100755 index 000000000..263a071b0 --- /dev/null +++ b/core/lib/Thelia/Exception/ModuleException.php @@ -0,0 +1,39 @@ +. */ +/* */ +/*************************************************************************************/ + +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); + } +} diff --git a/core/lib/Thelia/Module/BaseModule.php b/core/lib/Thelia/Module/BaseModule.php index a13403482..faae1e9ce 100755 --- a/core/lib/Thelia/Module/BaseModule.php +++ b/core/lib/Thelia/Module/BaseModule.php @@ -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(); diff --git a/core/lib/Thelia/Tests/Module/BaseModuleTestor.php b/core/lib/Thelia/Tests/Module/BaseModuleTestor.php new file mode 100644 index 000000000..ce70cd4ad --- /dev/null +++ b/core/lib/Thelia/Tests/Module/BaseModuleTestor.php @@ -0,0 +1,53 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Module; + + +/** + * + * @author Etienne Roudeix + * + */ +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(); + } +} + diff --git a/core/lib/Thelia/Tools/Image.php b/core/lib/Thelia/Tools/Image.php new file mode 100755 index 000000000..5fa4d3c6c --- /dev/null +++ b/core/lib/Thelia/Tools/Image.php @@ -0,0 +1,44 @@ +. */ +/* */ +/*************************************************************************************/ + +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; + } +} diff --git a/install/insert.sql b/install/insert.sql index 1882250e2..9ca1d3ba2 100755 --- a/install/insert.sql +++ b/install/insert.sql @@ -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), diff --git a/local/modules/Cheque/Cheque.php b/local/modules/Cheque/Cheque.php index 7d3ef5819..10d78c809 100755 --- a/local/modules/Cheque/Cheque.php +++ b/local/modules/Cheque/Cheque.php @@ -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'; + } + } diff --git a/local/modules/Colissimo/Colissimo.php b/local/modules/Colissimo/Colissimo.php index 80dfddd78..cfeaab994 100755 --- a/local/modules/Colissimo/Colissimo.php +++ b/local/modules/Colissimo/Colissimo.php @@ -81,4 +81,9 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface // TODO: Implement destroy() method. } + public function getCode() + { + return 'Colissimo'; + } + } diff --git a/local/modules/DebugBar/DebugBar.php b/local/modules/DebugBar/DebugBar.php index 7dde5fa8d..dcdf91313 100755 --- a/local/modules/DebugBar/DebugBar.php +++ b/local/modules/DebugBar/DebugBar.php @@ -41,4 +41,9 @@ class DebugBar extends BaseModule { // TODO: Implement destroy() method. } + + public function getCode() + { + return 'DebugBar'; + } } diff --git a/local/modules/Paypal/Config/config.xml b/local/modules/FakeCB/Config/config.xml similarity index 100% rename from local/modules/Paypal/Config/config.xml rename to local/modules/FakeCB/Config/config.xml diff --git a/local/modules/Paypal/Config/plugin.xml b/local/modules/FakeCB/Config/plugin.xml similarity index 100% rename from local/modules/Paypal/Config/plugin.xml rename to local/modules/FakeCB/Config/plugin.xml diff --git a/local/modules/Paypal/Paypal.php b/local/modules/FakeCB/FakeCB.php similarity index 84% rename from local/modules/Paypal/Paypal.php rename to local/modules/FakeCB/FakeCB.php index bc38855f3..e59651a47 100755 --- a/local/modules/Paypal/Paypal.php +++ b/local/modules/FakeCB/FakeCB.php @@ -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'; + } + } diff --git a/local/modules/FakeCB/Tests/FakeCBTest.php b/local/modules/FakeCB/Tests/FakeCBTest.php new file mode 100755 index 000000000..b3e2302f2 --- /dev/null +++ b/local/modules/FakeCB/Tests/FakeCBTest.php @@ -0,0 +1,54 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace FakeCB\Tests; + +use FakeCB\FakeCB; +use Thelia\Tests\Module\BaseModuleTestor; + +/** + * + * @author Etienne Roudeix + * + */ +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; + } +} diff --git a/local/modules/FakeCB/images/mastercard.png b/local/modules/FakeCB/images/mastercard.png new file mode 100644 index 000000000..28701c3dd Binary files /dev/null and b/local/modules/FakeCB/images/mastercard.png differ diff --git a/local/modules/FakeCB/images/visa.png b/local/modules/FakeCB/images/visa.png new file mode 100644 index 000000000..ef0447105 Binary files /dev/null and b/local/modules/FakeCB/images/visa.png differ diff --git a/phpunit.xml b/phpunit.xml index 1cad09b60..a45fc5bb3 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,6 +12,7 @@ core/lib/Thelia/Tests + local/modules/*/Tests