ソースを参照

SetDistanceAsVirtualField

euromark 13 年 前
コミット
0bc8a7ab9d

+ 1 - 0
Console/Command/FolderSyncShell.php

@@ -5,6 +5,7 @@ App::uses('AppShell', 'Console/Command');
 
 /**
  * Folder Sync Shell to update all files from location a with new files from location b
+ * You can also remove files in a if they are not longer existent in b
  *
  * @version 1.0
  * @cakephp 2.x

+ 18 - 5
Model/Behavior/GeocoderBehavior.php

@@ -206,10 +206,18 @@ class GeocoderBehavior extends ModelBehavior {
 		return $return;
 	}
 
+	/**
+	 * Add the distance to this point as a virtual field
+	 *
+	 * @return void
+	 */
 	public function setDistanceAsVirtualField(Model $Model, $lat, $lng, $modelName = null) {
-		$Model->virtualFields['distance'] = $this->distance($Model, $lat, $lng, $modelName);
+		$Model->virtualFields['distance'] = $this->distance($Model, $lat, $lng, null, null, $modelName);
 	}
 
+	/**
+	 * @return array
+	 */
 	public function distanceConditions(Model $Model, $distance = null, $fieldName = null, $modelName = null) {
 		if ($modelName === null) {
 			$modelName = $Model->alias;
@@ -225,15 +233,20 @@ class GeocoderBehavior extends ModelBehavior {
 		return $conditions;
 	}
 
+	/**
+	 * @return string
+	 */
 	public function distanceField(Model $Model, $lat, $lng, $fieldName = null, $modelName = null) {
 		if ($modelName === null) {
 			$modelName = $Model->alias;
 		}
-		return $this->distance($Model, $lat, $lng, $modelName).
-			' '.
-			'AS '.(!empty($fieldName) ? $fieldName : 'distance');
+		$fieldName = (!empty($fieldName) ? $fieldName : 'distance');
+		return $this->distance($Model, $lat, $lng, null, null, $modelName) . ' AS '.$modelName.'.'.$fieldName;
 	}
 
+	/**
+	 * @return string
+	 */
 	public function distance(Model $Model, $lat, $lng, $fieldLat = null, $fieldLng = null, $modelName = null) {
 		if ($fieldLat === null) {
 			$fieldLat = $this->settings[$Model->alias]['lat'];
@@ -262,7 +275,7 @@ class GeocoderBehavior extends ModelBehavior {
 			$byFieldName = 'radius';
 		}
 
-		return $this->distance($Model, $lat, $lng, $modelName).' '.$byFieldName;
+		return $this->distance($Model, $lat, $lng, null, null, $modelName).' '.$byFieldName;
 	}
 
 	public function paginateDistanceCount(Model $Model, $conditions = null, $recursive = -1, $extra = array()) {

+ 23 - 7
Test/Case/Model/Behavior/GeocoderBehaviorTest.php

@@ -6,7 +6,7 @@ App::uses('AppModel', 'Model');
 class GeocoderBehaviorTest extends CakeTestCase {
 
 	public $fixtures = array(
-		'core.comment'
+		'core.comment', 'plugin.tools.address'
 	);
 
 
@@ -56,7 +56,7 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		echo '<h3>'.__FUNCTION__.'</h3>';
 
 		$this->Comment->Behaviors->detach('Geocoder');
-		$this->Comment->Behaviors->attach('Geocoder', array('real'=>false, 'min_accuracy'=>0));
+		$this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>0));
 		// accuracy = 1
 		$data = array(
 	 		//'street' => 'Leopoldstraße',
@@ -76,7 +76,7 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		echo '<h3>'.__FUNCTION__.'</h3>';
 
 		$this->Comment->Behaviors->detach('Geocoder');
-		$this->Comment->Behaviors->attach('Geocoder', array('real'=>false, 'min_accuracy'=>4));
+		$this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>4));
 		// accuracy = 1
 		$data = array(
 	 		//'street' => 'Leopoldstraße',
@@ -97,7 +97,7 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		echo '<h3>'.__FUNCTION__.'</h3>';
 
 		$this->Comment->Behaviors->detach('Geocoder');
-		$this->Comment->Behaviors->attach('Geocoder', array('real'=>false, 'min_accuracy'=>GeocodeLib::ACC_SUBLOC));
+		$this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>GeocodeLib::ACC_SUBLOC));
 
 		$this->assertEquals(GeocodeLib::ACC_SUBLOC, $this->Comment->Behaviors->Geocoder->settings['Comment']['min_accuracy']);
 
@@ -122,7 +122,7 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		echo '<h3>'.__FUNCTION__.'</h3>';
 
 		$this->Comment->Behaviors->detach('Geocoder');
-		$this->Comment->Behaviors->attach('Geocoder', array('real'=>false, 'allow_inconclusive'=>true));
+		$this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'allow_inconclusive'=>true));
 		// accuracy = 1
 		$data = array(
 	 		//'street' => 'Leopoldstraße',
@@ -144,7 +144,7 @@ class GeocoderBehaviorTest extends CakeTestCase {
 
 	public function testExpect() {
 		$this->Comment->Behaviors->detach('Geocoder');
-		$this->Comment->Behaviors->attach('Geocoder', array('real'=>false, 'expect'=>array('postal_code')));
+		$this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'expect'=>array('postal_code')));
 		// accuracy = 1
 		$data = array(
 	 		//'street' => 'Leopoldstraße',
@@ -177,12 +177,28 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertEquals($expected, $res);
 
 		$this->Comment->Behaviors->detach('Geocoder');
-		$this->Comment->Behaviors->attach('Geocoder', array('lat'=>'x', 'lng'=>'y'));
+		$this->Comment->Behaviors->attach('Tools.Geocoder', array('lat'=>'x', 'lng'=>'y'));
 		$res = $this->Comment->distance(12, 14);
 		$expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.x)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.x) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.y)) * SIN( PI()/2 - RADIANS(90 - 12)))';
 		$this->assertEquals($expected, $res);
 	}
 
+	public function testDistanceField() {
+		$res = $this->Comment->distanceField(12, 14);
+		$expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12))) AS Comment.distance';
+		$this->assertEquals($expected, $res);
+	}
+
+	public function testSetDistanceAsVirtualField() {
+		$this->Address = ClassRegistry::init('Address');
+		$this->Address->Behaviors->attach('Tools.Geocoder');
+		$this->Address->setDistanceAsVirtualField(13.3, 19.2);
+		$options = array('order' => array('Address.distance' => 'ASC'));
+		$res = $this->Address->find('all', $options);
+		$this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
+		$this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
+	}
+
 }