<?php

require_once dirname(__FILE__)."/../../../../lib-tk/shield.php";
require_once ("lib-action/action.php");

class EditMyProfile extends MB_Action
{
	public function execute()
	{
		parent::execute();

		$session = getCurrentSession();
		if ($session->isReseller()) {
			return $this->editResellerProfile($session->getReseller()->id);
		}

		require_once("object_managers/AdminManager.php");
		$admin = AdminManager::getAdmin();

		$controller = UIFactory()->PageController(action_url("admins", "ShowAdmins"));

		require_once("UI/Page/UIUpdateAdminPage.php");
		$controller->addPage($page = new UIUpdateAdminPage($controller, $admin->id, blmsg("TRANS_ADMIN_EDIT_MY_PROFILE"), false));
		$controller->processRequest();

		require_once("object_managers/SecurityManager.php");
		if (SecurityManager::isPasswordExpired())
			$page->addWarningMessage(UIFactory()->WarningMessage(blmsg("TRANS_ADMIN_PASSWORD_WAS_EXPIRED_NEED_TO_CHANGE", array("DAYS"  => Settings()->security_PasswordExpirationInterval))));

		if ($admin->isSyncable() && !is_null($admin->sync_error)) {
			getCurrentSession()->Storage->errorMessages[] = blmsg(
				"TRANS_SYNC_THIS_ADMIN_ERROR_MSG",
				array("ERROR" => billing_safetyhtml($admin->sync_error))
			);
		}

		$this->smartyVars["page"] = $page;
		$this->addTemplate("page");
		return $this->hasExecutedSuccessfully();
	}

	private function editResellerProfile($reseller_id)
	{
		$controller = UIFactory()->PageController(action_url("dashboard", "ShowDashboard"));

		require_once("UI/Page/UIUpdateResellerPage.php");
		$controller->addPage(new UIUpdateResellerPage($controller, $reseller_id, blmsg("TRANS_RESELLER_EDIT_PROFILE")));
		$controller->processRequest();

		$this->smartyVars["page"] = $controller->getPage();
		$this->addTemplate("page");
		return $this->hasExecutedSuccessfully();
	}
}
