<?php

/**
 * 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
 * @copyright Copyright (c) 2012 - 2017 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfBackendSelectsiteAction Class
 *
 * The action class to set the site to edit.
 *
 * @package clearFusionCMS
 */
class cfBackendSelectsiteAction extends cfBackendControllerAction {
	/**
	 * Process the page.
	 */
	function process() {
		$this->breadcrumbTrail->addCrumb(__('Select the Site to Manage', 'cfBackend'), $this->assemble('', 'selectsite'));

		$siteMgr = new cfSiteManager($this->db);

		$msg = $this->session->flashGet('cfManagerOpSuccess') ? $this->alertMessage('Success', __('The site being managed has been updated.', 'cfBackend')) : '';

		$form = new flForm($this->request, 'siteform', $this->assemble('', 'selectsite'), true);

		$site = new flFormElementSelect($form, 'site', clearFusionCMS::getSite(), $siteList = $siteMgr->listSites($this->user->getPermissions()));
		$site->setLabel(__('Manage Site', 'cfBackend'));

		$submit = new flFormElementSubmit($form, 'update');
		$submit->setCaption(__('Manage Site', 'cfBackend'))->setCSS('primary');

		$form->focusElement($site);

		if($form->isSubmitted() && $form->isValid()) {
			// Look update domain and redirect to the alternative admin
			$row = $this->db->fetchRow('SELECT domain,puressl FROM cf_sites WHERE shortname=? && redirectto=\'\'', $site->getValue());
			$uri = ($row['puressl'] ? 'https://' : 'http://') . $row['domain'] . $this->assemble('', 'dashboard');
			$this->response->redirectTo($uri);
		}

		// Draw the page
		$this->renderPage(__('Select the Site to Manage', 'cfBackend'), $this->renderBlock(__('Select the Site to Manage', 'cfBackend'),
			$form->render($this->tpl->getCodeBuffer()), '', $msg));
	}

}
