Exemple #1
0
 /**
  * Собирает города
  * 
  * @param string $typeCode
  * @param string $format CSV или JSON формат
  * @param \Kladr\Core\Plugins\Base\PluginResult $result
  */
 private function processCities($typeCode, $format, PluginResult $result)
 {
     set_time_limit(600);
     ini_set('max_execution_time', 600);
     ini_set('memory_limit', '600M');
     $format = $format == self::FORMAT_JSON ? self::FORMAT_JSON : self::FORMAT_CSV;
     $cacheKey = 'all_cities';
     $typeCodes = FindPlugin::ConvertCodeTypeToArray($typeCode);
     if ($typeCodes != null) {
         foreach ($typeCodes as $code) {
             $cacheKey .= '_' . $code;
         }
     }
     $cacheKey .= $format == self::FORMAT_JSON ? '_json' : '';
     if (!$this->checkCache($cacheKey)) {
         $cities = new Cities();
         $mongo = $cities->getConnection();
         $tmp = $this->getCachePath($cacheKey) . '_' . rand(10000, 10000000);
         $cities = $typeCodes == null ? $mongo->cities->find(array('Bad' => false)) : $mongo->cities->find(array('Bad' => false, 'TypeCode' => array('$in' => $typeCodes)));
         $fp = fopen($tmp, 'w');
         if ($format == self::FORMAT_CSV) {
             fputcsv($fp, $this->cityToArray());
             foreach ($cities as $city) {
                 $districtCode = $city['CodeDistrict'];
                 $regionCode = $city['CodeRegion'];
                 $district = null;
                 $region = null;
                 if ($districtCode != null) {
                     $district = $mongo->district->findOne(array('CodeDistrict' => (int) $districtCode, 'CodeRegion' => (int) $regionCode, 'Bad' => false));
                 }
                 $region = $mongo->regions->findOne(array('CodeRegion' => (int) $regionCode, 'Bad' => false));
                 fputcsv($fp, $this->cityToArray($city, $district, $region));
             }
         } else {
             fwrite($fp, '{ "result" : [');
             $first = true;
             foreach ($cities as $city) {
                 if ($first) {
                     $first = false;
                 } else {
                     fwrite($fp, ',' . PHP_EOL);
                 }
                 $districtCode = $city['CodeDistrict'];
                 $regionCode = $city['CodeRegion'];
                 $district = null;
                 $region = null;
                 if ($districtCode != null) {
                     $district = $mongo->district->findOne(array('CodeDistrict' => (int) $districtCode, 'CodeRegion' => (int) $regionCode, 'Bad' => false));
                 }
                 $region = $mongo->regions->findOne(array('CodeRegion' => (int) $regionCode, 'Bad' => false));
                 fwrite($fp, $this->cityToJson($city, $district, $region));
             }
             fwrite($fp, ']}');
         }
         fclose($fp);
         copy($tmp, $this->getCachePath($cacheKey));
         unlink($tmp);
     }
     $result->fileToSend = $this->getCachePath($cacheKey);
 }