Hello,
I’m trying to catch the {birthday} field when it turns up empty - either because it’s not set, or because the user is not logged in. I assumed it would just return a blank/no/null value, but it doesn’t seem to be working.
$rawdob = $TMPL->fetch_param('dob');
if ($rawdob == '')
{
$rawdob = date("m d, Y", time());
}
The ‘dob’ parameter should be replaced by whatever value {birthday} returns. When it does return a value - user is logged in and has set their birthday - it works great. When the birthday information is missing though, I can’t seem to be able to override it with that default value (today’s date.) When I try to return the value of $rawdob directly (when I log in as a user without a birthday set) it gives me a parse error within core.functions.php.
What is wrong with this code?
Nevermind.. I think I fixed it.. I gave up on this and decided to continue integrating vBulletin and saw something I thought might work, and it seems to have done the trick.
if (empty($rawdob))
That change seems to pick up the case when no value is returned due to the birthdate not being set, and I’m getting the correct returned value of 0 instead of -1.
Thanks guys, I was about to think no one wanted to reply to my threads, and I’d have to hoof it on my own lol.
The error I got when I tried to call the value of $rawdob directly? It pointed to a parse error in core.functions.php during the eval() function.
I knew there were other ways of doing it, but I’m not sure which is the most efficient way. Te rest of my plugin code is floating around here a few threads below. Justin- Does the {birthday} tag return ‘FALSE’ when no birthday is entered? I tried NULL and just a blank ” but neither worked, either I used == or === Victor- Won’t that if statement always run since $rawdob will return true, or am I reading it incorrectly?
Anyway, the plugin now works when I use it to determine a very, very simple test-only output, but I’m getting parse errors again trying to replace the text-only output with more EE tags. Essentially, I need it to evaluate the age using this plugin, then compare it to a number, and decide which set of EE tags to display.
This works:
{if {exp:age dob="{birthday}"} >= 18}Hello, adult!
{if:elseif {exp:age dob="{birthday}"} < 18}Hello, Minor!{/if}
This does NOT work, and I’m having trouble figuring out why.
The error I get with that is Notice: Undefined index: 1 in /home/********/public_html/eetest/cc/plugins/pi.age.php on line 27 Notice: Undefined index: 2 in /home/********/public_html/eetest/cc/plugins/pi.age.php on line 28 [These two correspond to the array of the exploded $rawdob] Parse error: syntax error, unexpected T_ELSE in /home/********/public_html/eetest/cc/core/core.functions.php(637) : eval()’d code on line 211
If it doesn’t work here, it definitely won’t work when I apply it to the exp:weblog:entries tag to conditionally display entries from those respective categories.
Edit: Blanked out username.
Thanks, I’ll give that a try - but again, that part DOES work as it currently is.
What does NOT work is when “Hello Adult/minor!” changes into another set of EE tags.
Edit: I just tried out all the patterns of ” and ’ I could think of.. while the page will at least render now, it’s still saying something about undefined indexes in my array.. is it affecting the plugin’s ability to call the dob parameter in some way?
Quite oddly enough, I can’t seem to access these forums for some reason.. I’m using a proxy for now to post this reply I was typing. As I hit post, I could no longer connect to the expressionengine site. I hope whatever it is gets fixed soon.
May I ask you to post your current code to your plugin? Also your test template that you are testing the plugin with.
Yes, certainly. Here is the plugin in its entirety (excuse the length - I can edit out the bottom “usage” section if you wish):
<?php
$plugin_info = array(
'pi_name' => 'EE Age Calculator',
'pi_version' =>'1.0',
'pi_author' =>'Teny Ng',
'pi_author_url' => 'http://www.darkscythe.com/',
'pi_description' => 'Calculates Registered Member Age',
'pi_usage' => Age::usage()
);
class Age
{
var $return_data = '';
function Age()
{
global $TMPL;
$rawdob = $TMPL->fetch_param('dob');
if (empty($rawdob))
{
$rawdob = date("m d, Y", time());
}
$rmob = explode(' ', $rawdob);
$mob = date("m", strtotime($rmob['0']));
$dob = intval($rmob['1']);
$yob = $rmob['2'];
$curtime = explode(' ', date("m d Y", time()));
$dobday = "$mob"."$dob";
$today = "$curtime[0]"."$curtime[1]";
$dobyear = "$yob";
$thisyear = "$curtime[2]";
if ($today >= $dobday)
{
$age = $thisyear - $dobyear;
}
else
{
$age = ($thisyear - $dobyear) -1;
}
$this->return_data = $age;
}
// ---------------------- //
// How to Use This Plugin //
// ---------------------- //
function usage()
{
ob_start();
?>
This plugin is used in conjunction with EE's native {Birthday} tag
to calculate a member's age. The member must have entered their
birthday into EE for the tag to pull anything up. Members who do
not have a birthday set, or are unregistered guests will have an
age of 0 (zero.) Input format for manually hard-coding a birthday is
mm dd, YYYY mirroring the format EE uses to display the {Birthday}
tag. The month can be 2-digit numeric or standard text. The day must
be numerical and the year must be 4-digit numeric for clarity.
Usage should be {exp:age dob="{birthday}"}
This function can be altered to take any standard date format, at
the expense of some accuracy by utilizing the strtotime() function
on the entire entry string, followed by the date() function to return
a mm dd, YYYY formatted date. Some additional checks will need to be
made for birthdays before 1970.
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
// END
}
?>
The template I am using right now is the Blue Essence theme from the add-on download pages. I originally installed it with the default EE theme, but since EE does not offer “one-click theming” I decided to manually change it to Blue Essence to see what it requires, so I can effectively make my own design. I believe I followed everything corrrectly, it’s viewable here.
If you want me to post the whole thing for that, please tell me to do so, but as it is really long, I’ll only post the bits that I modified with my plugin:
<?php if ($type != 'core') { ?>
<h2 class="sidetitle">Members</h2>
{if logged_in}
<a href="http://{path=member/profile}">Your Account</a> | <a href="http://{path=logout}">Logout</a>
{/if}
{if logged_out}
<a href="http://{path=member/login}">Login</a> | <a href="http://{path=member/register}">Register</a>
{/if}
| <a href="http://{path=member/memberlist}">Member List</a> |
{exp:member:custom_profile_data}
{birthday} {exp:age dob="{birthday}"} {if "{exp:age dob="{birthday}"}" >= 18}Hello, adult!
{if:elseif "{exp:age dob="{birthday}"}" < 18}Hello, Minor!{/if}
{/exp:member:custom_profile_data}
<?php } ?>
The first one works fine, the second set doesn’t though. I wonder if that’s because {exp:member:custom_profile_data} is missing.
Couple issues I’m trying to work out as well: 1) I can’t seem to include logged_in or logged_out status with those conditions. I’m trying to get it so that only registered, logged in, and age>18 users will see every category, while non-registered guests, logged-out users, and logged-in users with age<18 will see the second, more restricted set of categories. 2) This will also have to apply to the actual weblog entries themselves, but I’m not sure whether to use another set of categories tags or something else, and how to wrap all these different conditions into each other. 3) Even if I were to prevent these categories and entries from being visible, would that stop someone who typed in the category name directly into the URL to access the category list that way? I’ve enabled names so that users cant just ‘guess’ by going from C1 to C10 or something. 4) I assume I don’t need to have those “if core” checks all over my design, since I believe they’re only checking for the “EE Core” demo version, correct?
Somewhat unrelated, but what value does {my_weblog} hold? I changed my site name last night, and for some reason the categories and entries stopped appearing until I manually changed {my_weblog} to the new weblog shortname.
Sorry for all the questions, and thanks in advance for any help/advice/tips.
Well, I’m stumped..
I thought it might’ve been the {exp:member:custom_profile_data} tag that was missing, but putting that around the category {if} does not work. It still complains about the array being empty, despite the fact that I thought I fixed it to return a default value of the current time if no values were found. As a result, nothing displays in the category.. When it was broken, it returned a value of -1 which should pass the condition of being less than 18, but I don’t know what’s going on there.. It doesn’t seem to even be heeding that section and proceeds to display.. nothing.
Edit: Just to make sure it didn’t have anything to do with my if empty() call, I tried out the other 2 suggestions. Justin’s version results in the same..result. Victor’s I think did what I thought it would do, it’s returning the current date overriding any value it would have gotten from the template since it will evaluate as true. Actually, it even goes so far as to affect the rest of the page from loading, oddly enough..
Thanks- I edited my plugin to reflect those changes.
It didn’t seem to help though, still complains about an empty array. Again, the first section works (when the IF conditional is told to return plain text) but it seems to break when told to return a set of EE tags here:
This fails with empty arrays, even with that updated section of code.
Firstly, {birthday} requires {exp:member:custom_profile_data}. Secondly, when a parameter isn’t present fetch_param() returns FALSE. However, if it is specified but there is no value, it returns and empty string ”. For now I would simplify things by just making a very simple plugin that outputs a birthday passed to it. Figure out how to deal with the different scenarios it returns, and then program the rest accordingly.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.