Skip to content

Commit

Permalink
Merge pull request #12 from JZechy/1.2
Browse files Browse the repository at this point in the history
1.2
  • Loading branch information
JZechy authored Oct 29, 2016
2 parents e6aa275 + f28a47c commit 8120d92
Show file tree
Hide file tree
Showing 13 changed files with 1,183 additions and 620 deletions.
122 changes: 105 additions & 17 deletions src/FileUploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ class FileUploadControl extends \Nette\Forms\Controls\UploadControl {
/**
* @static
* @param $systemContainer
* @param string $uploadModel
* @param array $configuration
*/
public static function register($systemContainer, $uploadModel = NULL) {
public static function register(\Nette\DI\Container $systemContainer, $configuration = []) {
$class = __CLASS__;
\Nette\Forms\Container::extensionMethod("addFileUpload", function (
\Nette\Forms\Container $container, $name, $maxFiles = 25, $maxFileSize = NULL
) use ($class, $systemContainer, $uploadModel) {
$component = new $class($name, $maxFiles, $maxFileSize);
\Nette\Forms\Container $container, $name, $maxFiles = NULL, $maxFileSize = NULL
) use ($class, $systemContainer, $configuration) {
$maxFiles = is_null($maxFiles) ? $configuration["maxFiles"] : $maxFiles;
$maxFileSize = is_null($maxFileSize) ? $configuration["maxFileSize"] : $maxFileSize;

/** @var FileUploadControl $component */
$component = new $class($name, $maxFiles, $maxFileSize);
$component->setContainer($systemContainer);
$component->setUploadModel($uploadModel);
$component->setUploadModel($configuration["uploadModel"]);
$component->setFileFilter($configuration["fileFilter"]);
$component->setUIMode($configuration["uiMode"]);

$container->addComponent($component, $name);

return $component;
Expand All @@ -36,10 +42,24 @@ public static function register($systemContainer, $uploadModel = NULL) {
* Vloží CSS do stránky.
* @static
* @param string $basePath
* @throws \Nette\DeprecatedException Use FileUploadControl::getHead()
*/
public static function getStyleSheet($basePath) {
throw new \Nette\DeprecatedException("Use FileUploadControl::getHead() instead.");

echo '<link rel="stylesheet" type="text/css" href="' . $basePath . '/fileupload/css/jquery.fileupload.css">';
echo '<link rel="stylesheet" type="text/css" href="' . $basePath . '/fileupload/style.css">';
}

/**
* Vloží CSS do stránky.
* @static
* @param string $basePath
*/
public static function getHead($basePath) {
echo '<link rel="stylesheet" type="text/css" href="' . $basePath . '/fileupload/css/jquery.fileupload.css">';
echo '<link rel="stylesheet" type="text/css" href="' . $basePath . '/fileupload/style.css">';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/functions.js"></script>';
}

/**
Expand All @@ -56,7 +76,10 @@ public static function getScripts($basePath) {
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/js/jquery.fileupload-process.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/js/jquery.fileupload-image.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/js/jquery.fileupload-video.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/functions.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/controller.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/ui/uiRenderer.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/ui/full.js"></script>';
echo '<script type="text/javascript" src="' . $basePath . '/fileupload/ui/minimal.js"></script>';
}

# --------------------------------------------------------------------
Expand Down Expand Up @@ -85,6 +108,18 @@ public static function getScripts($basePath) {
* @var string
*/
const FILTER_AUDIO = 'Zet\FileUpload\Filter\AudioFilter';

/**
* Plnohodntné a detailní rozhraní pro nahrávání souborů.
* @var int
*/
const UI_FULL = 1;

/**
* Minimální rozhraní.
* @var int
*/
const UI_MINIMAL = 2;

/**
* @var \Nette\DI\Container
Expand Down Expand Up @@ -120,17 +155,30 @@ public static function getScripts($basePath) {
* @var string
*/
private $uploadModel;

/**
* @var int
*/
private $uiMode = self::UI_FULL;

/**
* Třída pro filtrování nahrávaných souborů.
* @var string
*/
private static $fileFilter;
private $fileFilter;

/**
* @var string
*/
private $token;

/**
* @var array
*/
private $uiTemplates = [
self::UI_FULL => __DIR__ . "/Template/full.latte",
self::UI_MINIMAL => __DIR__ . "/Template/minimal.latte"
];

/**
* FileUploadControl constructor.
Expand All @@ -151,7 +199,23 @@ public function __construct($name, $maxFiles, $maxFileSize = NULL) {
$this->controller = new Model\UploadController($this);
$this->token = uniqid();
}


/**
* @param int $type
* @return int|NULL
*/
public function getUiTemplate($type) {
return isset($this->uiTemplates[$type]) ? $this->uiTemplates[$type] : NULL;
}

/**
* @param int $type
* @param string $file
*/
public function setUiTemplate($type, $file) {
$this->uiTemplates[$type] = $file;
}

/**
* @param $form
*/
Expand Down Expand Up @@ -227,6 +291,22 @@ public function setUploadModel($uploadModel) {
$this->uploadModel = $uploadModel;
return $this;
}

/**
* @param int $mode
* @return $this
*/
public function setUIMode($mode) {
$this->uiMode = $mode;
return $this;
}

/**
* @return int
*/
public function getUIMode() {
return $this->uiMode;
}

/**
* @return int
Expand Down Expand Up @@ -265,15 +345,17 @@ public function getFileSizeString() {
* @internal
*/
public function getFileFilter() {
return self::$fileFilter;
return $this->fileFilter;
}

/**
* Nastaví třídu pro filtrování nahrávaných souborů.
* @param string $fileFilter
* @return $this
*/
public function setFileFilter($fileFilter) {
self::$fileFilter = $fileFilter;
$this->fileFilter = $fileFilter;
return $this;
}

/**
Expand Down Expand Up @@ -317,11 +399,17 @@ public function getControl() {
$container->id = $this->getHtmlId() . "-container";

$token = \Nette\Utils\Html::el("input type='hidden' value='" . $this->token . "'");
$token->setAttribute("name", $this->getHtmlName() ."-token");
$container->addHtml($token);

$container->addHtml($this->controller->getJavaScriptTemplate());
$container->addHtml($this->controller->getControlTemplate());
$token->addAttributes(["name" => $this->getHtmlName() ."-token"]);

if(method_exists(\Nette\Utils\Html::class, "addHtml")) {
$container->addHtml($token);
$container->addHtml($this->controller->getJavaScriptTemplate());
$container->addHtml($this->controller->getControlTemplate());
} else { // pro starší nette
$container->add($token);
$container->add($this->controller->getJavaScriptTemplate());
$container->add($this->controller->getControlTemplate());
}

return $container;
}
Expand Down
62 changes: 62 additions & 0 deletions src/FileUploadExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Zet\FileUpload;

/**
* Class FileUploadExtension
* @author Zechy <[email protected]>
* @package Zet\FileUpload
*/
class FileUploadExtension extends \Nette\DI\CompilerExtension {

/**
* Výchozí konfigurační hodnoty.
* @var array
*/
private $defaults = [
"maxFiles" => 25,
"maxFileSize" => NULL,
"uploadModel" => NULL,
"fileFilter" => NULL,
"uiMode" => FileUploadControl::UI_FULL
];

/**
* Konfigurace nastavená uživatelem.
* @var array
*/
private $configuration = [];

/**
*
*/
public function loadConfiguration() {
$this->configuration = $this->getConfig($this->defaults);
if(is_string($this->configuration["uiMode"])) {
$value = $this->configuration["uiMode"];

switch($value) {
case "full":
$this->configuration["uiMode"] = FileUploadControl::UI_FULL;
break;
case "minimal":
$this->configuration["uiMode"] = FileUploadControl::UI_MINIMAL;
break;
default:
$this->configuration["uiMode"] = FileUploadControl::UI_FULL;
break;
}
}
}

/**
* @param \Nette\PhpGenerator\ClassType $class
*/
public function afterCompile(\Nette\PhpGenerator\ClassType $class) {
$init = $class->methods["initialize"];

$init->addBody('\Zet\FileUpload\FileUploadControl::register($this->getService(?), ?);', [
$this->getContainerBuilder()->getByType('\Nette\DI\Container'), $this->configuration
]);
}
}
15 changes: 14 additions & 1 deletion src/Model/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public function getFilter() {

return $this->filter;
}

/**
* @return string
*/
private function getControlFile() {
$template = $this->uploadControl->getUiTemplate($this->uploadControl->getUIMode());
if(is_null($template)) {
throw new \Nette\InvalidArgumentException();
}
return $template;
}

/**
* Vytvoření šablony s JavaScriptem pro FileUpload.
Expand All @@ -78,6 +89,7 @@ public function getJavaScriptTemplate() {
$template->fileSizeString = $this->uploadControl->getFileSizeString();
$template->productionMode = \Tracy\Debugger::$productionMode;
$template->token = $this->uploadControl->getToken();
$template->uiMode = $this->uploadControl->getUIMode();

return (string) $template;
}
Expand All @@ -88,7 +100,7 @@ public function getJavaScriptTemplate() {
*/
public function getControlTemplate() {
$template = $this->template;
$template->setFile(__DIR__ . "/../Template/control.latte");
$template->setFile($this->getControlFile());
$template->htmlId = $this->uploadControl->getHtmlId();
$template->htmlName = $this->uploadControl->getHtmlName();

Expand Down Expand Up @@ -172,6 +184,7 @@ public function handleRename() {

$cache = $this->uploadControl->getCache();
$cacheFiles = $cache->load($this->uploadControl->getTokenizedCacheName($token));

if(isset($cacheFiles[$id])) {
$cacheFiles[$id] = $this->uploadControl->getUploadModel()->rename($cacheFiles[$id], $newName);
$cache->save($this->uploadControl->getTokenizedCacheName($token), $cacheFiles);
Expand Down
27 changes: 0 additions & 27 deletions src/Template/control.latte

This file was deleted.

30 changes: 30 additions & 0 deletions src/Template/full.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="panel panel-primary">
<div class="panel-heading clearfix">
<h3 class="panel-title pull-left">Nahrávání souborů</h3>
<div class="btn-group pull-right">
{var $text = $maxFiles > 1 ? "Vybrat soubory" : "Vybrat soubor"}
<span class="fileinput-button btn btn-warning btn-xs" title="{$text}" data-toggle="tooltip">
<span>
<i class="glyphicon glyphicon-plus"></i>
<span class="hidden-xs">
{$text}
</span>
</span>
<input class="fileupload-input" type="file" name="{$htmlName}" id="{$htmlId}" multiple>
</span>
</div>
</div>

<div class="panel-body">
<div class="progress zet-fileupload-progress" id="{$htmlId}-progress">
<div id="{$htmlId}-progressbar" class="progress-bar progress-bar-success progress-bar-striped active" style="width: 0%">
<span id="{$htmlId}-progressbar-value">0</span>%
</div>
</div>
</div>

<table class="table zet-fileupload-table" id="{$htmlId}-table">
<tbody id="{$htmlId}-table-tbody">
</tbody>
</table>
</div>
Loading

0 comments on commit 8120d92

Please sign in to comment.