diff --git a/KCFinderAsset.php b/KCFinderAsset.php new file mode 100644 index 0000000..9264843 --- /dev/null +++ b/KCFinderAsset.php @@ -0,0 +1,12 @@ + + */ +class KCFinderInputWidget extends InputWidget +{ + + /** + * Multiple selection + * @var boolean + */ + public $multiple = false; + + /** + * KCFinder dynamic settings (using session) + * @link http://kcfinder.sunhater.com/install#dynamic + * @var array + */ + public $kcfOptions = []; + + /** + * KCFinder default dynamic settings + * @link http://kcfinder.sunhater.com/install#dynamic + * @var array + */ + public $kcfDefaultOptions = [ + 'disabled'=>false, + 'denyZipDownload' => true, + 'denyUpdateCheck' => true, + 'denyExtensionRename' => true, + 'theme' => 'default', + 'access' =>[ // @link http://kcfinder.sunhater.com/install#_access + 'files' =>[ + 'upload' => false, + 'delete' => false, + 'copy' => false, + 'move' => false, + 'rename' => false, + ], + 'dirs' =>[ + 'create' => false, + 'delete' => false, + 'rename' => false, + ], + ], + 'types'=>[ // @link http://kcfinder.sunhater.com/install#_types + 'files' => [ + 'type' => '', + ], + 'images' => [ + 'type' => '*img', + ], + ], + 'thumbsDir' => '.thumbs', + 'thumbWidth' => 100, + 'thumbHeight' => 100, + ]; + + /** + * KCFinder client options + * @var array + */ + public $clientOptions = []; + + /** + * KCFinder input parameters + * @link http://kcfinder.sunhater.com/integrate#input + * @var array + */ + public $kcfBrowseOptions = []; + + /** + * Button label + * @var string + */ + public $buttonLabel = 'Add Media'; + + /** + * Button options + * @var array + */ + public $buttonOptions = []; + + public $template = '
{button}
{thumbs}
'; + public $thumbTemplate = '
  • '; + + /** + * Initializes the widget. + */ + public function init() + { + parent::init(); + + if (!isset($this->kcfOptions['uploadURL'])) + { + $this->kcfOptions['uploadURL'] = Yii::getAlias('@web/upload'); + //$this->kcfOptions['uploadDir'] = Yii::getAlias('@app/web/upload'); + } + + $this->kcfOptions = array_merge($this->kcfDefaultOptions, $this->kcfOptions); + Yii::$app->session['KCFINDER'] = $this->kcfOptions; + + $this->clientOptions['browseOptions'] = $this->kcfBrowseOptions; + $this->clientOptions['uploadURL'] = $this->kcfOptions['uploadURL']; + $this->clientOptions['multiple'] = $this->multiple; + $this->clientOptions['inputName'] = $this->getInputName(); + $this->clientOptions['thumbsDir'] = $this->kcfOptions['thumbsDir']; + $this->clientOptions['thumbsSelector'] = '#'.$this->getThumbsId(); + $this->clientOptions['thumbTemplate'] = $this->thumbTemplate; + + $this->buttonOptions['id'] = $this->getButtonId(); + + Html::addCssClass($this->options, 'form-control'); + Html::addCssClass($this->buttonOptions, 'kcf btn btn-default'); + } + + /** + * Renders the widget. + */ + public function run() + { + $this->registerClientScript(); + + $button = Html::button(FontAwesome::icon('picture-o').' '.$this->buttonLabel, $this->buttonOptions); + + $thumbs = ''; + + echo Html::tag('div', strtr($this->template, [ + '{button}' => $button, + '{thumbs}' => $thumbs, + ]), ['class'=>'kcf-input-group']); + } + + /** + * Registers the needed JavaScript. + */ + public function registerClientScript() + { + static $kcfAssetPathRegistered = false; + + $view = $this->getView(); + KCFinderWidgetAsset::register($view); + + if (!$kcfAssetPathRegistered) + { + $assetPath = Json::encode(Yii::$app->assetManager->getPublishedUrl((new KCFinderAsset)->sourcePath)); + $view->registerJs("var kcfAssetPath = $assetPath;", View::POS_BEGIN); + $kcfAssetPathRegistered = true; + } + + $clientOptions = Json::encode($this->clientOptions); + $view->registerJs("jQuery('#{$this->buttonOptions['id']}').KCFinderInputWidget($clientOptions)"); + } + + public function getInputName() + { + if ($this->hasModel()) { + return Html::getInputName($this->model, $this->attribute); + } else { + return $this->name; + } + } + + public function getButtonId() + { + return $this->getId().'-button'; + } + + public function getThumbsId() + { + return $this->getId().'-thumbs'; + } + +} \ No newline at end of file diff --git a/KCFinderWidgetAsset.php b/KCFinderWidgetAsset.php new file mode 100644 index 0000000..bcc7ed7 --- /dev/null +++ b/KCFinderWidgetAsset.php @@ -0,0 +1,29 @@ + + */ +class KCFinderWidgetAsset extends AssetBundle +{ + + public $sourcePath = '@vendor/iutbay/yii2-kcfinder/assets'; + public $publishOptions = [ + 'forceCopy' => YII_DEBUG, + ]; + public $css = [ + 'kcfinder.css', + ]; + public $js = [ + 'kcfinder.js', + ]; + public $depends = [ + 'yii\web\JqueryAsset', + 'iutbay\yii2kcfinder\KCFinderAsset', + ]; + +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..b483657 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +KCFinder for Yii2 +================= + +[KCFinder](http://kcfinder.sunhater.com/) for Yii2. + +WIP... + +Installation +------------ +The preferred way to install this helper is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require "iutbay/yii2-kcfinder" "*" +``` + +or add + +```json +"iutbay/yii2-kcfinder" : "*" +``` + +to the require section of your application's `composer.json` file. \ No newline at end of file diff --git a/assets/kcfinder.css b/assets/kcfinder.css new file mode 100644 index 0000000..cec4d48 --- /dev/null +++ b/assets/kcfinder.css @@ -0,0 +1,5 @@ +/* line 2, D:\MovAMP_PHP5.4\mnt\www\yii2-basic\extensions\kcfinder\assets\kcfinder.less */ +@media -sass-debug-info{filename{font-family:file\:\/\/D\:\/MovAMP_PHP5\.4\/mnt\/www\/yii2-basic\/extensions\/kcfinder\/assets\/kcfinder\.less}line{font-family:\000032}} +.kcf-input-group .kcf-thumbs { + list-style-type: none; +} diff --git a/assets/kcfinder.js b/assets/kcfinder.js new file mode 100644 index 0000000..d50b4e3 --- /dev/null +++ b/assets/kcfinder.js @@ -0,0 +1,96 @@ +/** + * KCFinderInputWidget. + * This is the Jquery plugin used by KCFinderInputWidget. + * @author Kevin LEVRON + */ +(function($) { + + // KCFinderInputWidget constructor + var KCFinderInputWidget = function($element, options){ + this.options = options; + this.$button = $element; + this.$thumbs = $(this.options.thumbsSelector); + + $element.on('click', $.proxy(function(e){ + e.preventDefault(); + this.open(); + }, this)); + }; + + // KCFinderInputWidget default options + KCFinderInputWidget.DEFAULTS = { + multiple: false, + browseOptions: {}, + thumbsDir: '.thumbs' + }; + + /** + * Add thumb + * @param string url + */ + KCFinderInputWidget.prototype.addThumb = function(url) { + // empty list + if (!this.options.multiple) { + this.$thumbs.text(''); + } + + // thumb url + var uploadURL = this.options.uploadURL, + thumbsUrl = uploadURL+'/'+this.options.thumbsDir, + thumbUrl = url; + if (url.search(thumbsUrl)==-1) { + thumbUrl = url.replace(uploadURL, thumbsUrl); + } + + // add thumb + var tpl = this.options.thumbTemplate; + var thumb = tpl + .replace('{thumbSrc}', thumbUrl) + .replace('{inputName}', this.options.inputName) + .replace('{inputValue}', url); + this.$thumbs.append(thumb); + }; + + /** + * Open KCFinderInputWidget + */ + KCFinderInputWidget.prototype.open = function(){ + var kthis = this; + window.KCFinder = { + callBack: function(url) { + console.log('callBack : '+url); + kthis.addThumb(url); + window.KCFinder = null; + }, + callBackMultiple: function(files) { + console.log('callBackMultiple : '+files); + window.KCFinder = null; + for (var i; i < files.length; i++) { + kthis.addThumb(files[i]); + } + } + }; + + window.open(kcfAssetPath+'/browse.php?'+$.param(this.options.browseOptions), + 'kcfinder', 'status=0, toolbar=0, location=0, menubar=0, ' + + 'directories=0, resizable=1, scrollbars=0, width=800, height=600' + ); + }; + + /** + * JQuery plugin + */ + $.fn.KCFinderInputWidget = function(option) { + return this.each(function () { + var $this = $(this); + var data = $this.data('KCFinderInputWidget'); + var options = $.extend({}, KCFinderInputWidget.DEFAULTS, $this.data(), typeof option == 'object' && option); + + if (!data) + $this.data('KCFinderInputWidget', (data = new KCFinderInputWidget($this, options))); + if (typeof option == 'string') + data[option](); + }); + }; + +}(jQuery)); \ No newline at end of file diff --git a/assets/kcfinder.less b/assets/kcfinder.less new file mode 100644 index 0000000..edb900d --- /dev/null +++ b/assets/kcfinder.less @@ -0,0 +1,5 @@ +.kcf-input-group { + .kcf-thumbs { + list-style-type: none; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..46be827 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "iutbay/yii2-kcfinder", + "description": "KCFinder for Yii2", + "keywords": ["yii2", "extension", "kcfinder"], + "homepage": "https://github.com/iutbay/yii2-kcfinder", + "type": "yii2-extension", + "authors": [ + { + "name": "Kevin LEVRON", + "email": "kevin.levron@gmail.com" + } + ], + "require": { + "sunhater/kcfinder": "*" + }, + "autoload": { + "psr-4": {"iutbay\\yii2kcfinder\\": ""} + } +}