1: <?php if(!defined('APPLICATION')) exit();
2:
3:
4: 5: 6: 7: 8: 9:
10: class BadgeController extends DashboardController {
11:
12: 13: 14: 15:
16: public $Uses = array('Form', 'BadgeModel', 'BadgeAwardModel');
17:
18: 19: 20: 21: 22: 23:
24: public function Initialize() {
25: parent::Initialize();
26: $this->Application = 'Yaga';
27: Gdn_Theme::Section('Dashboard');
28: if($this->Menu) {
29: $this->Menu->HighlightRoute('/badge');
30: }
31: $this->AddJsFile('admin.badges.js');
32: $this->AddCssFile('badges.css');
33: }
34:
35: 36: 37: 38: 39:
40: public function Settings($Page = '') {
41: $this->Permission('Yaga.Badges.Manage');
42: $this->AddSideMenu('badge/settings');
43:
44: $this->Title(T('Yaga.ManageBadges'));
45:
46:
47: list($Offset, $Limit) = OffsetLimit($Page, PagerModule::$DefaultPageSize);
48: $this->SetData('Badges', $this->BadgeModel->GetLimit($Limit, $Offset));
49: $this->SetData('Rules', RulesController::GetRules());
50:
51: $this->Render();
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function Edit($BadgeID = NULL) {
61: $this->Permission('Yaga.Badges.Manage');
62: $this->AddSideMenu('badge/settings');
63: $this->Form->SetModel($this->BadgeModel);
64:
65:
66: if(!RulesController::GetRules()) {
67: throw new Gdn_UserException(T('Yaga.Error.NoRules'));
68: }
69:
70: $Edit = FALSE;
71: if($BadgeID) {
72: $this->Title(T('Yaga.EditBadge'));
73: $this->Badge = $this->BadgeModel->GetByID($BadgeID);
74: $this->Form->AddHidden('BadgeID', $BadgeID);
75: $Edit = TRUE;
76: }
77: else {
78: $this->Title(T('Yaga.AddBadge'));
79: }
80:
81: if($this->Form->IsPostBack() == FALSE) {
82: if(property_exists($this, 'Badge')) {
83:
84: $Criteria = (array) unserialize($this->Badge->RuleCriteria);
85: $BadgeArray = (array) $this->Badge;
86:
87: $Data = array_merge($BadgeArray, $Criteria);
88: $this->Form->SetData($Data);
89: }
90: }
91: else {
92:
93: $Upload = new Gdn_Upload();
94: $TmpImage = $Upload->ValidateUpload('PhotoUpload', FALSE);
95:
96: if($TmpImage) {
97:
98: $TargetImage = $Upload->GenerateTargetName(PATH_UPLOADS);
99: $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME);
100:
101:
102: $Parts = $Upload->SaveAs($TmpImage, 'yaga' . DS . $ImageBaseName);
103: $RelativeUrl = StringBeginsWith($Parts['Url'], Gdn_Url::WebRoot(TRUE), TRUE, TRUE);
104:
105: $this->Form->SetFormValue('Photo', $RelativeUrl);
106: }
107: else if(!$Edit) {
108:
109: $this->Form->SetFormValue('Photo', C('Yaga.Badges.DefaultPhoto'));
110: }
111:
112:
113: $FormValues = $this->Form->FormValues();
114: $Criteria = array();
115: foreach($FormValues as $Key => $Value) {
116: if(substr($Key, 0, 7) == '_Rules/') {
117: $RealKey = substr($Key, 7);
118: $Criteria[$RealKey] = $Value;
119: }
120: }
121:
122:
123: $RuleClass = new $FormValues['RuleClass'];
124: $Rule = new $RuleClass();
125:
126: $Rule->Validate($Criteria, $this->Form);
127:
128: $SerializedCriteria = serialize($Criteria);
129: $this->Form->SetFormValue('RuleCriteria', $SerializedCriteria);
130: if($this->Form->Save()) {
131: if($Edit) {
132: $this->InformMessage(T('Yaga.BadgeUpdated'));
133: }
134: else {
135: $this->InformMessage(T('Yaga.BadgeAdded'));
136: }
137: Redirect('/badge/settings');
138: }
139: }
140:
141: $this->Render('edit');
142: }
143:
144: 145: 146:
147: public function Add() {
148: $this->Edit();
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function Delete($BadgeID) {
158: $Badge = $this->BadgeModel->GetByID($BadgeID);
159:
160: if(!$Badge) {
161: throw NotFoundException(T('Yaga.Badge'));
162: }
163:
164: $this->Permission('Yaga.Badges.Manage');
165:
166: if($this->Form->IsPostBack()) {
167: if(!$this->BadgeModel->Delete($BadgeID)) {
168: $this->Form->AddError(sprintf(T('Yaga.Error.DeleteFailed'), T('Yaga.Badge')));
169: }
170:
171: if($this->Form->ErrorCount() == 0) {
172: if($this->_DeliveryType === DELIVERY_TYPE_ALL) {
173: Redirect('badge/settings');
174: }
175:
176: $this->JsonTarget('#BadgeID_' . $BadgeID, NULL, 'SlideUp');
177: }
178: }
179:
180: $this->AddSideMenu('badge/settings');
181: $this->SetData('Title', T('Yaga.Badge.Delete'));
182: $this->Render();
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function Toggle($BadgeID) {
192: if(!$this->Request->IsPostBack()) {
193: throw new Gdn_UserException(T('Yaga.Error.NeedJS'));
194: }
195: $this->Permission('Yaga.Badges.Manage');
196: $this->AddSideMenu('badge/settings');
197:
198: $Badge = $this->BadgeModel->GetByID($BadgeID);
199:
200: if($Badge->Enabled) {
201: $Enable = FALSE;
202: $ToggleText = T('Disabled');
203: $ActiveClass = 'InActive';
204: }
205: else {
206: $Enable = TRUE;
207: $ToggleText = T('Enabled');
208: $ActiveClass = 'Active';
209: }
210:
211: $Slider = Wrap(Wrap(Anchor($ToggleText, 'badge/toggle/' . $Badge->BadgeID, 'Hijack SmallButton'), 'span', array('class' => "ActivateSlider ActivateSlider-{$ActiveClass}")), 'td');
212: $this->BadgeModel->Enable($BadgeID, $Enable);
213: $this->JsonTarget('#BadgeID_' . $BadgeID . ' td:nth-child(6)', $Slider, 'ReplaceWith');
214: $this->Render('Blank', 'Utility', 'Dashboard');
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function DeletePhoto($BadgeID = FALSE, $TransientKey = '') {
224:
225: $this->Permission('Yaga.Badges.Manage');
226:
227: $RedirectUrl = 'badge/edit/'.$BadgeID;
228:
229: if (Gdn::Session()->ValidateTransientKey($TransientKey)) {
230: $this->BadgeModel->SetField($BadgeID, 'Photo', C('Yaga.Badges.DefaultPhoto'));
231: $this->InformMessage(T('Yaga.BadgePhotoDeleted'));
232: }
233:
234: if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
235: Redirect($RedirectUrl);
236: } else {
237: $this->ControllerName = 'Home';
238: $this->View = 'FileNotFound';
239: $this->RedirectUrl = Url($RedirectUrl);
240: $this->Render();
241: }
242: }
243:
244: 245: 246: 247: 248: 249:
250: public function Award($UserID) {
251:
252: $this->Permission('Yaga.Badges.Add');
253: $this->AddSideMenu('badge/settings');
254:
255:
256: if(!$this->BadgeModel->GetCount()) {
257: throw new Gdn_UserException(T('Yaga.Error.NoBadges'));
258: }
259:
260: $UserModel = Gdn::UserModel();
261: $User = $UserModel->GetID($UserID);
262:
263: $this->SetData('Username', $User->Name);
264:
265: $Badges = $this->BadgeModel->Get();
266: $Badgelist = array();
267: foreach($Badges as $Badge) {
268: $Badgelist[$Badge->BadgeID] = $Badge->Name;
269: }
270: $this->SetData('Badges', $Badgelist);
271:
272: if($this->Form->IsPostBack() == FALSE) {
273:
274: $this->Form->AddHidden('UserID', $User->UserID);
275: }
276: else {
277: $Validation = new Gdn_Validation();
278: $Validation->ApplyRule('UserID', 'ValidateRequired');
279: $Validation->ApplyRule('BadgeID', 'ValidateRequired');
280: if($Validation->Validate($this->Request->Post())) {
281: $FormValues = $this->Form->FormValues();
282: if($this->BadgeAwardModel->Exists($FormValues['UserID'], $FormValues['BadgeID'])) {
283: $this->Form->AddError(sprintf(T('Yaga.BadgeAlreadyAwarded'), $User->Name), 'BadgeID');
284:
285: $this->Form->AddHidden('UserID', $User->UserID);
286: }
287:
288: if($this->Form->ErrorCount() == 0) {
289: $this->BadgeAwardModel->Award($FormValues['BadgeID'], $FormValues['UserID'], Gdn::Session()->UserID, $FormValues['Reason']);
290:
291: if($this->Request->Get('Target')) {
292: $this->RedirectUrl = $this->Request->Get('Target');
293: }
294: elseif($this->DeliveryType() == DELIVERY_TYPE_ALL) {
295: $this->RedirectUrl = Url(UserUrl($User));
296: }
297: else {
298: $this->JsonTarget('', '', 'Refresh');
299: }
300: }
301: }
302: else {
303: $this->Form->SetValidationResults($Validation->Results());
304: }
305: }
306:
307: $this->Render();
308: }
309:
310: }
311: