Helpers

These are functions that may be used in many contexts.

Accessing Helpers

From controllers:

$this->form_helper->select_date();

From view templates (.tpl):

<?= $form_helper->select_date() ?>

From within helpers:

$this->_controller->form_helper->hidden_field();

Capturing code segments into variables

Capture lets you extract parts of code into instance variables which can be used in other points of the template or even layout file.

Blocks

<?php $capture_helper->begin (); ?> 
    [some html...] 
<?php $script = $capture_helper->end (); ?>

Javascript to header

$capture_helper→content_for(“name”); is a wrapper for Capture which will store the fragment in an instance variable similar to $content_for_layout.

Example:
layout.tpl:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
        <title>layout with js</title> 
        <script type="text/javascript"> {content_for_script} </script> 
    </head> 
    <body> {content_for_layout} </body> 
</html>

view.tpl:
This page shows an alert box!

<?php $capture_helper->begin ('script'); ?>
    alert('hello world'); 
<?php $capture_helper->end (); ?>
Normal view text

Date and Time

locale_date

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'date_helper.php');

Syntax:
locale_date( [ $iso_date ])
Description:
Converts an ISO date (format: yyyy-mm-dd) to the current date locale format. If $iso_date is omitted, the current date is assumed.

locale_date_time

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'date_helper.php');

Syntax:
locale_date_time( [ $iso_date_time = null])
Description:
Converts an ISO date/time (format: yyyy-mm-dd hh:mm:ss) to the current locale format. If $iso_date_time is omitted, the current date/time is assumed.

Text of difference between 2 times

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'date_helper.php');

Syntax:
distance_of_time_in_words( $from_time, [ $to_time = 0], [ $include_seconds = false])
Description:
Reports the approximate difference in time between $from_time and $to_time in an ISO string translated to the current language:

For example, if the distance is 47 minutes, it'll return “about 1 hour”. Integers are interpreted as seconds. So,

$date_helper->distance_of_time_in_words(50);

returns “less than a minute”.

Set $include_seconds to true if you want more detailed approximations for distances of < 1 minute.

Here are the returned messages:

Time Difference Message
< 20 seconds less than x seconds*
< 40 seconds less than half a minute*
< 60 seconds less than a minute
1 minute 1 minute
< = 45 minutes x minutes
< = 90 minutes about 1 hour
< = 1440 minutes about x hours
< = 2880 minutes 1 day
> 2880 minutes x days

* reported only if $include_seconds = true; otherwise “less than a minute”.

Text of difference From time to now

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'date_helper.php');

Syntax:
distance_of_time_in_words_to_now ( $from_time, [ $include_seconds = false])
OR time_ago_in_words ( $from_time, [ $include_seconds ])
Description:
Reports the approximate difference in time between $from_time until now (the current time) in an ISO string translated to the current language:

Time Difference Message
< 20 seconds less than x seconds*
< 40 seconds less than half a minute*
< 60 seconds less than a minute
1 minute 1 minute
< = 45 minutes x minutes
< = 90 minutes about 1 hour
< = 1440 minutes about x hours
< = 2880 minutes 1 day
> 2880 minutes x days

* reported only if $include_seconds = true; otherwise “less than a minute”.

Number Helpers

Add one

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'date_helper.php');

Syntax:
_add_one( $number)

Description:
Returns $number++ or $number+1

Human size

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
human_size($number)

Description:
Formats the bytes in $number into a more understandable representation (e.g., giving it 1500 yields 1.5 KB). This method is useful for reporting file sizes to users. This method returns “0 Bytes” if $number cannot be converted into a number.
This is the same as $number_helper→number_to_human_size, where the default $decimal = 1 is assumed.

Examples:

$number_helper->human_size(123)             # returns 123 Bytes
$number_helper->human_size(1234)            # returns 1.2 KB
$number_helper->human_size(12345)           # returns 12.1 KB
$number_helper->human_size(1234567)         # returns 1.2 MB
$number_helper->human_size(1234567890)      # returns 1.1 GB
$number_helper->human_size(1234567890123)   # returns 1.1 TB

Human size to bytes

human_size_to_bytes ( $size) Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
human_size_to_bytes($number)

Description:
Converts the humanized $number as an integer.

Examples:

$number_helper->human_size_to_bytes("123 bytes")       # returns 123
$number_helper->human_size_to_bytes("0.123 kb")        # returns 126
$number_helper->human_size_to_bytes("1.234 KILOBYTES") # returns 1264
$number_helper->human_size_to_bytes("12.1 KB")         # returns 12391
$number_helper->human_size_to_bytes("1.2 mb")          # returns 1258292
$number_helper->human_size_to_bytes('1.1 GB')          # returns 1181116007
$number_helper->human_size_to_bytes('1.23tb')          # returns 1352399302165

Number to currency

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
number_to_currency ( $number, [ $options = array()])

Description:
Formats a number into a currency string (e.g., $13.65).

