panique / huge

Simple user-authentication solution, embedded into a small framework.
2.14k stars 788 forks source link

[DONE][documentation needed] Dynamic Texts/Feedback Messages using Text::get(); #758

Open videsignz opened 8 years ago

videsignz commented 8 years ago

Hi Chris, While expanding on the app, many times I have needed dynamic/user specific text for positive/negative feedback. But I like keeping with using the texts array because its very easy to manage that way.

So with a little thought and only 6 small lines of code, I came up with a super simple way. This does NOT effect anything in the app, only offers the option for customization.

core/text.php Change this...

public static function get($key)

To this...

public static function get($key, $data=NULL)

Then right below here....

if (!$key) {
    return null;
}

Add this...

if ($data) {
    foreach ($data as $var => $value) {
        ${$var} = $value;
    } 
}

And....viola!

Now in the texts array you can do something like

"FEEDBACK_USER_EMAIL_ALREADY_TAKEN" => "Sorry, the email $user_email is already in use."

And when you add feedback you do it like normal yet add the variable like this

Text::get('FEEDBACK_USER_EMAIL_ALREADY_TAKEN', ['user_email' => $user_email])

This of course will print _Sorry, the email user@mysite.com is already in use._

Just like that you can now have dynamic and user specific texts and feedback messages! Also, I am not saying to change the existing text array here in the repository, just the core/text.php class to allow for this feature :)

panique commented 8 years ago

This is awesome! Thank you very much, this should definitely go into the project! Please gimme some days for testing.

videsignz commented 8 years ago

Sweet!! I'm happy you like the idea! :)

slaveek commented 8 years ago

Small typo here.

Text::get('FEEDBACK_USER_EMAIL_ALREADY_TAKEN', ['user_email' => $user_email'])

Too many quotation marks ;)

videsignz commented 8 years ago

@slaveek Haha, good eye!! Thanks man! Fixed :)

panique commented 8 years ago

Thanks, this is now implemented in dev + master branch, big thanks to @abmmhasan to doing this!

panique commented 8 years ago

Would be cool if this would also get a little notice in the readme, so people know how to use this feature!

panique commented 8 years ago

Works perfectly btw :)

videsignz commented 8 years ago

Awesome!! Glad one of my Ideas made it into production :)

videsignz commented 8 years ago

I'm so bummed. I can not get this working on my local dev setup. Not sure if it's a php setting that I am missing.

videsignz commented 8 years ago

Ok, so....if I change the core Text class to A and use it like A::get(); it works. Not sure what to do about this one. Seems it is a loading order issue.

videsignz commented 8 years ago

I isolated the real issue. I know the following is by design, but is there any real downfall to changing it?

Due to the file being loaded once, there is times it loads the texts before the dynamic text variable is assigned.

// load config file (this is only done once per application lifecycle)
if (!self::$texts) {
    self::$texts = require('../application/config/texts.php');
}

With that being said, what would be the downfall of eliminating the check...

if (!self::$texts)
JOOSTR commented 8 years ago

You could fix that by doing:

public static function get($key, $data = null) if ($data) { return vsprintf(self::$texts[$key], $data); }

Though then you would have to use it like this:

"FEEDBACK_LOGIN_FAILED" => "Login failed. %s", // Example from texts file Text::get('FEEDBACK_LOGIN_FAILED', ['because something']);

Output: Login failed. because something