Source of file LinkItem.php
Size: 7,197 Bytes - Last Modified: 2021-12-23T10:36:44+00:00
/var/www/docs.ssmods.com/process/src/src/Items/LinkItem.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 | <?php /** * This file is part of SilverWare. * * PHP version >=5.6.0 * * For full copyright and license information, please view the * LICENSE.md file that was distributed with this source code. * * @package SilverWare\Contact\Items * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-contact */ namespace SilverWare\Contact\Items; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\SelectionGroup; use SilverStripe\Forms\SelectionGroup_Item; use SilverStripe\Forms\TextField; use SilverWare\Contact\Model\ContactItem; use SilverWare\Forms\FieldSection; use SilverWare\Forms\PageDropdownField; use Page; /** * An extension of the contact item class for a link item. * * @package SilverWare\Contact\Items * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-contact */ class LinkItem extends ContactItem { /** * Define constants. */ const MODE_PAGE = 'page'; const MODE_URL = 'url'; /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = 'Link Item'; /** * Human-readable plural name. * * @var string * @config */ private static $plural_name = 'Link Items'; /** * Description of this object. * * @var string * @config */ private static $description = 'A contact item to show a link'; /** * Defines the table name to use for this object. * * @var string * @config */ private static $table_name = 'SilverWare_Contact_LinkItem'; /** * Defines an ancestor class to hide from the admin interface. * * @var string * @config */ private static $hide_ancestor = ContactItem::class; /** * Maps field names to field types for this object. * * @var array * @config */ private static $db = [ 'Name' => 'Varchar(255)', 'LinkTo' => 'Varchar(8)', 'LinkURL' => 'Varchar(2048)', 'OpenLinkInNewTab' => 'Boolean' ]; /** * Defines the has-one associations for this object. * * @var array * @config */ private static $has_one = [ 'LinkPage' => Page::class ]; /** * Defines the default values for the fields of this object. * * @var array * @config */ private static $defaults = [ 'LinkTo' => 'page', 'FontIcon' => 'external-link', 'OpenLinkInNewTab' => 0 ]; /** * Answers a list of field objects for the CMS interface. * * @return FieldList */ public function getCMSFields() { // Obtain Field Objects (from parent): $fields = parent::getCMSFields(); // Create Main Fields: $fields->addFieldsToTab( 'Root.Main', [ TextField::create( 'Name', $this->fieldLabel('Name') ), SelectionGroup::create( 'LinkTo', [ SelectionGroup_Item::create( self::MODE_PAGE, PageDropdownField::create( 'LinkPageID', '' ), $this->fieldLabel('Page') ), SelectionGroup_Item::create( self::MODE_URL, TextField::create( 'LinkURL', '' ), $this->fieldLabel('URL') ) ] )->setTitle($this->fieldLabel('LinkTo')) ] ); // Create Options Fields: $fields->addFieldToTab( 'Root.Options', FieldSection::create( 'LinkOptions', $this->fieldLabel('LinkOptions'), [ CheckboxField::create( 'OpenLinkInNewTab', $this->fieldLabel('OpenLinkInNewTab') ) ] ) ); // Answer Field Objects: return $fields; } /** * Answers a validator for the CMS interface. * * @return RequiredFields */ public function getCMSValidator() { return parent::getCMSValidator()->addRequiredField('Name'); } /** * Answers the labels for the fields of the receiver. * * @param boolean $includerelations Include labels for relations. * * @return array */ public function fieldLabels($includerelations = true) { // Obtain Field Labels (from parent): $labels = parent::fieldLabels($includerelations); // Define Field Labels: $labels['URL'] = _t(__CLASS__ . '.URL', 'URL'); $labels['Name'] = _t(__CLASS__ . '.NAME', 'Name'); $labels['Page'] = _t(__CLASS__ . '.PAGE', 'Page'); $labels['LinkTo'] = _t(__CLASS__ . '.LINKTO', 'Link to'); $labels['LinkURL'] = _t(__CLASS__ . '.LINKURL', 'Link URL'); $labels['LinkPageID'] = _t(__CLASS__ . '.LINKPAGE', 'Link page'); $labels['OpenLinkInNewTab'] = _t(__CLASS__ . '.OPENLINKINNEWTAB', 'Open link in new tab'); $labels['LinkOptions'] = _t(__CLASS__ . '.LINK', 'Link'); // Define Relation Labels: if ($includerelations) { $labels['LinkPage'] = _t(__CLASS__ . '.has_one_LinkPage', 'Link Page'); } // Answer Field Labels: return $labels; } /** * Answers the page link for the template. * * @return string */ public function getPageLink() { if ($this->isURL() && $this->LinkURL) { return $this->dbObject('LinkURL')->URL(); } if ($this->isPage() && $this->LinkPageID) { return $this->LinkPage()->Link(); } } /** * Answers true if the link is to a page. * * @return boolean */ public function isPage() { return ($this->LinkTo == self::MODE_PAGE); } /** * Answers true if the link is to a URL. * * @return boolean */ public function isURL() { return ($this->LinkTo == self::MODE_URL); } /** * Answers true if the item is disabled within the template. * * @return boolean */ public function isDisabled() { if (!$this->PageLink) { return true; } return parent::isDisabled(); } } |