Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Created January 28, 2020 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgi-caesar/b264df5e174add7818839c987b3685c7 to your computer and use it in GitHub Desktop.
Save cgi-caesar/b264df5e174add7818839c987b3685c7 to your computer and use it in GitHub Desktop.
aMember (site.php): Sync aMember User information to Xenforo 2 custom fields
<?php
/**
* You need to create custom user field within Xenforo admin interface
* Users -> Custom user Fields
*
* Then you can use the following code to sync some data from aMember to Xenforo fields
*
* Then you can use the following syntax within Xenforo template to display this information:
*
* Subscriptions: {$xf.visitor.Profile.custom_fields.active_products}
* Expire Date: {$xf.visitor.Profile.custom_fields.expire}
*/
Am_Di::getInstance()->hook->add(
Am_Event::SUBSCRIPTION_ADDED,
function (Am_Event $e) {
$user = $e->getUser();
$product_titles = implode(", ", $e->getDi()->productTable->getProductTitles($user->getActiveProductIds()));
syncUserFieldToXf($user, 'active_products', $product_titles);
});
Am_Di::getInstance()->hook->add([
Am_Event::ACCESS_AFTER_UPDATE,
Am_Event::ACCESS_AFTER_INSERT,
Am_Event::ACCESS_AFTER_DELETE
],
function (Am_Event $e) {
/* @var User $user */
$user = $e->getAccess()->getUser();
$v = amDate($user->getExpire());
syncUserFieldToXf($user, 'expire', $v);
});
function syncUserFieldToXf(User $user, $fn, $v)
{
$di = Am_Di::getInstance();
if ($pl = $di->plugins_protect->loadGet('xenforo')) {
$table = $pl->getTable();
if ($visitor = $table->findByAmember($user)) {
$table->getAdapter()->query(<<<CUT
INSERT INTO xf_user_field_value (user_id, field_id, field_value) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE field_value = VALUES(field_value)
CUT
, $visitor->user_id, $fn, $v);
$val = $table->getAdapter()->selectCell("SELECT custom_fields FROM xf_user_profile WHERE user_id=?", $visitor->user_id);
$val = $val ? json_decode($val, true) : [];
$val[$fn] = $v;
$val = json_encode($val);
$table->getAdapter()->query("UPDATE xf_user_profile SET custom_fields=? WHERE user_id=? LIMIT 1", $val, $visitor->user_id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment