Skip to content

Commit

Permalink
CRM-19829 - Attachment API: return icons
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jan 23, 2017
1 parent 2e0b9fb commit 5188227
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
42 changes: 42 additions & 0 deletions CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,46 @@ 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) {
// 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;
}
}
$mimeParts = explode('/', $mimeType, 2);
return CRM_Utils_Array::value($mimeParts[0], $iconClasses, 'fa-file-o');
}

}
1 change: 1 addition & 0 deletions api/v3/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/api/v3/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -552,6 +554,25 @@ 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']);
}

/**
* @param $name
* @return string
Expand Down

0 comments on commit 5188227

Please sign in to comment.