functions - adamjimenez/shiftlib GitHub Wiki

ShiftLib Functions

This is a list of useful functions that are provided by ShiftLib

age

int age( string $date )

Calculates and returns the age from a date.

alert

alert( string $error )

Outputs a javascript alert with displaying the supplied error message.

current_tab

current_tab( string $tab )

If the tab matches the first level of the URI then it will output ' class="current over"'.
You can use this with CSS to highlight the current section of the navigation.

date_diff

date_diff( string $endDate, string $beginDate )

returns the difference in days between the provided dates.

dateformat

dateformat( string $format, string $date )

Similar to PHP date function - but accepts date in the format of YYYY-MM-DD'. Which is the native mysql format.

email_template

email_template(string $email, string|int $subject, array $reps)

Send an email using an email template. $email - recipient address. $subject - subject or id of the email template $reps - an array of token names and replacements e.g. To replace tokens in the email "Dear {$name},", you could use email_template($email, $subject, ['name' => 'John']);

escape

string escape( string $string )

An alias for the PHP function mysql_real_escape_string

ends_with

bool ends_with( string $haystack, string $needle )

Returns true if $haystack ends with $needle.

file_ext

string file_ext( string $file )

Returns the file extension part of a file name.

file_size

string file_size( int $size )

Converts a file size in bytes to a string e.g. 7000 = 7K

format_mobile

string format_mobile( string $mobile )

Remove invalid characters and prefixes UK internation dialing code.

format_postcode

string format_postcode(string $postcode)

Removes invalid chracters and adds correct spacing. Returns false if the postcode is completely invalid.

generate_password

string generate_password()

Returns a random 6 character password.

html_options

string html_options( array $options,mixed $selected, bool $force_assoc=false )

Takes any type of array and returns a list of html options for use in a select element e.g. Email

The selected argument will return the matching options as selectedd e.g. Email

The function will try to distinguish if options are an associative array. In some circumstances it is impossible to tell and it will defaul to non-associative.

You can over-ride this by setting $force_assoc to true.

image

mixed image($file, $w, $h, $attribs=true, $crop=false)

Returns a thumbnail link

Example #1 Thumbnail image tag

This will show a 320x240 thumbnail for the specified image.

<?=image($v['image'], 320, 240, 'class="image"');?>

is_alphanumeric

bool is_alphanumeric( string $string )

Checks if a string is alphanumeric

is_assoc_array

bool is_assoc_array( array $array)

Checks if an array is assoicative.

is_domain

bool is_domain( string $domain )

Checks if a string is a valid domain

is_email

bool is_email( string $email )

Checks if a string is a valid email address

is_nino

bool is_nino( string $nino )

Checks if a string is a valid UK national insurance number.

is_postcode

bool is_postcode( string $postcode )

Checks if a string is a valid UK postcode

is_url

bool is_url( string $url )

Checks if a string is a valid url.

load_js

load_js( mixed $libs )

Used for loading AJAX libraries e.g.

<? load_js('lightbox'); ?>

Or for more than one:

<? load_js(array('lightbox','swfobject')); ?>

This should be placed in the head section of template.php.
Where supported the libraries will be loaded via a CDN (content delivery network), resulting in faster loading.
The following AJAX options are supported:

  • jquery
  • jqueryui
  • lightbox
  • cycle
  • swfobject
  • bootstrap
  • cms

Dependencies are automatically worked out. So for example if you include lightbox - it will automatically load jquery. Only call load_js once to avoid loading jquery multiple times.
It will load the latest versions of the libraries. It will detect if SSL is enabled and load SSL versions if neccessary.

CMS is a shiftlib script used for form validation.

redirect

redirect( $url, bool $_301=false )

Issues a header redirect to the provided URL. Adds a 301 header if $_301 is set to true.

sec2hms

string sec2hms ( int $sec, bool $padHours = false )

Converts seconds to HH:MM:SS

spaced

string spaced( string $str )

Replaces underscores with spaces.

sql_query

array sql_query( string $query )

Execures a sql query with error handling. Returns an associative array of rows.

starts_with

bool starts_with( string $haystack, string $needle )

Returns true if $haystack starts with $needle.

table_exists

bool table_exists( string $table )

Checks if a table exits

truncate

string truncate( string $string, int $max = 50, string $rep = '..' )

Shortens a string to a length of $max characters. Appends $rep to the string.

underscored

string underscored( string $st r)

Replaces spaces with underscores.

validate

array validate( array $fields,array $required )

Compares the array of $fields against the array of $required fields.
Any missing or invalid fields are returned.

⚠️ **GitHub.com Fallback** ⚠️