You can customize the format in the $options array. (The defaults are in config/locales/LOCALE.php, where LOCALE is the one currently selected. The default LOCALE is “en”):

Examples:

$number_helper->number_to_currency(1234567890.50)                           # returns $1,234,567,890.50
$number_helper->number_to_currency(1234567890.506)                          # returns $1,234,567,890.51
$number_helper->number_to_currency(1234567890.50,
    array('unit' => "&pound;", 'separator' => ",", 'delimiter' => ""))      # returns &pound;1234567890,50
$number_helper->number_to_currency(1234567890.50,
    array('unit' => " &euro;", 'separator' => ",", 'delimiter' => ".",
    'unit_position' => 'right'))                                            # returns 1.234.567.890,50 &euro;
$number_helper->number_to_currency(1234567890.506, array('precision' => 3)) # returns $1,234,567,890.506
$number_helper->number_to_currency(1234567890.50, array('unit' => "&pound;", 
    'separator' => ",", 'delimiter' => ""))                                 # returns &pound;1234567890,50
$number_helper->number_to_currency(1234567890.50, array('unit' => "&pound;", 
    'separator' => ",",'delimiter' => "", 'unit_position' => 'right'))      # returns 1234567890,50 &pound;

Number to human size

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
number_to_human_size($number, [ $decimal = 1])

Description:
Formats the bytes in $number into a more understandable representation (e.g., giving it 1500 yields 1.5 KB). This method is useful for reporting file sizes to users. This method returns “0 Bytes” if $number cannot be converted into a number. The result is rounded to $decimal places.

Examples:

$number_helper->number_to_human_size(123)                # returns 123 Bytes
$number_helper->number_to_human_size(1234,2)             # returns 1.23 KB
$number_helper->number_to_human_size(12345,2)            # returns 12.35 KB
$number_helper->number_to_human_size(1234567,4)          # returns 1.2346 MB
$number_helper->number_to_human_size(1234567890,3)       # returns 1.235 GB
$number_helper->number_to_human_size(1234567890123,2)    # returns 1.23 TB

Number to percentage

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
number_to_percentage ( $number, [ $options = array()])

Description:
Formats a number as a percentage string (e.g., 65%). You can customize the format in the $options array:

Examples:

$number_helper->number_to_percentage(100)                                                 # => 100.00%
$number_helper->number_to_percentage(100, array('precision' => 0))                        # => 100%
$number_helper->number_to_percentage(1000, array('separator' => ','))                     # => 1000,00%
$number_helper->number_to_percentage(302.24398923423, array('precision' => 5))            # => 302.24399%

Number to phone

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
number_to_phone ( $number, [ $options = array()])

Description:
Formats a number into a US phone number (e.g., (555) 123-9876). You can customize the format in the options array.

Examples:

$number_helper->number_to_phone(1235551234)                                 # => 123-555-1234
$number_helper->number_to_phone(1235551234, array('area_code' => true))     # => (123) 555-1234
$number_helper->number_to_phone(1235551234, array('delimiter' => " "))      # => 123 555 1234
$number_helper->number_to_phone(1235551234, array('area_code' => true, 'extension' => 555)) 
                                                                            # => (123) 555-1234 x 555
$number_helper->number_to_phone(1235551234, array('extension' => 123,'extension_delimiter' => 'ext.'))
                                                                            # => 123-555-1234 ext. 123
$number_helper->number_to_phone(1235551234, array(
    'extension_delimiter' => 'extension', 'extension' => 1343, 'delimiter' => "."))
                                                                            # => 123.555.1234 extension 1343

Number with delimiter

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
number_with_delimiter ( $number, [ $delimiter = ','])

Description:
Inserts $delimiter into the thousands positions of $number converted to a string.

Examples:

$number_helper->number_with_delimiter(12345678)         # returns 12,345,678
$number_helper->number_with_delimiter(12345678.05)      # returns 12,345,678.05
$number_helper->number_with_delimiter(12345678, ".")    # returns 12.345.678
$number_helper->number_with_delimiter(98765432.98, " ") # returns 98 765 432.98

Number with precision

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
number_with_precision ( $number, [ $precision = 3])

Description:
Formats $number with $precision level of precision.

Examples:

$number_helper->number_with_precision(111.2345)         # returns 111.235
$number_helper->number_with_precision(12345678.05)      # returns 12345678.6
$number_helper->number_with_precision(12345678, ".")    # returns 12345678
$number_helper->number_with_precision(98765432.98, " ") # returns 98765433

Pad single digits with leading zero(s)

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'date_helper.php');

Syntax:
_leading_zero_on_single_digits( $number)

Description:
This returns

$number > 9 ? $number : "0$number";

Pad with zeros

Include:

require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'number_helper.php');

Syntax:
zeropad ( $number, $length)

Description:
Formats $number with $precision level of precision.

Examples:

$number_helper->zeropad(123, 6) #  returns the string "123".   FIXME!