Skip to content

Commit

Permalink
Rošzíření kompileru.
Browse files Browse the repository at this point in the history
  • Loading branch information
JZechy committed Oct 29, 2016
1 parent 6fe99fa commit 3661140
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
24 changes: 15 additions & 9 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 Down Expand Up @@ -159,7 +165,7 @@ public static function getScripts($basePath) {
* Třída pro filtrování nahrávaných souborů.
* @var string
*/
private static $fileFilter;
private $fileFilter;

/**
* @var string
Expand Down Expand Up @@ -315,7 +321,7 @@ public function getFileSizeString() {
* @internal
*/
public function getFileFilter() {
return self::$fileFilter;
return $this->fileFilter;
}

/**
Expand All @@ -324,7 +330,7 @@ public function getFileFilter() {
* @return $this
*/
public function setFileFilter($fileFilter) {
self::$fileFilter = $fileFilter;
$this->fileFilter = $fileFilter;
return $this;
}

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

0 comments on commit 3661140

Please sign in to comment.