From 892be376046615640635d1a900608026c60c7b2d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 23 Jan 2017 17:46:31 -0500 Subject: [PATCH] CRM-19829 - Move icon classes to data file --- CRM/Utils/File.php | 31 ++++--------------------- CRM/Utils/File/mimeIcons.json | 22 ++++++++++++++++++ tests/phpunit/api/v3/AttachmentTest.php | 2 +- 3 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 CRM/Utils/File/mimeIcons.json diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index ebc1774e0b..c5906f4b90 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -895,37 +895,16 @@ HTACCESS; * @return string */ public static function getIconFromMimeType($mimeType) { - // List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml - $iconClasses = array( - // Media - 'image' => 'fa-file-image-o', - 'audio' => 'fa-file-audio-o', - 'video' => 'fa-file-video-o', - // Documents - 'application/pdf' => 'fa-file-pdf-o', - 'application/msword' => 'fa-file-word-o', - 'application/vnd.ms-word' => 'fa-file-word-o', - 'application/vnd.oasis.opendocument.text' => 'fa-file-word-o', - 'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word-o', - 'application/vnd.ms-excel' => 'fa-file-excel-o', - 'application/vnd.openxmlformats-officedocument.spreadsheetml' => 'fa-file-excel-o', - 'application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel-o', - 'application/vnd.ms-powerpoint' => 'fa-file-powerpoint-o', - 'application/vnd.openxmlformats-officedocument.presentationml' => 'fa-file-powerpoint-o', - 'application/vnd.oasis.opendocument.presentation' => 'fa-file-powerpoint-o', - 'text/plain' => 'fa-file-text-o', - 'text/html' => 'fa-file-code-o', - 'application/json' => 'fa-file-code-o', - // Archives - 'application/gzip' => 'fa-file-archive-o', - 'application/zip' => 'fa-file-archive-o', - ); + if (!isset(Civi::$statics[__CLASS__]['mimeIcons'])) { + Civi::$statics[__CLASS__]['mimeIcons'] = json_decode(file_get_contents(__DIR__ . '/File/mimeIcons.json'), TRUE); + } + $iconClasses = Civi::$statics[__CLASS__]['mimeIcons']; foreach ($iconClasses as $text => $icon) { if (strpos($mimeType, $text) === 0) { return $icon; } } - return 'fa-file-o'; + return $iconClasses['*']; } } diff --git a/CRM/Utils/File/mimeIcons.json b/CRM/Utils/File/mimeIcons.json new file mode 100644 index 0000000000..12baeb969f --- /dev/null +++ b/CRM/Utils/File/mimeIcons.json @@ -0,0 +1,22 @@ +{ + "image": "fa-file-image-o", + "audio": "fa-file-audio-o", + "video": "fa-file-video-o", + "application/pdf": "fa-file-pdf-o", + "application/msword": "fa-file-word-o", + "application/vnd.ms-word": "fa-file-word-o", + "application/vnd.oasis.opendocument.text": "fa-file-word-o", + "application/vnd.openxmlformats-officedocument.wordprocessingml": "fa-file-word-o", + "application/vnd.ms-excel": "fa-file-excel-o", + "application/vnd.openxmlformats-officedocument.spreadsheetml": "fa-file-excel-o", + "application/vnd.oasis.opendocument.spreadsheet": "fa-file-excel-o", + "application/vnd.ms-powerpoint": "fa-file-powerpoint-o", + "application/vnd.openxmlformats-officedocument.presentationml": "fa-file-powerpoint-o", + "application/vnd.oasis.opendocument.presentation": "fa-file-powerpoint-o", + "text/plain": "fa-file-text-o", + "text/html": "fa-file-code-o", + "application/json": "fa-file-code-o", + "application/gzip": "fa-file-archive-o", + "application/zip": "fa-file-archive-o", + "*": "fa-file-o" +} \ No newline at end of file diff --git a/tests/phpunit/api/v3/AttachmentTest.php b/tests/phpunit/api/v3/AttachmentTest.php index 2608a51d60..3aef359794 100644 --- a/tests/phpunit/api/v3/AttachmentTest.php +++ b/tests/phpunit/api/v3/AttachmentTest.php @@ -571,7 +571,7 @@ class api_v3_AttachmentTest extends CiviUnitTestCase { )); $fileId = $createResult['id']; $this->assertEquals('fa-file-word-o', $createResult['values'][$fileId]['icon']); - + $createResult = $this->callAPISuccess('Attachment', 'create', array( 'name' => self::getFilePrefix() . 'hasIcon.jpg', 'mime_type' => 'image/jpg', -- 2.25.1