Using Font Awsome icons in a file list

In a recent WordPress project, the client needed the ability to display a list of downloads on certain pages.

The site was already using the aptly named Font Awesome, which has an icon for every occasion including many for file types, so I thought why not use an icon as a nice visual aid in the list of files.

This PHP function retrieves either the Font Awesome icon class or character code based on a mime-type by looking it up in a multi-dimensional array. If it can’t find the mime-type, it falls back on a generic file icon.

function getFontawesomeFileIcon( $mime_type, $class = true )
{
	$data = array(

		'default' => array('fa-file', 'f15b'),
		'application/x-7z-compressed' => array('fa-file-archive-o', 'f1c6'),
		'application/x-bzip' => array('fa-file-archive-o', 'f1c6'),
		'application/x-bzip2' => array('fa-file-archive-o', 'f1c6'),
		'application/x-rar-compressed' => array('fa-file-archive-o', 'f1c6'),
		'application/x-tar' => array('fa-file-archive-o', 'f1c6'),

		'audio/x-wav' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-aac' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-aiff' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-ms-wma' => array('fa-file-audio-o', 'f1c7'),
		'audio/mpeg' => array('fa-file-audio-o', 'f1c7'),
		'audio/mp4' => array('fa-file-audio-o', 'f1c7'),
		'audio/ogg' => array('fa-file-audio-o', 'f1c7'),
		'audio/webm' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-wav' => array('fa-file-audio-o', 'f1c7'),

		'text/css' => array('fa-file-code-o', 'f1c9'),
		'text/x-java-source,java' => array('fa-file-code-o', 'f1c9'),
		'application/javascript' => array('fa-file-code-o', 'f1c9'),
		'application/json' => array('fa-file-code-o', 'f1c9'),
		'text/x-pascal' => array('fa-file-code-o', 'f1c9'),

		'application/vnd.ms-excel' => array('fa-file-excel-o', 'f1c3'),
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => array('fa-file-excel-o', 'f1c3'),
		'text/csv' => array('fa-file-excel-o', 'f1c3'),

		'image/png' => array('fa-file-image-o', 'f1c5'),
		'image/svg+xml' => array('fa-file-image-o', 'f1c5'),
		'image/tiff' => array('fa-file-image-o', 'f1c5'),
		'image/bmp' => array('fa-file-image-o', 'f1c5'),
		'image/vnd.dwg' => array('fa-file-image-o', 'f1c5'),
		'image/gif' => array('fa-file-image-o', 'f1c5'),
		'image/jpeg' => array('fa-file-image-o', 'f1c5'),
		'image/vnd.adobe.photoshop' => array('fa-file-image-o', 'f1c5'),
		'image/x-pict' => array('fa-file-image-o', 'f1c5'),

		'video/quicktime' => array('fa-file-movie-o', 'f1c8'),
		'video/x-msvideo' => array('fa-file-movie-o', 'f1c8'),
		'video/x-flv' => array('fa-file-movie-o', 'f1c8'),
		'video/x-ms-asf' => array('fa-file-movie-o', 'f1c8'),
		'video/x-ms-wmv' => array('fa-file-movie-o', 'f1c8'),
		'video/mpeg' => array('fa-file-movie-o', 'f1c8'),
		'video/mp4' => array('fa-file-movie-o', 'f1c8'),
		'video/ogg' => array('fa-file-movie-o', 'f1c8'),
		'video/webm' => array('fa-file-movie-o', 'f1c8'),
		'video/x-matroska' => array('fa-file-movie-o', 'f1c8'),

		'application/pdf' => array('fa-file-pdf-o', 'f1c1'),

		'application/vnd.ms-powerpoint' => array('fa-file-powerpoint-o', 'f1c4'),
		'application/vnd.openxmlformats-officedocument.presentationml.presentation' => array('fa-file-powerpoint-o', 'f1c4'),

		'application/msword' => array('fa-file-word-o', 'f1c2'),
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => array('fa-file-word-o', 'f1c2'),

		'application/zip' => array('fa-file-zip-o', 'f1c6'),
	);

	if( !array_key_exists( $mime_type, $data) )
	{
		$mime_type = 'default';
	}

	if( $class )
	{
		return $data[$mime_type][0];
	}

	return $data[$mime_type][1];

}

htaccess Tips

Editing htaccess files on an Apache webserver is one of those tasks which I don’t do often enough for anything more than the most basic commands to stick in my brain.

Of course you can go to the Apache manual, but it is not the easiest read.

So, more often than not, I end up at Perishablepress’s ‘Stupid htaccess tricks’ which has loads of solid examples which are really easy to understand.

Determining if an array is multidimensional

This is quick boolean test to see if an array is multi-dimensional (contains other arrays). It simply checks each element in an array to see if it is also an array and returns true as soon as one is found:

function isArrayMultiDimensional( $arr = [] )
{
	if( is_array($arr) )
	{
		foreach( $arr as $key => $value )
		{
			if( is_array($value) )
			{
				return true;
			}
		}
	}
	return false;
}

Checking if tables exist in a MySQL database

I recently needed to create an add-on for a shopping cart system which required it’s own database table and relied on database tables from other add-ons.

It needed an install method to create it’s own tables and needed to check for the existence of other tables.

Previously, I’ve needed to check for the existence of one table, which is easy enough:

SHOW TABLES LIKE 'TABLE_NAME'

And, retrieving all tables allows you to check the result for multiple tables in the application layer:

SHOW TABLES

But I wondered if there might be a more elegant solution to checking for multiple tables.

The answer is to query the information_schema database directly which provides a bit more flexibility:

SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'DB_NAME' 
AND (TABLE_NAME = 'TABLE1_NAME' OR TABLE_NAME = 'TABLE2_NAME')

The application can use the result for a simple boolean check if all required tables exist by counting the number of results and can also determine which tables need creating from the list.

The query can also be modified with more criteria, such as whether the table is a view or base table.

Useful array functions

Here area couple of array functions which I keep on finding myself searching for.

objectToArray converts an object into an array. This is not as trivial as it sounds, but luckily a PHP function exists that sort of does this, json_decode. The object is first converted into JSON then decoded from JSON into an associative array:

function objectToArray( $object )
	{
		if( is_object( $object ) )
		{
			return json_decode( json_encode($object), true );
		}

		return $object;
	}

flattenArray converts a multi-dimensional array into a flat array containing only the values:

function flattenArray( $arr = [] )
{
	$return = array();
	array_walk_recursive($arr, function($a) use (&$return) { $return[] = $a; });
	return $return;
}

This came from StackOverflow: http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array