Exemple #1
0
 public function actionBindCats()
 {
     \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $request = \Yii::$app->request->post();
     $dgis_cat = Category2gis::findOne($request['dgis_cat']);
     if (!$dgis_cat) {
         return ['error' => 'Error! 2gis category not found!'];
     }
     $binded_cats_all = $dgis_cat->cats2dis ? explode(',', $dgis_cat->cats2dis) : [];
     $binded_cats = Category2dis::findAll(['parentid' => $request['ddis_root'], 'catid' => explode(',', $dgis_cat->cats2dis)]);
     $binded_cats_ids = [];
     foreach ($binded_cats as $cat) {
         $binded_cats_ids[] = $cat->catid;
     }
     $result_binded_cats_id = array_diff($binded_cats_all, $binded_cats_ids);
     $need_to_bind = isset($request['cats']) ? $request['cats'] : [];
     $result = array_merge($result_binded_cats_id, $need_to_bind);
     $dgis_cat->cats2dis = count($result) ? implode(',', $result) : null;
     $dgis_cat->save();
     return ArrayHelper::map(Category2dis::findAll(['catid' => $result]), 'catid', 'catname');
 }
Exemple #2
0
 private function stealRubric($rubric, $region)
 {
     $page_size = 50;
     $pages = ceil($rubric->branch_count / $page_size);
     // get branches count (rubric contains all branches)
     $this->_report("\tзаявлено фирм: {$rubric->branch_count}");
     $stealed_branches = 0;
     $records_count = 0;
     $keyword = "";
     $binded_cat_ids = $this->getBindedCatsIdArray($rubric->alias);
     if (!$binded_cat_ids) {
         return false;
     }
     foreach (Category2dis::find()->select('catname')->where(['catid' => $binded_cat_ids])->all() as $cat) {
         $keyword .= ', ' . $cat->catname;
     }
     for ($page = 1; $page <= $pages; $page++) {
         $branches = $this->dgApiRequest('catalog/branch/list', ['page' => $page, 'page_size' => $page_size, 'rubric_id' => $rubric->id, 'region_id' => $region->id, 'fields' => "items.org,items.adm_div,\n\t\t\t\t\t\t\titems.contact_groups,\n\t\t\t\t\t\t\titems.address,items.point,\n\t\t\t\t\t\t\titems.schedule,\n\t\t\t\t\t\t\titems.ads.options"]);
         if (!$branches) {
             continue;
         }
         foreach ($branches->items as $branch) {
             $stealed_now = $this->stealBranch($branch, $binded_cat_ids, $keyword);
             $records_count += $stealed_now;
             $stealed_branches += $stealed_now ? 1 : 0;
         }
     }
     $this->_report("\tимпортировано фирм: {$stealed_branches}, записей создано: {$records_count}");
 }