Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-19829 - Attachment API: return icons #9714

Merged
merged 2 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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['*'];
}

}
22 changes: 22 additions & 0 deletions CRM/Utils/File/mimeIcons.json
Original file line number Diff line number Diff line change
@@ -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"
}
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
32 changes: 32 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,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
Expand Down