Source of file ContactComponent.php
Size: 9,840 Bytes - Last Modified: 2021-12-23T10:36:44+00:00
/var/www/docs.ssmods.com/process/src/src/Components/ContactComponent.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 | <?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\Components * @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\Components; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\DropdownField; use SilverWare\Components\BaseComponent; use SilverWare\Contact\Model\ContactItem; use SilverWare\Extensions\Style\AlignmentStyle; use SilverWare\Forms\FieldSection; /** * An extension of the base component class for a contact component. * * @package SilverWare\Contact\Components * @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 ContactComponent extends BaseComponent { /** * Define constants. */ const ITEM_MODE_BLOCK = 'block'; const ITEM_MODE_INLINE = 'inline'; const ITEM_MODE_LIST = 'list'; /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = 'Contact Component'; /** * Human-readable plural name. * * @var string * @config */ private static $plural_name = 'Contact Components'; /** * Description of this object. * * @var string * @config */ private static $description = 'A component which shows contact information'; /** * Icon file for this object. * * @var string * @config */ private static $icon = 'silverware/contact: admin/client/dist/images/icons/ContactComponent.png'; /** * Defines the table name to use for this object. * * @var string * @config */ private static $table_name = 'SilverWare_ContactComponent'; /** * Defines an ancestor class to hide from the admin interface. * * @var string * @config */ private static $hide_ancestor = BaseComponent::class; /** * Maps field names to field types for this object. * * @var array * @config */ private static $db = [ 'HeadingLevel' => 'Varchar(2)', 'ItemMode' => 'Varchar(8)', 'HideTitles' => 'Boolean', 'ShowIcons' => 'Boolean' ]; /** * Defines the default values for the fields of this object. * * @var array * @config */ private static $defaults = [ 'HideTitles' => 0, 'ShowIcons' => 1 ]; /** * Defines the allowed children for this object. * * @var array|string * @config */ private static $allowed_children = [ ContactItem::class ]; /** * Defines the extension classes to apply to this object. * * @var array * @config */ private static $extensions = [ AlignmentStyle::class ]; /** * Defines the default item mode to use. * * @var string * @config */ private static $default_item_mode = 'block'; /** * Defines the default heading level to use. * * @var array * @config */ private static $heading_level_default = 'h4'; /** * Answers a list of field objects for the CMS interface. * * @return FieldList */ public function getCMSFields() { // Obtain Field Objects (from parent): $fields = parent::getCMSFields(); // Define Placeholder: $placeholder = _t(__CLASS__ . '.DROPDOWNDEFAULT', '(default)'); // Create Style Fields: $fields->addFieldsToTab( 'Root.Style', [ FieldSection::create( 'ContactStyle', $this->fieldLabel('ContactStyle'), [ DropdownField::create( 'HeadingLevel', $this->fieldLabel('HeadingLevel'), $this->getTitleLevelOptions() )->setEmptyString(' ')->setAttribute('data-placeholder', $placeholder), DropdownField::create( 'ItemMode', $this->fieldLabel('ItemMode'), $this->getItemModeOptions() )->setEmptyString(' ')->setAttribute('data-placeholder', $placeholder) ] ) ] ); // Create Options Fields: $fields->addFieldsToTab( 'Root.Options', [ FieldSection::create( 'ContactOptions', $this->fieldLabel('ContactOptions'), [ CheckboxField::create( 'HideTitles', $this->fieldLabel('HideTitles') ), CheckboxField::create( 'ShowIcons', $this->fieldLabel('ShowIcons') ) ] ) ] ); // Answer Field Objects: return $fields; } /** * 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['ItemMode'] = _t(__CLASS__ . '.ITEMMODE', 'Item mode'); $labels['ShowIcons'] = _t(__CLASS__ . '.SHOWICONS', 'Show icons'); $labels['HideTitles'] = _t(__CLASS__ . '.HIDETITLES', 'Hide titles'); $labels['HeadingLevel'] = _t(__CLASS__ . '.HEADINGLEVEL', 'Heading level'); $labels['ContactStyle'] = $labels['ContactOptions'] = _t(__CLASS__ . '.CONTACT', 'Contact'); // Answer Field Labels: return $labels; } /** * Answers an array of wrapper class names for the HTML template. * * @return array */ public function getWrapperClassNames() { $classes = ['items']; $classes[] = $this->getItemModeClass(); if ($this->ShowIcons && $this->IsList) { $classes[] = 'fa-ul'; } $this->extend('updateWrapperClassNames', $classes); return $classes; } /** * Answers the tag for the item wrapper element. * * @return string */ public function getWrapperTag() { return $this->IsList ? 'ul' : 'div'; } /** * Answers the item mode class for the receiver. * * @return string */ public function getItemModeClass() { return sprintf('item-mode-%s', $this->ItemMode ? $this->ItemMode : $this->config()->default_item_mode); } /** * Answers a list of all items within the receiver. * * @return DataList */ public function getItems() { return $this->getAllChildren(); } /** * Answers a list of the enabled items within the receiver. * * @return ArrayList */ public function getEnabledItems() { return $this->getItems()->filterByCallback(function ($item) { return $item->isEnabled(); }); } /** * Answers the first enabled item found matching the given class name. * * @param string $class * * @return ContactItem */ public function getEnabledItemByClass($class) { return $this->getEnabledItems()->find('ClassName', $class); } /** * Answers the heading tag for the receiver. * * @return string */ public function getHeadingTag() { if ($tag = $this->getField('HeadingLevel')) { return $tag; } return $this->config()->heading_level_default; } /** * Answers an array of custom CSS required for the template. * * @return array */ public function getCustomCSS() { $css = parent::getCustomCSS(); foreach ($this->getEnabledItems() as $item) { $css = array_merge($css, $item->getCustomCSS()); } return $css; } /** * Answers true if the object is disabled within the template. * * @return boolean */ public function isDisabled() { if (!$this->getEnabledItems()->exists()) { return true; } return parent::isDisabled(); } /** * Answers true if the receiver is set to list item mode. * * @return boolean */ public function getIsList() { return ($this->ItemMode == self::ITEM_MODE_LIST); } /** * Answers an array of options for the item mode field. * * @return array */ public function getItemModeOptions() { return [ self::ITEM_MODE_BLOCK => _t(__CLASS__ . '.BLOCK', 'Block'), self::ITEM_MODE_INLINE => _t(__CLASS__ . '.INLINE', 'Inline'), self::ITEM_MODE_LIST => _t(__CLASS__ . '.LIST', 'List') ]; } } |