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
*/
protected $possible_sources = array('category', 'product', 'folder', 'content');
protected $possible_sources = array('category', 'product', 'folder', 'content', 'module');
/**
* @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;
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();

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