/** * Возвращает новые элементы * * @return array * [[ * 'name' * 'url' * ],...] */ public function getNewItems() { $class_name = get_called_class(); $items = $this->getItems(); $dbItems = Investigator::query(['class_name' => $class_name])->andWhere(['in', 'status', [1, 2]])->select('url')->column(); $ret = []; foreach ($items as $item) { if (!in_array($item['url'], $dbItems)) { $ret[] = $item; } } return $ret; }
public function actionIndex() { if (count(Yii::$app->request->post()) > 0) { $formValues = Yii::$app->request->post((new Investigator())->formName()); $add = []; $skip = []; foreach ($formValues as $name => $value) { if (StringHelper::startsWith($name, 'id')) { $id = substr($name, 2); foreach (Yii::$app->session->get('items') as $sessionItem) { if ($sessionItem['id'] == $id) { switch ($value) { // пропустить case 1: $skip[] = $sessionItem['id']; break; // добавить // добавить case 2: $add[] = $sessionItem['id']; break; } } } } } if (count($skip) > 0) { (new Query())->createCommand()->update(Inv::TABLE, ['status' => Inv::STATUS_SKIP], ['in', 'id', $skip])->execute(); } foreach ($add as $item) { $i = Inv::find($item); $class = $i->getField('class_name'); // послание /** @var \app\services\investigator\InvestigatorInterface $class */ $class = new $class(); $extractor = $class->getItem($i->getField('url')); // добавляю Chenneling::insertExtractorInterface($extractor); } if (count($add) > 0) { (new Query())->createCommand()->update(Inv::TABLE, ['status' => Inv::STATUS_ADD], ['in', 'id', $add])->execute(); } Yii::$app->session->remove('items'); Yii::$app->session->setFlash('contactFlash'); return $this->render([]); } else { $items = \app\models\Investigator::query(['status' => \app\models\Investigator::STATUS_NEW])->select(['class_name as class', 'id', 'url', 'date_insert', 'status', 'name'])->all(); Yii::$app->session->set('items', $items); return $this->render(['items' => $items]); } }
/** * Делает рассылку писем из списка рассылки */ public function actionIndex($isEcho = 1) { $this->isEcho = $isEcho; $items = []; $list = Collection::getList(); $c = $this->getIndex(); $c++; if ($c >= count($list)) { $c = 0; } $item = $list[$c]; $class = $item['class']; self::log('класс = ', $class); /** @var \app\services\investigator\InvestigatorInterface $class */ $class = new $class(); $className = $class->className(); try { $new = $class->getNewItems(); foreach ($new as $i) { $i['class'] = $className; $i['id'] = $c; $items[] = $i; } } catch (\Exception $e) { } if (count($items) > 0) { Application::mail('*****@*****.**', 'Появились новые послания', 'new_channeling', ['items' => $items]); self::log('новые = ', $items); } else { self::log('Нет ничего'); } // получаю какие есть $existList = Investigator::query(['class_name' => $className, 'status' => \app\models\Investigator::STATUS_NEW])->select('url')->column(); // добавляю свежие $rows = []; foreach ($items as $i) { if (!in_array($i['url'], $existList)) { $rows[] = [$i['class'], $i['url'], $i['name'], time(), \app\models\Investigator::STATUS_NEW]; } } // добавляю в БД if (count($rows) > 0) { Investigator::batchInsert(['class_name', 'url', 'name', 'date_insert', 'status'], $rows); } $this->setIndex($c); }