Skip to content

Commit

Permalink
Allow for display value on rules to act as a way of grouping 'OR' rul…
Browse files Browse the repository at this point in the history
…es for output in rbtme_get_rule_list
  • Loading branch information
cargowire committed Mar 29, 2012
1 parent 7ad69c8 commit 3aa1051
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 11 additions & 4 deletions rule-based-themes-settings.php
Expand Up @@ -98,6 +98,7 @@
var rules = []
jQuery('#rule-table tr.rule').each(function(i,v){
var ruleJson = "\"" + jQuery('input[name=rulekey]', this).val() + "\": {";
ruleJson += "\"display\":\"" + jQuery('input[name=display]', this).val() + "\",";
ruleJson += "\"group\":\"" + jQuery('input[name=group]', this).val() + "\",";
ruleJson += "\"value\":\"" + jQuery('input[name=value]', this).val() + "\",";
ruleJson += "\"rules\": {";
Expand Down Expand Up @@ -136,8 +137,11 @@ function(data) {
<script type="text/html" id="rule-form">
<tr class="rule">
<td><button class="deleterule">x</button></td>
<td><input type="text" name="rulekey" required="required" pattern="^[a-zA-Z0-9\-]*$" value=""/></td>
<td><input type="text" name="group" pattern="^[a-zA-Z0-9\-]*$" value=""/></td>
<td>
<input type="text" name="rulekey" required="required" pattern="^[a-zA-Z0-9\-\s]*$" placeholder="Rule name" value=""/><br/>
<input type="text" name="display" pattern="^[a-zA-Z0-9\-\s]*$" placeholder="Display" value=""/>
</td>
<td><input type="text" name="group" pattern="^[a-zA-Z0-9\-\s]*$" value=""/></td>
<td><input type="text" name="value" pattern="^[a-zA-Z0-9\-]*$" value="" /></td>
<td></td>
</tr>
Expand Down Expand Up @@ -225,8 +229,11 @@ function(data) {
?>
<tr class="rule">
<td><button class="deleterule">x</button></td>
<td><input type="text" name="rulekey" required="required" pattern="^[a-zA-Z0-9\-]*$" value="<?php echo $rulekey; ?>"/></td>
<td><input type="text" name="group" pattern="^[a-zA-Z0-9\-]*$" value="<?php echo $parentRule["group"]; ?>"/></td>
<td>
<input type="text" name="rulekey" required="required" pattern="^[a-zA-Z0-9\-\s]*$" placeholder="Rule name" value="<?php echo $rulekey; ?>"/><br/>
<input type="text" name="display" pattern="^[a-zA-Z0-9\-\s]*$" placeholder="Display" value="<?php echo $parentRule["display"]; ?>"/>
</td>
<td><input type="text" name="group" pattern="^[a-zA-Z0-9\-\s]*$" value="<?php echo $parentRule["group"]; ?>"/></td>
<td><input type="text" name="value" pattern="^[a-zA-Z0-9\-]*$" value="<?php echo $parentRule["value"]; ?>" /></td>
<td>
<?php
Expand Down
12 changes: 8 additions & 4 deletions rulebasedthemes.php
Expand Up @@ -23,7 +23,8 @@ static function serialize($rules){

foreach ($rules as $rulekey => $parentRule){
$serialized .= " \"".rulebasedthemes::normalize_rule_name($rulekey)."\" => array(\n\r";
$serialized .= " \"group\" => \"".rulebasedthemes::normalize_value($parentRule["group"])."\",\n\r";
$serialized .= " \"display\" => \"".rulebasedthemes::normalize_rule_name($parentRule["display"])."\",\n\r";
$serialized .= " \"group\" => \"".rulebasedthemes::normalize_rule_name($parentRule["group"])."\",\n\r";
$serialized .= " \"value\" => \"".rulebasedthemes::normalize_value($parentRule["value"])."\",\n\r";
$serialized .= " \"rules\" => array(\n\r";
foreach ($parentRule["rules"] as $srulekey => $rule){
Expand Down Expand Up @@ -59,7 +60,7 @@ static function serialize($rules){
return $serialized;
}

static function normalize_rule_name($value){ return rulebasedthemes::regexnormalize('/^[a-zA-Z0-9\-]*$/', (string)$value); }
static function normalize_rule_name($value){ return rulebasedthemes::regexnormalize('/^[a-zA-Z0-9\-\s]*$/', (string)$value); }
static function normalize_day_of_week($value) { return rulebasedthemes::regexnormalize('/^[0-7]$/',substr($value,0,1)); }
static function normalize_day_of_month($value) { return rulebasedthemes::regexnormalize('/^[0-9]{1,2}$/',substr($value,0,2)); }
static function normalize_month($value){ return rulebasedthemes::regexnormalize('/^[0-9]{1,2}$/',substr($value,0,2)); }
Expand Down Expand Up @@ -138,10 +139,13 @@ static function get_rule_list(){
global $rbtme_rules;
$ruleGroups = array();
foreach ($rbtme_rules as $parentrulekey => $parentRule){
$ruleOutput = "<li data-value=\"".$parentRule["value"]."\">".($parentRule["display"] == "" ? $parentrulekey:$parentRule["display"])."</li>";
if($ruleGroups[$parentRule["group"]]){
array_push($ruleGroups[$parentRule["group"]], "<li data-value=\"".$parentRule["value"]."\">".$parentrulekey."</li>");
if(!in_array($ruleOutput, $ruleGroups[$parentRule["group"]])) {
array_push($ruleGroups[$parentRule["group"]], $ruleOutput);
}
}else{
$ruleGroups[$parentRule["group"]] = array("<li data-value=\"".$parentRule["value"]."\">".$parentrulekey."</li>");
$ruleGroups[$parentRule["group"]] = array($ruleOutput);
}
}
$rulesOutput = "";
Expand Down

0 comments on commit 3aa1051

Please sign in to comment.