public function prepare(CM_Frontend_Environment $environment, CM_Frontend_ViewResponse $viewResponse) { if (CM_Http_Request_Abstract::hasInstance()) { $ip = CM_Http_Request_Abstract::getInstance()->getIp(); if ($locationGuess = CM_Model_Location::findByIp($ip)) { $this->getField('location')->setValue($locationGuess); } } }
protected function _getSuggestions($term, array $options, CM_Frontend_Render $render) { $ip = CM_Http_Request_Abstract::getInstance()->getIp(); $locations = new CM_Paging_Location_Suggestions($term, $options['levelMin'], $options['levelMax'], CM_Model_Location::findByIp($ip), $options['scopeLocation']); $locations->setPage(1, 15); $out = array(); foreach ($locations as $location) { $out[] = $this->getSuggestion($location, $render); } return $out; }
/** * @return CM_Model_Location|null */ public function getLocation() { $ipAddress = $this->getIp(); if (null === $ipAddress) { return null; } return CM_Model_Location::findByIp($ipAddress); }
public function testFindByIp() { $cityId1 = CM_Db_Db::getRandId('cm_model_location_city', 'id'); CM_Db_Db::insert('cm_model_location_ip', array('id' => $cityId1, 'level' => CM_Model_Location::LEVEL_CITY, 'ipStart' => 1, 'ipEnd' => 5)); $cityId2 = CM_Db_Db::getRandId('cm_model_location_city', 'id'); CM_Db_Db::insert('cm_model_location_ip', array('id' => $cityId2, 'level' => CM_Model_Location::LEVEL_CITY, 'ipStart' => 123456789, 'ipEnd' => 223456789)); $countryId1 = CM_Db_Db::getRandId('cm_model_location_country', 'id'); CM_Db_Db::insert('cm_model_location_ip', array('id' => $countryId1, 'level' => CM_Model_Location::LEVEL_COUNTRY, 'ipStart' => 10, 'ipEnd' => 15)); $countryId2 = CM_Db_Db::getRandId('cm_model_location_country', 'id'); CM_Db_Db::insert('cm_model_location_ip', array('id' => $countryId2, 'level' => CM_Model_Location::LEVEL_COUNTRY, 'ipStart' => 1234567890, 'ipEnd' => 2234567890)); $this->assertEquals(new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $cityId1), CM_Model_Location::findByIp(3)); $this->assertNull(CM_Model_Location::findByIp(6)); $this->assertEquals(new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $cityId2), CM_Model_Location::findByIp(223456700)); $this->assertNull(CM_Model_Location::findByIp(223456800)); $this->assertEquals(new CM_Model_Location(CM_Model_Location::LEVEL_COUNTRY, $countryId1), CM_Model_Location::findByIp(12)); $this->assertNull(CM_Model_Location::findByIp(16)); $this->assertEquals(new CM_Model_Location(CM_Model_Location::LEVEL_COUNTRY, $countryId2), CM_Model_Location::findByIp(2234567870)); $this->assertNull(CM_Model_Location::findByIp(2234567900)); }