Skip to content

Helper functions

Introduction

There are several helper functions custom wrote for OTAS and they may be handy for data manipulation or getting some DB fields, here are some of them which are the most used and how you can use them:

carbon

to instantiate a Carbon instance from a date string, in the format d/m/Y which is the standard date format in OTAS. you can pass the second argument to modify the format your string is:

// The second argument is optional, default is d/m/Y
$date = carbon('12/12/2012', 'Y-m-d');

carbonWithFormat

to instantiate a Carbon instance from a date string, in the format d/m/Y and then convert its format to a specific format. you can pass the second argument to modify the format your string is:

// The third argument is optional, default is d/m/Y
$date = carbonWithFormat('12/12/2012', 'Y-m-d', 'd/m/Y');

formatDate

formats a DateTime instance or Carbon instance to the given string date format, the default is d/m/Y:

$dateTime = new DateTime('2012-12-12 12:00:00');
echo formatDate($dateTime); // 12/12/2012
echo formatDate($dateTime, 'd m Y'); // 12 12 2012

formatTime

formats a DateTime instance or Carbon instance to the given string time format, the default is h:i:s A:

$dateTime = new DateTime('2012-12-12 12:00:00');
echo formatTime($dateTime); // 12:00:00 PM
echo formatTime($dateTime, 'h::i, s a'); // 12::00, 00 pm

formatDateTime

formats a DateTime instance or Carbon instance to the given string date&time format, the default is d/m/Y h:i:s A:

$dateTime = new DateTime('2012-12-12 12:00:00');
echo formatDateTime($dateTime); // 12/12/2012 12:00:00 PM
echo formatDateTime($dateTime, 'd m Y h::i, s a'); // 12 12 2012 12::00, 00 pm

settings

returns a settings item from October CMS/OTAS settings table system_settings, you can pass the second parameters for the default value if there is none:

$someSetting = settings('some_settings', 'default'); // gets the value or default

authorize

Authorizes a policy given and if denies it abort a 403 code:

$publishPolicy = new PostPolicy(action: 'publish', resources: ['post' => $post]);
authorize($publishPolicy); // aborts if denies() method returned true

user

returns an instance of authenticated user, or null if there is none, is it the short equivalent to AuthManager::getUser():

$user = user(); //

getTranslatedLabel

Used to get a translated label for translatable fields, in OTAS the other locales are suffixed by _ab where ab is the locale two letters standard, e.g: tr, ar, fr. for example in twig part:

# Will return equivalent to {{$group->title_tr}} if the locale is tr
<h3>Group title: {{getTranslatedLabel($group, 'title')}}</h3>