View source
<?php
function _fb_registration_form_alter_fields(&$form) {
if (isset($form['name'])) {
$form['name']['#fb_registration_field'] = 'name';
}
if (isset($form['mail'])) {
$form['mail']['#fb_registration_field'] = 'email';
}
if (isset($form['captcha'])) {
$form['captcha']['#fb_registration_field'] = 'captcha';
}
foreach (element_children($form) as $key) {
_fb_registration_form_alter_fields($form[$key]);
}
}
function fb_registration_form_alter(&$form, &$form_state, $form_id) {
if (!isset($GLOBALS['_fb'])) {
return;
}
if ($GLOBALS['user']->uid) {
return;
}
if ($form_id == 'user_register') {
$form['#fb_registration'] = TRUE;
_fb_registration_form_alter_fields($form);
}
if (isset($form['#fb_registration'])) {
$sr = $GLOBALS['_fb']
->getSignedRequest();
if ($sr && ($registration = $sr['registration'])) {
$form['captcha'] = NULL;
if (!isset($form_state['fb_registration_avoid_recursion'])) {
$state = array(
'fb_registration_avoid_recursion' => TRUE,
'values' => $registration,
'fb_regisration_values' => $registration,
);
foreach ($registration as $key => $value) {
if (is_array($value) && $value['name']) {
$state['values'][$key] = $value['name'];
}
elseif ($key == 'email' && is_string($value)) {
if (!isset($state['values']['mail'])) {
$state['values']['mail'] = $value;
}
}
}
drupal_execute($form_id, $state);
}
return;
}
$fb_fields = array();
_fb_registration_extract_fb_fields($fb_fields, $form);
$redirect_url = url(fb_scrub_urls($_REQUEST['q']), array(
'absolute' => TRUE,
));
$fields = json_encode($fb_fields);
if (FALSE) {
$attrs = "fields='" . json_encode($fb_fields) . "' redirect-uri='" . url(fb_scrub_urls($_REQUEST['q']), array(
'absolute' => TRUE,
)) . "'";
$form['fb_registration'] = array(
'#type' => 'markup',
'#value' => '<fb:registration ' . $attrs . '></fb:registration>',
);
}
else {
$id = $GLOBALS['_fb_app']->id;
$url = urlencode($redirect_url);
$fields = urlencode($fields);
$markup = "<iframe src=http://www.facebook.com/plugins/registration.php?client_id={$id}&redirect_uri={$url}&fields={$fields}\n scrolling=\"auto\"\n frameborder=\"no\"\n style=\"border:none;\"\n allowTransparency=\"true\"\n width=\"100%\"\n height=\"360px\">\n</iframe>";
$form['fb_registration'] = array(
'#type' => 'markup',
'#value' => $markup,
);
}
}
}
function _fb_registration_extract_fb_fields(&$fb_fields, &$form) {
foreach (element_children($form) as $key) {
$field = NULL;
if (isset($form[$key]['#fb_registration_field'])) {
if (is_string($form[$key]['#fb_registration_field'])) {
$field = array(
'name' => $form[$key]['#fb_registration_field'],
);
}
elseif (is_array($form[$key]['#fb_registration_field'])) {
$field = $form[$key]['#fb_registration_field'];
}
}
else {
if (!isset($form[$key]['#type'])) {
_fb_registration_extract_fb_fields($fb_fields, $form[$key]);
}
elseif ($form[$key]['#type'] == 'fieldset') {
_fb_registration_extract_fb_fields($fb_fields, $form[$key]);
$form[$key] = NULL;
}
elseif ($form[$key]['#type'] == 'submit') {
$form[$key] = NULL;
}
elseif ($form[$key]['#type'] == 'textfield') {
$field = array(
'name' => $key,
'type' => 'text',
'description' => $form[$key]['#title'],
);
}
elseif ($form[$key]['#type'] == 'textarea') {
$field = array(
'name' => $key,
'type' => 'text',
'description' => $form[$key]['#title'],
);
}
elseif ($form[$key]['#type'] == 'checkbox') {
$field = array(
'name' => $key,
'type' => 'checkbox',
'description' => $form[$key]['#title'],
'default' => isset($form[$key]['#default_value']) ? $form[$key]['#default_value'] : NULL,
);
}
elseif ($form[$key]['#type'] == 'select') {
$field = array(
'name' => $key,
'type' => 'select',
'description' => $form[$key]['#title'],
'options' => $form[$key]['#options'],
'default' => $form[$key]['#default_value'],
);
}
}
if (count($field)) {
$fb_fields[] = $field;
$form[$key] = NULL;
}
}
}