• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Modern aac] Guilds Fix!

lbakiller

New Member
Joined
Aug 27, 2009
Messages
90
Reaction score
1
Location
The Netherlands
Hey there,

Here's a fix for the guilds error

in system\application\controllers\guilds.php

PHP:
<?php
class Guilds extends Controller {

	public function index() {
		$this->load->helper("form");
		$this->load->model("guilds_model");
		require_once("system/application/config/create_character.php");
		$data = array();
		$data['config'] = $config;
		$data['guilds'] = @$this->guilds_model->getGuildsList((int)$_REQUEST['world_id']);
		$this->load->view("guilds", $data);
	}
	
	public function view($id = null, $action = 0) {
		$ide = new IDE;
		if(empty($id)) $ide->redirect('../');
		if($action == 1) { success("You have joined the guild."); echo "<br />";}
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$guild = $ots->createObject('Guild');
		try {$guild->load($id); } catch(Exception $e) {show_error('Problem occured while loading guild. Err code: 220612072010 Futher information: '.$e->getMessage()); }
		if(!$guild->isLoaded()) error("Could not find guild.");
		$data['guild'] = $guild;
		$this->load->view("view_guild", $data);
	
	}
	
	public function _checkPlayer($id) {
		$this->load->model("guilds_model");
		if($this->guilds_model->checkPlayerCreatingGuild($id)) {
			return true;
		}
		else {
			$this->form_validation->set_message('_checkPlayer', 'Could not find character.');
			return false;
		}
	}
	
	public function _checkGuildName($name){
		$this->load->model("guilds_model");
		if($this->guilds_model->checkGuildName($name)) {
			return true;
		}
		else {
			$this->form_validation->set_message('_checkGuildName', 'Guild name is already taken.');
			return false;
		}
	}
	
	public function create() {
		$ide = new IDE;
		$ide->requireLogin();
		$this->load->helper("form");
		$this->load->model("guilds_model");
		require_once("system/application/config/create_character.php");
		$this->load->library("form_validation");
		if(isset($_POST['submit'])) {
			$this->form_validation->set_rules('character', 'Character', 'required|numeric|callback__checkPlayer');
			$this->form_validation->set_rules('name', 'Guild Name', 'required|alpha_space|callback__checkGuildName');
		}
		if($this->form_validation->run() == true) {
			$id = $this->guilds_model->createGuild($_POST['name'], $_POST['character']);
			$this->load->model("forum_model");
			global $config;
			
			// Get name of the creator, for he is gonna be a moderator!
			$ots = POT::getInstance();
			$ots->connect(POT::DB_MYSQL, connection());
			$owner = new OTS_Player();
			try {$owner->load($_POST['character']); } catch(Exception $e) {show_error('Problem occured while loading character. Err code: 220812072010 Futher information: '.$e->getMessage()); }
        	if(!$owner->isLoaded())
        		error("A strange error happend while trying to create the guild board, this guild won't have a board!");
        	else	
				$this->forum_model->createBoard(str_replace('%NAME%', $_POST['name'], $config['guildboardTitle']), str_replace('%NAME%', $_POST['name'], $config['guildboardTitle']), 0, 0, 9999, 1, $owner->getName(), $id);
					
			$ide->redirect(WEBSITE."/index.php/guilds/view/".$id);
			success("{$_POST['name']} has been created.");
		}
		$data = array();
		$data['characters'] = $this->guilds_model->getCharactersAllowedToCreateGuild($config['levelToCreateGuild']);
		$data['config'] = $config;
		$this->load->view("create_guild", $data);
		
	}
	
