vanilla-application-yaga/controllers/class.badgescontroller.php

220 lines
6.3 KiB
PHP
Raw Normal View History

2013-11-01 16:14:14 -05:00
<?php if(!defined('APPLICATION')) exit();
/* Copyright 2013 Zachary Doll */
/**
2013-11-04 17:32:04 -06:00
* Contains management code for creating badges.
2013-11-01 16:14:14 -05:00
*
* @since 1.0
* @package Yaga
*/
class BadgesController extends DashboardController {
/** @var array List of objects to prep. They will be available as $this->$Name. */
public $Uses = array('Form', 'BadgeModel', 'Gdn_Filecache');
2013-11-01 16:14:14 -05:00
/**
* If you use a constructor, always call parent.
* Delete this if you don't need it.
*
* @access public
*/
public function __construct() {
parent::__construct();
}
/**
* This is a good place to include JS, CSS, and modules used by all methods of this controller.
*
* Always called by dispatcher before controller's requested method.
*
* @since 1.0
* @access public
*/
public function Initialize() {
parent::Initialize();
Gdn_Theme::Section('Dashboard');
if($this->Menu) {
$this->Menu->HighlightRoute('/badges');
}
$this->AddJsFile('badges.js');
$this->AddCssFile('badges.css');
$this->Filecache->AddContainer(array(Gdn_Cache::CONTAINER_LOCATION=>'./cache/'));
2013-11-01 16:14:14 -05:00
}
public function Settings($Page = '') {
$this->Permission('Yaga.Badges.Manage');
$this->AddSideMenu('badges/settings');
$this->Title('Manage Badges');
// Get list of badges from the model and pass to the view
$this->SetData('Badges', $this->BadgeModel->GetBadges());
$this->Render();
}
2013-11-04 17:32:04 -06:00
public function GetRules() {
$Rules = $this->Filecache->Get('Yaga.Badges.Rules');
if($Rules == Gdn_Cache::CACHEOP_FAILURE) {
foreach(glob(PATH_APPLICATIONS . DS . 'yaga' . DS . 'rules' . DS . '*.php') as $filename) {
include_once $filename;
}
$TempRules = array();
foreach(get_declared_classes() as $className) {
if(in_array('YagaRule', class_implements($className))) {
$Rule = new $className();
$TempRules[$className] = $Rule->FriendlyName();
}
}
$Rules = serialize($TempRules);
$this->Filecache->Store('Yaga.Badges.Rules', $Rules, array(Gdn_Cache::FEATURE_EXPIRY => C('Yaga.Rules.CacheExpire', 86400)));
2013-11-04 17:32:04 -06:00
}
return unserialize($Rules);
}
public function test() {
$Session = Gdn::Session();
if(!$Session->IsValid())
return;
$UserID = $Session->UserID;
$BadgeModel = new BadgeModel();
$Badges = $BadgeModel->GetEnabledBadges();
$UserBadges = $BadgeModel->GetUserBadgeAwards($UserID);
$Rules = array();
foreach($Badges as $Badge) {
if(!InSubArray($Badge->BadgeID, $UserBadges)) {
// The user doesn't have this badge
$Class = $Badge->RuleClass;
// Create a rule object if needed
if(!in_array($Class, $Rules)) {
$Rule = new $Class();
$Rules[$Class] = $Rule;
}
// execute the Calculated
$Rule = $Rules[$Class];
if($Rule->CalculateAward($UserID, $Badge->RuleCriteria)) {
$BadgeModel->AwardBadge($Badge->BadgeID, $UserID);
// TODO: Record to Activity Table
$this->InformMessage('Badge "' . $Badge->Name . '" was awarded to you!');
// Notify user of badge award
}
2013-11-04 17:32:04 -06:00
}
}
$this->Render('add');
2013-11-04 17:32:04 -06:00
}
2013-11-01 16:14:14 -05:00
public function Edit($BadgeID = NULL) {
$this->Permission('Yaga.Badges.Manage');
$this->AddSideMenu('badges/settings');
$this->Form->SetModel($this->BadgeModel);
$Edit = FALSE;
if($BadgeID) {
$this->Badge = $this->BadgeModel->GetBadge($BadgeID);
$this->Form->AddHidden('BadgeID', $BadgeID);
$Edit = TRUE;
}
if($this->Form->IsPostBack() == FALSE) {
2013-11-04 17:32:04 -06:00
if(property_exists($this, 'Badge')) {
$this->Form->SetData($this->Badge);
}
2013-11-01 16:14:14 -05:00
}
else {
2013-11-04 17:32:04 -06:00
$Upload = new Gdn_Upload();
$TmpImage = $Upload->ValidateUpload('PhotoUpload', FALSE);
2013-11-01 16:14:14 -05:00
2013-11-04 17:32:04 -06:00
if($TmpImage) {
// Generate the target image name
$TargetImage = $Upload->GenerateTargetName(PATH_UPLOADS);
$ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME);
// Save the uploaded image
$Parts = $Upload->SaveAs($TmpImage, $ImageBaseName);
$this->Form->SetFormValue('Photo', $Parts['SaveName']);
}
if($this->Form->Save()) {
2013-11-01 16:14:14 -05:00
if($Edit) {
$this->InformMessage('Badge updated successfully!');
}
else {
$this->InformMessage('Badge added successfully!');
}
2013-11-04 17:32:04 -06:00
Redirect('/yaga/badges/settings');
2013-11-01 16:14:14 -05:00
}
}
$this->Render('add');
}
public function Add() {
$this->Edit();
}
public function Delete($BadgeID) {
$this->Permission('Yaga.Badges.Manage');
$this->AddSideMenu('badges/settings');
$this->BadgeModel->DeleteBadge($BadgeID);
redirect('badges/settings');
}
public function Toggle($BadgeID) {
if(!$this->Request->IsPostBack()) {
2013-11-04 17:32:04 -06:00
throw PermissionException('Javascript');
2013-11-01 16:14:14 -05:00
}
$this->Permission('Yaga.Reactions.Manage');
$this->AddSideMenu('badges/settings');
2013-11-04 17:32:04 -06:00
2013-11-01 16:14:14 -05:00
$Badge = $this->BadgeModel->GetBadge($BadgeID);
2013-11-04 17:32:04 -06:00
if($Badge->Enabled) {
$Enable = FALSE;
$ToggleText = T('Disabled');
$ActiveClass = 'InActive';
}
else {
$Enable = TRUE;
$ToggleText = T('Enabled');
$ActiveClass = 'Active';
}
$Slider = Wrap(Wrap(Anchor($ToggleText, 'yaga/badges/toggle/' . $Badge->BadgeID, 'Hijack SmallButton'), 'span', array('class' => "ActivateSlider ActivateSlider-{$ActiveClass}")), 'td');
2013-11-01 16:14:14 -05:00
$this->BadgeModel->EnableBadge($BadgeID, $Enable);
2013-11-04 17:32:04 -06:00
$this->JsonTarget('#BadgeID_' . $BadgeID . ' td:nth-child(7)', $Slider, 'ReplaceWith');
2013-11-01 16:14:14 -05:00
$this->Render('Blank', 'Utility', 'Dashboard');
}
2013-11-04 17:32:04 -06:00
public function DeletePhoto($BadgeID = FALSE, $TransientKey = '') {
// Check permission
$this->Permission('Garden.Badges.Manage');
$RedirectUrl = 'yaga/badges/edit/'.$BadgeID;
if (Gdn::Session()->ValidateTransientKey($TransientKey)) {
// Do removal, set message, redirect
$BadgeModel = new BadgeModel();
$BadgeModel->SetField($BadgeID, 'Photo', NULL);
$this->InformMessage(T('Badge photo has been deleted.'));
}
if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
Redirect($RedirectUrl);
} else {
$this->ControllerName = 'Home';
$this->View = 'FileNotFound';
$this->RedirectUrl = Url($RedirectUrl);
$this->Render();
}
}
2013-11-01 16:14:14 -05:00
}