Overview

Packages

  • None
  • Yaga

Classes

  • ActedModel
  • ActionController
  • ActionModel
  • AwardCombo
  • BadgeAwardModel
  • BadgeController
  • BadgeModel
  • BadgesController
  • BadgesModule
  • BestController
  • BestFilterModule
  • CakeDayPost
  • CommentCount
  • CommentMarathon
  • DiscussionBodyLength
  • DiscussionCategory
  • DiscussionCount
  • DiscussionPageCount
  • HasMentioned
  • HolidayVisit
  • LeaderBoardModule
  • LengthOfService
  • ManualAward
  • NecroPost
  • NewbieComment
  • PhotoExists
  • PostCount
  • PostReactions
  • QnAAnserCount
  • RankController
  • RankModel
  • ReactController
  • ReactionCount
  • ReactionModel
  • ReflexComment
  • RulesController
  • SocialConnection
  • Yaga
  • YagaController
  • YagaHooks

Interfaces

  • YagaRule
  • Overview
  • Package
  • Class
  • Tree
  • Todo
  • Download
  1: <?php if(!defined('APPLICATION')) exit();
  2: /* Copyright 2013 Zachary Doll */
  3: 
  4: /**
  5:  * Contains management code for creating badges.
  6:  *
  7:  * @since 1.0
  8:  * @package Yaga
  9:  */
 10: class BadgeController extends DashboardController {
 11: 
 12:   /**
 13:    * @var array These objects will be created on instantiation and available via
 14:    * $this->ObjectName
 15:    */
 16:   public $Uses = array('Form', 'BadgeModel', 'BadgeAwardModel');
 17: 
 18:   /**
 19:    * Make this look like a dashboard page and add the resources
 20:    *
 21:    * @since 1.0
 22:    * @access public
 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:    * Manage the current badges and add new ones
 37:    *
 38:    * @param int $Page
 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:     // Get list of badges from the model and pass to the view
 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:    * Edit an existing badge or add a new one
 56:    *
 57:    * @param int $BadgeID
 58:    * @throws ForbiddenException if no proper rules are found
 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:     // Only allow editing if some rules exist
 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:         // Manually merge the criteria into the badge object
 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:       // Handle the photo upload
 93:       $Upload = new Gdn_Upload();
 94:       $TmpImage = $Upload->ValidateUpload('PhotoUpload', FALSE);
 95: 
 96:       if($TmpImage) {
 97:         // Generate the target image name
 98:         $TargetImage = $Upload->GenerateTargetName(PATH_UPLOADS);
 99:         $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME);
100: 
101:         // Save the uploaded image
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:         // Use default photo from config if this is a new badge
109:         $this->Form->SetFormValue('Photo', C('Yaga.Badges.DefaultPhoto'));
110:       }
111: 
112:       // Find the rule criteria
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:       // Validate the criteria
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:    * Convenience function for nice URLs
146:    */
147:   public function Add() {
148:     $this->Edit();
149:   }
150: 
151:   /**
152:    * Remove the badge via model.
153:    *
154:    * @param int $BadgeID
155:    * @throws NotFoundException
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:    * Toggle the enabled state of a badge. Must be done via JS.
187:    *
188:    * @param int $BadgeID
189:    * @throws PermissionException
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:    * Remove the photo association of a badge. This does not remove the actual file
219:    *
220:    * @param int $BadgeID
221:    * @param string $TransientKey
222:    */
223:   public function DeletePhoto($BadgeID = FALSE, $TransientKey = '') {
224:       // Check permission
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:     * You can manually award badges to users for special cases
246:     *
247:     * @param int $UserID
248:     * @throws Gdn_UserException
249:     */
250:    public function Award($UserID) {
251:     // Check permission
252:     $this->Permission('Yaga.Badges.Add');
253:     $this->AddSideMenu('badge/settings');
254: 
255:     // Only allow awarding if some badges exist
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:       // Add the user id field
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:           // Need to respecify the user id
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: 
Yaga API documentation generated by ApiGen 2.8.0