The helpers.php and us_helpers.php are filled with all sorts of fun functions (FUNctions?).   Over time, all the cool ones will get documented here. Sorry for the lack of camelCase on some of them. They’re used in other projects I have.

Site Offline/Master Account

Ok, this is not technically a FUNtion, but this seemed like the best place to explain this.  In your users/init.php, you have a line that says

$master_account = [1];

If you don’t have it, you can add it.

This line gives you a few options.  If you go in your admin panel and take your site offline, whichever users’ ids that are in this array (you can do [1,103,126]), will have access to the site, even if it is offline. Additionally, they will have access to pages even if for some reason they don’t have explicit permission in the admin panel.

hasPerm ([permission levels],user’s id)

The venerable, old checkMenu function from the UserCake days was nice, but not really flexible as your number of permission levels grew. So, we have also added hasPerm.  If you want something to show up (or not show up) or go through (or fail) based on a person’s permission level, you can now pass in an array.

if (hasPerm([2,103],$user->data()->id)){
//do something
}

You can pass in as many permission levels as you want into that [] array.  Permission level ids can be found by going to the Admin dashboard and manage your permission levels.  Click on one to get its id.  Note that if you don’t make that first argument an array [] , it will throw an error, so use [] even if it is only for one permission level.

 

echouser($id)

4.2.1 – This has been made more dynamic. In the admin panel, there is now a dropdown that lets you format this output differently depending on if you want to show the username, first/last name, username and first/last or username and first.  The great thing is that you can code your project once if you decide to change your formatting, you can do it all at once from the back.  Although if you want to show just the username, you can always do $user->data()->username.  It’s a personal preference thing.

4.2.0 – A really quick way to echo out a user’s first and last name.

echouser($user->data()->id);

 

generateForm($table,$id, $skip=[])

Let’s say you want someone to be able to update every column of a table (on a particular row). Why go through all the work to generate that form manually, when you could do it in 2 lines (the function and the submit button!)?  You almost always want to skip the id column, but you could decide to skip others, too. Just put a comma between them. Try this for a demo…

generateForm('settings',1, $skip=['id']);

 

generateAddForm($table, $skip=[])

What if you don’t want to update an existing row, but want to add a new one instead. We’ve got you covered.

 

echoId($id,$table,$column)

Make short work of echoing pretty much any random thing from the database. For instance, let’s say you want to echo out the page name in the pages table with an id of 30.

echoId(30,'pages','page');

It’s not super practical by itself, but if you start tossing foreach loops and variables in there and you can do some pretty cool stuff.

 

bin($number)

I use 1s and 0s a lot in my database.  Let’s say you added a column in your user table called likes_dogs.

bin($user->data()->likes_dogs);

If the result is 0, you get a red No.
If the result is 1, you get a green Yes.
If the result is something else, you get a blue Other.

Hint: You could copy this function to usersc/includes/custom_functions.php and call it myBin.  You could do something like…

function myBin($number){
if ($number == 1){
echo "<strong><font color='green'>Yes</font></strong>";
}else{
echo "<strong><font color='red'>No</font></strong>";
}
}

If you just want a truly binary yes/no result. This is great if you don’t put a zero in your column by default.

 

dump

Doing a var_dump on large amounts of data can get messy, so the dump function makes things neat. Instead of concatenating <pre> tags on before and after your var_dump just write…
dump($variable);

and you will get back a nice, neat preformatted list of everything that is inside of that variable.

dnd (Dump & Die!)

When you’re tracking down bugs, it’s often a good idea to do a var_dump and then kill the page. The dnd function saves you 4 lines of code by doing a preformatted var_dump and killing the page for you. Simply write…
dnd($variable);

bold

Outputting test messages can be pretty difficult when you use the dark backgrounds that UserSpice uses by default You often wind up with black on black text. If you need to flash a test message(or any message) on the screen and want to do it in a really simple way use the bold function. It gives you large black text on a white background. All you have to do is
bold("Your message here");
or
bold($variable);

 

currentPage

Sometimes you need to be able to echo the name of the page you’re on or save it to the database. The currentPage function will do that for you. To echo it…
echo currentPage();