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. */
|
2013-11-08 15:01:09 -06:00
|
|
|
public $Uses = array('Form', 'BadgeModel');
|
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');
|
|
|
|
}
|
2013-11-07 17:34:54 -06:00
|
|
|
$this->AddJsFile('admin.badges.js');
|
2013-11-01 16:14:14 -05:00
|
|
|
$this->AddCssFile('badges.css');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
2013-11-07 17:34:54 -06:00
|
|
|
|
|
|
|
// Only allow editing if some rules exist
|
2013-11-08 15:01:09 -06:00
|
|
|
if(!RulesController::GetRules()) {
|
2013-11-07 17:44:36 -06:00
|
|
|
throw ForbiddenException('add or edit badges without rules');
|
|
|
|
}
|
2013-11-01 16:14:14 -05:00
|
|
|
|
|
|
|
$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')) {
|
2013-11-07 17:34:54 -06:00
|
|
|
// Manually merge the criteria into the badge object
|
2013-11-11 14:45:31 -06:00
|
|
|
$Criteria = (array) unserialize($this->Badge->RuleCriteria);
|
2013-11-07 17:34:54 -06:00
|
|
|
$BadgeArray = (array) $this->Badge;
|
|
|
|
|
|
|
|
$Data = array_merge($BadgeArray, $Criteria);
|
|
|
|
$this->Form->SetData($Data);
|
2013-11-04 17:32:04 -06:00
|
|
|
}
|
2013-11-01 16:14:14 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-11-07 17:34:54 -06:00
|
|
|
// Handle the photo upload
|
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']);
|
|
|
|
}
|
2013-11-07 17:34:54 -06:00
|
|
|
|
|
|
|
// Find the rule criteria
|
|
|
|
$FormValues = $this->Form->FormValues();
|
|
|
|
$Criteria = array();
|
|
|
|
foreach($FormValues as $Key => $Value) {
|
|
|
|
if(substr($Key, 0, 7) == '_Rules/') {
|
|
|
|
$RealKey = substr($Key, 7);
|
|
|
|
$Criteria[$RealKey] = $Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$SerializedCriteria = serialize($Criteria);
|
|
|
|
$this->Form->SetFormValue('RuleCriteria', $SerializedCriteria);
|
2013-11-04 17:32:04 -06:00
|
|
|
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
|
2013-11-11 17:34:57 -06:00
|
|
|
$this->BadgeModel->SetField($BadgeID, 'Photo', NULL);
|
2013-11-04 17:32:04 -06:00
|
|
|
$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-11 17:34:57 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Add this in as a full up option
|
|
|
|
* @param int $UserID
|
|
|
|
* @param int $BadgeID
|
|
|
|
* @param type $TransientKey
|
|
|
|
*/
|
|
|
|
public function Award($UserID, $BadgeID, $TransientKey = '') {
|
|
|
|
// Check permission
|
|
|
|
$this->Permission('Garden.Badges.Add');
|
|
|
|
|
|
|
|
$RedirectUrl = 'yaga/badges/settings';
|
|
|
|
|
|
|
|
if (Gdn::Session()->ValidateTransientKey($TransientKey)) {
|
|
|
|
// Do removal, set message, redirect
|
|
|
|
$this->BadgeModel->AwardBadge($BadgeID, $UserID);
|
|
|
|
$this->InformMessage(T('Badge has been awarded.'));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|