diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index a465f9fedb2b..c5906f4b90d3 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -887,4 +887,24 @@ public static function getImageURL($imageURL) { return self::getFileURL($path, $mimeType); } + + /** + * Get file icon class for specific MIME Type + * + * @param string $mimeType + * @return string + */ + public static function getIconFromMimeType($mimeType) { + 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 $iconClasses['*']; + } + } diff --git a/CRM/Utils/File/mimeIcons.json b/CRM/Utils/File/mimeIcons.json new file mode 100644 index 000000000000..12baeb969f87 --- /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/api/v3/Attachment.php b/api/v3/Attachment.php index 39b2da995ea6..b503938b47e1 100644 --- a/api/v3/Attachment.php +++ b/api/v3/Attachment.php @@ -430,6 +430,7 @@ function _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $retur 'upload_date' => is_numeric($fileDao->upload_date) ? CRM_Utils_Date::mysqlToIso($fileDao->upload_date) : $fileDao->upload_date, 'entity_table' => $entityFileDao->entity_table, 'entity_id' => $entityFileDao->entity_id, + 'icon' => CRM_Utils_File::getIconFromMimeType($fileDao->mime_type), ); $result['url'] = CRM_Utils_System::url( 'civicrm/file', 'reset=1&id=' . $result['id'] . '&eid=' . $result['entity_id'], diff --git a/tests/phpunit/api/v3/AttachmentTest.php b/tests/phpunit/api/v3/AttachmentTest.php index 09dc385df5c5..3aef35979489 100644 --- a/tests/phpunit/api/v3/AttachmentTest.php +++ b/tests/phpunit/api/v3/AttachmentTest.php @@ -404,6 +404,8 @@ public function testCreateWithWeirdName() { $fileId = $createResult['id']; $this->assertTrue(is_numeric($fileId)); $this->assertEquals(self::getFilePrefix() . 'weird_na_me.txt', $createResult['values'][$fileId]['name']); + // Check for appropriate icon + $this->assertEquals('fa-file-text-o', $createResult['values'][$fileId]['icon']); } /** @@ -552,6 +554,36 @@ public function testDeleteByEntity() { $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']); } + /** + * Ensure mime type is converted to appropriate icon. + */ + public function testGetIcon() { + $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); + $this->assertTrue(is_numeric($entity->id)); + + $createResult = $this->callAPISuccess('Attachment', 'create', array( + 'name' => self::getFilePrefix() . 'hasIcon.docx', + 'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'description' => 'My test description', + 'content' => 'My test content', + 'entity_table' => 'civicrm_activity', + 'entity_id' => $entity->id, + )); + $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', + 'description' => 'My test description', + 'content' => 'My test content', + 'entity_table' => 'civicrm_activity', + 'entity_id' => $entity->id, + )); + $fileId = $createResult['id']; + $this->assertEquals('fa-file-image-o', $createResult['values'][$fileId]['icon']); + } + /** * @param $name * @return string