This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/**
* Converts vars with values equivalent to zero (0) to an empty string.
* This is useful as an output filter, when we need and empty textbox.
*
* @param mixed $var Input value to convert.
* @return string Empty string if value is equivalent to zero.
*/
function ak_zero_clean( $var )
{
return ( 0 == intval($var) ) ? '' : $var;
}
/**
* Displays admin notices.
*
* @param $message Message to display.
* @return void
*/
function ak_admin_notify( $message = '' )
{
if ( is_admin() ) {
if ( empty($message) ) {
$message = __('Settings saved.', 'akfw');
}
echo '
';
}
}
/**
* Displays admin ERRORS.
*
* @param $message Message to display.
* @return void
*/
function ak_admin_error( $message )
{
if ( is_admin() ) {
echo '';
}
}
/**
* Displays dashboard notices by setting the 'admin_notices' action hook.
* This is used for global warnings that have to show in all dashboard pages.
*
* @uses akAdminNotice
* @param $message Message to display.
* @return void
*/
function ak_dashboard_notice( $message )
{
if ( is_admin() ) {
require_once ( AK_CLASSES . '/admin-notices.php' );
new akAdminNotice($message);
}
}
/**
* Generic pager.
*
* @param int $total Total elements to paginate.
* @param int $per_page Number of elements per page.
* @param $current Current page number.
* @param $url Base url for links. Only page numbers are appended.
* @return string Formated pager.
*/
function ak_pager( $total, $per_page, $url, $current = 0 )
{
if ( 0 == $current ) $current = 1;
$pages = $total / $per_page;
$pages = ( $pages == intval($pages) ) ? intval($pages) : intval($pages) + 1;
if ( $pages == 1 ) {
$out = '';
} else {
$out = "\n";
}
return $out;
}
/**
* Creates an string telling how many time ago
*
* @param $datetime Date Time in mySql Format.
* @return string The time from the date to now, just looks to yesterday.
*/
function ak_time_ago( $datetime )
{
$before = strtotime($datetime);
$is_today = ( date('Y-m-d') == substr($datetime, 0, 10) );
$now = time();
$times = array (
'd' => 43200, // 12 hours
'h' => 3600, // 1 hour
'm' => 60, // 1 minute
's' => 1 // 1 second
);
$diff = $now - $before;
foreach ( $times as $unit => $seconds ) {
if ( $diff >= $seconds ) {
$value = intval($diff / $seconds);
break;
}
}
$format = get_option('date_format') . ' | ' . get_option('time_format'); // Date-Time format
switch ( $unit ) {
case 's':
$ago = __('Just Now', 'akfw');
break;
case 'm':
$ago = sprintf(_n('1 minute ago', '%d minutes ago', $value, 'akfw'), $value);
break;
case 'h' :
$ago = sprintf(_n('1 hour ago', '%d hours ago', $value, 'akfw'), $value);
break;
case 'd' :
if ( 1 == $value ) {
$literal = ( $is_today ) ? __('Today at %s', 'akfw') : __('Yesterday at %s', 'akfw');
$ago = sprintf($literal, date('H:i', $before));
} else {
$ago = mysql2date($format, $datetime);
}
break;
}
return $ago;
}