Add trophy image upload
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / trophy / Trophy.class.php
1 <?php
2 namespace wcf\data\trophy;
3 use wcf\data\trophy\category\TrophyCategory;
4 use wcf\data\trophy\category\TrophyCategoryCache;
5 use wcf\data\DatabaseObject;
6 use wcf\data\ITitledLinkObject;
7 use wcf\system\event\EventHandler;
8 use wcf\system\request\IRouteController;
9 use wcf\system\request\LinkHandler;
10 use wcf\system\WCF;
11 use wcf\util\StringUtil;
12
13 /**
14 * Represents a user trophy.
15 *
16 * @author Joshua Ruesweg
17 * @copyright 2001-2017 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Data\Trophy
20 * @since 3.1
21 *
22 * @property-read integer $trophyID unique id for the trophy
23 * @property-read string $title the trophy title
24 * @property-read integer $description the trophy description
25 * @property-read integer $categoryID the categoryID of the trophy
26 * @property-read integer $type the trophy type
27 * @property-read string $iconFile the file location of the icon
28 * @property-read string $iconName the icon name
29 * @property-read string $iconColor the icon color
30 * @property-read string $badgeColor the icon badge color
31 * @property-read integer $isDisabled `1` if the trophy is disabled
32 * @property-read integer $awardAutomatically `1` if the trophy is awarded automatically
33 */
34 class Trophy extends DatabaseObject implements ITitledLinkObject, IRouteController {
35 /**
36 * The type value, if this trophy is an image trophy.
37 * @var integer
38 */
39 const TYPE_IMAGE = 1;
40
41 /**
42 * The type value, if this trophy is a badge trophy (based on CSS icons).
43 * @var integer
44 */
45 const TYPE_BADGE = 2;
46
47 /**
48 * The default icon size.
49 */
50 const DEFAULT_SIZE = 32;
51
52 /**
53 * @inheritDoc
54 */
55 public function getTitle() {
56 return WCF::getLanguage()->get($this->title);
57 }
58
59 /**
60 * @inheritDoc
61 */
62 public function getLink() {
63 return LinkHandler::getInstance()->getLink('Trophy', [
64 'object' => $this,
65 'forceFrontend' => true
66 ]);
67 }
68
69 /**
70 * Renders a trophy.
71 *
72 * @param integer $size
73 * @return string
74 */
75 public function renderTrophy($size = self::DEFAULT_SIZE) {
76 switch ($this->type) {
77 case self::TYPE_IMAGE: {
78 return WCF::getTPL()->fetch('trophyImage', 'wcf', [
79 'size' => $size,
80 'trophy' => $this
81 ], true);
82 break;
83 }
84
85 case self::TYPE_BADGE:
86 return WCF::getTPL()->fetch('trophyBadge', 'wcf', [
87 'size' => $size,
88 'trophy' => $this
89 ], true);
90 break;
91
92 default:
93 $parameters = [
94 'renderedTemplate' => null,
95 'size' => $size
96 ];
97
98 EventHandler::getInstance()->fireAction($this, 'renderTrophy', $parameters);
99
100 if ($parameters['renderedTemplate']) {
101 return $parameters['renderedTemplate'];
102 }
103
104 throw new \LogicException("Unable to render the trophy with the type '". $this->type ."'.");
105 break;
106 }
107 }
108
109 /**
110 * Returns the category for this trophy.
111 *
112 * @return TrophyCategory
113 */
114 public function getCategory() {
115 return TrophyCategoryCache::getInstance()->getCategoryByID($this->categoryID);
116 }
117
118 /**
119 * Returns true if the current trophy is disabled. Returns also true if the trophy category is disabled.
120 *
121 * @return boolean
122 */
123 public function isDisabled() {
124 if ($this->isDisabled) {
125 return true;
126 }
127
128 if ($this->getCategory()->isDisabled) {
129 return true;
130 }
131
132 return false;
133 }
134
135 /**
136 * Returns the parsed description for the trophy.
137 *
138 * @return string
139 */
140 public function getDescription() {
141 return nl2br(StringUtil::encodeHTML(WCF::getLanguage()->get($this->description)), false);
142 }
143 }