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 designing ranks.
  6:  *
  7:  * @since 1.0
  8:  * @package Yaga
  9:  */
 10: class RankController 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', 'RankModel');
 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('/rank');
 30:     }
 31:     $this->AddJsFile('jquery-ui-1.10.0.custom.min.js');
 32:     $this->AddJsFile('admin.ranks.js');
 33:   }
 34: 
 35:   /**
 36:    * Manage the current ranks and add new ones
 37:    */
 38:   public function Settings() {
 39:     $this->Permission('Yaga.Ranks.Manage');
 40:     $this->AddSideMenu('rank/settings');
 41: 
 42:     $this->Title(T('Yaga.ManageRanks'));
 43: 
 44:     // Get list of ranks from the model and pass to the view
 45:     $this->SetData('Ranks', $this->RankModel->Get());
 46: 
 47:     if($this->Form->IsPostBack() == TRUE) {
 48:       // Handle the photo upload
 49:       $Upload = new Gdn_Upload();
 50:       $TmpImage = $Upload->ValidateUpload('PhotoUpload', FALSE);
 51: 
 52:       if($TmpImage) {
 53:         // Generate the target image name
 54:         $TargetImage = $Upload->GenerateTargetName(PATH_UPLOADS);
 55:         $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME);
 56: 
 57:         // Save the uploaded image
 58:         $Parts = $Upload->SaveAs($TmpImage, 'yaga' . DS . $ImageBaseName);
 59:         $RelativeUrl = StringBeginsWith($Parts['Url'], Gdn_Url::WebRoot(TRUE), TRUE, TRUE);
 60:         SaveToConfig('Yaga.Ranks.Photo', $RelativeUrl);
 61: 
 62:         if(C('Yaga.Ranks.Photo') == $Parts['SaveName']) {
 63:           $this->InformMessage(T('Yaga.Rank.PhotoUploaded'));
 64:         }
 65:       }
 66:     }
 67: 
 68:     include_once $this->FetchViewLocation('helper_functions', 'rank');
 69:     $this->Render();
 70:   }
 71: 
 72:   /**
 73:    * Edit an existing rank or add a new one
 74:    *
 75:    * @param int $RankID
 76:    * @throws ForbiddenException if no proper rules are found
 77:    */
 78:   public function Edit($RankID = NULL) {
 79:     $this->Permission('Yaga.Ranks.Manage');
 80:     $this->AddSideMenu('rank/settings');
 81:     $this->Form->SetModel($this->RankModel);
 82: 
 83:     $Edit = FALSE;
 84:     if($RankID) {
 85:       $this->Title(T('Yaga.EditRank'));
 86:       $this->Rank = $this->RankModel->GetByID($RankID);
 87:       $this->Form->AddHidden('RankID', $RankID);
 88:       $Edit = TRUE;
 89:     }
 90:     else {
 91:       $this->Title(T('Yaga.AddRank'));
 92:     }
 93: 
 94:     // Load up all roles
 95:     $RoleModel = new RoleModel();
 96:     $Roles = $RoleModel->GetArray();
 97:     $this->SetData('Roles', $Roles);
 98: 
 99:     if($this->Form->IsPostBack() != TRUE) {
100:       if(property_exists($this, 'Rank')) {
101:         $PerkOptions = (array) unserialize($this->Rank->Perks);
102:         $RankArray = (array) $this->Rank;
103: 
104:         $Data = array_merge($RankArray, $PerkOptions);
105:         $this->Form->SetData($Data);
106:       }
107:     }
108:     else {
109:       // Find the perk options
110:       $FormValues = $this->Form->FormValues();     
111:       $PerkOptions = array();
112:       foreach($FormValues as $Key => $Value) {
113:         // Don't save default settings
114:         if($Value === '') {
115:           continue;
116:         }
117:         
118:         if(substr($Key, 0, 7) == '_Perks/') {
119:           $RealKey = substr($Key, 7);
120:           $PerkOptions[$RealKey] = $Value;
121:         }
122:       }
123: 
124:       // Fire event for validating perk options
125:       $this->EventArguments['PerkOptions'] =& $PerkOptions;
126:       $this->FireEvent('BeforeValidation');
127:       
128:       $this->Form->SetFormValue('Perks', serialize($PerkOptions));
129:       
130:       if($this->Form->Save()) {
131:         if($Edit) {
132:           $this->InformMessage(T('Yaga.RankUpdated'));
133:         }
134:         else {
135:           $this->InformMessage(T('Yaga.RankAdded'));
136:         }
137:         Redirect('/rank/settings');
138:       }
139:     }
140: 
141:     include_once $this->FetchViewLocation('helper_functions', 'rank');
142:     $this->Render('edit');
143:   }
144: 
145:   /**
146:    * Convenience function for nice URLs
147:    */
148:   public function Add() {
149:     $this->Edit();
150:   }
151: 
152:   /**
153:    * Remove the rank via model.
154:    *
155:    * @param int $RankID
156:    * @throws NotFoundException
157:    */
158:   public function Delete($RankID) {
159:     $Rank = $this->RankModel->GetByID($RankID);
160: 
161:     if(!$Rank) {
162:       throw NotFoundException(T('Yaga.Rank'));
163:     }
164: 
165:     $this->Permission('Yaga.Ranks.Manage');
166: 
167:     if($this->Form->IsPostBack()) {
168:       if(!$this->RankModel->Delete($RankID)) {
169:         $this->Form->AddError(sprintf(T('Yaga.Error.DeleteFailed'), T('Yaga.Rank')));
170:       }
171: 
172:       if($this->Form->ErrorCount() == 0) {
173:         if($this->_DeliveryType === DELIVERY_TYPE_ALL) {
174:           Redirect('rank/settings');
175:         }
176: 
177:         $this->JsonTarget('#RankID_' . $RankID, NULL, 'SlideUp');
178:       }
179:     }
180: 
181:     $this->AddSideMenu('rank/settings');
182:     $this->SetData('Title', T('Yaga.Rank.Delete'));
183:     $this->Render();
184:   }
185: 
186:   /**
187:    * Toggle the enabled state of a rank. Must be done via JS.
188:    *
189:    * @param int $RankID
190:    * @throws PermissionException
191:    */
192:   public function Toggle($RankID) {
193:     if(!$this->Request->IsPostBack()) {
194:       throw PermissionException('Javascript');
195:     }
196:     $this->Permission('Yaga.Ranks.Manage');
197:     $this->AddSideMenu('rank/settings');
198: 
199:     $Rank = $this->RankModel->Get($RankID);
200: 
201:     if($Rank->Enabled) {
202:       $Enable = FALSE;
203:       $ToggleText = T('Disabled');
204:       $ActiveClass = 'InActive';
205:     }
206:     else {
207:       $Enable = TRUE;
208:       $ToggleText = T('Enabled');
209:       $ActiveClass = 'Active';
210:     }
211: 
212:     $Slider = Wrap(Wrap(Anchor($ToggleText, 'rank/toggle/' . $Rank->RankID, 'Hijack SmallButton'), 'span', array('class' => "ActivateSlider ActivateSlider-{$ActiveClass}")), 'td');
213:     $this->RankModel->Enable($RankID, $Enable);
214:     $this->JsonTarget('#RankID_' . $RankID . ' td:nth-child(5)', $Slider, 'ReplaceWith');
215:     $this->Render('Blank', 'Utility', 'Dashboard');
216:   }
217: 
218:   /**
219:    * Remove the photo association of rank promotions. This does not remove the
220:    * actual file.
221:    *
222:    * @param string $TransientKey
223:    */
224:   public function DeletePhoto($TransientKey = '') {
225:       // Check permission
226:       $this->Permission('Yaga.Ranks.Manage');
227: 
228:       $RedirectUrl = 'rank/settings';
229: 
230:       if (Gdn::Session()->ValidateTransientKey($TransientKey)) {
231:          SaveToConfig('Yaga.Ranks.Photo', NULL, array('RemoveEmpty' => TRUE));
232:          $this->InformMessage(T('Yaga.RankPhotoDeleted'));
233:       }
234: 
235:       if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
236:           Redirect($RedirectUrl);
237:       } else {
238:          $this->ControllerName = 'Home';
239:          $this->View = 'FileNotFound';
240:          $this->RedirectUrl = Url($RedirectUrl);
241:          $this->Render();
242:       }
243:    }
244: 
245:    /**
246:     * You can manually award ranks to users for special cases
247:     *
248:     * @param int $UserID
249:     * @throws Gdn_UserException
250:     */
251:    public function Promote($UserID) {
252:     // Check permission
253:     $this->Permission('Yaga.Ranks.Add');
254:     $this->AddSideMenu('rank/settings');
255: 
256:     // Only allow awarding if some ranks exist
257:     if(!$this->RankModel->GetCount()) {
258:       throw new Gdn_UserException(T('Yaga.Error.NoRanks'));
259:     }
260: 
261:     $UserModel = Gdn::UserModel();
262:     $User = $UserModel->GetID($UserID);
263: 
264:     $this->SetData('Username', $User->Name);
265: 
266:     $Ranks = $this->RankModel->Get();
267:     $Ranklist = array();
268:     foreach($Ranks as $Rank) {
269:       $Ranklist[$Rank->RankID] = $Rank->Name;
270:     }
271:     $this->SetData('Ranks', $Ranklist);
272: 
273:     if($this->Form->IsPostBack() == FALSE) {
274:       // Add the user id field
275:       $this->Form->AddHidden('UserID', $User->UserID);
276:     }
277:     else {
278:       $Validation = new Gdn_Validation();
279:       $Validation->ApplyRule('UserID', 'ValidateRequired');
280:       $Validation->ApplyRule('RankID', 'ValidateRequired');
281:       if($Validation->Validate($this->Request->Post())) {
282:         $FormValues = $this->Form->FormValues();
283:         if($this->Form->ErrorCount() == 0) {
284:           $this->RankModel->Set($FormValues['RankID'], $FormValues['UserID'], $FormValues['RecordActivity']);
285:           $UserModel->SetField($UserID, 'RankProgression', $FormValues['RankProgression']);
286:           if($this->Request->Get('Target')) {
287:             $this->RedirectUrl = $this->Request->Get('Target');
288:           }
289:           elseif($this->DeliveryType() == DELIVERY_TYPE_ALL) {
290:             $this->RedirectUrl = Url(UserUrl($User));
291:           }
292:           else {
293:             $this->JsonTarget('', '', 'Refresh');
294:           }
295:         }
296:       }
297:       else {
298:         $this->Form->SetValidationResults($Validation->Results());
299:       }
300:     }
301: 
302:     $this->Render();
303:   }
304: 
305:   /**
306:    * This takes in a sort array and updates the rank sort order.
307:    * 
308:    * Renders the Save tree and/or the Result of the sort update.
309:    */
310:   public function Sort() {
311:       // Check permission
312:       $this->Permission('Yaga.Ranks.Manage');
313: 
314:       $Request = Gdn::Request();
315:       if($Request->IsPostBack()) {
316:          $SortArray = $Request->GetValue('SortArray', NULL);
317:          $Saves = $this->RankModel->SaveSort($SortArray);
318:          $this->SetData('Result', TRUE);
319:          $this->SetData('Saves', $Saves);
320:       }
321:       else {
322:         $this->SetData('Result', FALSE);
323:       }
324: 
325:       $this->RenderData();
326:    }
327: }
328: 
Yaga API documentation generated by ApiGen 2.8.0