Source of file DBIcon.php
Size: 1,921 Bytes - Last Modified: 2021-12-23T10:10:12+00:00
/var/www/docs.ssmods.com/process/src/code/DBIcon.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 | <?php use PlasticStudio\DevTools\IconSelectField; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\DB; class Icon extends DBField { function requireField() { DB::require_field($this->tableName, $this->name, 'Varchar(1024)'); } private static $casting = array( 'URL' => 'HTMLFragment', 'IMG' => 'HTMLFragment', 'SVG' => 'HTMLFragment' ); /** * Default casting for this field * * @return string */ public function forTemplate() { return $this->getTag(); } /** * Default casting for this field * * @return string */ public function getTag() { $url = $this->URL(); // We are an SVG, so return the SVG data if (substr($url, strlen($url) - 4) === '.svg'){ return $this->SVG(); } else { return $this->IMG(); } } /** * Get just the URL for this icon * * @return string **/ public function URL(){ return $this->getValue(); } /** * Construct IMG tag * * @return string **/ public function IMG(){ $url = $this->URL(); return '<img class="icon" src="'.$url.'" />'; } /** * Construct SVG data * * @return string **/ public function SVG(){ $url = $this->URL(); if (substr($url, strlen($url) - 4) !== '.svg'){ user_error('Deprecation notice: Direct access to $Icon.SVG in templates is deprecated, please use $Icon', E_USER_WARNING); } // figure out the full system location for the file $filePath = BASE_PATH.$url; if (!file_exists($filePath)){ return false; } $svg = file_get_contents($filePath); return '<span class="icon svg">'.$svg.'</span>'; } /** * (non-PHPdoc) * @see DBField::scaffoldFormField() */ public function scaffoldFormField($title = null, $params = null) { return IconSelectField::create($this->name, $title); } } |