Source of file LoyalsConfigExtension.php
Size: 12,961 Bytes - Last Modified: 2021-12-23T10:02:28+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/LoyalsConfigExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 | <?php /** * An extension of the data extension class to add Loyals Admin settings to site config. */ class LoyalsConfigExtension extends DataExtension { private static $db = array( 'LoyalsLinkColor' => 'Color', 'LoyalsLogoBkgColor' => 'Color', 'LoyalsHighlightColor' => 'Color', 'LoyalsProfileLinkColor' => 'Color', 'LoyalsApplicationName' => 'Varchar(255)', 'LoyalsApplicationLink' => 'Varchar(2048)', 'LoyalsLogoImageWidth' => 'Varchar(16)', 'LoyalsLogoImageHeight' => 'Varchar(16)', 'LoyalsLogoImageResize' => 'Varchar(16)', 'LoyalsLoadingImageWidth' => 'Varchar(16)', 'LoyalsLoadingImageHeight' => 'Varchar(16)', 'LoyalsLoadingImageResize' => 'Varchar(16)', 'LoyalsSupportRetina' => 'Boolean', 'LoyalsHideSiteName' => 'Boolean' ); private static $has_one = array( 'LoyalsLogoImage' => 'Image', 'LoyalsLoadingImage' => 'Image' ); private static $defaults = array( 'LoyalsLinkColor' => '007FBA', 'LoyalsLogoBkgColor' => '1B354C', 'LoyalsHighlightColor' => '139FDA', 'LoyalsProfileLinkColor' => '3EBAE0', 'LoyalsSupportRetina' => 1, 'LoyalsHideSiteName' => 0 ); /** * @config * @var string */ private static $asset_path = "Uploads/Loyals"; /** * Answers the path to use for uploading images. * * @return string */ public static function get_asset_path() { return Config::inst()->get(__CLASS__, 'asset_path'); } /** * Answers an array of image resize methods. * * @return array */ public static function get_resize_methods() { return Config::inst()->get(__CLASS__, 'resize_methods'); } /** * Updates the CMS fields of the extended object. * * @param FieldList $fields */ public function updateCMSFields(FieldList $fields) { // Create Loyals Tab Set: $fields->addFieldToTab( 'Root', TabSet::create( 'Loyals', _t('LoyalsConfigExtension.LOYALS', 'Loyals') ) ); // Create Colors Tab: $fields->findOrMakeTab('Root.Loyals.Colors', _t('LoyalsConfigExtension.COLORS', 'Colors')); // Create Colors Fields: $fields->addFieldsToTab( 'Root.Loyals.Colors', array( ColorField::create( 'LoyalsHighlightColor', _t('LoyalsConfigExtension.HIGHLIGHTCOLOR', 'Highlight color') ), ColorField::create( 'LoyalsLogoBkgColor', _t('LoyalsConfigExtension.LOGOBACKGROUNDCOLOR', 'Logo background color') ), ColorField::create( 'LoyalsLinkColor', _t('LoyalsConfigExtension.LINKCOLOR', 'Link color') ), ColorField::create( 'LoyalsProfileLinkColor', _t('LoyalsConfigExtension.PROFILELINKCOLOR', 'Profile link color') ) ) ); // Create Branding Tab: $fields->findOrMakeTab('Root.Loyals.Branding', _t('LoyalsConfigExtension.BRANDING', 'Branding')); // Create Branding Fields: $fields->addFieldsToTab( 'Root.Loyals.Branding', array( TextField::create( 'LoyalsApplicationName', _t('LoyalsConfigExtension.APPLICATIONNAME', 'Application name') ), TextField::create( 'LoyalsApplicationLink', _t('LoyalsConfigExtension.APPLICATIONLINK', 'Application link') ), ToggleCompositeField::create( 'LoyalsLogoToggle', _t('LoyalsConfigExtension.LOGOIMAGETOGGLETITLE', 'Logo Image'), array( UploadField::create( 'LoyalsLogoImage', _t('LoyalsConfigExtension.LOGOIMAGE', 'Logo image') )->setAllowedFileCategories('image')->setFolderName(self::get_asset_path()), FieldGroup::create( _t('LoyalsConfigExtension.DIMENSIONSINPIXELS', 'Dimensions (in pixels)'), array( TextField::create('LoyalsLogoImageWidth', '')->setAttribute( 'placeholder', _t('LoyalsConfigExtension.WIDTH', 'Width') ), LiteralField::create('LoyalsLogoImageBy', '<i class="fa fa-times by"></i>'), TextField::create('LoyalsLogoImageHeight', '')->setAttribute( 'placeholder', _t('LoyalsConfigExtension.HEIGHT', 'Height') ) ) ), DropdownField::create( 'LoyalsLogoImageResize', _t('LoyalsConfigExtension.RESIZEMETHOD', 'Resize method'), self::get_resize_methods() )->setEmptyString(' '), CheckboxField::create( 'LoyalsHideSiteName', _t('LoyalsConfigExtension.HIDESITENAME', 'Hide site name') ), CheckboxField::create( 'LoyalsSupportRetina', _t('LoyalsConfigExtension.SUPPORTRETINADEVICES', 'Support Retina devices') ) ) ), ToggleCompositeField::create( 'LoyalsLoadingToggle', _t('LoyalsConfigExtension.LOADINGIMAGETOGGLETITLE', 'Loading Image'), array( UploadField::create( 'LoyalsLoadingImage', _t('LoyalsConfigExtension.LOADINGIMAGE', 'Loading image') )->setAllowedFileCategories('image')->setFolderName(self::get_asset_path()), FieldGroup::create( _t('LoyalsConfigExtension.DIMENSIONSINPIXELS', 'Dimensions (in pixels)'), array( TextField::create('LoyalsLoadingImageWidth', '')->setAttribute( 'placeholder', _t('LoyalsConfigExtension.WIDTH', 'Width') ), LiteralField::create('LoyalsLoadingImageBy', '<i class="fa fa-times by"></i>'), TextField::create('LoyalsLoadingImageHeight', '')->setAttribute( 'placeholder', _t('LoyalsConfigExtension.HEIGHT', 'Height') ) ) ), DropdownField::create( 'LoyalsLoadingImageResize', _t('LoyalsConfigExtension.RESIZEMETHOD', 'Resize method'), self::get_resize_methods() )->setEmptyString(' ') ) ) ) ); } /** * Event method called before the receiver is written to the database. */ public function onBeforeWrite() { if ($w = $this->owner->LoyalsLogoImageWidth) { $this->owner->LoyalsLogoImageWidth = is_numeric($w) ? (int) $w : null; } if ($h = $this->owner->LoyalsLogoImageHeight) { $this->owner->LoyalsLogoImageHeight = is_numeric($h) ? (int) $h : null; } if ($w = $this->owner->LoyalsLoadingImageWidth) { $this->owner->LoyalsLoadingImageWidth = is_numeric($w) ? (int) $w : null; } if ($h = $this->owner->LoyalsLoadingImageHeight) { $this->owner->LoyalsLoadingImageHeight = is_numeric($h) ? (int) $h : null; } } /** * Answers true if a custom logo image exists. * * @return boolean */ public function LoyalsLogoImageExists() { if ($image = $this->owner->LoyalsLogoImage()) { return ($image->exists() && file_exists($image->getFullPath())); } return false; } /** * Answers true if a custom loading image exists. * * @return boolean */ public function LoyalsLoadingImageExists() { if ($image = $this->owner->LoyalsLoadingImage()) { return ($image->exists() && file_exists($image->getFullPath())); } return false; } /** * Answers a resized version of the logo image. * * @return Image */ public function LoyalsLogoImageResized() { if ($this->owner->LoyalsLogoImageExists()) { return $this->performImageResize('LoyalsLogoImage'); } } /** * Answers a retina version of the logo image. * * @return Image */ public function LoyalsLogoImageRetina() { if ($this->owner->LoyalsLogoImageExists()) { return $this->performImageResize('LoyalsLogoImage', true); } } /** * Answers the background-size for the retina version of the logo image. * * @return string */ public function LoyalsLogoRetinaBackgroundSize() { if ($this->owner->LoyalsLogoImageExists()) { // Obtain Target Dimensions: list($tw, $th) = $this->getTargetDimensions('LoyalsLogoImage'); // Answer Background Size: return "{$tw}px {$th}px"; } } /** * Answers a resized version of the loading image. * * @return Image */ public function LoyalsLoadingImageResized() { if ($this->owner->LoyalsLoadingImageExists()) { return $this->performImageResize('LoyalsLoadingImage'); } } /** * Answers the target dimensions for the specified image. * * @param string $image * @return array */ private function getTargetDimensions($image) { // Obtain Source Image: $si = $this->owner->{$image}(); // Obtain Source Image Dimensions: $sw = $si->getWidth(); $sh = $si->getHeight(); // Obtain Target Image Dimensions: $wp = "{$image}Width"; $hp = "{$image}Height"; $tw = $this->owner->$wp; $th = $this->owner->$hp; // Calculate Target Width/Height (if required): if ($tw && !$th && $sw) { $th = round(($tw / $sw) * $sh); } elseif (!$tw && $th && $sh) { $tw = round(($th / $sh) * $sw); } // Answer Dimensions: return array($tw, $th); } /** * Answers a resized version of the loading image. * * @param string $image * @param boolean $retina * @return Image */ private function performImageResize($image, $retina = false) { // Obtain Source Image: $si = $this->owner->{$image}(); // Obtain Resize Method: $rp = "{$image}Resize"; $tr = $this->owner->$rp; // Obtain Target Dimensions: list($tw, $th) = $this->getTargetDimensions($image); // Perform Image Resize: if ($tw && $th && $tr) { // Handle Retina Flag: if ($retina) { $tw = ($tw * 2); $th = ($th * 2); } // Build Argument Array: if (strpos($tr, 'Width') !== false) { $args = array($tw); } elseif (strpos($tr, 'Height') !== false) { $args = array($th); } else { $args = array($tw, $th); } // Call Resize Method: if ($si->hasMethod($tr)) { return call_user_func_array(array($si, $tr), $args); } } // Answer Source Image (no resize): return $si; } } |