WowPress-Tailwind/theme/wowpress/Models/Character.php

191 lines
5.4 KiB
PHP
Raw Normal View History

2024-04-15 16:00:49 +02:00
<?php
namespace WoWPress\Models;
use Wenprise\Eloquent\Model;
use WoWPress\Api\BattleNet;
class Character extends Model
{
protected $table = "wowpress_characters";
public $timestamps = false;
protected $primaryKey = 'ID';
protected $guarded = ['ID'];
private $api;
public function __construct($attrs = [])
{
parent::__construct($attrs);
2024-04-15 23:07:29 +02:00
$options = get_option('wowpress_api');
2024-11-02 20:27:01 +01:00
if (isset($options['bnet'])) {
2024-04-15 23:07:29 +02:00
$key = get_option('wowpress_api')['bnet']['key'];
$id = get_option('wowpress_api')['bnet']['id'];
2024-11-02 20:27:01 +01:00
} else {
2024-04-15 23:07:29 +02:00
return false;
}
2024-11-02 20:27:01 +01:00
$this->api = new BattleNet($id, $key);
2024-04-15 16:00:49 +02:00
}
2024-11-02 20:27:01 +01:00
public function lists()
{
return $this->belongsToMany(CharacterList::class, 'wowpress_charlists_item', 'list_id', 'character_id')->withPivot('comment', 'date', 'id');
2024-08-07 22:34:54 +02:00
}
2024-11-02 20:27:01 +01:00
public function user()
{
2024-04-22 15:38:22 +02:00
return $this->belongsTo(User::class);
}
2024-11-02 20:27:01 +01:00
public function getCanEditAttribute()
{
if (empty($this->user->ID)) {
2024-04-23 00:44:42 +02:00
return current_user_can('wowpress_edit_raids');
}
return current_user_can('wowpress_edit_raids') || (get_current_user_id() == $this->user->ID);
2024-04-22 15:38:22 +02:00
}
2024-11-02 20:27:01 +01:00
public function sanitize($string, $delimiter_space = "")
2024-04-15 16:00:49 +02:00
{
2024-04-16 15:25:22 +02:00
$string = strtolower(str_replace(" ", $delimiter_space, $string));
$string = strtolower(str_replace("'", "", $string));
return $string;
2024-04-15 16:00:49 +02:00
}
2024-11-02 20:27:01 +01:00
public function getRankAttribute($rank)
{
switch ($rank) {
case 0:
return "Gildenmeister";
break;
case 1:
return "Offizier";
break;
case 2:
return "Offi-Twink";
break;
case 3:
return "Raidlead";
break;
case 4:
return "Ehrenmitglied";
break;
case 5:
return "Raider";
break;
case 6:
return "Raider-Twink";
break;
case 7:
return "F&F";
break;
case 8:
return "Novize";
break;
case 9:
return "Sleeper";
break;
default:
return "Gast";
2024-04-15 23:07:29 +02:00
}
}
2024-04-15 16:00:49 +02:00
public function getColorAttribute()
{
return $this->sanitize($this->class);
}
public function getClassIconAttribute()
{
$classname = $this->sanitize($this->class);
return get_template_directory_uri() . "/icons/class/$classname/$classname.png";
}
2024-11-02 20:27:01 +01:00
public static function classIconLink($class)
{
2024-04-26 17:47:19 +02:00
$classname = (new self())->sanitize($class);
return get_template_directory_uri() . "/icons/class/$classname/$classname.png";
}
2024-04-15 16:00:49 +02:00
public function getSpecIconAttribute()
{
$classname = $this->sanitize($this->class);
$specname = $this->sanitize($this->spec);
return get_template_directory_uri() . "/icons/class/$classname/$specname.png";
}
2024-11-02 20:27:01 +01:00
public static function specIconLink($class, $spec)
{
2024-04-26 17:47:19 +02:00
$classname = (new self())->sanitize($class);
$specname = (new self())->sanitize($spec);
return get_template_directory_uri() . "/icons/class/$classname/$specname.png";
}
2024-04-15 16:00:49 +02:00
public function updateFromAPI()
{
2024-11-02 20:27:01 +01:00
$realm = $this->sanitize($this->realm, "-");
$char_from_api = $this->api->getCharacter($this->name, $realm);
if (!array_key_exists('character_class', $char_from_api)) {
2024-04-15 23:07:29 +02:00
return false;
}
2024-04-15 16:00:49 +02:00
$this->class = $char_from_api['character_class']['name'];
$this->spec = $char_from_api['active_spec']['name'];
2024-04-16 15:25:22 +02:00
$this->id_blizz = $char_from_api['id'];
2024-11-02 20:27:01 +01:00
$guild_data = $this->api->getGuildRank($this->name, $realm);
if (!empty($guild_data['rank'])) {
$this->guild = $guild_data['guild'];
$this->rank = $guild_data['rank'];
} else {
2024-04-22 15:38:22 +02:00
$this->rank = 99;
}
2024-04-15 16:00:49 +02:00
$this->save();
return $char_from_api;
}
2024-11-02 20:27:01 +01:00
public function updateMedia($force = false)
{
if (empty($this->id_blizz)) {
2024-04-16 15:25:22 +02:00
return false;
}
2024-11-02 20:27:01 +01:00
$character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/";
if (!is_dir($character_dir)) {
mkdir($character_dir, 0777, true);
2024-04-15 16:00:49 +02:00
}
2024-04-16 15:25:22 +02:00
$id = $this->id_blizz;
$mod = $id % 256;
2024-11-02 20:27:01 +01:00
$realm = $this->sanitize($this->realm, '-');
$data = $this->api->getMedia($this->name, $this->realm, 0);
if (key_exists("assets", $data)) {
foreach ($data['assets'] as $asset) {
if ($asset['key'] == 'avatar') {
file_put_contents($character_dir . "avatar.jpg", file_get_contents($asset['value']));
return true;
}
}
}
2024-04-15 16:00:49 +02:00
}
2024-11-02 20:27:01 +01:00
public function getAvatarAttribute()
{
$character_dir = wp_upload_dir()['basedir'] . "/characters/" . $this->ID . "/avatar.jpg";
$character_dir_uri = wp_upload_dir()['baseurl'] . "/characters/" . $this->ID . "/avatar.jpg";
if (file_exists($character_dir)) {
2024-04-15 16:00:49 +02:00
return $character_dir_uri;
2024-11-02 20:27:01 +01:00
} else {
2024-04-15 16:00:49 +02:00
return "";
}
}
2024-04-17 22:10:17 +02:00
2024-11-02 20:27:01 +01:00
public function getClassButtonAttribute()
{
require(get_template_directory() . "/template-parts/components/class-button.php");
2024-04-17 22:10:17 +02:00
}
2024-04-15 16:00:49 +02:00
}