Skip to content

Commit

Permalink
#66 Sluggable plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailDev committed Jan 6, 2017
1 parent 3721891 commit fe5a9f8
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions plugin/Sluggable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Date: 07.01.2017
* Time: 0:14
*/

namespace mihaildev\elfinder\plugin;


use mihaildev\elfinder\PluginInterface;
use yii\helpers\Inflector;

class Sluggable extends PluginInterface
{
public $bind = [
'upload.presave' => 'createSlug'
];

public $lowercase = true;

public $replacement = '-';

/**
* @return string
*/
public function getName()
{
return 'Sluggable';
}

public function createSlug(&$path, &$name, $src, $elfinder, $volume){
if(!$this->isEnable($volume))
return false;

$ext = pathinfo($name, PATHINFO_EXTENSION);
$filename = pathinfo($name, PATHINFO_FILENAME);

$lowercase = $this->getOption('lowercase', $volume);

$replacement = $this->getOption('replacement', $volume);

$filename = Inflector::slug($filename, $replacement, $lowercase);

if($lowercase)
$ext = strtolower($ext);

$name = empty($ext) ? $filename : $filename . "." . $ext;

return true;
}
}

0 comments on commit fe5a9f8

Please sign in to comment.