<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @package    Core
 * @subpackage Forms
 */

include_once('config.inc.php');
include_once('classes/core/forms/ObjectForm.class.php');

class UserForm extends ObjectForm {

	var $userId;

	function UserForm(&$site) {
		parent::ObjectForm($site);
		$this->userId		= $this->site->getSubmittedVar('user_id', 0);
		$this->action 	= $this->site->getSubmittedVar('mode');

//todo : check if all needed vars are found : otherwise should generate errors

		if (!($lid = $site->getSubmittedVar('lid', 0))) {
			if (!($lid = $site->getPersistentVar('lid', 0))) {
				$lid = $this->site->getDefaultLangId();
			}
		}

		$this->objectManager = &$this->site->getUsersManager();

		if($this->userId == 0) {
			$user = &$this->objectManager->getNewUser();
      $user->setUserLangId($lid);
		}
		else {
			$user = &$this->objectManager->getUser($this->userId);
		}
		$this->object = &$user;
	}

	function processForm() {
		$user = &$this->site->getCurrentUser();
		if(!$user->checkGroupMembership(GROUP_ID_ADMINS)){
			return ;
		}

		switch($this->action) {
			case ASK_MODIF_USER:
				break;
			case MODIF_USER:
				$this->processFormModif();
				break;
			case DELETE_USER:
				$this->processFormDelete();
				break;
		}
	}

	function processFormModif() {
		$this->object->setUserValues($_POST);
		$this->objectManager->setUser($this->object);
		
		$user = &$this->site->getCurrentUser();
    $user_id = $user->getUserId();

    if($this->object->getUserId() == USER_ID_ROOT && $user_id != USER_ID_ROOT) {
      return false;
    }

		if (!$this->objectManager->storeUser($this->object->getUserId())) {
			return false;
		}
		return true;
	}

	function processFormDelete() {
    $result = false;
    $invalid_users = array(USER_ID_ROOT, USER_ID_ADMIN, USER_ID_ANONYMOUS);
    if(!in_array($this->userId, $invalid_users)) {
      $result = $this->objectManager->removeUser($this->userId);
    }
		return $result;
	}
}
?>