From 9e4d74eb34c313a3b461ee512e72149db7d4c1df Mon Sep 17 00:00:00 2001 From: Zachary Doll Date: Fri, 8 Nov 2013 15:01:09 -0600 Subject: [PATCH] Added rules controller and AJAX criteria form retrieval --- controllers/class.badgescontroller.php | 31 +------- controllers/class.rulescontroller.php | 76 +++++++++++++++++++ js/admin.badges.js | 54 ++++++++++++- ...tcount.rule.php => class.commentcount.php} | 21 ++++- ...unt.rule.php => class.discussioncount.php} | 11 ++- ...ice.rule.php => class.lengthofservice.php} | 15 +++- rules/interface.yagarule.php | 2 +- views/badges/add.php | 2 +- 8 files changed, 170 insertions(+), 42 deletions(-) create mode 100644 controllers/class.rulescontroller.php rename rules/{class.commentcount.rule.php => class.commentcount.php} (62%) rename rules/{class.discussioncount.rule.php => class.discussioncount.php} (83%) rename rules/{class.lengthofservice.rule.php => class.lengthofservice.php} (78%) diff --git a/controllers/class.badgescontroller.php b/controllers/class.badgescontroller.php index f57e9a7..6d3c302 100644 --- a/controllers/class.badgescontroller.php +++ b/controllers/class.badgescontroller.php @@ -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'); } diff --git a/controllers/class.rulescontroller.php b/controllers/class.rulescontroller.php new file mode 100644 index 0000000..8f8f15b --- /dev/null +++ b/controllers/class.rulescontroller.php @@ -0,0 +1,76 @@ +$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); + } +} diff --git a/js/admin.badges.js b/js/admin.badges.js index 8b75e13..62609f3 100644 --- a/js/admin.badges.js +++ b/js/admin.badges.js @@ -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(); + }); + } + }); + } }); }); diff --git a/rules/class.commentcount.rule.php b/rules/class.commentcount.php similarity index 62% rename from rules/class.commentcount.rule.php rename to rules/class.commentcount.php index 7288b39..cb36986 100644 --- a/rules/class.commentcount.rule.php +++ b/rules/class.commentcount.php @@ -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() { diff --git a/rules/class.discussioncount.rule.php b/rules/class.discussioncount.php similarity index 83% rename from rules/class.discussioncount.rule.php rename to rules/class.discussioncount.php index fdc233b..4e2c3c2 100644 --- a/rules/class.discussioncount.rule.php +++ b/rules/class.discussioncount.php @@ -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() { diff --git a/rules/class.lengthofservice.rule.php b/rules/class.lengthofservice.php similarity index 78% rename from rules/class.lengthofservice.rule.php rename to rules/class.lengthofservice.php index 4456d15..d2f6258 100644 --- a/rules/class.lengthofservice.rule.php +++ b/rules/class.lengthofservice.php @@ -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() { diff --git a/rules/interface.yagarule.php b/rules/interface.yagarule.php index 776899e..9c6bc04 100644 --- a/rules/interface.yagarule.php +++ b/rules/interface.yagarule.php @@ -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. diff --git a/views/badges/add.php b/views/badges/add.php index ad72d75..7e6fb04 100644 --- a/views/badges/add.php +++ b/views/badges/add.php @@ -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');