Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Taylor committed Sep 16, 2021
1 parent 08f9847 commit be10839
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 78 deletions.
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
{
"name": "percipioglobal/craft-shortlink",
"description": "A plugin to use your own subdomain as a url shortener",
"description": "A plugin to generate a url short link",
"type": "craft-plugin",
"version": "1.0.0",
"version": "0.0.1",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"craft-shortlink"
],
Expand Down
1 change: 1 addition & 0 deletions src/Craftshortlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected function createSettingsModel()
return new Settings();
}


/**
* @inheritdoc
*/
Expand Down
34 changes: 30 additions & 4 deletions src/fields/CraftshortlinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace percipioglobal\craftshortlink\fields;

use percipioglobal\craftshortlink\Craftshortlink;
use percipioglobal\craftshortlink\Craftshortlink as Plugin;
use percipioglobal\craftshortlink\assetbundles\craftshortlinkfield\CraftshortlinkFieldAsset;

use Craft;
Expand All @@ -33,6 +33,7 @@ class CraftshortlinkField extends Field
public $shortLinkLength = '6';
public $shortLinkFormat = 'alphanumeric';
public $shortLinkTextOnly = null;
public $shortLinkBase = null;


// Static Methods
Expand Down Expand Up @@ -89,11 +90,36 @@ public function getSettingsHtml()
return Craft::$app->getView()->renderTemplate(
'craft-shortlink/_components/fields/CraftshortlinkField_settings',
[
'field' => $this,
'field' => $this
]
);
}

private function generateShortLink($settings,$shortLink){
// get options
$shortLinkLength = $settings->shortLinkLength;
$shortLinkFormat = $settings->shortLinkFormat;
// remove any spaces / foreign chars
$shortLink = str_replace(' ', '', $shortLink); // removes all spaces
$shortLink = preg_replace('/[^A-Za-z0-9\-]/', '', $shortLink); // Removes special chars
// generate allowed char arrays
$formats = array();
$formats[] = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
$formats[] = 'abcdefghjkmnpqrstuvwxyz';
$formats[] = '1234567890';

if(!$shortLink){
foreach ($formats as $format) {
$shortLink .= $format[array_rand(str_split($format))];
}
while(strlen($shortLink) < $shortLinkLength) {
$randomFormat = $formats[array_rand($formats)];
$shortLink .= $randomFormat[array_rand(str_split($randomFormat))];
}
}
return $shortLink;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -121,10 +147,10 @@ public function getInputHtml($value, ElementInterface $element = null): string
'craft-shortlink/_components/fields/CraftshortlinkField_input',
[
'name' => $this->handle,
'value' => $value,
'value' => $this->generateShortLink($this, $value),
'field' => $this,
'id' => $id,
'namespacedId' => $namespacedId,
'namespacedId' => $namespacedId
]
);
}
Expand Down
46 changes: 0 additions & 46 deletions src/models/CraftshortlinkModel.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Settings extends Model
/**
* @var string
*/
public $shortlinkSiteFrom = null;
public $shortlinkSiteTo = null;

public $config = null;

// Public Methods
// =========================================================================
Expand Down
12 changes: 8 additions & 4 deletions src/templates/_components/fields/CraftshortlinkField_input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
#}

{% import "_includes/forms" as forms %}
{% set shortLinkBase = field.shortLinkBase %}

{{ forms.textField({
label: 'Some Field',
instructions: 'Enter some text here.',
instructions: 'Shortlink for this page: ' ~ shortLinkBase ~ value,
required: true,
id: name,
class: 'nicetext code',
name: name,
value: value})
}}
value: value,
code: true
})
}}
17 changes: 12 additions & 5 deletions src/templates/_components/fields/CraftshortlinkField_settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@

{% do view.registerAssetBundle("percipioglobal\\craftshortlink\\assetbundles\\craftshortlink\\CraftshortlinkAsset") %}

{{ forms.textField({
label: 'Shortlink Base URL'|t('craft-shortlink'),
instructions: 'Only used to populate the instruction field. Include the trailing slash.'|t('craft-shortlink'),
placeholder: 'https://short.li'|t('craft-shortlink'),
name: 'shortLinkBase',
value: field['shortLinkBase'],
required: true,
autofocus: true,
}) }}


{{ forms.selectField
({
id: 'shortLinkLength',
Expand All @@ -36,18 +47,14 @@
})
}}



{{ forms.selectField
({
id: 'shortLinkFormat',
name: 'shortLinkFormat',
label: 'Link formatting'|t('craft-shortlink'),
instructions: 'Select the default link formatting option.'|t('craft-shortlink'),
options: {
'alphanumeric': 'Alphanumeric',
'numeric': 'Numeric',
'alpha': 'Alpha'
'alphanumeric': 'Alphanumeric'
},
value: field['shortLinkFormat']
})
Expand Down
7 changes: 0 additions & 7 deletions src/templates/settings.twig
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,3 @@

{% do view.registerAssetBundle("percipioglobal\\craftshortlink\\assetbundles\\craftshortlink\\CraftshortlinkAsset") %}

{{ forms.textField({
label: 'Some Field',
instructions: 'Enter some setting here.',
id: 'someAttribute',
name: 'someAttribute',
value: settings['someAttribute']})
}}
6 changes: 1 addition & 5 deletions src/variables/CraftshortlinkVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class CraftshortlinkVariable
*/
public function exampleVariable($optional = null)
{
$result = "And away we go to the Twig template...";
if ($optional) {
$result = "I'm feeling optional today...";
}
return $result;

}
}

0 comments on commit be10839

Please sign in to comment.