<?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 GroupForm extends ObjectForm {

	var $groupId;

	function GroupForm(&$site) {
		parent::ObjectForm($site);
		$this->groupId	= $this->site->getSubmittedVar('group_id', 0);
		$this->action 	= $this->site->getSubmittedVar('mode');

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

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

		if($this->groupId == 0) {
			$group = &$this->objectManager->getNewGroup();
		}
		else {
			$group = &$this->objectManager->getGroup($this->groupId);
		}
		$this->object = &$group;
	}

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

		switch($this->action) {
			case ASK_MODIF_GROUP:
				break;
			case MODIF_GROUP:
				$this->processFormModif();
				break;
			case DELETE_GROUP:
				$this->processFormDelete();
				break;
		}
	}

	function processFormModif() {
		$this->object->setGroupValues($_POST);
		$this->objectManager->setGroup($this->object);

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

	function processFormDelete() {
    $result = false;
    $invalid_groups = array(GROUP_ID_ADMINS, GROUP_ID_EVERYONE);
    if(!in_array($this->groupId, $invalid_groups)) {
      $result = $this->objectManager->removeGroup($this->groupId);
    }
		return $result;
	}
}
?>