Added rules controller and AJAX criteria form retrieval
This commit is contained in:
parent
154a5b6452
commit
9e4d74eb34
@ -10,7 +10,7 @@
|
|||||||
class BadgesController extends DashboardController {
|
class BadgesController extends DashboardController {
|
||||||
|
|
||||||
/** @var array List of objects to prep. They will be available as $this->$Name. */
|
/** @var array List of objects to prep. They will be available as $this->$Name. */
|
||||||
public $Uses = array('Form', 'BadgeModel', 'Gdn_Filecache');
|
public $Uses = array('Form', 'BadgeModel');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If you use a constructor, always call parent.
|
* If you use a constructor, always call parent.
|
||||||
@ -38,7 +38,6 @@ class BadgesController extends DashboardController {
|
|||||||
}
|
}
|
||||||
$this->AddJsFile('admin.badges.js');
|
$this->AddJsFile('admin.badges.js');
|
||||||
$this->AddCssFile('badges.css');
|
$this->AddCssFile('badges.css');
|
||||||
$this->Filecache->AddContainer(array(Gdn_Cache::CONTAINER_LOCATION=>'./cache/'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Settings($Page = '') {
|
public function Settings($Page = '') {
|
||||||
@ -53,32 +52,6 @@ class BadgesController extends DashboardController {
|
|||||||
$this->Render();
|
$this->Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(empty($TempRules)) {
|
|
||||||
$Rules = serialize(FALSE);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$Rules = serialize($TempRules);
|
|
||||||
}
|
|
||||||
//$this->Filecache->Store('Yaga.Badges.Rules', $Rules, array(Gdn_Cache::FEATURE_EXPIRY => C('Yaga.Rules.CacheExpire', 86400)));
|
|
||||||
//}
|
|
||||||
|
|
||||||
return unserialize($Rules);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test() {
|
public function test() {
|
||||||
$Session = Gdn::Session();
|
$Session = Gdn::Session();
|
||||||
if(!$Session->IsValid())
|
if(!$Session->IsValid())
|
||||||
@ -122,7 +95,7 @@ class BadgesController extends DashboardController {
|
|||||||
$this->Form->SetModel($this->BadgeModel);
|
$this->Form->SetModel($this->BadgeModel);
|
||||||
|
|
||||||
// Only allow editing if some rules exist
|
// Only allow editing if some rules exist
|
||||||
if(!$this->GetRules()) {
|
if(!RulesController::GetRules()) {
|
||||||
throw ForbiddenException('add or edit badges without rules');
|
throw ForbiddenException('add or edit badges without rules');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
76
controllers/class.rulescontroller.php
Normal file
76
controllers/class.rulescontroller.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php if(!defined('APPLICATION')) exit();
|
||||||
|
/* Copyright 2013 Zachary Doll */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains management code for creating badges.
|
||||||
|
*
|
||||||
|
* @since 1.0
|
||||||
|
* @package Yaga
|
||||||
|
*/
|
||||||
|
class RulesController extends YagaController {
|
||||||
|
|
||||||
|
/** @var array List of objects to prep. They will be available as $this->$Name. */
|
||||||
|
public $Uses = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function GetRules() {
|
||||||
|
$Filecache = new Gdn_Filecache();
|
||||||
|
$Filecache->AddContainer(array(Gdn_Cache::CONTAINER_LOCATION=>'./cache/'));
|
||||||
|
|
||||||
|
$Rules = $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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(empty($TempRules)) {
|
||||||
|
$Rules = serialize(FALSE);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$Rules = serialize($TempRules);
|
||||||
|
}
|
||||||
|
$Filecache->Store('Yaga.Badges.Rules', $Rules, array(Gdn_Cache::FEATURE_EXPIRY => C('Yaga.Rules.CacheExpire', 86400)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return unserialize($Rules);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function GetCriteriaForm($RuleClass) {
|
||||||
|
$Rule = new $RuleClass();
|
||||||
|
$Form = Gdn::Factory('Form');
|
||||||
|
$Form->InputPrefix = '_Rules';
|
||||||
|
$FormString = $Rule->RenderCriteriaInterface($Form, FALSE);
|
||||||
|
$Description = $Rule->Description();
|
||||||
|
|
||||||
|
$Data = array( 'CriteriaForm' => $FormString, 'RuleClass' => $RuleClass, 'Description' => $Description);
|
||||||
|
echo json_encode($Data);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,55 @@
|
|||||||
/* Copyright 2013 Zachary Doll */
|
/* Copyright 2013 Zachary Doll */
|
||||||
|
var Cache = {
|
||||||
|
data: {},
|
||||||
|
remove: function(key) {
|
||||||
|
delete Cache.data[key];
|
||||||
|
},
|
||||||
|
exists: function(key) {
|
||||||
|
return Cache.data.hasOwnProperty(key) && Cache.data[key] !== null;
|
||||||
|
},
|
||||||
|
get: function(key) {
|
||||||
|
console.log('Getting cache for key ' + key);
|
||||||
|
return Cache.data[key];
|
||||||
|
},
|
||||||
|
set: function(key, cachedData) {
|
||||||
|
console.log('Setting cache for key ' + key);
|
||||||
|
Cache.remove(key);
|
||||||
|
Cache.data[key] = cachedData;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
jQuery(document).ready(function($) {
|
jQuery(document).ready(function($) {
|
||||||
$('form.Badge select').change(function() {
|
// TODO: Save form inputs to cache as well as the elements
|
||||||
var Option = $(this).val();
|
$('form.Badge select').focus(function() {
|
||||||
alert(Option + 'was selected!');
|
// Update the cache before the change
|
||||||
|
var Rule = $(this).val();
|
||||||
|
var FormHtml = $('#Rule-Criteria').html();
|
||||||
|
Cache.set(Rule, FormHtml);
|
||||||
|
}).change(function() {
|
||||||
|
|
||||||
|
// Grab the form from cache or ajax
|
||||||
|
var NewRule = $(this).val();
|
||||||
|
if (Cache.exists(NewRule)) {
|
||||||
|
$('#Rule-Criteria').fadeOut(function() {
|
||||||
|
$(this).html(Cache.get(NewRule)).fadeIn();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Grab the form via ajax
|
||||||
|
var url = gdn.url('/rules/getcriteriaform/' + NewRule);
|
||||||
|
$.ajax({
|
||||||
|
url: url,
|
||||||
|
global: false,
|
||||||
|
type: "GET",
|
||||||
|
data: null,
|
||||||
|
dataType: "json",
|
||||||
|
success: function(data) {
|
||||||
|
Cache.set(NewRule, data.CriteriaForm);
|
||||||
|
$('#Rule-Criteria').fadeOut(function() {
|
||||||
|
$(this).html(Cache.get(NewRule)).fadeIn();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -21,8 +21,25 @@ class CommentCount implements YagaRule{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RenderCriteriaInterface($Form) {
|
public function RenderCriteriaInterface($Form, $Echo = TRUE) {
|
||||||
return TRUE;
|
$Comparisons = array(
|
||||||
|
'gt' => 'more than:',
|
||||||
|
'lt' => 'less than:',
|
||||||
|
'gte' => 'more than or equal to:'
|
||||||
|
);
|
||||||
|
|
||||||
|
$String = $Form->Label('Total comments', 'CommentCount');
|
||||||
|
$String .= 'User has ';
|
||||||
|
$String .= $Form->DropDown('Comparison', $Comparisons);
|
||||||
|
$String .= $Form->Textbox('Target');
|
||||||
|
$String .= ' comments';
|
||||||
|
|
||||||
|
if($Echo) {
|
||||||
|
echo $String;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return $String;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Description() {
|
public function Description() {
|
@ -21,8 +21,15 @@ class DiscussionCount implements YagaRule{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RenderCriteriaInterface($Form) {
|
public function RenderCriteriaInterface($Form, $Echo = TRUE) {
|
||||||
return TRUE;
|
$String = 'LOLOLOL';
|
||||||
|
|
||||||
|
if($Echo) {
|
||||||
|
echo $String;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return $String;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Description() {
|
public function Description() {
|
@ -23,16 +23,23 @@ class LengthOfService implements YagaRule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RenderCriteriaInterface($Form) {
|
public function RenderCriteriaInterface($Form, $Echo = TRUE) {
|
||||||
$Lengths = array(
|
$Lengths = array(
|
||||||
'day' => 'Days',
|
'day' => 'Days',
|
||||||
'week' => 'Weeks',
|
'week' => 'Weeks',
|
||||||
'year' => 'Years'
|
'year' => 'Years'
|
||||||
);
|
);
|
||||||
|
|
||||||
echo $Form->Label('Time Served', 'LengthOfService');
|
$String = $Form->Label('Time Served', 'LengthOfService');
|
||||||
echo $Form->Textbox('Duration');
|
$String .= $Form->Textbox('Duration');
|
||||||
echo $Form->DropDown('Period', $Lengths);
|
$String .= $Form->DropDown('Period', $Lengths);
|
||||||
|
|
||||||
|
if($Echo) {
|
||||||
|
echo $String;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return $String;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Description() {
|
public function Description() {
|
@ -23,7 +23,7 @@ interface YagaRule {
|
|||||||
*
|
*
|
||||||
* @param Gdn_Form $Form
|
* @param Gdn_Form $Form
|
||||||
*/
|
*/
|
||||||
public function RenderCriteriaInterface($Form);
|
public function RenderCriteriaInterface($Form, $Echo = TRUE);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string representing a user friendly name of this rule.
|
* Returns a string representing a user friendly name of this rule.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
/* Copyright 2013 Zachary Doll */
|
/* Copyright 2013 Zachary Doll */
|
||||||
|
|
||||||
// Gnab the rules so we can render the first criteria form by default
|
// Gnab the rules so we can render the first criteria form by default
|
||||||
$Rules = $this->GetRules();
|
$Rules = RulesController::GetRules();
|
||||||
|
|
||||||
if(property_exists($this, 'Badge')) {
|
if(property_exists($this, 'Badge')) {
|
||||||
echo Wrap(T('Edit Badge'), 'h1');
|
echo Wrap(T('Edit Badge'), 'h1');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user