Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zauberfisch committed Jan 24, 2021
0 parents commit 4982af1
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# For more information about the properties used in this file,
# please see the EditorConfig documentation:
# http://editorconfig.org

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
indent_size = 2

# The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
3 changes: 3 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<FilesMatch "\.(php|php3|php4|php5|phtml|inc)$">
Deny from all
</FilesMatch>
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (c) 2021, Zauberfisch - www.zauberfisch.at
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# SilverStripe (inline) LinkField module

Allows adding one or multiple links to any object and saves into a single DB field
Editing happens inline in the form field, no GridField or popup is used

## Know Bugs/Limitations (fixes are being worked on)

- At the moment, there is no way to limit the amount of links a user can add
- Uploading a file only works in the popup for attaching a file. the direct "upload new" link is not working (1)
- Using normal DropdownField with indentation instead of TreeDropdownField for Page selection (1)
- Files are not automatically published

(1) Problem is a bug in the underlying dependency. Sub-Routes in ArrayListField are currently not working

## Maintainer Contact

* Zauberfisch <[email protected]>

## Requirements

* php >=7.1
* silverstripe/framework >=4.5
* zauberfisch/silverstripe-serialized-dataobject >=4

## Installation

* `composer require "zauberfisch/silverstripe-inline-linkfield"`
* rebuild manifest (flush)

## Documentation

```php
<?php

class Page extends SilverStripe\CMS\Model\SiteTree {
private static $db = [
'Buttons' => \zauberfisch\LinkField\DBLinkList::class,
];

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab( 'Root.Main', [
(new \zauberfisch\LinkField\LinkListField( 'Buttons', 'My Buttons'))
->setOrderable(true),
]);
return $fields;
}
}
```

You can also limit the types of links that are allowed (possible values are: 'internal', 'external', 'file', 'email', 'phone'):

```php
<?php

class MyClass extends \SilverStripe\ORM\DataObject {
private static $db = [
'ContactDetails' => \zauberfisch\LinkField\DBLinkList::class,
];

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab( 'Root.Main', [
(new \zauberfisch\LinkField\LinkListField( 'ContactDetails', 'My Contact Details', ['email', 'phone']))
->setOrderable(true),
]);
return $fields;
}
}
```

Accessing the values in php:
```php
$list = $page->obj('Buttons')->getValue(); // $page being a Page object with a field Buttons from the example above
foreach($list as $button) {
/** @var \zauberfisch\LinkField\Link\AbstractLink $button */
// Always available Variables: getLink(), getAbsoluteLink(), getLinkType(), getTitle(), getNewTab()
// And depending on the type: getPage() (internal), getPageID() (internal), getURL() (external), getFile() (file), getFileID() (file), getEmail() (email), getCountryPrefix() (phone), getNumber() (phone), getPhoneNumber() (phone)
$link = $button->getLink();
$absoluteLink = $button->getAbsoluteLink();
$type = $button->getLinkType(); // one of 'internal', 'external', 'file', 'email', 'phone'
$title = $button->getTitle();
$openInNewTab = $button->getNewTab();
// use the values here
}
```

