Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
klevron committed Nov 28, 2014
0 parents commit 13e5ce1
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 0 deletions.
12 changes: 12 additions & 0 deletions KCFinderAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace iutbay\yii2kcfinder;

use yii\web\AssetBundle;

class KCFinderAsset extends AssetBundle
{

public $sourcePath = '@vendor/sunhater/kcfinder';

}
186 changes: 186 additions & 0 deletions KCFinderInputWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

namespace iutbay\yii2kcfinder;

use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use yii\widgets\InputWidget;

use iutbay\yii2fontawesome\FontAwesome;

/**
* KCFinder Input Widget.
* @author Kevin LEVRON <[email protected]>
*/
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 = '<div class="input-group">{button}</div><div class="input-group">{thumbs}</div>';
public $thumbTemplate = '<li><img src="{thumbSrc}" /><input type="hidden" name="{inputName}" value="{inputValue}"></li>';

/**
* 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 = '<ul class="kcf-thumbs" id="'.$this->getThumbsId().'"></ul>';

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

}
29 changes: 29 additions & 0 deletions KCFinderWidgetAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace iutbay\yii2kcfinder;

use yii\web\AssetBundle;

/**
* This declares the asset files required by KCFinder Widget.
* @author Kevin LEVRON <[email protected]>
*/
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',
];

}
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions assets/kcfinder.css
Original file line number Diff line number Diff line change
@@ -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;
}
96 changes: 96 additions & 0 deletions assets/kcfinder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* KCFinderInputWidget.
* This is the Jquery plugin used by KCFinderInputWidget.
* @author Kevin LEVRON <[email protected]>
*/
(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));
5 changes: 5 additions & 0 deletions assets/kcfinder.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.kcf-input-group {
.kcf-thumbs {
list-style-type: none;
}
}
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"require": {
"sunhater/kcfinder": "*"
},
"autoload": {
"psr-4": {"iutbay\\yii2kcfinder\\": ""}
}
}

0 comments on commit 13e5ce1

Please sign in to comment.