src-empire/Empire/Core/Award.php line 237

Open in your IDE?
  1. <?php
  2. namespace Empire\Core;
  3. use Empire\Legacy\db;
  4. use Empire\Legacy\cgt;
  5. class Award extends AbstractEntity implements EntityNamedLoadable {
  6.     const AWARD_GEJS_DISPLAY_IMAGE 0;
  7.     const AWARD_GEJS_DISPLAY_NAME 1;
  8.     const AWARD_GEJS_DISPLAY_SHORTNAME 2;
  9.     public $hierarchy;
  10.     public $id;
  11.     public $typeName;
  12.     public $name;
  13.     public $shortName;
  14.     public $link;
  15.     public $childID;
  16.     public $parentID;
  17.     public $description;
  18.     public $count;
  19.     public $maxCount;
  20.     public $authority;
  21.     public $memberAwardID;
  22.     public $awardedBy;
  23.     public $stamp;
  24.     public $reason;
  25.     public $ooc;
  26.     protected $AWAR_ID;
  27.     protected $AWAR_NAME;
  28.     protected $AWAR_SHORTNAME;
  29.     protected $AWAR_LINK_MULTI;
  30.     protected $AWAR_LINK;
  31.     protected $AWAR_CHILD_ID;
  32.     protected $AWAR_PARENT_ID;
  33.     protected $AWAR_DESC;
  34.     protected $AWAR_HIERARCHY;
  35.     protected $AWAR_AUTHORITY;
  36.     protected $MEMB_ID;
  37.     
  38.     function __construct(){
  39.         $this->type cint_TYPE_AWARD;
  40.         parent::__construct();
  41.     }
  42.     function getPrimaryField(){
  43.         return 'AWAR_ID';
  44.     }
  45.     function getTable(){
  46.         return 'AWARD';
  47.     }
  48.     function initialize(){
  49.         $this->id $this->AWAR_ID;
  50.         $this->typeName 'Award';
  51.         if(isset($this->COUNT) && $this->COUNT 1) {
  52.             $this->name $this->AWAR_NAME " x" $this->COUNT;
  53.             $this->shortName $this->AWAR_SHORTNAME "x" $this->COUNT;
  54.             $this->link preg_replace("/\{n\}/"$this->COUNT$this->AWAR_LINK_MULTI);
  55.         } else {
  56.             $this->name $this->AWAR_NAME;
  57.             $this->shortName $this->AWAR_SHORTNAME;
  58.             $this->link $this->AWAR_LINK;
  59.             $this->childID $this->AWAR_CHILD_ID;
  60.             $this->parentID $this->AWAR_PARENT_ID;
  61.         }
  62.         $this->description $this->AWAR_DESC;
  63.         $this->hierarchy $this->AWAR_HIERARCHY;
  64.         $this->count = isset($this->COUNT) ? $this->COUNT 1;
  65.         $this->maxCount = isset($this->AWAR_MAX_COUNT) ? $this->AWAR_MAX_COUNT 1;
  66.         $this->authority json_decode($this->AWAR_AUTHORITYtrue);
  67.     }
  68.     /**
  69.      * @return Award[]
  70.      * @throws \Exception
  71.      */
  72.     public static function loadAll() {
  73.         $get db::prepare("SELECT * FROM AWARD WHERE AWAR_PARENT_ID IS NULL ORDER BY AWAR_HIERARCHY ASC");
  74.         
  75.         return db::fetchArrayOfObjects($getAward::class);
  76.     }
  77.     
  78.     public static function loadByMemberAwardID($intMEAWid) {
  79.         $get db::prepare("SELECT * FROM MEMBER_AWARD LEFT JOIN AWARD ON MEMBER_AWARD.AWAR_ID = AWARD.AWAR_ID WHERE MEAW_ID=?"$intMEAWid);
  80.         /** @var Award $award */
  81.         $award db::fetchObject($getAward::class);
  82.         $award->loadMemberData($award->MEMB_ID);
  83.         return $award ;
  84.     }
  85.     /**
  86.      * @param string $name the short name of an Award
  87.      * @throws \Exception
  88.      */
  89.     public static function loadByShortName(string $name): ?Award
  90.     {
  91.         $result db::prepare("SELECT * FROM AWARD WHERE AWAR_SHORTNAME = ? LIMIT 1"$name);
  92.         return db::fetchObject($resultget_called_class());
  93.     }
  94.     
  95.     function getName() {
  96.         return $this->name;
  97.     }
  98.     
  99.     function loadMemberData($intUserID) {
  100.         $get db::prepare("SELECT * FROM MEMBER_AWARD WHERE AWAR_ID=? AND MEMB_ID=?"$this->id$intUserID);
  101.         $arr db::fetchArrayOfObjects($get);
  102.         foreach($arr as $item) {
  103.             $this->memberAwardID[] = $item->MEAW_ID;
  104.             $this->awardedBy[] = User::load([$item->MEAW_AWARDBY_ID]);
  105.             $this->stamp[] = cgt::evaluateString($item->MEAW_STAMP);
  106.             $this->reason[] = $item->MEAW_REASON;
  107.             $this->ooc[] = $item->MEAW_OOC;
  108.         }
  109.         $this->count count($arr);
  110.         if($this->count 1) {
  111.             $this->name $this->AWAR_NAME " x" $this->count;
  112.             $this->shortName $this->AWAR_SHORTNAME "x" $this->count;
  113.             $this->link preg_replace("/\{n\}/"$this->count$this->AWAR_LINK_MULTI);
  114.         }
  115.         return $this;
  116.     }
  117.     function getRelated() {
  118.         $get db::prepare("SELECT * FROM AWARD WHERE AWAR_HIERARCHY=? AND AWAR_ID != ?"$this->hierarchy$this->id);
  119.         
  120.         if(db::numRows($get) == 0) {
  121.             return null;
  122.         }
  123.         
  124.         $res db::fetchArrayOfObjects($get);
  125.         $ids = [];
  126.         foreach($res as $row) {
  127.             $ids[] = $row->AWAR_ID;
  128.         }
  129.         
  130.         return Award::load($ids);
  131.     }
  132.     
  133.     function getParent() {
  134.         $get db::prepare("SELECT * FROM AWARD WHERE AWAR_CHILD_ID=?"$this->id);
  135.         
  136.         if(db::numRows($get) > 0) {
  137.             $row db::fetchObject($get);
  138.             return Award::load(array($row->AWAR_ID));
  139.         }
  140.         
  141.         return false;
  142.     }
  143.     
  144.     function isParent() {
  145.         return ($this->childID != null);
  146.     }
  147.     
  148.     function isChild() {
  149.         return ($this->parentID != null);
  150.     }
  151.     
  152.     function getChild() {
  153.         $get db::prepare("SELECT * FROM AWARD WHERE AWAR_PARENT_ID=?"$this->id);
  154.         
  155.         if(db::numRows($get) > 0) {
  156.             $row db::fetchObject($get);
  157.             return Award::load(array($row->AWAR_ID));
  158.         }
  159.         
  160.         return false;
  161.     }
  162.     public function displayGEjs($display self::AWARD_GEJS_DISPLAY_IMAGE) {
  163.         $ensureList = function ($string) {
  164.           if(substr($string,03) !== "<ul") {
  165.               return "<ul class='gejs award-tooltip-content-authoritylist'><li>"$string "</li></ul>";
  166.           } else {
  167.               return $string;
  168.           }
  169.         };
  170.         $return "";
  171.         $return .= "<div class='gejs award-tooltip' data-id='" $this->getID() . "'>";
  172.         if($display == self::AWARD_GEJS_DISPLAY_IMAGE) {
  173.             $return .= "<img src='"Core::get('base_url') . "$this->link' alt='$this->shortName'>";
  174.         } elseif ($display == self::AWARD_GEJS_DISPLAY_NAME) {
  175.             $return .= $this->name;
  176.         } elseif ($display == self::AWARD_GEJS_DISPLAY_SHORTNAME) {
  177.             $return .= $this->shortName;
  178.         }
  179.         $return .= "<div class='gejs award-tooltip-content'><strong>$this->name</strong> [$this->shortName]";
  180.         $return .= "<img src='"Core::get('base_url') . "$this->link' alt='$this->shortName' class='gejs award-tooltip-content-image'>";
  181.         $return .= "<p style='text-align: justify'>$this->description</p>";
  182.         $return .= "<div><b>Awarding authorities:</b><br>"$ensureList($this->getAuthority()) . "</div>";
  183.         for($i 0is_array($this->reason) && $i count($this->reason); $i++) {
  184.             $return .= "<p><b>"$this->awardedBy[$i]->getName() .
  185.                 " - " $this->stamp[$i]->cgtDateString ." - <span class=\"button\" style=\"padding: 2px\">" . ($this->ooc[$i] == "<span class='fa fa-user'></span> OOC" "<span class='fab fa-empire'></span> IC") . "</span></b><br>"$this->reason[$i] ."</p>";
  186.         }
  187.         $return .="</div></div>";
  188.         return $return;
  189.     }
  190.     public function getAuthority() {
  191.         if(!is_array($this->authority)) return "Undefined";
  192.         $test = function($key$value) use (&$test) {
  193.             switch($key) {
  194.                 case 'tier':
  195.                     return Position::POSITION_TIER_NAMES[$value] . ($value " and above" "");
  196.                     break;
  197.                 case 'position':
  198.                     $position Position::load([$value]);
  199.                     if(null !== $position) {
  200.                         return $position->getName();
  201.                     } else {
  202.                         return "<i>Unknown Position</i>";
  203.                     }
  204.                     break;
  205.                 case 'unit':
  206.                     return Unit::load([$value])->getName();
  207.                 case 'branch':
  208.                     return Branch::load([$value])->getName();
  209.                     break;
  210.                 case 'and':
  211.                     $string "";
  212.                     foreach($value as $item) {
  213.                         foreach($item as $k => $v) {
  214.                             $string .= $test($k,$v).", ";
  215.                         }
  216.                     }
  217.                     return rtrim($string", ");
  218.                 case 'or':
  219.                     $string "<ul class='gejs award-tooltip-content-authoritylist'>";
  220.                     foreach($value as $item) {
  221.                         foreach($item as $k => $v) {
  222.                             $string .= "<li><i class='fa fa-caret-right' style='color: #669;'></i> ".$test($k,$v)."</li>";
  223.                         }
  224.                     }
  225.                     return $string "</ul>";
  226.             }
  227.         };
  228.         foreach($this->authority as $key => $value) {
  229.             return $test($key$value);
  230.         }
  231.     }
  232.     public function hasAuthority(User $user) {
  233.         if(!is_array($this->authority)) return true;
  234.         $test = function($key$value) use (&$test$user) {
  235.             switch($key) {
  236.                 case 'tier':
  237.                     $positions $user->getPositions();
  238.                     foreach($positions as $position) {
  239.                         if($position->tier <= $value) {
  240.                             return true;
  241.                         }
  242.                     }
  243.                     return false;
  244.                 case 'position':
  245.                     $positons $user->getPositions();
  246.                     foreach($positons as $position) {
  247.                         if($position->id == $value) return true;
  248.                     }
  249.                     return false;
  250.                 case 'unit':
  251.                     $units $user->getUnits();
  252.                     foreach($units as $unit) {
  253.                         if($unit->id == $value) return true;
  254.                     }
  255.                     return false;
  256.                 case 'branch':
  257.                     return $user->hasBranch(Branch::load([$value]));
  258.                 case 'and':
  259.                     foreach($value as $item) {
  260.                         foreach($item as $k => $v) {
  261.                             if(!$test($k,$v)) return false;
  262.                         }
  263.                     }
  264.                     return true;
  265.                 case 'or':
  266.                     foreach($value as $item) {
  267.                         foreach($item as $k => $v) {
  268.                             if($test($k,$v)) return true;
  269.                         }
  270.                     }
  271.                     return false;
  272.             }
  273.         };
  274.         foreach($this->authority as $key => $value) {
  275.             return $test($key$value);
  276.         }
  277.     }
  278.     public static function processBulk(array $awardsUser $giver): array
  279.     {
  280.         $errors = [];
  281.         foreach ($awards as $award) {
  282.             try {
  283.                 $split explode(' - '$award);
  284.                 $handle $split[0];
  285.                 $user User::getByName($handle);
  286.                 if (!$user) {
  287.                     $errors[] = $award ' :: User with name ' $handle ' not found.';
  288.                     continue;
  289.                 }
  290.                 $shortname $split[1];
  291.                 $reason $split[2];
  292.                 $ooc $split[3] === 'true';
  293.                 $awardObj Award::loadByShortName($shortname);
  294.                 if (!$awardObj) {
  295.                     $errors[] = $award ' :: Award with short name ' $shortname ' not found.';
  296.                     continue;
  297.                 }
  298.                 $success $user->giveAward($awardObj->getID(), $giver$reason$ooc);
  299.                 if (!$success) {
  300.                     $errors[] = $award ' :: Receiver has already reached the limit for this award';
  301.                 }
  302.             } catch (\Exception $e) {
  303.                 $errors[] = $award ' :: ' $e->getMessage();
  304.             }
  305.         }
  306.         return $errors;
  307.     }
  308. }