From 4994819edd3d8b92efea80bdb4ea68fd1c34fb27 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 23 Jan 2017 10:56:46 -0500 Subject: [PATCH] CRM-19829 - Attachment API: return icons --- CRM/Utils/File.php | 41 +++++++++++++++++++++++++ api/v3/Attachment.php | 1 + tests/phpunit/api/v3/AttachmentTest.php | 32 +++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index a465f9fedb..ebc1774e0b 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -887,4 +887,45 @@ HTACCESS; return self::getFileURL($path, $mimeType); } + + /** + * Get file icon class for specific MIME Type + * + * @param string $mimeType + * @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', + ); + foreach ($iconClasses as $text => $icon) { + if (strpos($mimeType, $text) === 0) { + return $icon; + } + } + return 'fa-file-o'; + } + } diff --git a/api/v3/Attachment.php b/api/v3/Attachment.php index 39b2da995e..b503938b47 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 09dc385df5..2608a51d60 100644 --- a/tests/phpunit/api/v3/AttachmentTest.php +++ b/tests/phpunit/api/v3/AttachmentTest.php @@ -404,6 +404,8 @@ class api_v3_AttachmentTest extends CiviUnitTestCase { $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 @@ class api_v3_AttachmentTest extends CiviUnitTestCase { $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 -- 2.25.1