浏览代码

geocoding behavior unit

euromark 13 年之前
父节点
当前提交
d514b5b1f9

+ 15 - 17
Lib/MyHelper.php

@@ -100,41 +100,39 @@ class MyHelper extends Helper {
 			$this->tags['time'] = '<time%s>%s</time>';
 		}
 		$options = array_merge(array(
-		'datetime' => '%Y-%m-%d %T',
-		'pubdate' => false,
+			'datetime' => '%Y-%m-%d %T',
+			'pubdate' => false,
 			'format' => '%Y-%m-%d %T',
 		), $options);
 
 		if ($options['format'] !== null) {
-		App::import('helper', 'Time');
-		$t = new TimeHelper(new View(null));
+			App::uses('TimeHelper', 'View/Helper');
+			$TimeHelper = new TimeHelper($this->_View);
 		}
 		if ($options['format']) {
 			if (method_exists($t, $options['format'])) {
-				$content = $t->$options['format']($content);
-		} else {
-			$content = $t->i18nFormat($content, $options['format']);
-		}
-		$options['datetime'] = $t->i18nFormat(strtotime($content), $options['datetime']);
+				$content = $TimeHelper->$options['format']($content);
+			} else {
+				$content = $TimeHelper->i18nFormat($content, $options['format']);
+			}
+			$options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
 		} elseif ($options['format'] === false && $options['datetime']) {
-			$options['datetime'] = $t->i18nFormat(strtotime($content), $options['datetime']);
+			$options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
 		}
 
-		if ($options['pubdate'])
+		if ($options['pubdate']) {
 			$pubdate = true;
-
+		}
 		unset($options['format']);
 		unset($options['pubdate']);
 		$attributes = $this->_parseAttributes($options, array(0), ' ', '');
 
-		if (isset($pubdate))
+		if (isset($pubdate)) {
 			$attributes .= ' pubdate';
-
-		return sprintf($this->tags['time'],  $attributes, $content);
+		}
+		return sprintf($this->tags['time'], $attributes, $content);
 	}
 
-
-
 	# for convienience function Html::defaultLink()
 	protected $linkDefaults = null;
 

+ 12 - 2
Model/Behavior/GeocoderBehavior.php

@@ -44,7 +44,7 @@ class GeocoderBehavior extends ModelBehavior {
 			'require' => false, 'allowEmpty' => true, 'invalidate' => array(), 'expect' => array(),
 			'lat' => 'lat', 'lng' => 'lng', 'formatted_address' => 'formatted_address', 'host' => 'de', 'language' => 'de', 'region' => '', 'bounds' => '',
 			'overwrite' => false, 'update' => array(), 'before' => 'save',
-			'min_accuracy' => 0, 'allow_inconclusive' => true,
+			'min_accuracy' => 0, 'allow_inconclusive' => true, 'unit' => GeocodeLib::UNIT_KM,
 			'log' => true, // log successfull results to geocode.log (errors will be logged to error.log in either case)
 		);
 
@@ -233,7 +233,10 @@ class GeocoderBehavior extends ModelBehavior {
 		if ($modelName === null) {
 			$modelName = $Model->alias;
 		}
-		return '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - '.$modelName.'.'.$fieldLat.')) * ' .
+
+		$value = $this->_calculationValue($this->settings[$Model->alias]['unit']);
+
+		return $value . ' * ACOS( COS( PI()/2 - RADIANS(90 - '.$modelName.'.'.$fieldLat.')) * ' .
 			'COS( PI()/2 - RADIANS(90 - '. $lat .')) * ' .
 			'COS( RADIANS('.$modelName.'.'.$fieldLat.') - RADIANS('. $lng .')) + ' .
 			'SIN( PI()/2 - RADIANS(90 - '.$modelName.'.'.$fieldLng.')) * ' .
@@ -375,4 +378,11 @@ class GeocoderBehavior extends ModelBehavior {
 		return $res;
 	}
 
+	protected function _calculationValue($unit) {
+		if (!isset($this->Geocode)) {
+			$this->Geocode = new GeocodeLib();
+		}
+		return $this->Geocode->convert(6371.04, GeocodeLib::UNIT_KM, $unit);
+	}
+
 }

+ 32 - 21
Model/Behavior/LogableBehavior.php

