Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: samborabora on July 16, 2014, 06:00:34 PM

Title: Check and/or multiple profile options are filled?
Post by: samborabora on July 16, 2014, 06:00:34 PM
I need to check if the gender selection is chosen (not left blank) and/or one of the custom fields has been filled in. This doesn't work:

if (!empty($context['member']['gender']['name']) || !empty($context['custom_fields']))
echo '
f00
';


Where did I go wrong?
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 16, 2014, 10:01:37 PM
Were are you trying to do that? Registration?
Title: Re: Check and/or multiple profile options are filled?
Post by: Sir Osis of Liver on July 16, 2014, 11:29:45 PM
You can set a custom field as 'require entry', so user must enter something to register.  Deactivate the Gender field, and replace it with a required custom field.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 12:33:16 AM
Sorry, I always stupidly forget to mention which page it's wanted on! It's profile.template.php, so a users profile.
Title: Re: Check and/or multiple profile options are filled?
Post by: Sir Osis of Liver on July 17, 2014, 01:05:18 AM
You have the option to make custom fields visible in user profile in the field settings.  You have to activate the feature in Admin -> Core Features -> Advanced Profile Fields, then you'll see it in Admin -> Features and Options -> Profile Fields.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 01:08:22 AM
Thanks, yes that's ture, but I just need to check IF any of the fields are filled in, just a simple query to see IF any of the fields and/or a gender has been picked from the gender selection box. I think the gender check is:

if (!empty($context['member']['gender']['name'])

But I can't quite figure out how to check IF any of the custom fields have anything other than a null value.
Title: Re: Check and/or multiple profile options are filled?
Post by: Sir Osis of Liver on July 17, 2014, 01:15:08 AM
I think you'd have to check each field individually, with the correct index, same way you're trying to check the gender field.
 
Title: Re: Check and/or multiple profile options are filled?
Post by: Kindred on July 17, 2014, 01:16:42 AM
custom fields are not as simple - I don't think the get loaded into context
Title: Re: Check and/or multiple profile options are filled?
Post by: Sir Osis of Liver on July 17, 2014, 01:18:47 AM
Then you'd have to do a db query to check the fields.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 04:41:08 AM
I'm trying to check if ANY of the custom fields have a value, not one specific field, why is !empty($context['custom_fields']) not quite right?
Title: Re: Check and/or multiple profile options are filled?
Post by: Kindred on July 17, 2014, 07:45:30 AM
because.... to the best of my knowledge, NONE of the custom field are ever loaded into the context array
Title: Re: Check and/or multiple profile options are filled?
Post by: Illori on July 17, 2014, 07:48:48 AM
from Display.template.php

// Any custom fields to show as icons?
if (!empty($message['member']['custom_fields']))
{
$shown = false;
foreach ($message['member']['custom_fields'] as $custom)
{
if ($custom['placement'] != 1 || empty($custom['value']))
continue;
if (empty($shown))
{
$shown = true;
echo '
<li class="im_icons">
<ul>';
}
echo '
<li>', $custom['value'], '</li>';
}
if ($shown)
echo '
</ul>
</li>';
}
Title: Re: Check and/or multiple profile options are filled?
Post by: Kindred on July 17, 2014, 07:50:40 AM
yeah... but that is ONLY loaded in DIsplay -- and is loaded for the display of the stuff in the message....   it's loaded by poster for each message and is never added to $context for the current user
Title: Re: Check and/or multiple profile options are filled?
Post by: Arantor on July 17, 2014, 08:00:55 AM
Except in the profile area, the fields relevant to a given page are loaded into $context['custom_fields'] as already shown.

Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically. It would help if the OP actually provided sufficient detail in this case such as which field(s) need to be present as well as gender to output the relevant text.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 08:33:30 AM
Quote from: ‽ on July 17, 2014, 08:00:55 AM
Except in the profile area, the fields relevant to a given page are loaded into $context['custom_fields'] as already shown.

Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically. It would help if the OP actually provided sufficient detail in this case such as which field(s) need to be present as well as gender to output the relevant text.

No fields NEED to be present, I just need to check if they have any value at all. If "gender" is anything but blank (not male or female selected) AND/OR any of my custom fields "facebook", "twitter", "soundcloud" or "last.fm" have any value in them, I want to show a bounding "hide/show" wrapper around this output. If they are all blank, I won't show a wrapper, and therefore, no show/hide.
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 17, 2014, 09:36:55 AM
Are you talking about profile modify (where one introduces profile information) or profile summary?

If it's the first, using custom profile fields and let SMF deal with the automatic error/empty messages is probably easier and better.
If it's the second, and you are working on a "template tweak", then you have to iterate $context['custom_fields'] as explained.
Quote from: ‽ on July 17, 2014, 08:00:55 AM
Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 10:17:19 AM
Quote from: ‽ on July 17, 2014, 08:00:55 AM
Checking if multiple are set is a proper pain in the arse though because it specifically requires iterating over $context['custom_fields'] and checking each one specifically.

Sorry, yes it's profile summary, I've been trying to iterate the custom fields (and I assume I'd have to run a check for all 4 in addition to the gender dropdown) but I'm having trouble getting the code right. What would I use to query if all 4 fields and the dropdown are selected? Custom field names are "face_pro", cust_last", cust_soundc" and "cust_twitte".
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 17, 2014, 10:39:24 AM
Before the content get's echo'd you need to run a foreach on $context['custom_fields']. The establish some logic inside the foreach.

I also don't know the custom fields structure, but a piece of

echo '<pre>';
print_r($context['custom_fields']);
echo '</pre>';

Will allow you to show the array structure and the correct way to pick up whatever values you wish to analyze. ;)
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 12:38:54 PM
Quote from: margarett on July 17, 2014, 10:39:24 AM
Before the content get's echo'd you need to run a foreach on $context['custom_fields']. The establish some logic inside the foreach.

I also don't know the custom fields structure, but a piece of

echo '<pre>';
print_r($context['custom_fields']);
echo '</pre>';

Will allow you to show the array structure and the correct way to pick up whatever values you wish to analyze. ;)