Accessing the values in a template:
```html
<% loop $Buttons.getValue %>
<!-- Always available Variables: $Link, $AbsoluteLink, $LinkType, $Title, $NewTab -->
<!-- And depending on the type: $Page (internal), $PageID (internal), $URL (external), $File (file), $FileID (file), $Email (email), $CountryPrefix (phone), $Number (phone), $PhoneNumber (phone) -->
<a href="$Link" <% if $NewTab %>target="_blank"<% end_if %>>$Title</a>
<% end_loop %>
```
Empty file added _config/.gitignore
Empty file.
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "zauberfisch/silverstripe-easy-linkfield",
"description": "SilverStripe inline link field that allows adding one or multiple links to any object and saves into a single DB field",
"type": "silverstripe-vendormodule",
"keywords": [
"silverstripe",
"serialize",
"link",
"dbfield"
],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Zauberfisch",
"email": "[email protected]"
}
],
"require": {
"php": "^7.1",
"ext-json": "*",
"silverstripe/framework": "^4.5",
"silverstripe/vendor-plugin": "*",
"zauberfisch/silverstripe-serialized-dataobject": "^4"
},
"autoload": {
"psr-4": {
"zauberfisch\\LinkField\\": "src/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}
9 changes: 9 additions & 0 deletions src/DBLinkList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace zauberfisch\LinkField;

use zauberfisch\SerializedDataObject\DBField\ArrayListField;

class DBLinkList extends ArrayListField {}
43 changes: 43 additions & 0 deletions src/Link/AbstractLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace zauberfisch\LinkField\Link;

use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use zauberfisch\SerializedDataObject\AbstractDataObject;

/**
* @property string $Title
* @property string $NewTab
* @method string getTitle()
* @method string|boolean|int getNewTab()
* @method static setTitle(string $title)
* @method static setNewTab(boolean $title)
*/
abstract class AbstractLink extends AbstractDataObject {
private static $fields = [
'Title',
'NewTab',
];

public function getCMSFields(): FieldList {
return new FieldList([
new FieldGroup([
new TextField('Title', $this->fieldLabel('Title')),
new CheckboxField('NewTab', $this->fieldLabel('NewTab')),
]),
]);
}

abstract public function getLink(): string;

public function getAbsoluteLink(): string {
return $this->getLink();
}

abstract public function getLinkType(): string;
}
34 changes: 34 additions & 0 deletions src/Link/EmailLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace zauberfisch\LinkField\Link;

use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\FieldList;

/**
* @property string $Email
* @method string getEmail()
* @method static setEmail(string $email)
*/
class EmailLink extends AbstractLink {
private static $fields = [
'Email',
];

public function getCMSFields(): FieldList {
$fields = parent::getCMSFields();
$fields->insertBefore('NewTab', new EmailField('Email', $this->fieldLabel('Email')));
$fields->removeByName('NewTab');
return $fields;
}

public function getLink(): string {
return sprintf('mailto:%s', $this->getEmail());
}

public function getLinkType(): string {
return 'email';
}
}
33 changes: 33 additions & 0 deletions src/Link/ExternalLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace zauberfisch\LinkField\Link;

use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;

/**
* @property string $URL
* @method string getURL()
* @method static setURL(string $url)
*/
class ExternalLink extends AbstractLink {
private static $fields = [
'URL',
];

public function getCMSFields(): FieldList {
$fields = parent::getCMSFields();
$fields->insertBefore('NewTab', new TextField('URL', $this->fieldLabel('URL')));
return $fields;
}

public function getLink(): string {
return $this->getURL();
}

public function getLinkType(): string {
return 'external';
}
}
51 changes: 51 additions & 0 deletions src/Link/FileLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace zauberfisch\LinkField\Link;

use SilverStripe\Assets\File;
use SilverStripe\Forms\FieldList;
use zauberfisch\SerializedDataObject\Form\UploadField;

/**
* @property string $FileID
* @method string|int getFileID()
* @method static setFileID(int $fileID)
*/
class FileLink extends AbstractLink {
private static $fields = [
'FileID',
];

public function getCMSFields(): FieldList {
$fields = parent::getCMSFields();
$fields->push(
(new UploadField('FileID', $this->fieldLabel('File')))
->setIsMultiUpload(false)
->setDescription(_t('zauberfisch\LinkField\Link\FileLink.UploadBugHint', '"Upload new" is currently not working. For now, please click on "Choose existing" and then upload a file there'))
);
return $fields;
}

/**
* @return \SilverStripe\ORM\DataObject|null|File
*/
public function getFile(): ?File {
return $this->getFileID() ? File::get()->byID($this->getFileID()) : null;
}

public function getLink(): string {
$file = $this->getFile();
return $file && $file->exists() ? $file->getURL() : '';
}

public function getAbsoluteLink(): string {
$file = $this->getFile();
return $file && $file->exists() ? $file->getAbsoluteURL() : '';
}

public function getLinkType(): string {
return 'file';
}
}
Loading

0 comments on commit 4982af1

Please sign in to comment.