/**
  * Get icon path for a given extension
  *
  * @param $ext
  *
  * @return string
  */
 private function _getIconPath($ext)
 {
     $sourceIconPath = craft()->path->getResourcesPath() . 'images/file.svg';
     $extLength = mb_strlen($ext);
     if ($extLength > 5) {
         // Too long; just use the blank file icon
         return $sourceIconPath;
     }
     // See if the icon already exists
     $iconPath = craft()->path->getAssetsIconsPath() . StringHelper::toLowerCase($ext) . '.svg';
     if (IOHelper::fileExists($iconPath)) {
         return $iconPath;
     }
     // Create a new one
     $svgContents = IOHelper::getFileContents($sourceIconPath);
     $textSize = $extLength <= 3 ? '26' : ($extLength == 4 ? '22' : '18');
     $textNode = '<text x="50" y="73" text-anchor="middle" font-family="sans-serif" fill="#8F98A3" ' . 'font-size="' . $textSize . '">' . StringHelper::toUpperCase($ext) . '</text>';
     $svgContents = str_replace('<!-- EXT -->', $textNode, $svgContents);
     IOHelper::writeToFile($iconPath, $svgContents);
     return $iconPath;
 }
 /**
  * Get icon path for an extension and size
  *
  * @param $ext
  * @param $size
  *
  * @return string
  */
 private function _getIconPath($ext, $size)
 {
     if (mb_strlen($ext) > 4) {
         $ext = '';
     }
     $extAlias = array('docx' => 'doc', 'xlsx' => 'xls', 'pptx' => 'ppt', 'jpeg' => 'jpg', 'html' => 'htm');
     if (isset($extAlias[$ext])) {
         $ext = $extAlias[$ext];
     }
     $sizeFolder = craft()->path->getAssetsIconsPath() . $size;
     // See if we have the icon already
     $iconLocation = $sizeFolder . '/' . $ext . '.png';
     if (IOHelper::fileExists($iconLocation)) {
         return $iconLocation;
     }
     // We are going to need that folder to exist.
     IOHelper::ensureFolderExists($sizeFolder);
     // Determine the closest source size
     $sourceSizes = array(array('size' => 40, 'extSize' => 7, 'extY' => 32), array('size' => 350, 'extSize' => 60, 'extY' => 280));
     foreach ($sourceSizes as $sourceSize) {
         if ($sourceSize['size'] >= $size) {
             break;
         }
     }
     $sourceFolder = craft()->path->getAssetsIconsPath() . $sourceSize['size'];
     // Do we have a source icon that we can resize?
     $sourceIconLocation = $sourceFolder . '/' . $ext . '.png';
     if (!IOHelper::fileExists($sourceIconLocation)) {
         $sourceFile = craft()->path->getAppPath() . 'etc/assets/fileicons/' . $sourceSize['size'] . '.png';
         $image = imagecreatefrompng($sourceFile);
         // Text placement.
         if ($ext) {
             $color = imagecolorallocate($image, 153, 153, 153);
             $text = StringHelper::toUpperCase($ext);
             $font = craft()->path->getAppPath() . 'etc/assets/helveticaneue-webfont.ttf';
             // Get the bounding box so we can calculate the position
             $box = imagettfbbox($sourceSize['extSize'], 0, $font, $text);
             $width = $box[4] - $box[0];
             // place the text in the center-bottom-ish of the image
             imagettftext($image, $sourceSize['extSize'], 0, ceil(($sourceSize['size'] - $width) / 2), $sourceSize['extY'], $color, $font, $text);
         }
         // Preserve transparency
         imagealphablending($image, false);
         $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
         imagefill($image, 0, 0, $color);
         imagesavealpha($image, true);
         // Make sure we have a folder to save to and save it.
         IOHelper::ensureFolderExists($sourceFolder);
         imagepng($image, $sourceIconLocation);
     }
     if ($size != $sourceSize['size']) {
         // Resize the source icon to fit this size.
         craft()->images->loadImage($sourceIconLocation)->scaleAndCrop($size, $size)->saveAs($iconLocation);
     }
     return $iconLocation;
 }
 /**
  * @param $localeId
  *
  * @return array
  */
 private function _processFrameworkData($localeId)
 {
     $wideMonthKeys = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
     $abbreviatedMonthKeys = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
     $wideWeekdayNameKeys = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
     $abbreviatedWeekdayNameKeys = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
     $formattedFrameworkData = array();
     $locale = \CLocale::getInstance($localeId);
     $formattedFrameworkData = array_merge($formattedFrameworkData, array_combine($wideMonthKeys, $locale->getMonthNames()));
     $formattedFrameworkData = array_merge($formattedFrameworkData, array_combine($abbreviatedMonthKeys, $locale->getMonthNames('abbreviated')));
     $formattedFrameworkData = array_merge($formattedFrameworkData, array_combine($wideWeekdayNameKeys, $locale->getWeekDayNames()));
     $formattedFrameworkData = array_merge($formattedFrameworkData, array_combine($abbreviatedWeekdayNameKeys, $locale->getWeekDayNames('abbreviated')));
     // Because sometimes Twig (ultimately PHP) will return 'pm' or 'am' and sometimes it will return 'PM' or 'AM'
     // and array indexes are case sensitive.
     $amName = $locale->getAMName();
     $pmName = $locale->getPMName();
     $formattedFrameworkData['AM'] = StringHelper::toUpperCase($amName);
     $formattedFrameworkData['am'] = StringHelper::toLowerCase($amName);
     $formattedFrameworkData['PM'] = StringHelper::toUpperCase($pmName);
     $formattedFrameworkData['pm'] = StringHelper::toLowerCase($pmName);
     return $formattedFrameworkData;
 }
 /**
  * Get icon path for an extension and size
  *
  * @param $ext
  * @param $size
  *
  * @return string
  */
 private function _getIconPath($ext, $size)
 {
     if (mb_strlen($ext) > 4) {
         $ext = '';
     }
     $extAlias = array('docx' => 'doc', 'xlsx' => 'xls', 'pptx' => 'ppt', 'jpeg' => 'jpg', 'html' => 'htm');
     if (isset($extAlias[$ext])) {
         $ext = $extAlias[$ext];
     }
     $sizeFolder = craft()->path->getAssetsIconsPath() . $size;
     // See if we have the icon already
     $iconLocation = $sizeFolder . '/' . $ext . '.png';
     if (IOHelper::fileExists($iconLocation)) {
         return $iconLocation;
     }
     // We are going to need that folder to exist.
     IOHelper::ensureFolderExists($sizeFolder);
     // Determine the closest source size
     $sourceSizes = array(array('size' => 40, 'extSize' => 7, 'extY' => 25), array('size' => 350, 'extSize' => 60, 'extY' => 220));
     foreach ($sourceSizes as $sourceSize) {
         if ($sourceSize['size'] >= $size) {
             break;
         }
     }
     $sourceFolder = craft()->path->getAssetsIconsPath() . $sourceSize['size'];
     // Do we have a source icon that we can resize?
     $sourceIconLocation = $sourceFolder . '/' . $ext . '.png';
     if (!IOHelper::fileExists($sourceIconLocation)) {
         $sourceFile = craft()->path->getAppPath() . 'etc/assets/fileicons/' . $sourceSize['size'] . '.png';
         $image = craft()->images->loadImage($sourceFile);
         // Text placement.
         if ($ext) {
             $font = craft()->path->getAppPath() . 'etc/assets/helveticaneue-webfont.ttf';
             $image->setFontProperties($font, $sourceSize['extSize'], "#999");
             $text = StringHelper::toUpperCase($ext);
             $box = $image->getTextBox($text);
             $width = $box->getWidth();
             // place the text in the center-bottom-ish of the image
             $x = ceil(($sourceSize['size'] - $width) / 2);
             $y = $sourceSize['extY'];
             $image->writeText($text, $x, $y);
         }
         // Make sure we have a folder to save to and save it.
         IOHelper::ensureFolderExists($sourceFolder);
         $image->saveAs($sourceIconLocation);
     }
     if ($size != $sourceSize['size']) {
         // Resize the source icon to fit this size.
         craft()->images->loadImage($sourceIconLocation, $size, $size)->scaleAndCrop($size, $size)->saveAs($iconLocation);
     }
     return $iconLocation;
 }