@@ -74,10 +74,11 @@ class LogableBehavior extends ModelBehavior {
 
 	protected $_defaults = array(
 		'enabled' => true,
+		'on' => 'save', // validate/save
 		'userModel' => CLASS_USER,
 		'userKey' => 'user_id',
 		'change' => 'list',
-		'description_ids' => TRUE,
+		'descriptionIds' => true,
 		'skip' => array(),
 		'ignore' => array(),
 		'classField' => 'model',
@@ -86,13 +87,13 @@ class LogableBehavior extends ModelBehavior {
 	);
 
 	/**
-	 * Cake called intializer
 	 * Config options are :
-	 *    userModel 		: 'User'. Class name of the user model you want to use (User by default), if you want to save User in log
-	 *    userKey   		: 'user_id'. The field for saving the user to (user_id by default).
-	 * 	  change    		: 'list' > [name, age]. Set to 'full' for [name (alek) => (Alek), age (28) => (29)]
-	 * 	  description_ids 	: TRUE. Set to false to not include model id and user id in the title field
-	 *    skip  			: array(). String array of actions to not log
+	 * - userModel 		: 'User'. Class name of the user model you want to use (User by default), if you want to save User in log
+	 * - userKey   		: 'user_id'. The field for saving the user to (user_id by default).
+	 * - change    		: 'list' > [name, age]. Set to 'full' for [name (alek) => (Alek), age (28) => (29)]
+	 * - descriptionIds 	: TRUE. Set to false to not include model id and user id in the title field
+	 * - skip: array(). String array of actions to not log
+	 * - ignore: array(). Fields to ignore
 	 *
 	 * @param Object $Model
 	 * @param array $config
@@ -110,13 +111,6 @@ class LogableBehavior extends ModelBehavior {
 		} else {
 			$this->UserModel = $Model;
 		}
-
-		# session in the model available? use it!
-		/*
-		if (isset($Model->Session)) {
-			$this->setUserData($Model, $Model->Session->read('Auth'));
-		}
-		*/
 	}
 
 	public function settings(Model $Model) {
@@ -274,6 +268,7 @@ class LogableBehavior extends ModelBehavior {
 		}
 		return $result;
 	}
+
 	/**
 	 * Use this to supply a model with the data of the logged in User.
 	 * Intended to be called in AppController::beforeFilter like this :
@@ -340,7 +335,6 @@ class LogableBehavior extends ModelBehavior {
 		$this->userIP = $userIP;
 	}
 
-
 	public function beforeDelete(Model $Model) {
 		$this->setUserData($Model);
 		if (!$this->settings[$Model->alias]['enabled']) {
@@ -367,7 +361,7 @@ class LogableBehavior extends ModelBehavior {
 			if (isset($Model->data[$Model->alias][$Model->displayField]) && $Model->displayField != $Model->primaryKey) {
 				$logData['Log']['description'] .= ' "' . $Model->data[$Model->alias][$Model->displayField] . '"';
 			}
-			if ($this->settings[$Model->alias]['description_ids']) {
+			if ($this->settings[$Model->alias]['descriptionIds']) {
 				$logData['Log']['description'] .= ' (' . $Model->id . ') ';
 			}
 			$logData['Log']['description'] .= __('deleted');
@@ -376,14 +370,29 @@ class LogableBehavior extends ModelBehavior {
 		$this->_saveLog($Model, $logData);
 	}
 
+	public function beforeValidate(Model $Model) {
+		if (!$this->settings[$Model->alias]['enabled'] || $this->settings[$Model->alias]['on'] != 'validate') {
+			return true;
+		}
+		$this->_prepareLog($Model);
+		return true;
+	}
+
 	public function beforeSave(Model $Model) {
+		if (!$this->settings[$Model->alias]['enabled'] || $this->settings[$Model->alias]['on'] != 'save') {
+			return true;
+		}
+		$this->_prepareLog($Model);
+		return true;
+	}
+
+	protected function _prepareLog(Model $Model) {
 		if ($this->user === null) {
 			$this->setUserData($Model);
 		}
-		if ($this->Log->hasField('change') && $Model->id) {
+		if ($Model->id && empty($this->old)) {
 			$this->old = $Model->find('first', array('conditions' => array($Model->primaryKey => $Model->id), 'recursive' => -1));
 		}
-		return true;
 	}
 
 	public function afterSave(Model $Model, $created) {
@@ -414,7 +423,7 @@ class LogableBehavior extends ModelBehavior {
 				$logData['Log']['description'] .= '"' . $Model->data[$Model->alias][$Model->displayField] . '" ';
 			}
 
-			if ($this->settings[$Model->alias]['description_ids']) {
+			if ($this->settings[$Model->alias]['descriptionIds']) {
 				$logData['Log']['description'] .= '(' . $id . ') ';
 			}
 
@@ -452,7 +461,7 @@ class LogableBehavior extends ModelBehavior {
 				}
 			}
 			$changes = count($changed_fields);
-			if ($changes == 0) {
+			if (!$changes) {
 				return true;
 			}
 			if ($this->settings[$Model->alias]['change'] == 'serialize') {
@@ -479,6 +488,7 @@ class LogableBehavior extends ModelBehavior {
 	 *
 	 * @param Object $Model
 	 * @param array $logData
+	 * @return void
 	 */
 	public function _saveLog(Model $Model, $logData, $title = null) {
 		if ($title !== null) {
@@ -532,7 +542,7 @@ class LogableBehavior extends ModelBehavior {
 			}
 			if ($this->user && $this->UserModel && isset($this->user[$this->UserModel->alias])) {
 				$logData['Log']['description'] .= ' ' . __('by') . ' ' . $this->settings[$Model->alias]['userModel'] . ' "' . $this->user[$this->UserModel->alias][$this->UserModel->displayField] . '"';
-				if ($this->settings[$Model->alias]['description_ids']) {
+				if ($this->settings[$Model->alias]['descriptionIds']) {
 					$logData['Log']['description'] .= ' (' . $this->user[$this->UserModel->alias][$this->UserModel->primaryKey] . ')';
 				}
 
@@ -545,4 +555,5 @@ class LogableBehavior extends ModelBehavior {
 		$this->Log->create($logData);
 		$this->Log->save(null, false);
 	}
+
 }

+ 12 - 0
Test/Case/Model/Behavior/GeocoderBehaviorTest.php

@@ -42,6 +42,18 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$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']);
+		$this->assertTrue($res[0]['Address']['distance'] > 460 && $res[0]['Address']['distance'] < 490);
+	}
+
+	public function testSetDistanceAsVirtualFieldInMiles() {
+		$this->Address = ClassRegistry::init('Address');
+		$this->Address->Behaviors->attach('Tools.Geocoder', array('unit' => GeocodeLib::UNIT_MILES));
+		$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']);
+		$this->assertTrue($res[0]['Address']['distance'] > 270 && $res[0]['Address']['distance'] < 310);
 	}
 
 	public function testPagination() {