	public function join($guild_name, $player_name) {
		$guild_name = (int)$guild_name;
		$player_name = (int)$player_name;
		$ide = new IDE;
		if(empty($guild_name) or empty($player_name)) $ide->redirect(WEBSITE."/index.php/guilds");
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$guild = $ots->createObject('Guild');
		try {$guild->load($guild_name); } catch(Exception $e) {show_error('Problem occured while loading guild. Err code: 220912072010 Futher information: '.$e->getMessage()); }
		if(!$guild->isLoaded()) $ide->redirect(WEBSITE."/index.php/guilds");
		$player = new OTS_Player();
		try {$player->load($player_name);; } catch(Exception $e) {show_error('Problem occured while loading player. Err code: 221012072010 Futher information: '.$e->getMessage()); }
		if(!$player->isLoaded()) $ide->redirect(WEBSITE."/index.php/guilds");
		if($player->getAccount()->getId() != $_SESSION['account_id']) $ide->redirect(WEBSITE."/index.php/guilds");
		require('system/application/libraries/POT/InvitesDriver.php');
		new InvitesDriver($guild);
		$invited_list = $guild->listInvites();
		if(!in_array($player->getName(), $invited_list)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		if(!$this->guilds_model->canUpdate($player->getID())) error("`{$player_name}` are logged in, please logout first.");
		$guild->acceptInvite($player);
		$ide->redirect(WEBSITE."/index.php/guilds/view/".$guild->getId()."/1");
	}
	
	
	public function management($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_management", $data);
	}
	
	function _isInvitable($name) {
		$this->load->model("guilds_model");
		$player = $this->guilds_model->isInvitable($name);
			if(empty($player)) {
				$this->form_validation->set_message('_isInvitable', 'Could not find this player.');
				return false;
			}
			else if($player[0]['rank_id'] != 0) {
				$this->form_validation->set_message('_isInvitable', 'This player is already in guild.');
				return false;
			}
                        else if($player[0]['online'] != 0) {
                                $this->form_validation->set_message('_isInvitable', 'This player is online!');
                                return false;
                        }
			else
				return true;
	}
	
	public function invite($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
			if($_POST) {
				$_POST['name'] = decodeString($_POST['name']);
				$this->load->library("form_validation");
				$this->form_validation->set_rules('name', 'Player Name', 'required|nickname|callback__isInvitable');
				if($this->form_validation->run() == true) {
					$player_id = $this->guilds_model->getCharacterId($_POST['name']);
					$this->guilds_model->invite($id, $player_id[0]['id']);
					success($_POST['name']." has been invited to ".$data['guild'][0]['name']);
					$ide->redirect(WEBSITE."/index.php/guilds/management/".$id, 2);
				}
			}
		$this->load->helper("form_helper");
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_invite", $data);
	}
	
	public function members($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		$data['members'] = $this->guilds_model->getMembers($id);
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_members", $data);
	}
	
	public function changeDescription($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		if(empty($player)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->redirect(WEBSITE."/index.php/guilds/members/".$id);
		$data['description'] = $this->guilds_model->getMemberDescription($player);
		$this->load->helper("form_helper");
		$data['player'] = $player;
		$data['id'] = $id;
			if($_POST) {
				$this->load->library("form_validation");
				$this->form_validation->set_rules('description', 'Description', 'alpha_space');
				if($this->form_validation->run() == true) {
					$this->guilds_model->changeDescription($player, $_POST['description']);
					success("Description has been changed.");
					$ide->redirect(WEBSITE."/index.php/guilds/members/".$id, 2);
				}
			}
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_changeDescription", $data);
	}
	
	function _isValidRank($guild, $rank) {
		$this->load->model("guilds_model");
		$ranks = $this->guilds_model->getRanksID($guild);
			foreach($ranks as $ext) {
				$external[] = $ext['id'];
			}
		if(in_array($rank, $external)) {
			return true;
		}
		else
			return false;
	}
	
	public function changeRank($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		if(empty($player)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->redirect(WEBSITE."/index.php/guilds/members/".$id);
		if(!$this->guilds_model->canUpdate($player->getID())) error("Player are logged in, please logout first.");
		$this->load->helper("form_helper");
			if($_POST) {
				$_POST['new'] = (int)$_POST['new'];
				if(!is_int($_POST['new']))
					@$error .= "Rank must be an integer<br/>";
					
				if(empty($_POST['new']))
					@$error .= "Invalid Rank<br/>";
					
				if(!$this->_isValidRank($id, $_POST['new']))
					@$error .= "Invalid Rank Name</br>";
					
				if(!empty($error))
					error($error);
				else {
					$this->guilds_model->changeRank($player, $_POST['new']);
					success("Rank has been changed.");
					$ide->redirect(WEBSITE."/index.php/guilds/members/".$id, 2);
				}
					
			}
		$data['current'] = $this->guilds_model->getMemberRank($player);
		$data['ranks'] = $this->guilds_model->getRanks($id);
		$data['player'] = $player;
		$data['id'] = $id;
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_changeRank", $data);
	}
	
	public function kick($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->goPrevious();
		if(empty($player)) $ide->goPrevious();
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->goPrevious();
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->goPrevious();
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->goPrevious();
		if(!$this->guilds_model->canUpdate($player->getID())) error("Player are logged in, please logout first.");
		$this->guilds_model->kick($player);
		$ide->redirect(WEBSITE."/index.php/guilds/members/".$id);
	}
	
	public function leave($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		if(empty($player)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->redirect(WEBSITE."/index.php/guilds/view/".$id);
		if(!$this->guilds_model->canUpdate($player->getID())) error("Player are logged in, please logout first.");
		$this->guilds_model->leave($player);
		$ide->redirect(WEBSITE."/index.php/guilds/view/".$id);
	}
	
	public function motd($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		$data['motd'] = $data['guild'][0]['motd'];
			if($_POST) {
				$_POST['motd'] = decodeString($_POST['motd']);
				$this->load->library("form_validation");
				$this->form_validation->set_rules('motd', 'MOTD', 'max_lenght[255]|alpha_space');
				if($this->form_validation->run() == true) {
					$this->guilds_model->changeMotd($id, $_POST['motd']);
					success("MOTD has been changed.");
					$ide->redirect(WEBSITE."/index.php/guilds/management/".$id, 2);
				}
				
			}
		$this->load->helper("form_helper");
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_motd", $data);
	}
	
	public function logo($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->goPrevious();
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->goPrevious();
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		
				require("config.php");
				$cfg['upload_path'] = 'public/guild_logos';
				$cfg['allowed_types'] = 'gif';
				$cfg['max_size']	= '128';
				$cfg['max_width']  = '64';
				$cfg['max_height']  = '64';
				$cfg['file_name'] = $id;
				$cfg['overwrite'] = true;
				$this->load->library('upload', $cfg);
				if($this->upload->do_upload("logo"))
					success("Logo has been changed.");
				$data['error'] = $this->upload->display_errors();
		
			
		$this->load->helper("form_helper");
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_logo", $data);
	}
	
	public function delete($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->guilds_model->deleteGuild($id);
		
		$this->load->model("forum_model");		
		$this->forum_model->deleteBoardByGuild($id);
		
		$ide->redirect(WEBSITE."/index.php/guilds");
	}

}
?>

in system\application\models\guilds_model.php

PHP:
<?php

class Guilds_Model extends Model {
	public function getGuildsList($world_id = null) {
		$this->load->database();
		$ext = (!empty($world_id)) ? "WHERE `world_id` = '$world_id'": "";
		$guilds = array();
		$sql = $this->db->query("SELECT `id`, `world_id`, `name`, `motd` FROM `guilds` $ext")->result();
			foreach($sql as $cmd) {
				$guilds[] = array("id"=>$cmd->id, "world_id"=>$cmd->world_id, "name"=>$cmd->name, "motd"=>$cmd->motd);
			}
		return $guilds;
	}
	
	public function getCharactersAllowedToCreateGuild($level = 0) {
		$this->load->database();
		$characters = array();
		$sql = $this->db->query("SELECT `id`, `name`, `level` FROM `players` WHERE `rank_id` = '0' AND `online` = 0 AND `level` >= '{$level}' AND `account_id` = '{$_SESSION['account_id']}'")->result();
		foreach($sql as $cmd) {
			$characters[] = array('id'=>$cmd->id, 'name'=>$cmd->name,'level'=>$cmd->level);
		}
		return $characters;
	}
	
	public function checkPlayerCreatingGuild($id) {
		require_once("system/application/config/create_character.php");
		$this->load->database();
		$sql = $this->db->query("SELECT `id` FROM `players` WHERE `id` = '$id' AND `online` = 0 AND `level` >= '".LEVELTOCREATEGUILD."' AND `account_id` = '{$_SESSION['account_id']}'")->row_array();
		if(count($sql) == 0) return false; else return true;
	}
	
	public function checkGuildName($name) {
		$this->load->database();
		$name = $this->db->escape($name);
		$sql = $this->db->query('SELECT `id` FROM `guilds` WHERE `name` = "'.$name.'"')->row_array();
		if(count($sql) == 0) return true; else return false;
	
	}
	
	public function createGuild($name, $character) {
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$player = new OTS_Player();
		$player->load($character);
		$new_guild = new OTS_Guild();
		$new_guild->setCreationData(time());
		$new_guild->setName($name);
		$new_guild->setOwner($player);
		$new_guild->save();
		$new_guild->setCustomField('motd', 'New guild. Leader must edit this text :)');
		$new_guild->setCustomField('creationdata', time());
		$new_guild->setCustomField('world_id', $player->getWorld());
		$ranks = $new_guild->getGuildRanksList();
		$ranks->orderBy('level', POT::ORDER_DESC);
		foreach($ranks as $rank)
			if($rank->getLevel() == 3)
			{
				$player->setRank($rank);
				$player->save();
			}
		return $new_guild->getId();
	}
	
	public function getGuildInfo($id) {
		$this->load->database();
		return $this->db->query("SELECT `world_id`, `name`, `ownerid`, `creationdata`, `motd` FROM `guilds` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function isGuildLeader($id) {
		$this->load->database();
		return ($this->db->query("SELECT `id` FROM `players` WHERE `id` = '".$id."' AND `account_id` = '".$_SESSION['account_id']."' ")->num_rows == 0) ? false: true;
	}
	
	public function isInvitable($name) {
		$this->load->database();
		return $this->db->query("SELECT `rank_id`, `online` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function getCharacterId($name) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function invite($id, $player) {
		$this->load->database();
		$this->db->query("INSERT INTO `guild_invites` VALUES('".$player."', '".$id."')");
	}
	
	public function getMembers($id) {
		$this->load->database();
		return $this->db->query("SELECT players.id, players.name, guild_ranks.name AS guild_rank FROM guild_ranks LEFT JOIN players ON rank_id = guild_ranks.id WHERE guild_ranks.guild_id = '".$id."' ORDER BY guild_ranks.level DESC")->result_array();
	}
	
	public function isGuildMember($id, $player) {
		$this->load->database();
		return ($this->db->query("SELECT players.rank_id FROM players, guild_ranks WHERE players.rank_id = guild_ranks.id AND players.id = '".$player."' AND guild_ranks.guild_id = '".$id."'")->num_rows == 0) ? false: true;
	}
	
	public function getMemberDescription($id) {
		$this->load->database();
		return $this->db->query("SELECT `guildnick` FROM `players` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function changeDescription($player, $desc) {
		$this->load->database();
		$this->db->query("UPDATE players SET guildnick = \"".$desc."\" WHERE id = '".$player."'");
	}
	
	public function getMemberRank($id) {
		$this->load->database();
		return $this->db->query("SELECT rank_id FROM players WHERE id = '".$id."'")->result_array();
	}
	
	public function getRanks($id) {
		$this->load->database();
		return $this->db->query("SELECT `name`, `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function getRanksID($id) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function changeRank($player, $rank) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `rank_id` = '".$rank."' WHERE `id` = '".$player."'");
	}
	
	public function kick($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function leave($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function changeMotd($id, $motd) {
		$this->load->database();
		$this->db->query("UPDATE `guilds` SET `motd` = \"".$motd."\" WHERE `id` = '".$id."'");
	}
	
	public function deleteGuild($id) {
		$this->load->database();
		$this->db->query("DELETE FROM `guilds` WHERE `id` = '".$id."'");
		//Triggers should do rest of the job.
			if(file_exists("public/guild_logos/".$id.".gif"))
				unlink("public/guild_logos/".$id.".gif");
	}
	public function canUpdate($id) {
		$this->load->database();
		$sql = $this->db->query("SELECT 1 FROM `players` WHERE `id` = '$id' AND `online` = 0")->row_array();
	
		return count($sql);		
	}
	
}

?>

in system\application\views\view_guild.php

PHP:
<?php

class Guilds_Model extends Model {
	public function getGuildsList($world_id = null) {
		$this->load->database();
		$ext = (!empty($world_id)) ? "WHERE `world_id` = '$world_id'": "";
		$guilds = array();
		$sql = $this->db->query("SELECT `id`, `world_id`, `name`, `motd` FROM `guilds` $ext")->result();
			foreach($sql as $cmd) {
				$guilds[] = array("id"=>$cmd->id, "world_id"=>$cmd->world_id, "name"=>$cmd->name, "motd"=>$cmd->motd);
			}
		return $guilds;
	}
	
	public function getCharactersAllowedToCreateGuild($level = 0) {
		$this->load->database();
		$characters = array();
		$sql = $this->db->query("SELECT `id`, `name`, `level` FROM `players` WHERE `rank_id` = '0' AND `online` = 0 AND `level` >= '{$level}' AND `account_id` = '{$_SESSION['account_id']}'")->result();
		foreach($sql as $cmd) {
			$characters[] = array('id'=>$cmd->id, 'name'=>$cmd->name,'level'=>$cmd->level);
		}
		return $characters;
	}
	
	public function checkPlayerCreatingGuild($id) {
		require_once("system/application/config/create_character.php");
		$this->load->database();
		$sql = $this->db->query("SELECT `id` FROM `players` WHERE `id` = '$id' AND `online` = 0 AND `level` >= '".LEVELTOCREATEGUILD."' AND `account_id` = '{$_SESSION['account_id']}'")->row_array();
		if(count($sql) == 0) return false; else return true;
	}
	
	public function checkGuildName($name) {
		$this->load->database();
		$name = $this->db->escape($name);
		$sql = $this->db->query('SELECT `id` FROM `guilds` WHERE `name` = "'.$name.'"')->row_array();
		if(count($sql) == 0) return true; else return false;
	
	}
	
	public function createGuild($name, $character) {
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$player = new OTS_Player();
		$player->load($character);
		$new_guild = new OTS_Guild();
		$new_guild->setCreationData(time());
		$new_guild->setName($name);
		$new_guild->setOwner($player);
		$new_guild->save();
		$new_guild->setCustomField('motd', 'New guild. Leader must edit this text :)');
		$new_guild->setCustomField('creationdata', time());
		$new_guild->setCustomField('world_id', $player->getWorld());
		$ranks = $new_guild->getGuildRanksList();
		$ranks->orderBy('level', POT::ORDER_DESC);
		foreach($ranks as $rank)
			if($rank->getLevel() == 3)
			{
				$player->setRank($rank);
				$player->save();
			}
		return $new_guild->getId();
	}
	
	public function getGuildInfo($id) {
		$this->load->database();
		return $this->db->query("SELECT `world_id`, `name`, `ownerid`, `creationdata`, `motd` FROM `guilds` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function isGuildLeader($id) {
		$this->load->database();
		return ($this->db->query("SELECT `id` FROM `players` WHERE `id` = '".$id."' AND `account_id` = '".$_SESSION['account_id']."' ")->num_rows == 0) ? false: true;
	}
	
	public function isInvitable($name) {
		$this->load->database();
		return $this->db->query("SELECT `rank_id`, `online` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function getCharacterId($name) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function invite($id, $player) {
		$this->load->database();
		$this->db->query("INSERT INTO `guild_invites` VALUES('".$player."', '".$id."')");
	}
	
	public function getMembers($id) {
		$this->load->database();
		return $this->db->query("SELECT players.id, players.name, guild_ranks.name AS guild_rank FROM guild_ranks LEFT JOIN players ON rank_id = guild_ranks.id WHERE guild_ranks.guild_id = '".$id."' ORDER BY guild_ranks.level DESC")->result_array();
	}
	
	public function isGuildMember($id, $player) {
		$this->load->database();
		return ($this->db->query("SELECT players.rank_id FROM players, guild_ranks WHERE players.rank_id = guild_ranks.id AND players.id = '".$player."' AND guild_ranks.guild_id = '".$id."'")->num_rows == 0) ? false: true;
	}
	
	public function getMemberDescription($id) {
		$this->load->database();
		return $this->db->query("SELECT `guildnick` FROM `players` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function changeDescription($player, $desc) {
		$this->load->database();
		$this->db->query("UPDATE players SET guildnick = \"".$desc."\" WHERE id = '".$player."'");
	}
	
	public function getMemberRank($id) {
		$this->load->database();
		return $this->db->query("SELECT rank_id FROM players WHERE id = '".$id."'")->result_array();
	}
	
	public function getRanks($id) {
		$this->load->database();
		return $this->db->query("SELECT `name`, `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function getRanksID($id) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function changeRank($player, $rank) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `rank_id` = '".$rank."' WHERE `id` = '".$player."'");
	}
	
	public function kick($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function leave($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function changeMotd($id, $motd) {
		$this->load->database();
		$this->db->query("UPDATE `guilds` SET `motd` = \"".$motd."\" WHERE `id` = '".$id."'");
	}
	
	public function deleteGuild($id) {
		$this->load->database();
		$this->db->query("DELETE FROM `guilds` WHERE `id` = '".$id."'");
		//Triggers should do rest of the job.
			if(file_exists("public/guild_logos/".$id.".gif"))
				unlink("public/guild_logos/".$id.".gif");
	}
	public function canUpdate($id) {
		$this->load->database();
		$sql = $this->db->query("SELECT 1 FROM `players` WHERE `id` = '$id' AND `online` = 0")->row_array();
	
		return count($sql);		
	}
	
}

?>

that should do the trick.

Glad I could help.

(modern aac rev 244 guilds system :)
 
I do not have in-game guild management.
In my modern aac if you want to create, invite, join... guild you must be offline first.

Can players create guild being online using your script?
 
PHP:
    public function getCharactersAllowedToCreateGuild($level = 0) { 
        $this->load->database(); 
        $characters = array(); 
        $sql = $this->db->query("SELECT `id`, `name`, `level` FROM `players` WHERE `rank_id` = '0' AND `online` = '1' AND `level` >= '{$level}' AND `account_id` = '{$_SESSION['account_id']}'")->result(); 
        foreach($sql as $cmd) { 
            $characters[] = array('id'=>$cmd->id, 'name'=>$cmd->name,'level'=>$cmd->level); 
        } 
        return $characters; 
    }

I just tested it and I could create the guild while being online. :)
 
yea, but as theres more rev of modern acc I was using and old one, so..
 
Hey there,

Here's a fix for the guilds error

in system\application\controllers\guilds.php

PHP:
<?php
class Guilds extends Controller {

	public function index() {
		$this->load->helper("form");
		$this->load->model("guilds_model");
		require_once("system/application/config/create_character.php");
		$data = array();
		$data['config'] = $config;
		$data['guilds'] = @$this->guilds_model->getGuildsList((int)$_REQUEST['world_id']);
		$this->load->view("guilds", $data);
	}
	
	public function view($id = null, $action = 0) {
		$ide = new IDE;
		if(empty($id)) $ide->redirect('../');
		if($action == 1) { success("You have joined the guild."); echo "<br />";}
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$guild = $ots->createObject('Guild');
		try {$guild->load($id); } catch(Exception $e) {show_error('Problem occured while loading guild. Err code: 220612072010 Futher information: '.$e->getMessage()); }
		if(!$guild->isLoaded()) error("Could not find guild.");
		$data['guild'] = $guild;
		$this->load->view("view_guild", $data);
	
	}
	
	public function _checkPlayer($id) {
		$this->load->model("guilds_model");
		if($this->guilds_model->checkPlayerCreatingGuild($id)) {
			return true;
		}
		else {
			$this->form_validation->set_message('_checkPlayer', 'Could not find character.');
			return false;
		}
	}
	
	public function _checkGuildName($name){
		$this->load->model("guilds_model");
		if($this->guilds_model->checkGuildName($name)) {
			return true;
		}
		else {
			$this->form_validation->set_message('_checkGuildName', 'Guild name is already taken.');
			return false;
		}
	}
	
	public function create() {
		$ide = new IDE;
		$ide->requireLogin();
		$this->load->helper("form");
		$this->load->model("guilds_model");
		require_once("system/application/config/create_character.php");
		$this->load->library("form_validation");
		if(isset($_POST['submit'])) {
			$this->form_validation->set_rules('character', 'Character', 'required|numeric|callback__checkPlayer');
			$this->form_validation->set_rules('name', 'Guild Name', 'required|alpha_space|callback__checkGuildName');
		}
		if($this->form_validation->run() == true) {
			$id = $this->guilds_model->createGuild($_POST['name'], $_POST['character']);
			$this->load->model("forum_model");
			global $config;
			
			// Get name of the creator, for he is gonna be a moderator!
			$ots = POT::getInstance();
			$ots->connect(POT::DB_MYSQL, connection());
			$owner = new OTS_Player();
			try {$owner->load($_POST['character']); } catch(Exception $e) {show_error('Problem occured while loading character. Err code: 220812072010 Futher information: '.$e->getMessage()); }
        	if(!$owner->isLoaded())
        		error("A strange error happend while trying to create the guild board, this guild won't have a board!");
        	else	
				$this->forum_model->createBoard(str_replace('%NAME%', $_POST['name'], $config['guildboardTitle']), str_replace('%NAME%', $_POST['name'], $config['guildboardTitle']), 0, 0, 9999, 1, $owner->getName(), $id);
					
			$ide->redirect(WEBSITE."/index.php/guilds/view/".$id);
			success("{$_POST['name']} has been created.");
		}
		$data = array();
		$data['characters'] = $this->guilds_model->getCharactersAllowedToCreateGuild($config['levelToCreateGuild']);
		$data['config'] = $config;
		$this->load->view("create_guild", $data);
		
	}
	
	public function join($guild_name, $player_name) {
		$guild_name = (int)$guild_name;
		$player_name = (int)$player_name;
		$ide = new IDE;
		if(empty($guild_name) or empty($player_name)) $ide->redirect(WEBSITE."/index.php/guilds");
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$guild = $ots->createObject('Guild');
		try {$guild->load($guild_name); } catch(Exception $e) {show_error('Problem occured while loading guild. Err code: 220912072010 Futher information: '.$e->getMessage()); }
		if(!$guild->isLoaded()) $ide->redirect(WEBSITE."/index.php/guilds");
		$player = new OTS_Player();
		try {$player->load($player_name);; } catch(Exception $e) {show_error('Problem occured while loading player. Err code: 221012072010 Futher information: '.$e->getMessage()); }
		if(!$player->isLoaded()) $ide->redirect(WEBSITE."/index.php/guilds");
		if($player->getAccount()->getId() != $_SESSION['account_id']) $ide->redirect(WEBSITE."/index.php/guilds");
		require('system/application/libraries/POT/InvitesDriver.php');
		new InvitesDriver($guild);
		$invited_list = $guild->listInvites();
		if(!in_array($player->getName(), $invited_list)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		if(!$this->guilds_model->canUpdate($player->getID())) error("`{$player_name}` are logged in, please logout first.");
		$guild->acceptInvite($player);
		$ide->redirect(WEBSITE."/index.php/guilds/view/".$guild->getId()."/1");
	}
	
	
	public function management($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_management", $data);
	}
	
	function _isInvitable($name) {
		$this->load->model("guilds_model");
		$player = $this->guilds_model->isInvitable($name);
			if(empty($player)) {
				$this->form_validation->set_message('_isInvitable', 'Could not find this player.');
				return false;
			}
			else if($player[0]['rank_id'] != 0) {
				$this->form_validation->set_message('_isInvitable', 'This player is already in guild.');
				return false;
			}
                        else if($player[0]['online'] != 0) {
                                $this->form_validation->set_message('_isInvitable', 'This player is online!');
                                return false;
                        }
			else
				return true;
	}
	
	public function invite($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
			if($_POST) {
				$_POST['name'] = decodeString($_POST['name']);
				$this->load->library("form_validation");
				$this->form_validation->set_rules('name', 'Player Name', 'required|nickname|callback__isInvitable');
				if($this->form_validation->run() == true) {
					$player_id = $this->guilds_model->getCharacterId($_POST['name']);
					$this->guilds_model->invite($id, $player_id[0]['id']);
					success($_POST['name']." has been invited to ".$data['guild'][0]['name']);
					$ide->redirect(WEBSITE."/index.php/guilds/management/".$id, 2);
				}
			}
		$this->load->helper("form_helper");
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_invite", $data);
	}
	
	public function members($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		$data['members'] = $this->guilds_model->getMembers($id);
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_members", $data);
	}
	
	public function changeDescription($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		if(empty($player)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->redirect(WEBSITE."/index.php/guilds/members/".$id);
		$data['description'] = $this->guilds_model->getMemberDescription($player);
		$this->load->helper("form_helper");
		$data['player'] = $player;
		$data['id'] = $id;
			if($_POST) {
				$this->load->library("form_validation");
				$this->form_validation->set_rules('description', 'Description', 'alpha_space');
				if($this->form_validation->run() == true) {
					$this->guilds_model->changeDescription($player, $_POST['description']);
					success("Description has been changed.");
					$ide->redirect(WEBSITE."/index.php/guilds/members/".$id, 2);
				}
			}
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_changeDescription", $data);
	}
	
	function _isValidRank($guild, $rank) {
		$this->load->model("guilds_model");
		$ranks = $this->guilds_model->getRanksID($guild);
			foreach($ranks as $ext) {
				$external[] = $ext['id'];
			}
		if(in_array($rank, $external)) {
			return true;
		}
		else
			return false;
	}
	
	public function changeRank($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		if(empty($player)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->redirect(WEBSITE."/index.php/guilds/members/".$id);
		if(!$this->guilds_model->canUpdate($player->getID())) error("Player are logged in, please logout first.");
		$this->load->helper("form_helper");
			if($_POST) {
				$_POST['new'] = (int)$_POST['new'];
				if(!is_int($_POST['new']))
					@$error .= "Rank must be an integer<br/>";
					
				if(empty($_POST['new']))
					@$error .= "Invalid Rank<br/>";
					
				if(!$this->_isValidRank($id, $_POST['new']))
					@$error .= "Invalid Rank Name</br>";
					
				if(!empty($error))
					error($error);
				else {
					$this->guilds_model->changeRank($player, $_POST['new']);
					success("Rank has been changed.");
					$ide->redirect(WEBSITE."/index.php/guilds/members/".$id, 2);
				}
					
			}
		$data['current'] = $this->guilds_model->getMemberRank($player);
		$data['ranks'] = $this->guilds_model->getRanks($id);
		$data['player'] = $player;
		$data['id'] = $id;
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_changeRank", $data);
	}
	
	public function kick($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->goPrevious();
		if(empty($player)) $ide->goPrevious();
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->goPrevious();
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->goPrevious();
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->goPrevious();
		if(!$this->guilds_model->canUpdate($player->getID())) error("Player are logged in, please logout first.");
		$this->guilds_model->kick($player);
		$ide->redirect(WEBSITE."/index.php/guilds/members/".$id);
	}
	
	public function leave($id, $player) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		$player = (int)$player;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		if(empty($player)) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->load->model("guilds_model");
		$data = array();
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildMember($id, $player)) $ide->redirect(WEBSITE."/index.php/guilds/view/".$id);
		if(!$this->guilds_model->canUpdate($player->getID())) error("Player are logged in, please logout first.");
		$this->guilds_model->leave($player);
		$ide->redirect(WEBSITE."/index.php/guilds/view/".$id);
	}
	
	public function motd($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		$data['motd'] = $data['guild'][0]['motd'];
			if($_POST) {
				$_POST['motd'] = decodeString($_POST['motd']);
				$this->load->library("form_validation");
				$this->form_validation->set_rules('motd', 'MOTD', 'max_lenght[255]|alpha_space');
				if($this->form_validation->run() == true) {
					$this->guilds_model->changeMotd($id, $_POST['motd']);
					success("MOTD has been changed.");
					$ide->redirect(WEBSITE."/index.php/guilds/management/".$id, 2);
				}
				
			}
		$this->load->helper("form_helper");
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_motd", $data);
	}
	
	public function logo($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->goPrevious();
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->goPrevious();
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$data['id'] = $id;
		
				require("config.php");
				$cfg['upload_path'] = 'public/guild_logos';
				$cfg['allowed_types'] = 'gif';
				$cfg['max_size']	= '128';
				$cfg['max_width']  = '64';
				$cfg['max_height']  = '64';
				$cfg['file_name'] = $id;
				$cfg['overwrite'] = true;
				$this->load->library('upload', $cfg);
				if($this->upload->do_upload("logo"))
					success("Logo has been changed.");
				$data['error'] = $this->upload->display_errors();
		
			
		$this->load->helper("form_helper");
		$this->load->view("guild_menu", $data);
		$this->load->view("guild_logo", $data);
	}
	
	public function delete($id) {
		$ide = new IDE;
		$ide->requireLogin();
		$id = (int)$id;
		if(empty($id)) $ide->redirect(WEBSITE."/index.php/guilds");
		$data = array();
		$this->load->model("guilds_model");
		$data['guild'] = $this->guilds_model->getGuildInfo($id);
		if(empty($data['guild'])) $ide->redirect(WEBSITE."/index.php/guilds");
		if(!$this->guilds_model->isGuildLeader($data['guild'][0]['ownerid'])) $ide->redirect(WEBSITE."/index.php/guilds");
		$this->guilds_model->deleteGuild($id);
		
		$this->load->model("forum_model");		
		$this->forum_model->deleteBoardByGuild($id);
		
		$ide->redirect(WEBSITE."/index.php/guilds");
	}

}
?>

in system\application\models\guilds_model.php

PHP:
<?php

class Guilds_Model extends Model {
	public function getGuildsList($world_id = null) {
		$this->load->database();
		$ext = (!empty($world_id)) ? "WHERE `world_id` = '$world_id'": "";
		$guilds = array();
		$sql = $this->db->query("SELECT `id`, `world_id`, `name`, `motd` FROM `guilds` $ext")->result();
			foreach($sql as $cmd) {
				$guilds[] = array("id"=>$cmd->id, "world_id"=>$cmd->world_id, "name"=>$cmd->name, "motd"=>$cmd->motd);
			}
		return $guilds;
	}
	
	public function getCharactersAllowedToCreateGuild($level = 0) {
		$this->load->database();
		$characters = array();
		$sql = $this->db->query("SELECT `id`, `name`, `level` FROM `players` WHERE `rank_id` = '0' AND `online` = 0 AND `level` >= '{$level}' AND `account_id` = '{$_SESSION['account_id']}'")->result();
		foreach($sql as $cmd) {
			$characters[] = array('id'=>$cmd->id, 'name'=>$cmd->name,'level'=>$cmd->level);
		}
		return $characters;
	}
	
	public function checkPlayerCreatingGuild($id) {
		require_once("system/application/config/create_character.php");
		$this->load->database();
		$sql = $this->db->query("SELECT `id` FROM `players` WHERE `id` = '$id' AND `online` = 0 AND `level` >= '".LEVELTOCREATEGUILD."' AND `account_id` = '{$_SESSION['account_id']}'")->row_array();
		if(count($sql) == 0) return false; else return true;
	}
	
	public function checkGuildName($name) {
		$this->load->database();
		$name = $this->db->escape($name);
		$sql = $this->db->query('SELECT `id` FROM `guilds` WHERE `name` = "'.$name.'"')->row_array();
		if(count($sql) == 0) return true; else return false;
	
	}
	
	public function createGuild($name, $character) {
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$player = new OTS_Player();
		$player->load($character);
		$new_guild = new OTS_Guild();
		$new_guild->setCreationData(time());
		$new_guild->setName($name);
		$new_guild->setOwner($player);
		$new_guild->save();
		$new_guild->setCustomField('motd', 'New guild. Leader must edit this text :)');
		$new_guild->setCustomField('creationdata', time());
		$new_guild->setCustomField('world_id', $player->getWorld());
		$ranks = $new_guild->getGuildRanksList();
		$ranks->orderBy('level', POT::ORDER_DESC);
		foreach($ranks as $rank)
			if($rank->getLevel() == 3)
			{
				$player->setRank($rank);
				$player->save();
			}
		return $new_guild->getId();
	}
	
	public function getGuildInfo($id) {
		$this->load->database();
		return $this->db->query("SELECT `world_id`, `name`, `ownerid`, `creationdata`, `motd` FROM `guilds` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function isGuildLeader($id) {
		$this->load->database();
		return ($this->db->query("SELECT `id` FROM `players` WHERE `id` = '".$id."' AND `account_id` = '".$_SESSION['account_id']."' ")->num_rows == 0) ? false: true;
	}
	
	public function isInvitable($name) {
		$this->load->database();
		return $this->db->query("SELECT `rank_id`, `online` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function getCharacterId($name) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function invite($id, $player) {
		$this->load->database();
		$this->db->query("INSERT INTO `guild_invites` VALUES('".$player."', '".$id."')");
	}
	
	public function getMembers($id) {
		$this->load->database();
		return $this->db->query("SELECT players.id, players.name, guild_ranks.name AS guild_rank FROM guild_ranks LEFT JOIN players ON rank_id = guild_ranks.id WHERE guild_ranks.guild_id = '".$id."' ORDER BY guild_ranks.level DESC")->result_array();
	}
	
	public function isGuildMember($id, $player) {
		$this->load->database();
		return ($this->db->query("SELECT players.rank_id FROM players, guild_ranks WHERE players.rank_id = guild_ranks.id AND players.id = '".$player."' AND guild_ranks.guild_id = '".$id."'")->num_rows == 0) ? false: true;
	}
	
	public function getMemberDescription($id) {
		$this->load->database();
		return $this->db->query("SELECT `guildnick` FROM `players` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function changeDescription($player, $desc) {
		$this->load->database();
		$this->db->query("UPDATE players SET guildnick = \"".$desc."\" WHERE id = '".$player."'");
	}
	
	public function getMemberRank($id) {
		$this->load->database();
		return $this->db->query("SELECT rank_id FROM players WHERE id = '".$id."'")->result_array();
	}
	
	public function getRanks($id) {
		$this->load->database();
		return $this->db->query("SELECT `name`, `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function getRanksID($id) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function changeRank($player, $rank) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `rank_id` = '".$rank."' WHERE `id` = '".$player."'");
	}
	
	public function kick($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function leave($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function changeMotd($id, $motd) {
		$this->load->database();
		$this->db->query("UPDATE `guilds` SET `motd` = \"".$motd."\" WHERE `id` = '".$id."'");
	}
	
	public function deleteGuild($id) {
		$this->load->database();
		$this->db->query("DELETE FROM `guilds` WHERE `id` = '".$id."'");
		//Triggers should do rest of the job.
			if(file_exists("public/guild_logos/".$id.".gif"))
				unlink("public/guild_logos/".$id.".gif");
	}
	public function canUpdate($id) {
		$this->load->database();
		$sql = $this->db->query("SELECT 1 FROM `players` WHERE `id` = '$id' AND `online` = 0")->row_array();
	
		return count($sql);		
	}
	
}

?>

in system\application\views\view_guild.php

PHP:
<?php

class Guilds_Model extends Model {
	public function getGuildsList($world_id = null) {
		$this->load->database();
		$ext = (!empty($world_id)) ? "WHERE `world_id` = '$world_id'": "";
		$guilds = array();
		$sql = $this->db->query("SELECT `id`, `world_id`, `name`, `motd` FROM `guilds` $ext")->result();
			foreach($sql as $cmd) {
				$guilds[] = array("id"=>$cmd->id, "world_id"=>$cmd->world_id, "name"=>$cmd->name, "motd"=>$cmd->motd);
			}
		return $guilds;
	}
	
	public function getCharactersAllowedToCreateGuild($level = 0) {
		$this->load->database();
		$characters = array();
		$sql = $this->db->query("SELECT `id`, `name`, `level` FROM `players` WHERE `rank_id` = '0' AND `online` = 0 AND `level` >= '{$level}' AND `account_id` = '{$_SESSION['account_id']}'")->result();
		foreach($sql as $cmd) {
			$characters[] = array('id'=>$cmd->id, 'name'=>$cmd->name,'level'=>$cmd->level);
		}
		return $characters;
	}
	
	public function checkPlayerCreatingGuild($id) {
		require_once("system/application/config/create_character.php");
		$this->load->database();
		$sql = $this->db->query("SELECT `id` FROM `players` WHERE `id` = '$id' AND `online` = 0 AND `level` >= '".LEVELTOCREATEGUILD."' AND `account_id` = '{$_SESSION['account_id']}'")->row_array();
		if(count($sql) == 0) return false; else return true;
	}
	
	public function checkGuildName($name) {
		$this->load->database();
		$name = $this->db->escape($name);
		$sql = $this->db->query('SELECT `id` FROM `guilds` WHERE `name` = "'.$name.'"')->row_array();
		if(count($sql) == 0) return true; else return false;
	
	}
	
	public function createGuild($name, $character) {
		$ots = POT::getInstance();
		$ots->connect(POT::DB_MYSQL, connection());
		$player = new OTS_Player();
		$player->load($character);
		$new_guild = new OTS_Guild();
		$new_guild->setCreationData(time());
		$new_guild->setName($name);
		$new_guild->setOwner($player);
		$new_guild->save();
		$new_guild->setCustomField('motd', 'New guild. Leader must edit this text :)');
		$new_guild->setCustomField('creationdata', time());
		$new_guild->setCustomField('world_id', $player->getWorld());
		$ranks = $new_guild->getGuildRanksList();
		$ranks->orderBy('level', POT::ORDER_DESC);
		foreach($ranks as $rank)
			if($rank->getLevel() == 3)
			{
				$player->setRank($rank);
				$player->save();
			}
		return $new_guild->getId();
	}
	
	public function getGuildInfo($id) {
		$this->load->database();
		return $this->db->query("SELECT `world_id`, `name`, `ownerid`, `creationdata`, `motd` FROM `guilds` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function isGuildLeader($id) {
		$this->load->database();
		return ($this->db->query("SELECT `id` FROM `players` WHERE `id` = '".$id."' AND `account_id` = '".$_SESSION['account_id']."' ")->num_rows == 0) ? false: true;
	}
	
	public function isInvitable($name) {
		$this->load->database();
		return $this->db->query("SELECT `rank_id`, `online` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function getCharacterId($name) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM `players` WHERE `name` = \"".$name."\"")->result_array();
	}
	
	public function invite($id, $player) {
		$this->load->database();
		$this->db->query("INSERT INTO `guild_invites` VALUES('".$player."', '".$id."')");
	}
	
	public function getMembers($id) {
		$this->load->database();
		return $this->db->query("SELECT players.id, players.name, guild_ranks.name AS guild_rank FROM guild_ranks LEFT JOIN players ON rank_id = guild_ranks.id WHERE guild_ranks.guild_id = '".$id."' ORDER BY guild_ranks.level DESC")->result_array();
	}
	
	public function isGuildMember($id, $player) {
		$this->load->database();
		return ($this->db->query("SELECT players.rank_id FROM players, guild_ranks WHERE players.rank_id = guild_ranks.id AND players.id = '".$player."' AND guild_ranks.guild_id = '".$id."'")->num_rows == 0) ? false: true;
	}
	
	public function getMemberDescription($id) {
		$this->load->database();
		return $this->db->query("SELECT `guildnick` FROM `players` WHERE `id` = '".$id."'")->result_array();
	}
	
	public function changeDescription($player, $desc) {
		$this->load->database();
		$this->db->query("UPDATE players SET guildnick = \"".$desc."\" WHERE id = '".$player."'");
	}
	
	public function getMemberRank($id) {
		$this->load->database();
		return $this->db->query("SELECT rank_id FROM players WHERE id = '".$id."'")->result_array();
	}
	
	public function getRanks($id) {
		$this->load->database();
		return $this->db->query("SELECT `name`, `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function getRanksID($id) {
		$this->load->database();
		return $this->db->query("SELECT `id` FROM guild_ranks WHERE guild_id = '".$id."'")->result_array();
	}
	
	public function changeRank($player, $rank) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `rank_id` = '".$rank."' WHERE `id` = '".$player."'");
	}
	
	public function kick($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function leave($id) {
		$this->load->database();
		$this->db->query("UPDATE `players` SET `guildnick` = '', `rank_id` = '0' WHERE `id` = '".$id."'");
	}
	
	public function changeMotd($id, $motd) {
		$this->load->database();
		$this->db->query("UPDATE `guilds` SET `motd` = \"".$motd."\" WHERE `id` = '".$id."'");
	}
	
	public function deleteGuild($id) {
		$this->load->database();
		$this->db->query("DELETE FROM `guilds` WHERE `id` = '".$id."'");
		//Triggers should do rest of the job.
			if(file_exists("public/guild_logos/".$id.".gif"))
				unlink("public/guild_logos/".$id.".gif");
	}
	public function canUpdate($id) {
		$this->load->database();
		$sql = $this->db->query("SELECT 1 FROM `players` WHERE `id` = '$id' AND `online` = 0")->row_array();
	
		return count($sql);		
	}
	
}

?>

that should do the trick.

Glad I could help.

(modern aac rev 244 guilds system :)

is it better than this http://otland.net/f118/modern-aac-fixed-guild-system-114330/ ?
 
Back
Top