<?php

/**
 * Class file to clear user IP blocks.
 *
 * 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) 2013 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfBackendClearipblocksAction Class
 *
 * Action class to clear IP blocks.
 *
 * @package clearFusionCMS
 */
class cfBackendClearipblocksAction extends cfBackendControllerAction {
	/**
	 * Process the action.
	 */
	function process() {
		$this->testPermission('core.users');
		$this->breadcrumbTrail->addCrumb(__('Users', 'cfBackend'), $this->assemble('', 'listusers'));
		$this->breadcrumbTrail->addCrumb(__('Clear IP Blocks', 'cfBackend'), $this->assemble('', 'clearipblocks'));

		$content = '';

		// Build the search form
		$form = new flForm($this->request, 'searchform', $this->assemble('', 'listusers'), true);

		$searchfor = new flFormElementText($form, 'searchfor');
		$searchfor->setLabel(__('Search For', 'cfBackend'));

		$searchin = new flFormElementChecklist($form, 'searchin', array('username', 'email'), array(
			'username' => __('Username', 'cfBackend'),
			'email' => __('Email', 'cfBackend')));
		$searchin->setLabel(__('Search In', 'cfBackend'))
			->addValidator(new flFormValidatorMinChecklist(1));

		$exact = new flFormElementCheckbox($form, 'exact');
		$exact->setLabel(__('Exact Match', 'cfBackend'));

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

		$form->focusElement($searchfor);

		$this->db->query('TRUNCATE TABLE fl_usersipblocks');
		$content = $this->alertMessage('Done', __('All user IP blocks have been cleared.', 'cfBackend'));

		// Build floating action menu
		$fam = new flWidgetFloatingActionMenu();
		$fam->setCSS('[[+css]]');

		if($this->user->hasPermission('core.usersadd'))
			$fam->addAction('person_add', __('Add User', 'cfBackend'), $this->assemble('', 'adduser'));
		$fam->addAction('lock_open', __('Clear IP Blocks', 'cfBackend'), $this->assemble('', 'clearipblocks'));

		$this->tpl->registerChunk('fam', $fam->render());

		$this->renderPage(__('Clear IP Blocks', 'cfBackend'),
			$this->renderBlock(__('Clear IP Blocks', 'cfBackend'), $content),
			$this->renderBlock('', $form->render($this->tpl->getCodeBuffer())));
	}
}
