By Daniel Pierce on Wednesday, 16 August 2017
Replies 5
Likes 0
Views 1.5K
Votes 0
Support,

I have a small app that I want to display the user BIRTHDAY field and update it if it has not been filled in.

I'm able to use the getFieldValue('BIRTHDAY') to get the value that is set in the BIRTHDAY field. Works just fine. Here is the test code to pull the field value using the SocialFieldsUserDateTimeObject.


require_once ( JPATH_BASE . 'administrator' . DS . 'components' . DS . 'com_easysocial' . DS . 'includes' . DS . 'easysocial.php' );
require_once ( JPATH_BASE . 'administrator' . DS . 'components' . DS . 'com_easysocial' . DS . 'includes' . DS . 'user' . DS . 'user.php' );
require_once ( JPATH_BASE . 'media' . DS . 'com_easysocial' . DS . 'apps' . DS . 'fields' . DS . 'user' . DS . 'datetime' . DS . 'datetime.php' );

$user_id = (int) '658';
$user = ES::user($user_id);
echo ( '<br>');
echo ('Retreived Birthday' );
$user_birthday = $user->getFieldValue( 'BIRTHDAY' );
var_dump($user_birthday);
echo ( '<br><br>');

//$birthday = new DateTime( $user_birthday );
$new_birthday = new SocialFieldsUserDateTimeObject();
$new_birthday = $user_birthday->value;

echo ( '<br><br>');
echo( 'Year: ' . $user_birthday->value->year );
echo ( '<br>');
echo( 'Month: ' . $user_birthday->value->month );
echo ( '<br>');
echo( 'Day: ' . $user_birthday->value->day );
echo ( '<br>');
echo( 'hour: ' . $user_birthday->value->hour );
echo ( '<br>');
echo( 'minute: ' . $user_birthday->value->minute );
echo ( '<br>');
echo( 'second: ' . $user_birthday->value->second );



So I can get the users Birthday value which is GREAT. The problem I'm having is getting EasySocial to set the Birthday field value with the saveFieldValue. Here is the code I'm using which does nothing. What am I missing?


require_once ( JPATH_BASE . 'administrator' . DS . 'components' . DS . 'com_easysocial' . DS . 'includes' . DS . 'easysocial.php' );
require_once ( JPATH_BASE . 'administrator' . DS . 'components' . DS . 'com_easysocial' . DS . 'includes' . DS . 'user' . DS . 'user.php' );
require_once ( JPATH_BASE . 'media' . DS . 'com_easysocial' . DS . 'apps' . DS . 'fields' . DS . 'user' . DS . 'datetime' . DS . 'datetime.php' );

$user_id = (int) '658';
$dttm = new SocialFieldsUserDateTimeObject();

// Set the necessary data you want on this object.
$dttm->year = (string) '1965';
$dttm->month = (string) '02';
$dttm->day = (string) '01';
$dttm->hour = (string) '00';
$dttm->minute = (string) '00';
$dttm->second = (string) '00';

$user = ES::user($user_id);
$user->setFieldValue('BIRTHDAY', $dttm->toDate() );



Looking at the methods in the /media/com_easysocial/apps/fields/user/datetime.php the toDate() method should get the date from the object. I've tried just passing the DateTimeObject and also setting the $dttm->date value but that is a private field.

Love to get your direction why this is not working as it looks like it should.
Thanks for the help.

ALSO, where did the Developer documentation go? Can't find it on the web site.
Hi Daniel,

According to your code above, the reason why the data did not save correctly is because the value that you've pass to the setFieldValue .../administrator/components/com_easysocial/includes/user/user.php line 2098 going through the onEditValidate .../media/com_easysocial/apps/fields/user/birthday/birthday.php was incorrect.

I'm not really sure the purpose of it, perhaps you want to set the default birthday for the user that did not yet theirs?
Maybe you can try the code below:

$birthday = array();
$dttm = new SocialFieldsUserDateTimeObject();

// Set the necessary data you want on this object.
$dttm->year = (string) '1965';
$dttm->month = (string) '12';
$dttm->day = (string) '01';
$dttm->hour = (string) '00';
$dttm->minute = (string) '00';
$dttm->second = (string) '00';
$birthday['date'] = $dttm->toSql();
$user->setFieldValue('BIRTHDAY', $birthday );

and comment out the code below line 2098-2102 .../administrator/components/com_easysocial/includes/user/user.php temporary

$errors = $fields->trigger('onEditValidate', SOCIAL_FIELDS_GROUP_USER, $fieldArray, $args, array($handler, 'validate'));

if (is_array($errors) && count($errors) > 0) {
return $errors;
}


You're suggested to backup the file that you want to customize beforehand for precaution
·
Wednesday, 16 August 2017 13:58
·
0 Likes
·
0 Votes
·
0 Comments
·
Thanks Muhammad,

I made the changes that you recommended and it worked like a champ. Actually I could use $user->setFieldValue('BIRTHDAY', $dttm->toSql() ). Worked just fine after the validation check was removed.

Looked a little farther into how it is being validated in /media/com_easysocial/apps/fields/user/datetime/datetime.phhp

The method onEditValidate(&$post, &$user) simply calls the method validateDatetime($post). Looking at the validateDatetime method, it looks like it uses the $value->date part of the object to validate and that is empty. Is there a method to calculate/complete the value->date and value->timezone entries in the SocialFieldsUserDateTimeObject from the year,month, day, values because those fields are PRIVATE?

Don't want to remove the onEditValidate functionality in user.php, because it would eliminate validation on all fields.

Thanks for the help.
Dan
·
Thursday, 17 August 2017 02:59
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi Daniel,

I believe that you've able to use the code that I've given previously which is achieve your purpose (I'm afraid that I'm misunderstood your objective mainly )

Regarding to your inquiry, seems like it is not possible to use the setFieldValue function without remove the trigger of onEditValidate.
Perhaps can you add another function in the user library as I attached file below (line 23-73) and use the code that I've provide previously with a little tweak of the $user->setFieldCustomValue (line 1-20)

I thinks I've overlooks your inquiry regarding our developers documentation. Actually our PIC of documentation in the midst of updating our documentation. However, if you want to access the previous developer’s documentation, here is the link:
https://stackideas.com/docs/easysocial/developers/welcome
·
Thursday, 17 August 2017 18:22
·
0 Likes
·
0 Votes
·
0 Comments
·
Thank you so much for the help. It is really to bad that setFieldValue does not work for the BIRTHDAY field type.

Looks like a bug to me, but maybe it is a feature. Would hope it could be corrected at some point.

Dan
·
Wednesday, 30 August 2017 09:14
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi Dan,

Regarding this, I've create ticket so our developers will take a look if the any codes that caused you cannot use birthday field type using serFieldValue.

Thanks for your understanding.
·
Wednesday, 30 August 2017 15:14
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post