Skip to content
Snippets Groups Projects
Select Git revision
  • bc41312adc45e7e504cf0d04ead5a97eaa1a6141
  • master default protected
  • dev
3 results

functions.php

Blame
  • user avatar
    David Beniamine authored
    fe6b88fe
    History
    functions.php 2.60 KiB
    <?php
    
    /*
    	Custom theme functions
    
    	Note: we recommend you prefix all your functions to avoid any naming
    	collisions or wrap your functions with if function_exists braces.
    */
    function numeral($number, $hideIfOne = false) {
    	if($hideIfOne === true and $number == 1) {
    		return '';
    	}
    
    	$test = abs($number) % 10;
    	$ext = ((abs($number) % 100 < 21 and abs($number) % 100 > 4) ? 'th' : (($test < 4) ? ($test < 3) ? ($test < 2) ? ($test < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
    	return $number . $ext;
    }
    
    function count_words($str) {
    	return count(preg_split('/\s+/', strip_tags($str), null, PREG_SPLIT_NO_EMPTY));
    }
    
    function pluralise($amount, $str, $alt = '') {
    	return intval($amount) === 1 ? $str : $str . ($alt !== '' ? $alt : 's');
    }
    
    function relative_time($date) {
    	if(is_numeric($date)) $date = '@' . $date;
    
    	$user_timezone = new DateTimeZone(Config::app('timezone'));
    	$date = new DateTime($date, $user_timezone);
    
    	// get current date in user timezone
    	$now = new DateTime('now', $user_timezone);
    
    	$elapsed = $now->format('U') - $date->format('U');
    
    	if($elapsed <= 1) {
    		return 'Just now';
    	}
    
    	$times = array(
    		31104000 => 'year',
    		2592000 => 'month',
    		604800 => 'week',
    		86400 => 'day',
    		3600 => 'hour',
    		60 => 'minute',
    		1 => 'second'
    	);
    
    	foreach($times as $seconds => $title) {
    		$rounded = $elapsed / $seconds;
    
    		if($rounded > 1) {
    			$rounded = round($rounded);
    			return $rounded . ' ' . pluralise($rounded, $title) . ' ago';
    		}
    	}
    }
    
    function html_meta($key) {
        $output = html_entity_decode(site_meta($key));
        echo $output;
    }
    
    function social_logo($network) {
        $img=theme_url("img/$network.png");
        return "<img height=\"20\" width=\"20\" src=\"$img\" alt=\"$network/\"/>";
    }
    
    function social_account($network) {
        if ($network == "framasphere"){
            return site_meta($network.'_id', '');
        }
        return site_meta($network, '');
    }
    
    function social_list($properties) {
        $NETWORKS_URLS = array (
            'mail' => 'mailto:',
            'github' => 'https://github.com/',
            'framasphere' => 'https://framasphere.org/people/',
            'mastodon' => 'https://framapiaf.org/',
            'facebook' => 'https://facebook.com/',
            'twitter' => 'https://twitter.com/',
        );
        echo "<ul ".$properties.">";
        foreach ($NETWORKS_URLS as $network => $network_url){
            if(site_meta($network, '')){
                echo '<li><a href="'.$network_url.social_account($network).'">'.
                    social_logo($network).' '.site_meta($network, '').'</a></li>';
                }
        }
        echo "</ul>";
    }
    
    function total_articles() {
    	return Post::where(Base::table('posts.status'), '=', 'published')->count();
    }