Skip to content

Commit

Permalink
Поиск по типу населенного пункта
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliya Garakh committed Apr 17, 2014
1 parent 858b86f commit d784c2a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
9 changes: 8 additions & 1 deletion apps/core/models/Cities.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ public static function getCodes($id)
* @param string $name Название объекта
* @param array $codes Коды родительского объекта
* @param int $limit Максимальное количество возвращаемых объектов
* @param int $offset Сдвиг
* @param array $typeCodes Массив TypeCode для фильтрации
* @return array
*/
public static function findByQuery($name = null, $codes = array(), $limit = 5000, $offset = 0)
public static function findByQuery($name = null, $codes = array(), $limit = 5000, $offset = 0, $typeCodes = null)
{
$arQuery = array();
$isEmptyQuery = true;
Expand Down Expand Up @@ -123,6 +125,11 @@ public static function findByQuery($name = null, $codes = array(), $limit = 5000
$arQuery['conditions'][KladrFields::Bad] = false;
}

if($typeCodes != null)
{
$arQuery['conditions'][KladrFields::TypeCode] = array('$in' => $typeCodes);
}

$arQuery['sort'] = array(KladrFields::Sort => 1);

$arQuery['skip'] = $offset;
Expand Down
41 changes: 30 additions & 11 deletions apps/core/plugins/general/AllDataPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function process(Request $request, PluginResult $prevResult)

switch ($request->getQuery('param1'))
{
case self::PARAM_CITIES : $this->processCities($prevResult);
case self::PARAM_CITIES : $this->processCities($request->getQuery('typeCode'), $prevResult);
break;
case self::PARAM_STREETS : $this->processStreets($request->getQuery('param2'), $prevResult);
break;
Expand Down Expand Up @@ -113,26 +113,25 @@ private function processStreets($cityId, PluginResult $result)
$mongo = $cities->getConnection();
$city = $mongo->cities->findOne(array('Id' => $cityId));

$tmp = $this->getCachePath($cacheKey).'_'.rand(10000, 10000000);
$tmp = $this->getCachePath($cacheKey) . '_' . rand(10000, 10000000);
$fp = fopen($tmp, 'w');
fputcsv($fp, $this->streetToArray());

$streets = $mongo->streets->find(
array(
'Bad' => false,
'CodeCity' => $city['CodeCity'],
'CodeRegion' => (int)$city['CodeRegion']));
'Bad' => false,
'CodeCity' => $city['CodeCity'],
'CodeRegion' => (int) $city['CodeRegion']));

foreach ($streets as $street)
{
fputcsv($fp, $this->streetToArray($street));
}

fclose($fp);

copy($tmp, $this->getCachePath($cacheKey));
unlink($tmp);

}

$result->fileToSend = $this->getCachePath($cacheKey);
Expand All @@ -141,22 +140,39 @@ private function processStreets($cityId, PluginResult $result)
/**
* Собирает города
*
* @param string $typeCode
* @param \Kladr\Core\Plugins\Base\PluginResult $result
*/
private function processCities(PluginResult $result)
private function processCities($typeCode, PluginResult $result)
{
set_time_limit(600);
ini_set('max_execution_time', 600);

$cacheKey = 'all_cities';
$typeCodes = FindPlugin::ConvertCodeTypeToArray($typeCode);

if ($typeCodes != null)
foreach ($typeCodes as $code)
$cacheKey .= '_' . $code;


if (!$this->checkCache($cacheKey))
{
$cities = new Cities();
$mongo = $cities->getConnection();
$fp = fopen($this->getCachePath($cacheKey), 'w');

$tmp = $this->getCachePath($cacheKey) . '_' . rand(10000, 10000000);
$fp = fopen($tmp, 'w');

fputcsv($fp, $this->cityToArray());
foreach ($mongo->cities->find(array('Bad' => false)) as $city)

$cities = $typeCodes == null ?
$mongo->cities->find(array('Bad' => false)) :
$mongo->cities->find(array(
'Bad' => false,
'TypeCode' => array('$in' => $typeCodes)));

foreach ($cities as $city)
{
$districtCode = $city['CodeDistrict'];
$regionCode = $city['CodeRegion'];
Expand All @@ -179,6 +195,9 @@ private function processCities(PluginResult $result)
}

fclose($fp);

copy($tmp, $this->getCachePath($cacheKey));
unlink($tmp);
}

$result->fileToSend = $this->getCachePath($cacheKey);
Expand Down
31 changes: 27 additions & 4 deletions apps/core/plugins/general/FindPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public function process(Request $request, PluginResult $prevResult)
{
return $prevResult;
}

$objects = $this->cache->get('FindPlugin', $request);

$objects = null;
//$objects = $this->cache->get('FindPlugin', $request);

if ($objects === null)
{
Expand Down Expand Up @@ -108,7 +109,7 @@ public function process(Request $request, PluginResult $prevResult)
$offset = $request->getQuery('offset');
$offset = intval($offset);


$typeCodes = self::ConvertCodeTypeToArray($request->getQuery('typeCode'));

switch ($request->getQuery('contentType'))
{
Expand All @@ -119,7 +120,7 @@ public function process(Request $request, PluginResult $prevResult)
$objects = Districts::findByQuery($query, $arCodes, $limit, $offset);
break;
case Cities::ContentType:
$objects = Cities::findByQuery($query, $arCodes, $limit, $offset);
$objects = Cities::findByQuery($query, $arCodes, $limit, $offset, $typeCodes);
break;
case Streets::ContentType:
$objects = Streets::findByQuery($query, $arCodes, $limit, $offset);
Expand All @@ -138,6 +139,28 @@ public function process(Request $request, PluginResult $prevResult)
return $result;
}

/**
* Преобразует TypeCode в массив для поиска
*
* @param int $typeCode
* @return array | null Массив из TypeCode или null, если TypeCode учитывать не надо
*/
public static function ConvertCodeTypeToArray($typeCode)
{
$typeCode = (int)$typeCode;

// проверяем валидность. typeCode = 7 так же не нужен, т.к. это 0111, т.е. все варианты
if ($typeCode <= 0 || $typeCode > 6)
return null;


$result = array();
foreach (array(1, 2, 4) as $code)
if( ($typeCode & $code) > 0)
$result []= $code;

return $result;
}
}

}
1 change: 1 addition & 0 deletions apps/core/plugins/tools/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private function getCacheKey($plugin, Request $request)
$key .= 'withParent=' . $request->getQuery('withParent');
$key .= 'limit=' . $request->getQuery('limit');
$key .= 'offset=' . $request->getQuery('offset');
$key .= 'typeCode=' . $request->getQuery('typeCode');

return sha1($key);
}
Expand Down

0 comments on commit d784c2a

Please sign in to comment.