Well, this would probably be somewhat like what that might print out:
foreach ($context['custom_fields'] as $field)
if($field['colname'] == "cust_aboutm0"){
echo ' <p class="profeaboutme">';

But I'm not entirely sure how to CHECK if there's a value, or what way to phrase this exactly to get it to give the result I'm looking for. I'm still not quite 'with' the lingo of the custom fields, since it all seems to be done via arrays?
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 17, 2014, 12:46:23 PM
Probably something like
if(($field['colname'] == 'cust_aboutm0') && !empty($field['value']))
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 17, 2014, 02:39:15 PM
Quote from: margarett on July 17, 2014, 12:46:23 PM
Probably something like
if(($field['colname'] == 'cust_aboutm0') && !empty($field['value']))

Okay, trying something like this:
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
if(($field['colname'] == 'face_pro') && !empty($field['value']))
if(($field['colname'] == 'cust_last') && !empty($field['value']))
if(($field['colname'] == 'cust_soundc') && !empty($field['value']))
if(($field['colname'] == 'cust_twitte') && !empty($field['value']))
echo '
WORKS
';


But it doesn't work, how should I tie all these together?
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 17, 2014, 07:27:44 PM
Untested, can kill your forum, burn your house and drown your cat ;D

I assume you only want to know if either gender or one of the custom fields are filled, right?

If so:
//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty['value'] && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo 'WORKS';
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 18, 2014, 05:03:30 AM
Quote from: margarett on July 17, 2014, 07:27:44 PM
Untested, can kill your forum, burn your house and drown your cat ;D

I assume you only want to know if either gender or one of the custom fields are filled, right?

If so:
//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty['value'] && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo 'WORKS';


Trying this, but I'm getting a syntax error on the comma between ['colname'], $list_of_fields What can I substitute it with?
Title: Re: Check and/or multiple profile options are filled?
Post by: Shambles on July 18, 2014, 05:53:49 AM
Try changing

if (!empty['value'] && in_array($field['colname'], $list_of_fields))

to this

if (!empty($field['value']) && in_array($field['colname'], $list_of_fields))
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 18, 2014, 06:41:02 AM
Yes, thank you ;)
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 18, 2014, 07:14:56 AM
Thank you so much, everyone, that works perfectly!! Now to see if the server crashes over time  ;D
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 18, 2014, 05:19:18 PM
Here's the finished code for anyone that's interested:

//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty($field['value']) && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo '
  <input id="hidiv" type="checkbox" value=""/>
  <label id="showlabel" for="hidiv" >More...</label>
  <div class="toshow">
  ';


Then have all of your custom fields and your gender display, and at the end of it, put:

if ($check)
  echo '</div>';


Here's the css, mine doesn't have a hide, just a show but any old show/hide css will do:
#hidiv {
   display:none;
}
#showlabel  {
  display:block;
  font-family: arial, verdana, sans-serif;
  font-size:12px;
  font-weight:bold;
  line-height:34px;
  cursor:pointer;
  clear:both;
}
.toshow ,
#hidiv:checked + label {
  display:none;
}
#hidiv:checked ~ .toshow {
  display:block;
}

I guess it could be modded to add other fields you want to show hide, the hardest part was getting all of the custom fields, the other stuff should be easier.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 20, 2014, 10:18:14 AM
One last thing, I've added the members age to the check, and it works, but unfortunately SMF decides to tell me that Age: n/a if there is no birthday set, which really I can't understand why it would do that when everything else here is as meaningful or meaningless to display as each other. How do i stop it from giving me the N/A back? This is the currently adjusted code I am using:

//Control variable for the whole thing
$check = false;
//array with the list of custom fields that we want to check
$list_of_fields = array('face_pro', 'cust_last', 'cust_soundc', 'cust_twitte');

//Check for age
if (!empty($context['member']['age']))
$check = true;

//Check for gender
if (!isset($context['disabled_fields']['gender']) && !empty($context['member']['gender']['name']))
$check = true;

//loop the loop
foreach ($context['custom_fields'] as $field)
{
if (!empty($field['value']) && in_array($field['colname'], $list_of_fields))
{
   $check = true;
   break;
}
}

//Finally...
if ($check)
  echo '
  <input id="hidiv" type="checkbox" value=""/>
  <label id="showlabel" for="hidiv" >More...</label>
  <div class="toshow">
  ';

if (!empty($context['member']['age']))
echo '
<dt style="margin-top:10px;">', $txt['age'], ':</dt>
<dd style="margin-top:10px;">', $context['member']['age'] . ($context['member']['today_is_birthday'] ? ' &nbsp; <img src="' . $settings['images_url'] . '/cake.png" alt="" />' : ''), '</dd>';


I cut the code after where it is relevant for this particular issue.
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 21, 2014, 06:05:32 AM
Is N/A being stored in the mysql table, or is some other part reporting the age as N/A if the birthday value is empty? I really just want it to be a null value like all of the other options if it has nothing set rather than this N/A business.
Title: Re: Check and/or multiple profile options are filled?
Post by: margarett on July 21, 2014, 06:23:00 AM
Sources/Profile-View.php

// Set the age...
if (empty($context['member']['birth_date']))
{
$context['member'] += array(
'age' => $txt['not_applicable'],
'today_is_birthday' => false
);
}

Just have 'age' => '',
And it becomes empty ;)
Title: Re: Check and/or multiple profile options are filled?
Post by: samborabora on July 21, 2014, 11:16:55 AM
Quote from: margarett on July 21, 2014, 06:23:00 AM
Sources/Profile-View.php

// Set the age...
if (empty($context['member']['birth_date']))
{
$context['member'] += array(
'age' => $txt['not_applicable'],
'today_is_birthday' => false
);
}

Just have 'age' => '',
And it becomes empty ;)

I tried that, and it just gave me a 1, lol! I changed it in:

index.english.php:
$txt['not_applicable'] = '';

If I find any missing N/A's around the board after doing this, I'll just make a new txt string, blank it and change the age to refer to that instead, thanks for your help!