Beispiel #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = DomainPattern::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'domain_id' => $this->domain_id, 'model_id' => $this->model_id, 'type_id' => $this->type_id]);
     $query->andFilterWhere(['like', 'value', $this->value]);
     return $dataProvider;
 }
Beispiel #2
0
 /**
  * Finds the DomainPattern model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return DomainPattern the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = DomainPattern::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDomainPatterns()
 {
     return $this->hasMany(DomainPattern::className(), ['type_id' => 'id']);
 }
Beispiel #4
0
 public function actionGenerate($name, $alias = '')
 {
     // командер
     $commander = new Commander();
     // проверяем домен
     $domain = Domain::find()->where(['name' => $name])->with('language')->one();
     if (!$domain) {
         $this->stdout('No domain:' . $name . PHP_EOL, Console::FG_RED);
         return Controller::EXIT_CODE_ERROR;
     }
     // паттерны и модели
     $models = DomainPattern::find()->select(['model_id']);
     // todo только указаные модели, разделённые слешем
     if ($alias) {
         $alias = explode("/", $alias);
         $_models = PatternModel::find()->select('id')->where(['alias' => $alias])->column();
         $models = $models->where(['model_id' => $_models]);
     }
     $models = $models->groupBy('model_id')->column();
     $result = [];
     $addiction = [];
     foreach ($models as $model) {
         $patterns = DomainPattern::find()->where(['domain_id' => $domain->id, 'model_id' => $model])->asArray()->with(['domain', 'domain.language', 'domain.group', 'model', 'model.addiction', 'type'])->orderBy('model_id')->all();
         // зависимости
         foreach ($patterns as $pattern) {
             $result[$pattern['model']['alias']][$pattern['type']['alias']] = $pattern['value'];
             if (!empty($pattern['model']['addiction'])) {
                 $addiction[$pattern['model']['alias']] = strtolower($pattern['model']['addiction']['alias']);
             }
         }
     }
     // читаем модели
     foreach ($result as $model => $patterns) {
         $this->stdout('Delete ' . $model . PHP_EOL, Console::FG_GREEN);
         DomainGeneration::deleteAll(['domain' => $name, 'model' => $model]);
         $this->stdout('Generation ' . $model . PHP_EOL, Console::FG_GREEN);
         $_addiction = [];
         if (!empty($addiction[$model])) {
             if ($model == 'Direction') {
                 $_addiction = ['cityFrom', 'cityTo', 'cityFrom.country', 'cityTo.country', 'iataCode'];
             } elseif ($addiction[$model] != 'route') {
                 $_addiction = $addiction[$model];
             }
         }
         /** @var ActiveRecord $_model */
         $_model = 'common\\models\\' . $model;
         $elements = $_model::find()->asArray();
         $elements = $elements->with($_addiction);
         if ($model == 'Direction') {
             $elements = $elements->groupBy(['city_from_code', 'city_to_code']);
         }
         $elements = $elements->all();
         // проходим по записям
         $_count = count($elements);
         Console::startProgress(0, $_count);
         $count = 0;
         $count_ = 0;
         foreach ($elements as $element) {
             //                print_r($element);die();
             // генерим паттерны
             $commander->setMember($element);
             $pattern_result = [];
             foreach ($patterns as $alias => $pattern) {
                 $cmd = $pattern;
                 $cmd = $commander->getProperty($cmd);
                 $cmd = $commander->getFunction($cmd);
                 $cmd = $commander->setProperty($cmd);
                 $pattern_result[$alias] = $cmd;
             }
             $commander->clearMember();
             //                print_r($pattern_result);
             //                die();
             // todo сохраняем паттерны в базу
             $generation = DomainGeneration::find()->select('id')->where(['hash' => md5($domain->name . '/' . $pattern_result['url'])])->asArray()->one();
             //                $generation = true;
             if (!$generation) {
                 $generation = new DomainGeneration();
                 $generation->domain = $domain->name;
                 $generation->url = $pattern_result['url'];
                 $generation->parent_url = $pattern_result['parent_url'];
                 $generation->hash = md5($domain->name . '/' . $pattern_result['url']);
                 $generation->hash_ = md5($domain->name . '/' . $pattern_result['url']);
                 $generation->model = $model;
                 $generation->anchor = $pattern_result['anchor'];
                 unset($pattern_result['url']);
                 unset($pattern_result['parent_url']);
                 unset($pattern_result['anchor']);
                 unset($pattern_result['hash']);
                 unset($pattern_result['model']);
                 $generation->data = json_encode($pattern_result);
                 if (!$generation->save()) {
                     print_r($generation->errors);
                     return Controller::EXIT_CODE_ERROR;
                 }
             }
             $count++;
             Console::updateProgress(++$count_, $_count);
             //}
             // todo собираем ссылки
             // todo генерить redis
             // todo создавать сайтмэп
             //                die();
         }
         Console::endProgress();
     }
     return Controller::EXIT_CODE_NORMAL;
 }