Added rules controller and AJAX criteria form retrieval

This commit is contained in:
Zachary Doll 2013-11-08 15:01:09 -06:00
parent 154a5b6452
commit 9e4d74eb34
8 changed files with 170 additions and 42 deletions

View File

@ -10,7 +10,7 @@
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');
public $Uses = array('Form', 'BadgeModel');
/**
* If you use a constructor, always call parent.
@ -38,7 +38,6 @@ class BadgesController extends DashboardController {
}
$this->AddJsFile('admin.badges.js');
$this->AddCssFile('badges.css');
$this->Filecache->AddContainer(array(Gdn_Cache::CONTAINER_LOCATION=>'./cache/'));
}
public function Settings($Page = '') {
@ -53,32 +52,6 @@ class BadgesController extends DashboardController {
$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() {
$Session = Gdn::Session();
if(!$Session->IsValid())
@ -122,7 +95,7 @@ class BadgesController extends DashboardController {
$this->Form->SetModel($this->BadgeModel);
// Only allow editing if some rules exist
if(!$this->GetRules()) {
if(!RulesController::GetRules()) {
throw ForbiddenException('add or edit badges without rules');
}

View 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);
}
}

View File

@ -1,7 +1,55 @@
/* 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($) {
$('form.Badge select').change(function() {
var Option = $(this).val();
alert(Option + 'was selected!');
// TODO: Save form inputs to cache as well as the elements
$('form.Badge select').focus(function() {
// 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();
});
}
});
}
});
});

View File

@ -21,8 +21,25 @@ class CommentCount implements YagaRule{
}
}
public function RenderCriteriaInterface($Form) {
return TRUE;
public function RenderCriteriaInterface($Form, $Echo = 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() {

View File

@ -21,8 +21,15 @@ class DiscussionCount implements YagaRule{
}
}
public function RenderCriteriaInterface($Form) {
return TRUE;
public function RenderCriteriaInterface($Form, $Echo = TRUE) {
$String = 'LOLOLOL';
if($Echo) {
echo $String;
}
else {
return $String;
}
}
public function Description() {

View File

@ -23,16 +23,23 @@ class LengthOfService implements YagaRule {
}
}
public function RenderCriteriaInterface($Form) {
public function RenderCriteriaInterface($Form, $Echo = TRUE) {
$Lengths = array(
'day' => 'Days',
'week' => 'Weeks',
'year' => 'Years'
);
echo $Form->Label('Time Served', 'LengthOfService');
echo $Form->Textbox('Duration');
echo $Form->DropDown('Period', $Lengths);
$String = $Form->Label('Time Served', 'LengthOfService');
$String .= $Form->Textbox('Duration');
$String .= $Form->DropDown('Period', $Lengths);
if($Echo) {
echo $String;
}
else {
return $String;
}
}
public function Description() {

View File

@ -23,7 +23,7 @@ interface YagaRule {
*
* @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.

View File

@ -2,7 +2,7 @@
/* Copyright 2013 Zachary Doll */
// Gnab the rules so we can render the first criteria form by default
$Rules = $this->GetRules();
$Rules = RulesController::GetRules();
if(property_exists($this, 'Badge')) {
echo Wrap(T('Edit Badge'), 'h1');