Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MINOR Removed MatchingType Option.

  • Loading branch information...
commit be355adbae2bf62b15dca601e0127f46188d605d 1 parent 9e8f5c6
@yukiawano authored
View
1  _config/audiencetype.yml.sample
@@ -1,7 +1,6 @@
# Copy this yaml file to mysite/_config/audiencetype.yml and define your setting of AudienceType
# This file is a sample of defining audience type and if you don't need this, you can delete this file.
AudienceTypeDefinition:
- MatchingRule: InclusiveOR
AudienceTypes:
NewComer:
NewComer: true
View
3  code/CPEnvironment.php
@@ -150,8 +150,7 @@ private function getDefaultLocation() {
*/
public function getAudienceTypes() {
if($this->audienceTypes == null) {
- $audienceTypeLoader = new AudienceTypeLoader();
- $this->audienceTypes = $audienceTypeLoader->load();
+ $this->audienceTypes = Config::inst()->get("AudienceTypeDefinition", "AudienceTypes");
}
return $this->audienceTypes;
}
View
13 code/audiencetype/AudienceTypeLoader.php
@@ -4,25 +4,20 @@
* @package sscp
*/
class AudienceTypeLoader{
- public function load(){
- $audienceTypes = Config::inst()->get("AudienceTypeDefinition", "AudienceTypes");
- $matchingRule = Config::inst()->get("AudienceTypeDefinition", "MatchingRule");
-
- return array($matchingRule => $audienceTypes);
- }
/**
* Return audience types as array(list)
* @param array $audienceTypes
*/
public function getAudienceTypes($audienceTypes) {
- $matchingRule = key($audienceTypes);
- $types = $audienceTypes[$matchingRule];
+ // TODO This function should be moved to proper class.
+
$result = array();
- foreach($types as $audienceTypeName => $conds) {
+ foreach($audienceTypes as $audienceTypeName => $conds) {
//array_push($result, $audienceTypeName);
$result[$audienceTypeName] = $audienceTypeName;
}
return $result;
}
+
}
View
24 code/audiencetype/AudienceTypeManager.php
@@ -4,14 +4,14 @@
* @package sscp
*/
class AudienceTypeManager extends Object{
- public function getAudienceTypes($audienceTypes, CPEnvironment $env, $consideredTypes){
- $matchingRule = key($audienceTypes);
- $rules = $audienceTypes[$matchingRule];
+ public function getAudienceTypes(CPEnvironment $env, $consideredTypes) {
+ $rules = $env->getAudienceTypes();
$results = array();
foreach($rules as $audienceTypeName => $conditions){
if($consideredTypes != null && !in_array($audienceTypeName, $consideredTypes)) {
continue; // When the audience type is not in consideredTypes, then skip.
}
+
$match = true;
foreach($conditions as $conditionClass => $conditionArgs){
$condition = $this->getConditionClass($conditionClass);
@@ -20,12 +20,9 @@ public function getAudienceTypes($audienceTypes, CPEnvironment $env, $considered
$match = false;
}
}
- if($match){
+
+ if($match) {
array_push($results, $audienceTypeName);
- if($matchingRule == 'ExclusiveOR'){
- $env->commit();
- return $results;
- }
}
}
@@ -47,10 +44,8 @@ public function getNearestOptionedLocations($audienceTypes, $consideredAudienceT
}
};
- $matchingRule = key($audienceTypes);
- $rules = $audienceTypes[$matchingRule];
$results = array();
- foreach($rules as $audienceTypeName => $conditions) {
+ foreach($audienceTypes as $audienceTypeName => $conditions) {
if(in_array($audienceTypeName, $consideredAudienceTypes)) {
foreach($conditions as $conditionClass => $conditionArgs) {
if(preg_match('/nearest\((.+)\)/', $conditionArgs, $matches)){
@@ -68,10 +63,8 @@ public function getNearestOptionedLocations($audienceTypes, $consideredAudienceT
* @param array $audienceTypes
*/
public function prettyPrint($audienceTypeArray) {
- $matchingRule = key($audienceTypeArray);
-
$audienceTypes = new ArrayList();
- foreach($audienceTypeArray[$matchingRule] as $audienceTypeName => $conditionsArray) {
+ foreach($audienceTypeArray as $audienceTypeName => $conditionsArray) {
$conditions = new ArrayList();
foreach($conditionsArray as $condition => $args) {
$conditions->push(new ArrayData(array('Name' => $condition, 'Args' => $args)));
@@ -80,8 +73,7 @@ public function prettyPrint($audienceTypeArray) {
'Conditions' => $conditions)));
}
- $result = array( 'MatchingRule' => $matchingRule,
- 'AudienceTypes' => $audienceTypes );
+ $result = array('AudienceTypes' => $audienceTypes );
$template = new SSViewer('PrettyPrint');
return $template->process(new ArrayData($result), array());
}
View
2  code/blockholder/DefaultBlockHolder.php
@@ -70,7 +70,7 @@ public function getRelatedAudienceTypes() {
public function getBlock(CPEnvironment $env) {
$audienceTypeManager = new AudienceTypeManager();
$audienceTypes = $env->getAudienceTypes();
- $currentAudienceTypes = $audienceTypeManager->getAudienceTypes($audienceTypes, $env, $this->getRelatedAudienceTypes());
+ $currentAudienceTypes = $audienceTypeManager->getAudienceTypes($env, $this->getRelatedAudienceTypes());
// Get blocks of this block holder
$blocks = $this->Blocks();
View
2  code/blockholder/SSCP_Block.php
@@ -11,6 +11,8 @@ class SSCP_Block extends DataObject {
'SnippetBase' => 'SnippetBase'
);
+
+
public function getCMSFields() {
$fields = parent::getCMSFields();
View
7 code/controllers/BlockHolderMain.php
@@ -32,6 +32,8 @@ public function init(){
}
function getEditForm($id = null, $fields = null) {
+ $env = CPEnvironment::getCPEnvironment();
+
if($fields == null) $fields = new FieldList();
$fields = new FieldList(
@@ -57,7 +59,7 @@ function getEditForm($id = null, $fields = null) {
// AudienceType Tab
$audienceTypeLoader = new AudienceTypeLoader();
$audienceTypeManager = new AudienceTypeManager();
- $prettyPrintOfAudienceTypes = $audienceTypeManager->prettyPrint($audienceTypeLoader->load());
+ $prettyPrintOfAudienceTypes = $audienceTypeManager->prettyPrint($env->getAudienceTypes());
$audienceTypeTab->push(new LabelField('AudienceTypesTips', 'When you update audience types with the configuration file, you need to access /dev/build/?flush=all.<br /><br />'));
$audienceTypeTab->push(new LabelField('AudienceTypes', $prettyPrintOfAudienceTypes));
@@ -132,7 +134,8 @@ public function getAddBlockForm($id = null, $fields = null) {
// Audience Types
$audienceTypeLoader = new AudienceTypeLoader();
- $audienceTypesArray = $audienceTypeLoader->getAudienceTypes($audienceTypeLoader->load());
+ $env = CPEnvironment::getCPEnvironment();
+ $audienceTypesArray = $audienceTypeLoader->getAudienceTypes($env->getAudienceTypes());
$fields->push(new LabelField('description', "Create a new block to {$blockHolder->Title}."));
$fields->push(new TextField('Title', 'Title'));
View
1  templates/Includes/PrettyPrint.ss
@@ -1,4 +1,3 @@
-<p>MatchingRule: $MatchingRule</p>
<ul>
<% control $AudienceTypes %>
<li>
View
4 tests/CPEnvironmentTest.php
@@ -10,11 +10,11 @@ function setUp(){
}
function testGetNearestLocation() {
- $env = CPEnvironmentStub::getCPEnvironment(array('ExclusiveOR' => array(
+ $env = CPEnvironmentStub::getCPEnvironment(array(
'Osaka' => array('Location' => 'nearest((34.693744,135.502151))'),
'Tokyo' => array('Location' => 'nearest((35.6895,139.691729))'),
'Sapporo' => array('Location' => 'nearest((43.062092,141.354377))')
- )));
+ ));
$result = $env->getNearestLocation(array('Sapporo', 'Tokyo')); // Current location is kyoto, thus Tokyo is the nearest location.
$expected = "35.6895-139.691729";
View
8 tests/audiencetype/AudienceTypeLoaderTest.php
@@ -4,14 +4,15 @@
*
*/
-class AudienceTypeLoaderTest extends SapphireTest{
+class AudienceTypeLoaderTest extends SapphireTest {
+
function testGetAudienceTypes() {
- $data = array("InclusiveOR" => array(
+ $data = array(
"NewComer" => array("NewComer" => true),
"NewYorker" => array(
"Location" => "NewYork",
"Device" => "Linux")
- ));
+ );
$expected = array('NewComer' => 'NewComer', 'NewYorker' => 'NewYorker');
$audienceTypeLoader = new AudienceTypeLoader();
@@ -19,4 +20,5 @@ function testGetAudienceTypes() {
$this->assertEquals($expected, $result);
}
+
}
View
39 tests/audiencetype/AudienceTypeManagerTest.php
@@ -13,59 +13,50 @@ function setUp(){
function testGetAudienceTypesInclusive(){
$audienceTypeManager = new AudienceTypeManager();
- $audienceTypes = array('InclusiveOR' => array(
+ $audienceTypes = array(
'NewComer' => array('NewComer' => 'true'),
'Kyoto' => array('Location' => 'Kyoto', 'Device' => 'Linux')
- ));
- $result = $audienceTypeManager->getAudienceTypes($audienceTypes, CPEnvironmentStub::getCPEnvironment(), array('NewComer', 'Kyoto'));
+ );
+ $env = CPEnvironmentStub::getCPEnvironment($audienceTypes);
+ $result = $audienceTypeManager->getAudienceTypes($env, array('NewComer', 'Kyoto'));
$this->assertEquals(array('NewComer', 'Kyoto'), $result);
}
- function testGetAudienceTypesExclusive(){
- $audienceTypeManager = new AudienceTypeManager();
- $audienceTypes = array('ExclusiveOR' => array(
- 'NewComer' => array('NewComer' => 'true'),
- 'Kyoto' => array('Location' => 'Kyoto', 'Device' => 'Linux')
- ));
- $result = $audienceTypeManager->getAudienceTypes($audienceTypes, CPEnvironmentStub::getCPEnvironment(), array('NewComer', 'Kyoto'));
- $this->assertEquals(array('NewComer'), $result);
- }
-
function testGetAudienceTypeFiltered() {
$audienceTypeManager = new AudienceTypeManager();
- $audienceTypes = array('ExclusiveOR' => array(
+ $audienceTypes = array(
'NewComer' => array('NewComer' => 'true'),
'Kyoto' => array('Location' => 'kyoto'),
'LinuxUser' => array('Device' => 'linux')
- ));
+ );
$env = CPEnvironmentStub::getCPEnvironment($audienceTypes);
- $result = $audienceTypeManager->getAudienceTypes($audienceTypes, $env, array('Kyoto', 'LinuxUser'));
- $this->assertEquals(array('Kyoto'), $result);
+ $expected = array('Kyoto');
+ $result = $audienceTypeManager->getAudienceTypes($env, array('Kyoto'));
+ $this->assertEquals($expected, $result);
}
function testGetNearestOptionedLocations() {
$audienceTypeManager = new AudienceTypeManager();
- $audienceTypes = array('ExclusiveOR' => array(
+ $audienceTypes = array(
'OsakaResidents' => array('Location' => 'nearest((34.693744,135.502151))'),
'ShigaResidents' => array('Location' => 'shiga'),
'KyotoResidents' => array('Location' => 'nearest((35.011642,135.768031))'),
'TokyoResidents' => array('Location' => 'nearest((35.6895,139.691729))'),
- ));
+ );
$expected = array( array('lat' => 34.693744, 'lon' => 135.502151), // Osaka
array('lat' => 35.6895, 'lon' => 139.691729) // Tokyo
);
- $this->assertEquals($expected, $audienceTypeManager->getNearestOptionedLocations($audienceTypes, array('OsakaResidents', 'TokyoResidents')));
+ $result = $audienceTypeManager->getNearestOptionedLocations($audienceTypes, array('OsakaResidents', 'TokyoResidents'));
+ $this->assertEquals($expected, $result);
}
function testPrettyPrint(){
- // TODO Change to use template tracked on https://github.com/yukiawano/sscpmodule/issues/18
- $audienceTypes = array('InclusiveOR' => array(
+ $audienceTypes = array(
'NewComer' => array('NewComer' => 'true'),
'ShigaResidents' => array('Location' => 'SHIGA', 'Device' => 'Linux')
- ));
+ );
$expected = <<<EOT
-<p>MatchingRule: InclusiveOR</p>
<ul>
<li>
View
8 tests/blockholder/DefaultBlockHolderTest.php
@@ -10,11 +10,11 @@ public function testGetRelatedAudienceTypes() {
}
public function testGetContent() {
- $audienceTypes = array('ExclusiveOR' => array(
+ $audienceTypes = array(
'TypeA' => array('Location' => 'Hokkaido'),
'TypeB' => array('Location' => 'Akita'),
'TypeC' => array('NewComer' => 'true'),
- ));
+ );
$env = CPEnvironmentStub::getCPEnvironment($audienceTypes);
$blockHolder = $this->objFromFixture('DefaultBlockHolder', 'blockholdera');
@@ -26,11 +26,11 @@ public function testGetContent() {
}
public function testGetContentButNoBlock() {
- $audienceTypes = array('ExclusiveOR' => array(
+ $audienceTypes = array(
'TypeA' => array('Location' => 'Hokkaido'),
'TypeB' => array('Location' => 'Akita'),
'TypeC' => array('Location' => 'Aomori'),
- ));
+ );
$env = CPEnvironmentStub::getCPEnvironment($audienceTypes);
$blockHolder = $this->objFromFixture('DefaultBlockHolder', 'blockholdera');
View
13 tests/condition/LocationTest.php
@@ -8,11 +8,11 @@ function setUp(){
}
function testMatch() {
- $env = CPEnvironmentStub::getCPEnvironment(array('ExclusiveOR' => array(
+ $env = CPEnvironmentStub::getCPEnvironment(array(
'Osaka' => array('Location' => 'Osaka'),
'Tokyo' => array('Location' => 'Tokyo'),
'Kyoto' => array('Location' => 'Kyoto')
- )));
+ ));
$consideredAudienceTypes = array('Osaka', 'Tokyo', 'Kyoto');
$cond = new Location();
$this->assertEquals(true, $cond->doesSatisfy($env, 'Kyoto', $consideredAudienceTypes));
@@ -24,8 +24,9 @@ function testRadiusInOption() {
// Kyoto University(35.028388,135.780621) is in 100km from osaka station(34.702781,135.494723).
// Tokyo University(35.714957,139.762049) is not in 100km from osaka station.
- $env = CPEnvironmentStub::getCPEnvironment(array('ExclusiveOR' => array(
- 'Osaka' => array('Location' => 'nearest(osaka)'))));
+ $env = CPEnvironmentStub::getCPEnvironment(array(
+ 'Osaka' => array('Location' => 'nearest(osaka)')
+ ));
$env->setLocation(array('lat' => 34.702781, 'lon' => 135.494723));
$consideredAudienceTypes = array('Osaka');
@@ -40,10 +41,10 @@ function testNearestOptionedLocationWithLatLon() {
// Latitude and longitude of Tokyo University
// 35.713207,139.762659
- $env = CPEnvironmentStub::getCPEnvironment(array('ExclusiveOR' => array(
+ $env = CPEnvironmentStub::getCPEnvironment(array(
'StudentOfKyotoUniv' => array('Location' => 'nearest((35.028872,135.780673))'),
'StudentOfTokyoUniv' => array('Location' => 'nearest((35.713207,139.762659))')
- )));
+ ));
$consideredAudienceTypes = array('StudentOfKyotoUniv', 'StudentOfTokyoUniv');
$cond = new Location();
$this->assertEquals(true, $cond->doesSatisfy($env, 'nearest((35.028872,135.780673))', $consideredAudienceTypes));
Please sign in to comment.
Something went wrong with that request. Please try again.