<?php

/**
 * Snippet to display and action the members profile edit function for logged in users.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\module\members
 * @copyright Copyright (c) 2012 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/* @var $tplEngine cfTemplateEngine */
$controller = $tplEngine->controller;

// Only run if the user is logged in
if(clearFusionCMS::getUser()->isAuthenticated()) {

	$allowEdit = 0;
	if(isset($usernameEdit)) {
		if($usernameEdit == 'ifNotSet') $allowEdit = cfUserProfile::ALLOWIFNOTSET;
		else if($usernameEdit == 'allow') $allowEdit = cfUserProfile::ALLOWCHANGE;
	}

	$form = new flForm(clearFusionCMS::getRequest(), 'editprofile', $controller->content->getPageURI($controller->pageData['id']), true);

	$profile = new cfUserProfile(clearFusionCMS::getDb(), clearFusionCMS::getUser(), cfUserProfile::USEREDIT, $allowEdit, false, isset($with2FA) ? $with2FA : false, isset($withBio) ? $withBio : false);
	$profile->process($form);

	if($form->isSubmitted() && $form->isValid() && !$form->isInvalidated()) {
		if(isset($onUpdated))
			$controller->getResponse()->redirectTo($controller->content->getPageURI($onUpdated, true));
		else
			echo __('Your profile has been updated.', 'members');
	}
	else
		echo $form->render($tplEngine->getCodeBuffer());

}
