<?php
namespace Empire\Core;
use Empire\Legacy\db;
use Empire\Legacy\cgt;
class Award extends AbstractEntity implements Entity, Named, Loadable {
const AWARD_GEJS_DISPLAY_IMAGE = 0;
const AWARD_GEJS_DISPLAY_NAME = 1;
const AWARD_GEJS_DISPLAY_SHORTNAME = 2;
public $hierarchy;
public $id;
public $typeName;
public $name;
public $shortName;
public $link;
public $childID;
public $parentID;
public $description;
public $count;
public $maxCount;
public $authority;
public $memberAwardID;
public $awardedBy;
public $stamp;
public $reason;
public $ooc;
protected $AWAR_ID;
protected $AWAR_NAME;
protected $AWAR_SHORTNAME;
protected $AWAR_LINK_MULTI;
protected $AWAR_LINK;
protected $AWAR_CHILD_ID;
protected $AWAR_PARENT_ID;
protected $AWAR_DESC;
protected $AWAR_HIERARCHY;
protected $AWAR_AUTHORITY;
protected $MEMB_ID;
function __construct(){
$this->type = cint_TYPE_AWARD;
parent::__construct();
}
function getPrimaryField(){
return 'AWAR_ID';
}
function getTable(){
return 'AWARD';
}
function initialize(){
$this->id = $this->AWAR_ID;
$this->typeName = 'Award';
if(isset($this->COUNT) && $this->COUNT > 1) {
$this->name = $this->AWAR_NAME . " x" . $this->COUNT;
$this->shortName = $this->AWAR_SHORTNAME . "x" . $this->COUNT;
$this->link = preg_replace("/\{n\}/", $this->COUNT, $this->AWAR_LINK_MULTI);
} else {
$this->name = $this->AWAR_NAME;
$this->shortName = $this->AWAR_SHORTNAME;
$this->link = $this->AWAR_LINK;
$this->childID = $this->AWAR_CHILD_ID;
$this->parentID = $this->AWAR_PARENT_ID;
}
$this->description = $this->AWAR_DESC;
$this->hierarchy = $this->AWAR_HIERARCHY;
$this->count = isset($this->COUNT) ? $this->COUNT : 1;
$this->maxCount = isset($this->AWAR_MAX_COUNT) ? $this->AWAR_MAX_COUNT : 1;
$this->authority = json_decode($this->AWAR_AUTHORITY, true);
}
/**
* @return Award[]
* @throws \Exception
*/
public static function loadAll() {
$get = db::prepare("SELECT * FROM AWARD WHERE AWAR_PARENT_ID IS NULL ORDER BY AWAR_HIERARCHY ASC");
return db::fetchArrayOfObjects($get, Award::class);
}
public static function loadByMemberAwardID($intMEAWid) {
$get = db::prepare("SELECT * FROM MEMBER_AWARD LEFT JOIN AWARD ON MEMBER_AWARD.AWAR_ID = AWARD.AWAR_ID WHERE MEAW_ID=?", $intMEAWid);
/** @var Award $award */
$award = db::fetchObject($get, Award::class);
$award->loadMemberData($award->MEMB_ID);
return $award ;
}
/**
* @param string $name the short name of an Award
* @throws \Exception
*/
public static function loadByShortName(string $name): ?Award
{
$result = db::prepare("SELECT * FROM AWARD WHERE AWAR_SHORTNAME = ? LIMIT 1", $name);
return db::fetchObject($result, get_called_class());
}
function getName() {
return $this->name;
}
function loadMemberData($intUserID) {
$get = db::prepare("SELECT * FROM MEMBER_AWARD WHERE AWAR_ID=? AND MEMB_ID=?", $this->id, $intUserID);
$arr = db::fetchArrayOfObjects($get);
foreach($arr as $item) {
$this->memberAwardID[] = $item->MEAW_ID;
$this->awardedBy[] = User::load([$item->MEAW_AWARDBY_ID]);
$this->stamp[] = cgt::evaluateString($item->MEAW_STAMP);
$this->reason[] = $item->MEAW_REASON;
$this->ooc[] = $item->MEAW_OOC;
}
$this->count = count($arr);
if($this->count > 1) {
$this->name = $this->AWAR_NAME . " x" . $this->count;
$this->shortName = $this->AWAR_SHORTNAME . "x" . $this->count;
$this->link = preg_replace("/\{n\}/", $this->count, $this->AWAR_LINK_MULTI);
}
return $this;
}
function getRelated() {
$get = db::prepare("SELECT * FROM AWARD WHERE AWAR_HIERARCHY=? AND AWAR_ID != ?", $this->hierarchy, $this->id);
if(db::numRows($get) == 0) {
return null;
}
$res = db::fetchArrayOfObjects($get);
$ids = [];
foreach($res as $row) {
$ids[] = $row->AWAR_ID;
}
return Award::load($ids);
}
function getParent() {
$get = db::prepare("SELECT * FROM AWARD WHERE AWAR_CHILD_ID=?", $this->id);
if(db::numRows($get) > 0) {
$row = db::fetchObject($get);
return Award::load(array($row->AWAR_ID));
}
return false;
}
function isParent() {
return ($this->childID != null);
}
function isChild() {
return ($this->parentID != null);
}
function getChild() {
$get = db::prepare("SELECT * FROM AWARD WHERE AWAR_PARENT_ID=?", $this->id);
if(db::numRows($get) > 0) {
$row = db::fetchObject($get);
return Award::load(array($row->AWAR_ID));
}
return false;
}
public function displayGEjs($display = self::AWARD_GEJS_DISPLAY_IMAGE) {
$ensureList = function ($string) {
if(substr($string,0, 3) !== "<ul") {
return "<ul class='gejs award-tooltip-content-authoritylist'><li>". $string . "</li></ul>";
} else {
return $string;
}
};
$return = "";
$return .= "<div class='gejs award-tooltip' data-id='" . $this->getID() . "'>";
if($display == self::AWARD_GEJS_DISPLAY_IMAGE) {
$return .= "<img src='". Core::get('base_url') . "$this->link' alt='$this->shortName'>";
} elseif ($display == self::AWARD_GEJS_DISPLAY_NAME) {
$return .= $this->name;
} elseif ($display == self::AWARD_GEJS_DISPLAY_SHORTNAME) {
$return .= $this->shortName;
}
$return .= "<div class='gejs award-tooltip-content'><strong>$this->name</strong> [$this->shortName]";
$return .= "<img src='". Core::get('base_url') . "$this->link' alt='$this->shortName' class='gejs award-tooltip-content-image'>";
$return .= "<p style='text-align: justify'>$this->description</p>";
$return .= "<div><b>Awarding authorities:</b><br>". $ensureList($this->getAuthority()) . "</div>";
for($i = 0; is_array($this->reason) && $i < count($this->reason); $i++) {
$return .= "<p><b>". $this->awardedBy[$i]->getName() .
" - " . $this->stamp[$i]->cgtDateString ." - <span class=\"button\" style=\"padding: 2px\">" . ($this->ooc[$i] == 1 ? "<span class='fa fa-user'></span> OOC" : "<span class='fab fa-empire'></span> IC") . "</span></b><br>". $this->reason[$i] ."</p>";
}
$return .="</div></div>";
return $return;
}
public function getAuthority() {
if(!is_array($this->authority)) return "Undefined";
$test = function($key, $value) use (&$test) {
switch($key) {
case 'tier':
return Position::POSITION_TIER_NAMES[$value] . ($value > 0 ? " and above" : "");
break;
case 'position':
$position = Position::load([$value]);
if(null !== $position) {
return $position->getName();
} else {
return "<i>Unknown Position</i>";
}
break;
case 'unit':
return Unit::load([$value])->getName();
case 'branch':
return Branch::load([$value])->getName();
break;
case 'and':
$string = "";
foreach($value as $item) {
foreach($item as $k => $v) {
$string .= $test($k,$v).", ";
}
}
return rtrim($string, ", ");
case 'or':
$string = "<ul class='gejs award-tooltip-content-authoritylist'>";
foreach($value as $item) {
foreach($item as $k => $v) {
$string .= "<li><i class='fa fa-caret-right' style='color: #669;'></i> ".$test($k,$v)."</li>";
}
}
return $string . "</ul>";
}
};
foreach($this->authority as $key => $value) {
return $test($key, $value);
}
}
public function hasAuthority(User $user) {
if(!is_array($this->authority)) return true;
$test = function($key, $value) use (&$test, $user) {
switch($key) {
case 'tier':
$positions = $user->getPositions();
foreach($positions as $position) {
if($position->tier <= $value) {
return true;
}
}
return false;
case 'position':
$positons = $user->getPositions();
foreach($positons as $position) {
if($position->id == $value) return true;
}
return false;
case 'unit':
$units = $user->getUnits();
foreach($units as $unit) {
if($unit->id == $value) return true;
}
return false;
case 'branch':
return $user->hasBranch(Branch::load([$value]));
case 'and':
foreach($value as $item) {
foreach($item as $k => $v) {
if(!$test($k,$v)) return false;
}
}
return true;
case 'or':
foreach($value as $item) {
foreach($item as $k => $v) {
if($test($k,$v)) return true;
}
}
return false;
}
};
foreach($this->authority as $key => $value) {
return $test($key, $value);
}
}
public static function processBulk(array $awards, User $giver): array
{
$errors = [];
foreach ($awards as $award) {
try {
$split = explode(' - ', $award);
$handle = $split[0];
$user = User::getByName($handle);
if (!$user) {
$errors[] = $award . ' :: User with name ' . $handle . ' not found.';
continue;
}
$shortname = $split[1];
$reason = $split[2];
$ooc = $split[3] === 'true';
$awardObj = Award::loadByShortName($shortname);
if (!$awardObj) {
$errors[] = $award . ' :: Award with short name ' . $shortname . ' not found.';
continue;
}
$success = $user->giveAward($awardObj->getID(), $giver, $reason, $ooc);
if (!$success) {
$errors[] = $award . ' :: Receiver has already reached the limit for this award';
}
} catch (\Exception $e) {
$errors[] = $award . ' :: ' . $e->getMessage();
}
}
return $errors;
}
}