protected function createUser($profileData)
{
$query = "INSERT IGNORE INTO #__akeebasubs_users (userid) VALUES (" . $this->db->quote($this->joomlaId) . ")";
$this->db->setQuery($query);
$this->db->execute();
// Custom fields stored as params
$akCustomFieldKeys = array("agreetotos", "dob", "phone", "mobile","gender");
$akCustomFields = '';
//
foreach ($this->getProfileFields() as $name => $displayName){
// Regular fields
if(!in_array($name, $akCustomFieldKeys)){
if($name == "state"){ // We need to pass the country too
$this->saveProfileField($name, array($profileData->getFieldWithUserState($name), $profileData->getFieldWithUserState("country")));
}else{
$this->saveProfileField($name, $profileData->getFieldWithUserState($name));
}
}else{ // custom (params) fields
// concatenate string of values
$akCustomFields .= '"' . $name . '":"' . $profileData->getFieldWithUserState($name) . '"';
}
}
// Check if any custom (params) fields included
if(is_string($akCustomFields) && strlen($akCustomFields) > 5){
$akCustomFields = '{' . $akCustomFields . '}';
// Save the field
$this->saveProfileField('params', $akCustomFields);
}
}
protected function saveProfileField($fieldId, $value)
{
$addStateToAddress2 = false; // flag - used to determine if state should be appended to address2 field
// Load the language file for gender used below
$lang = JFactory::getLanguage();
$lang->load('com_jfbconnect');
switch ($fieldId)
{
case "dob":
//$value = date('m/d/Y',strtotime($value)); // - This is the original from system plugin
$value = new JDate($value);
$value = $value->toSql();
break;
case "gender":
$value = $this->getGenderIdFromString($value);
break;
case "state":
$originalValue = $value[0]; // The state
$value = $this->getLocationIdFromName('states', $value[0], $value[1]); // $value[1] is the country
if(trim($value[0]) == ""){
$addStateToAddress2 = true;
$state = $originalValue;
}
break;
case "country":
$value = $this->getLocationIdFromName('countries', $value);
break;
}
$this->db->setQuery("UPDATE #__akeebasubs_users SET `" . $fieldId . "` = " . $this->db->quote($value) . " WHERE userid=" . $this->joomlaId);
$this->db->execute();
// If a state was present from FB but doesn't exist in the Akeeba States
if($addStateToAddress2){
// Check that the state isn't already part of address2
$this->db->setQuery("SELECT address2 FROM #__akeebasubs_users WHERE userid =" . $this->joomlaId);
$result = $this->db->execute();
// If the state isn't found in address2 field
if(!strpos($state, $result)){
//append it to the value
$this->db->setQuery("UPDATE #__akeebasubs_users SET address2 = CONCAT(address2, ', " . $state . "') WHERE userid=" . $this->joomlaId);
$this->db->execute();
}
}
}
/**
* Method to get the Country/State ID from name of the state/country
*
* @param string $type The type to get - 'countries' or 'states'
* @param string $name The name of the country/state
* @param string $country The name of country - ONLY used in case of state lookup
* @return sting $returnID Either 2 letter ISO Country/State code or nothing if not found
*/
public function getLocationIdFromName($type, $name, $country="")
{
// Valid types to check against
$types = array('countries', 'states');
// To return
$returnID = ""; // Default
// Check we have values to search on
if((!is_string($name) || strlen($name) <= 0) ||
(!in_array($type, $types) || !is_string($type)))
return $returnID;
switch($type){
case "countries":
if(is_array($this->countries) && count($this->countries) > 0)
$returnID = (string)array_search($name, $this->$type);
break;
case "states":
// Bail out if no country is passed or if no states present for the country
if(is_string($country) && strlen($country) > 1 && array_key_exists($country, $this->$type)){
$states = $this->$type;
$returnID = (string)array_search($name, $states[$country]);
}
break;
default:
break;
}
// return
return $returnID;
}
/**
* Method to get the gender ID from string
* Transforms the 'male'/'female' strings from facebook
* to populate the subscription form with relevant numeric ID
*
* @param string $string "male" or "female" - anything else returns 0
* @return int $genderID 0 = not specified, 1 = male, 2 = female
*/
public function getGenderIdFromString($string)
{
$genderID = 0; //Default
// Check there was a string passed in
if(!is_string($string) || strlen($string) <= 1)
return $genderID; // return the default
switch($string){
case "male":
$genderID = 1;
break;
case "female":
$genderID = 2;
break;
default:
$genderID = 0;
break;
}
// return ID
return $genderID;
}/**
* Get field names and inputs to request additional information from users on registration
* @return string HTML of form fields to display to user on registration
*/
public function socialProfilesOnShowRegisterForm($network)
{
$this->loadSettings($network);
$profileData = $this->fetchProfileFromFieldMap(false);
$html = $this->getRegistrationForm($profileData);
return $html;
}<div class="control-group <?php echo $group_classes['country'] ?>">
<label for="country" class="control-label">
* <?php echo JText::_('COM_AKEEBASUBS_LEVEL_FIELD_COUNTRY')?>
</label>
<div class="controls">
<?php echo AkeebasubsHelperSelect::countries($field_data['country'], 'country', array('id'=>'country', 'show' => $cparamShowCountries, 'hide' => $cparamHideCountries)) ?>
<span id="country_empty" class="help-inline" <?php if($group_classes['country'] != 'error'):?>style="display:none"<?php endif?>>
<?php echo JText::_('COM_AKEEBASUBS_LEVEL_ERR_REQUIRED')?>
</span>
</div>
</div><?php if ($field_data['country'] != null)
$style = 'style="display:none"';
else
$style = "";
?>
<div class="control-group <?php echo $group_classes['country'] ?>" <?php echo $style ?> >
<label for="country" class="control-label">
* <?php echo JText::_('COM_AKEEBASUBS_LEVEL_FIELD_COUNTRY')?>
</label>
<div class="controls">
<?php echo AkeebasubsHelperSelect::countries($field_data['country'], 'country', array('id'=>'country', 'show' => $cparamShowCountries, 'hide' => $cparamHideCountries)) ?>
<span id="country_empty" class="help-inline" <?php if($group_classes['country'] != 'error'):?>style="display:none"<?php endif?>>
<?php echo JText::_('COM_AKEEBASUBS_LEVEL_ERR_REQUIRED')?>
</span>
</div>
</div>First, you save the mapping of the fields in the admin area. JFBConnect stores this 'mapping' in the database. Then, on login or registration, we automatically fetch any fields that you've requested about the user and automatically call that saveProfileField($fieldId, $value) function. The $fieldId parameter would be the Akeeba Subs field Id that is having data imported into it. The $value field is the data from Facebook to import. Basically, JFBConnect takes care of all the logic of fetching the profile and doing a lot of that stuff (if you already have the configuration page working), so all you have to do is decide how to save that data.1. How is JFBC/SCProfile handling updating the user's profile - I mean, which methods? Obviously, I need the saveProfileField()
In the constructor of your plugin, there should be a line like $this->defaultSettings->set('import_always', '0'); If you set that value to '1', then the profile will be imported on every login. If it's '0', then it will only happen on registration.2. Do I need to override them to have the users' fields Ito be updated at login based on my profile plugin's config? && Will this be effected by the fact that I'm using auto crate users
public function getJFBCState()
{
//default return
$return = false;
// Get app
$app = JFactory::getApplication();
// Get the state var
$jfbc_state = $app->getUserState('plg_socialprofiles.facebook.akeebasubs', null);
// If the state var exists
if(!is_null($jfbc_state)){
// JSON decode it
$jfbc_state = json_decode($jfbc_state);
// map the 'raw' fb profile to a custom session
$this->fbProfile = $this->mapJFBCProfileToAkeebasubs($jfbc_state);
// If the mapped profile empty, return
if(empty($this->fbProfile) || is_null($this->fbProfile))
return $return;
// set custom state var. This will be used to pre-fill forms with the user's FB profile data
$app->setUserState('mybo.elite.mapped.fb.profile', $this->fbProfile);
// set flag
$this->jfbcProfileExists = true;
//Return
$return = true;
}
return $return;
}/**
* Method to encode/decode facebook profile data from JFBC
* and map it to Akeeba subscription cache so that the
* subscription form is pre-populated
*
* @param object $profileData The profile object returned from facebook login
* @return array $fbDataCheck Associative array containing Akeebasubs-ready data to pre-populate subscription form
*/
public function mapJFBCProfileToAkeebasubs($profileData)
{
// To return
$fbDataCheck = array();
if(!is_object($profileData) || is_null($profileData))
return $fbDataCheck;
// location properties to check
$locationFields = array('country', 'state', 'city');
// Loop through all of the fbFields 'keys' to check/transform them for akeeba
foreach($profileData as $f => $v){
//If the field & value exist
if($v){
switch($f){
//Location related object
case 'current_location':
foreach($locationFields as $l){
//Check that the property exists in the location object & there's a value
if(property_exists($profileData->$f, $l) && $v){
switch($l){
//Country property
case "country":
//$fbDataCheck[$l] = $this->getLocationIdFromName('countries', $profileData->current_location->country);
$fbDataCheck[$l] = $this->getLocationIdFromName('countries', $v->$l);
break;
//State property
case "state":
// Check if the state exists in akeeba subs
$state = $this->getLocationIdFromName('states', $v->$l, $profileData->current_location->country);
// set state to 2 char ISO code if getLocationIdFromName() not empty OR to original string from FB id
$fbDataCheck[$l] = (!is_null($state) && strlen($state) >= 3) ? $state : $v->$l;
break;
//Other properties
default:
$fbDataCheck[$l] = $v->$l;
break;
}// End switch $l
}// End if property_exists
}// End foreach $locationFields
break;
//DOB
case 'birthday':
$fbDataCheck['dob'] = date('m/d/Y',strtotime($v));
break;
// GENDER
case 'sex':
$fbDataCheck['gender'] = $this->getGenderIdFromString($v);
break;
// Anything else
default:
$fbDataCheck[$f] = $v;
break;
}// End switch
}//End if
}// End foreach
return $fbDataCheck;
}public function onBeforeAkeebasubsControllerLevelRead(FOFController &$controller, FOFInput &$input)
{
// Get JFBCProfile Plugin
$plugin = JPluginHelper::getPlugin('socialprofiles', 'akeebasubs');
/* I NEED TO GET THE PLUGIN SETTINGS HERE BEFORE PROCEEDING
* This will allow me to show/hide fields populated by facebook
* according to the configuration of the akeebasubs social profile
*/
// Get the view
$view = $controller->getThisView();
$viewCache = (array)$view->cache;
if(!$this->jfbcLoaded || !$this->jfbcProfileExists)
return;
// To store akeeba field ids of fields filled by FB
$facebookData = array();
// Loop through each view->cache
foreach($viewCache as $k => $v){
// If a match found in FB data
if((array_key_exists($k, $this->fbProfile) && ($viewCache[$k] || $this->fbProfile[$k])) || $k == "custom"){
switch($k){
// Custom akeeba fields
case "custom":
// Check its an array
if(is_array($v) && !empty($v)){
//loop through
foreach($v as $key => $val){
// both vals
if($val && $this->fbProfile[$key]){
$viewCache[$k][$key] = $this->fbProfile[$key];
$facebookData[] = $key;
}
// just facebook val
elseif(!$val && $this->fbProfile[$key]){
$viewCache[$k][$key] = $this->fbProfile[$key];
$facebookData[] = $key;
}
}//end foreach
}else die('not array');
break;
// All other fields
default:
//if both vals
if($v && $this->fbProfile[$k]){
$viewCache[$k] = $this->fbProfile[$k];
$facebookData[] = $k;
}
// Just FB
elseif(!$v && $this->fbProfile[$k]){
$viewCache[$k] = $this->fbProfile[$k];
$facebookData[] = $k;
}
break;
}
}
}
//Set cache and fbFields properties of the view
$view->set('fbFields', $facebookData);
$view->set('cache', $viewCache);
}Join our newsletter to get alerts for Joomla releases, tips and tricks and extension updates.
