<?php
namespace Empire\Core;
use App\Service\AvatarImageService;
use Empire\Access\Access;
use Empire\Access\Key;
use Empire\Legacy\db;
class Position extends AbstractEntity implements Entity, Named, Loadable {
public $typeName, $branchID, $unitID, $tier, $public, $pay;
protected $POSI_NAME, $BRAN_ID, $POSI_TIER, $POSI_PUBLIC, $POSI_PAY;
const POSITION_TIER_RANKFILE = 5;
const POSITION_TIER_FOURTH = 4;
const POSITION_TIER_THIRD = 3;
const POSITION_TIER_SECOND = 2;
const POSITION_TIER_FIRST = 1;
const POSITION_TIER_THRONE = 0;
const POSITION_TIER_TRAINEE = 6;
const POSITION_TIER_NAMES = ["The Throne", "1st Tier", "2nd Tier", "3rd Tier", "4th Tier", "Rank and File", "Trainee"];
public $name;
protected $UNIT_ID;
protected $POSI_ID;
function __construct(){
$this->type = cint_TYPE_POSITION;
parent::__construct();
}
function __toString() {
return "" . $this->getID();
}
function getPrimaryField(){
return 'POSI_ID';
}
function getTable(){
return 'POSITION';
}
function initialize(){
$this->id = $this->POSI_ID;
$this->type = cint_TYPE_POSITION;
$this->typeName = 'Position';
$this->name = $this->POSI_NAME;
$this->branchID = $this->BRAN_ID;
$this->branch = Branch::load([$this->BRAN_ID]);
$this->unitID = $this->UNIT_ID;
$this->unit = Unit::load([$this->UNIT_ID]);
$this->tier = $this->POSI_TIER;
$this->public = (bool) $this->POSI_PUBLIC;
$this->pay = $this->POSI_PAY;
}
/** function delete($objUser) {
if($this->getUsers() == null) {
DB::prepare("DELETE FROM !! WHERE !! = ?", $this->getTable(), $this->getPrimaryField(), $this->getID());
Event::send($objUser->id, cint_RECEIVER_ADMIN, $objUser->name . 'has deleted the position ' . $this->name . 'from the database.', cint_EVENTTYPE_POSITION);
return true;
}
return false;
} **/
function getName() {
return $this->name;
}
function setName($name) {
$name = trim($name);
$name = str_replace("'", '`', $name);
if($name != $this->name && $name != '' && $name != NULL) {
$this->updateFields(array('POSI_NAME' => $name));
$this->refresh();
}
return true;
}
/**
* @return Unit
*/
function getUnit() {
if(!isset($this->unit)) {
$get = Unit::load(array($this->unitID));
$this->unit = $get;
}
return $this->unit;
}
/**
* @return Branch
*/
function getBranch() {
if(!isset($this->branch)) {
$get = Branch::load(array($this->branchID));
$this->branch = $get;
}
return $this->branch;
}
/**
* Get the users with this position
*
* @param bool $forceArray
* @return User|User[] array of User objects or a single User object, depending
* @throws \Exception
*/
function getUsers($forceArray = false) {
$get = db::prepare("SELECT * FROM MEMBER_POSITION WHERE POSI_ID = ?", $this->id);
$res = db::fetchArrayOfObjects($get);
if(count($res) > 0) {
if(count($res) > 1) {
$membIDs = [];
foreach($res as $row) {
$membIDs[] = $row->MEMB_ID;
}
$users = User::load($membIDs, $forceArray);
return $users;
} else {
$user = User::load(array($res[0]->MEMB_ID), $forceArray);
return $user;
}
} else {
return null;
}
}
function loadMemberData($intUserID, $refresh = false) {
if(!isset($this->MEMB_ID) || !isset($this->MEPO_ID) || $refresh) {
$get = db::prepare("SELECT * FROM MEMBER_POSITION WHERE POSI_ID=? AND MEMB_ID=?", $this->id, $intUserID);
$obj = db::fetchObject($get);
$this->MEMB_ID = $obj->MEMB_ID;
$this->MEPO_ID = $obj->MEPO_ID;
}
return;
}
/**
* @param $prefix
* @return string
*/
public function getTierImage($prefix) {
if($this->tier < 5) {
$co = ['DarkViolet', 'gold', 'silver', 'gold', 'silver'];
$ci = ['gold', 'gold', 'gold', 'silver', 'silver'];
return '<strong style="color: '.$co[$this->tier].'; cursor: help" title="'. self::POSITION_TIER_NAMES[$this->tier].'">{<span class="fab fa-empire" style="color: '. $ci[$this->tier] .'"></span>}</strong>';
} else {
return '<span class="fab fa-empire" style="cursor: help; color: silver;" title="Rank and File"></span>';
}
}
/**
* Determines whether a user has the permissions to edit this user. TODO
* @param User $user
* @return bool
*/
public function isEditable(User $user) {
return $this->getUnit()->isEditable($user);
}
/**
* Returns all keys associated with this position. Alias for Access::positionKeys.
* @return Key[];
*/
public function getKeys() {
return Access::positionKeys($this);
}
/**
* @param $strPosition
* @param $strBranch
* @param null $strUnit
* @return null
* @throws \Exception
*/
public static function getPositionIDByName($strPosition, $strBranch, $strUnit=null) {
if(!is_null($strUnit)) {
$query = db::prepare("SELECT POSI.POSI_ID FROM POSITION AS POSI
LEFT JOIN BRANCH AS BRAN ON POSI.BRAN_ID = BRAN.BRAN_ID
LEFT JOIN UNIT ON POSI.UNIT_ID = UNIT.UNIT_ID
WHERE POSI.POSI_NAME = ? AND BRAN.BRAN_NAME = ? AND UNIT.UNIT_NAME = ?", $strPosition, $strBranch, $strUnit);
} else {
$query = db::prepare("SELECT POSI.POSI_ID FROM POSITION AS POSI
LEFT JOIN BRANCH AS BRAN ON POSI.BRAN_ID = BRAN.BRAN_ID
LEFT JOIN UNIT ON POSI.UNIT_ID = UNIT.UNIT_ID
WHERE POSI.POSI_NAME = ? AND BRAN.BRAN_NAME = ?", $strPosition, $strBranch);
}
$ret = db::fetchObject($query);
if(isset($ret->POSI_ID)) {
return $ret->POSI_ID;
}
return null;
}
public static function getNameByID($intPositionID) {
$position = Position::load(array($intPositionID));
return $position->name;
}
public static function getNameByMEPOID($intMEPOID) {
$res = db::prepare("SELECT * FROM MEMBER_POSITION WHERE MEPO_ID = ?", $intMEPOID);
$obj = db::fetchObject($res);
$position = Position::load(array($obj->POSI_ID));
return $position->name;
}
public static function getPosByMEPOID($intMEPOID) {
$res = db::prepare("SELECT * FROM MEMBER_POSITION WHERE MEPO_ID = ?", $intMEPOID);
$obj = db::fetchObject($res);
$position = Position::load(array($obj->POSI_ID));
return $position;
}
/**
* @param $intBranchID
* @param $intUnitID
* @param $name
* @param $tier
* @return bool
* @throws \Exception
*/
public static function add($intBranchID, $intUnitID, $name, $tier, $public = false) {
if($intBranchID <= 0 || $intUnitID <= 0) {
return false;
}
$query = db::prepare("SELECT POSI.POSI_ID FROM POSITION AS POSI
LEFT JOIN BRANCH AS BRAN ON POSI.BRAN_ID = BRAN.BRAN_ID
LEFT JOIN UNIT ON POSI.UNIT_ID = UNIT.UNIT_ID
WHERE POSI.POSI_NAME = ? AND BRAN.BRAN_ID = ? AND UNIT.UNIT_ID = ?",
$name,
$intBranchID,
$intUnitID);
if(db::numRows($query) > 0) {
throw new \Exception('Position already exists.', 10);
}
$insertID = db::insert('POSITION', array('BRAN_ID' => (int)$intBranchID, 'UNIT_ID' => (int)$intUnitID, 'POSI_NAME' => $name, 'POSI_TIER' => $tier, 'POSI_PUBLIC' => $public, 'POSI_PAY' => 0));
if($insertID > 0) {
$position = Position::load(array($insertID));
return true;
}
return false;
}
/**
* @param $intPositionID
* @param bool $useAlias
* @param int $offset
* @return \stdClass
* @throws \Exception
*/
public static function getAvatarByPosition($intPositionID, $useAlias = false, $offset = 0) {
$ObjReturn = new \stdClass;
if($intPositionID > 0) {
if(null === ($position = Position::load(array($intPositionID)))) {
throw new \Exception('Unknown Position');
}
if(!$position->isPublic()){
// if the position isnt public, we return vacant to hide it. Get out of here, spies.
$ObjReturn->BRAN_NAME = '';
$ObjReturn->UNIT_NAME = '';
$ObjReturn->POSI_NAME = '';
$ObjReturn->MEMB_AVATAR = "<img src='http://art.swc-empire.com/avatar/posi_vacant.png'/>";
$ObjReturn->AVATAR_SRC = 'http://art.swc-empire.com/avatar/posi_vacant.png';
$ObjReturn->MEMB_NAME = "Position Vacant";
return $ObjReturn;
}
$ObjReturn->BRAN_NAME = $position->getBranch()->getName();
$ObjReturn->UNIT_NAME = $position->getUnit()->getName();
$ObjReturn->POSI_NAME = $position->name;
//Forcing the use of Alias, if branch is Sith.
if($position->getBranch()->id == cint_BRANCH_SITH) $useAlias = true;
$users = $position->getUsers();
if($users == null) {
//nobody has this position
$ObjReturn->MEMB_AVATAR = "<img src='http://art.swc-empire.com/avatar/posi_vacant.png'/>";
$ObjReturn->AVATAR_SRC = 'http://art.swc-empire.com/avatar/posi_vacant.png';
$ObjReturn->MEMB_NAME = "Position Vacant";
} elseif(is_array($users)) {
//more than one person has this position
if($offset >= count($users)) {
//we're looking for a higher offset than the number of people with the position
$ObjReturn->MEMB_AVATAR = "<img src='http://art.swc-empire.com/avatar/posi_vacant.png'/>";
$ObjReturn->AVATAR_SRC = 'http://art.swc-empire.com/avatar/posi_vacant.png';
$ObjReturn->MEMB_NAME = "Position Vacant";
} else {
$user = $users[$offset];
$ObjReturn->MEMB_NAME = $user->getName($useAlias);
$ObjReturn->MEMB_AVATAR = AvatarImageService::getAvatarUrl($user, $useAlias);
$ObjReturn->AVATAR_SRC = AvatarImageService::getAvatarUrl($user, $useAlias, false);
}
} else {
//only one person has this position
if($offset > 0) {
//we're looking for the Nth person with this position but only one person has it,
//so don't show that person multiple times, just throw up the vacant avatar
$ObjReturn->MEMB_AVATAR = "<img src='http://art.swc-empire.com/avatar/posi_vacant.png'/>";
$ObjReturn->AVATAR_SRC = 'http://art.swc-empire.com/avatar/posi_vacant.png';
$ObjReturn->MEMB_NAME = "Position Vacant";
} else {
$ObjReturn->MEMB_NAME = $users->getName($useAlias);
$ObjReturn->MEMB_AVATAR = AvatarImageService::getAvatarUrl($users, $useAlias);
$ObjReturn->AVATAR_SRC = AvatarImageService::getAvatarUrl($users, $useAlias, false);
}
}
} else {
$ObjReturn->BRAN_NAME = '';
$ObjReturn->UNIT_NAME = '';
$ObjReturn->POSI_NAME = '';
$ObjReturn->MEMB_AVATAR = "<img src='http://art.swc-empire.com/avatar/posi_vacant.png'/>";
$ObjReturn->AVATAR_SRC = 'http://art.swc-empire.com/avatar/posi_vacant.png';
$ObjReturn->MEMB_NAME = "Position Vacant";
}
return $ObjReturn;
}
public static function getPositionsForAutocomplete($search, User $user = null) {
if(!is_null($user)) {
$units = Unit::loadEditable($user, 0, true);
$perms = [];
foreach ($units as $unit) {
$perms[] = $unit->getID();
}
$get = db::prepare('SELECT * FROM POSITION WHERE UNIT_ID IN ? AND POSI_NAME LIKE ?', $perms, "%".$search."%");
} else {
$get = db::prepare('SELECT * FROM POSITION WHERE POSI_NAME LIKE ?', "%".$search."%");
}
$arr = db::fetchArrayOfObjects($get, Position::class);
if(!is_array($arr)) $arr = [];
return array_map(function(Position $item) {
return [
'value' => $item->getID(),
'label' => $item->getName(),
'unit' => $item->getUnit()->getName(),
'parent' => !is_null($item->getUnit()->getParent()) ? $item->getUnit()->getParent()->getName() : null,
'branch' => $item->getBranch()->getName(),
];
}, $arr);
}
public static function getTierNames() {
$return = [];
foreach (self::POSITION_TIER_NAMES as $val => $item) {
$return[] = ['id' => $val, 'name' => $item];
}
return $return;
}
public static function getMemberIDs($positionid) {
$fetch = db::prepare("SELECT * FROM MEMBER_POSITION WHERE POSI_ID = ?", $positionid);
$arr = db::fetchArrayOfObjects($fetch);
if(count($arr) > 0) {
$get = [];
foreach($arr as $row) {
$get[] = $row->MEMB_ID;
}
return $get;
}
}
public static function getActiveDiscordMembers($positionid){
$activestatus = cint_STATUS_ACTIVE;
$fetch = db::prepare("SELECT * FROM MEMBER_POSITION
LEFT JOIN MEMBER ON MEMBER_POSITION.MEMB_ID = MEMBER.MEMB_ID
WHERE POSI_ID = $positionid AND STAT_ID = $activestatus AND MEMB_SNOWFLAKE IS NOT NULL");
$arr = db::fetchArrayOfObjects($fetch);
if(count($arr) > 0) {
$get = [];
foreach($arr as $row) {
$get[] = $row->MEMB_ID;
}
return $get;
}
}
public function isPublic(){
if ($this->public){
return true;
} else {
return false;
}
}
/**
* @param string $pay the pay.
* @throws \Doctrine\DBAL\DBALException
*/
function setPay($pay) {
$this->pay = $pay;
$this->POSI_PAY = $pay;
$this->updateFields(['POSI_PAY' => $pay]);
return $this;
}
/**
* @param string $name the branch name
* @return static
* @throws \Exception
*/
public static function loadByName($name) {
$result = db::prepare("SELECT * FROM POSITION WHERE POSI_NAME LIKE ? LIMIT 1", $name);
return db::fetchObject($result, get_called_class());
}
}