Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
johanzandstra committed Oct 6, 2017
1 parent 0c80898 commit 3b5748d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 55 deletions.
8 changes: 2 additions & 6 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@

class Plugin extends \craft\base\Plugin
{

public function init()
{
parent::init();

// Register field type
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = Iconpicker::class;
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function (RegisterComponentTypesEvent $event) {
$event->types[] = Iconpicker::class;
});
}

}

?>
2 changes: 1 addition & 1 deletion src/assets/appAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public function init()

parent::init();
}
}
}
2 changes: 1 addition & 1 deletion src/assets/sharedAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public function init()

parent::init();
}
}
}
57 changes: 27 additions & 30 deletions src/fields/Iconpicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use yii\db\Schema;
use yii\helpers\ArrayHelper;


class Iconpicker extends Field implements PreviewableFieldInterface
{
// Static
Expand All @@ -29,22 +28,22 @@ class Iconpicker extends Field implements PreviewableFieldInterface
/**
* @var string The directory where the fonts are
*/
CONST FONT_DIR = '@vendor/dolphiq/iconpicker/src/resources-shared/fonts/';
const FONT_DIR = '@vendor/dolphiq/iconpicker/src/resources-shared/fonts/';

/**
* @var array All extentions that are allowed to be imported as a font
*/
CONST FONT_EXT = ['*.woff', '*.ttf'];
const FONT_EXT = ['*.woff', '*.ttf'];

/**
* @var string Icon class
*/
CONST ICON_CLASS = 'dq-icon-';
const ICON_CLASS = 'dq-icon-';

/**
* @var string Pattern to format a safe file name
*/
CONST SAFE_NAME_PATTERN = '/[^A-Za-z0-9_\-]/';
const SAFE_NAME_PATTERN = '/[^A-Za-z0-9_\-]/';

/**
* @inheritdoc
Expand Down Expand Up @@ -167,13 +166,14 @@ public function normalizeValue($value, ElementInterface $element = null)
* @return array
*/

private function getFonts(){
if(empty($this->fonts)) {
private function getFonts()
{
if (empty($this->fonts)) {
$files = FileHelper::findFiles(Craft::getAlias(self::FONT_DIR), ['only' => self::FONT_EXT]);
$filenames = [];
$fonts = [];

foreach ($files as $file){
foreach ($files as $file) {
$pathInfo = pathinfo($file);
$safename = in_array($pathInfo['filename'], $filenames) ? $pathInfo['basename'] : $pathInfo['filename'];
$safename = $this->safeName($safename);
Expand All @@ -198,9 +198,10 @@ private function getFonts(){
* Returns a options list for the settings dropdown when defining a field
* @return array
*/
private function getFontOptions(){
private function getFontOptions()
{
$f = $this->getFonts();
if(!empty($f)){
if (!empty($f)) {
return ArrayHelper::map($f, 'safename', 'basename');
}
}
Expand All @@ -210,11 +211,11 @@ private function getFontOptions(){
*
* @return array|null
*/
private function getIcons(){

if(!empty($this->iconFont)) {
private function getIcons()
{
if (!empty($this->iconFont)) {
$fonts = $this->getFonts();
if(!empty($fonts) && isset($fonts[$this->iconFont])) {
if (!empty($fonts) && isset($fonts[$this->iconFont])) {
$font = Font::load($fonts[$this->iconFont]['path']);
$font->parse();

Expand All @@ -231,10 +232,11 @@ private function getIcons(){
* @param $font \FontLib\TrueType\File|null
* @return array|null
*/
private function getUnicodeList($font){
if(!empty($font)){
private function getUnicodeList($font)
{
if (!empty($font)) {
$unicodes = $font->getUnicodeCharMap();
if(!empty($unicodes)){
if (!empty($unicodes)) {
return array_keys($unicodes);
}
}
Expand All @@ -246,21 +248,20 @@ private function getUnicodeList($font){
/**
* Generate a css file that creates font families for each font file in the font directory
*/
public function getFontCss(){

public function getFontCss()
{
$sharedAsset = new sharedAsset();
$scss = "";

foreach ($this->getFonts() as $safeName => $pathInfo){
foreach ($this->getFonts() as $safeName => $pathInfo) {
$fontFile = $pathInfo['path'];
$font = Font::load($fontFile);
$font->parse();

if(!empty($font)) {
if (!empty($font)) {
$iconFontName = $safeName;

if (!empty($iconFontName)) {

$scss .= "
@font-face {
font-family: 'dq-iconpicker-".$iconFontName."';
Expand Down Expand Up @@ -288,9 +289,6 @@ public function getFontCss(){
-moz-osx-font-smoothing: grayscale;
}'."\n\n";



}
}
}
Expand All @@ -299,17 +297,16 @@ public function getFontCss(){

// Register the assetbundle that loads the generated css
Craft::$app->view->registerAssetBundle(sharedAsset::className());

}

// Get the fontname of the currently selected font.
public function getIconFontName(){

public function getIconFontName()
{
return $this->iconFont;

}

private function safeName($filename){
private function safeName($filename)
{
$name = preg_replace(self::SAFE_NAME_PATTERN, '-', $filename);
return $name;
}
Expand Down
24 changes: 13 additions & 11 deletions src/models/IconpickerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
use Craft;
use craft\base\Model;



class IconpickerModel extends Model{

class IconpickerModel extends Model
{
public $icon;
public $iconFont;

Expand All @@ -34,24 +32,28 @@ public function rules()
];
}

public function getIconChar(){
public function getIconChar()
{
return '&#'.$this->icon.';';
}

public function getIconCharHex(){
public function getIconCharHex()
{
return '&#x'.$this->getIconHex().';';
}

public function getIconHex(){
public function getIconHex()
{
return dechex($this->icon);
}

public function getIconSpan(){
public function getIconSpan()
{
return '<span class="'.$this->getIconClass().'">'.$this->getIconCharHex().'</span>';
}

public function getIconClass(){
public function getIconClass()
{
return 'dq-icon-'.$this->iconFont;
}

}
}
13 changes: 7 additions & 6 deletions src/views/main/_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

$field->getFontCss();

if(count($icons) > 0) {

if (count($icons) > 0) {
?>


Expand Down Expand Up @@ -45,8 +44,7 @@
}
} else {
echo 'No icons found in the selected font';
}
?>
} ?>
</div>
</div>
</div>
Expand All @@ -61,6 +59,9 @@
<div class="resizehandle"></div>
</div>

<?php } else { ?>
<?php
} else {
?>
<p>There is no font uploaded to the font folder of this plugin, no font selected in the field settings, or the font contains no icons</p>
<?php } ?>
<?php
} ?>

0 comments on commit 3b5748d

Please sign in to comment.