Commit 73d52829 by Скуратович Александр

CRUD hosts

1 parent 36888283
......@@ -20,7 +20,7 @@ class AdminAsset extends AssetBundle
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = ['css/admin.css'];
public $js = ['js/chosen.jquery.min.js'];
public $js = [];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
......
......@@ -4,6 +4,7 @@ namespace app\commands;
use yii;
use yii\console\Controller;
use yii\db\Expression;
use yii\helpers\Console;
use app\components\Collection;
use app\jobs\HostJob;
......@@ -38,24 +39,21 @@ class ScanController extends Controller
*/
public function actionIndex()
{
$ids = [];
$row = 1;
$hostsArr = Collection::getHostsByCsv(Yii::$app->params['csv_url']);
$hosts = Collection::getHostsByCsv(Yii::$app->params['csv_url']);
$grabUrls = Yii::$app->params['registrators_urls'];
foreach ($hostsArr as $host) {
/* if ((int)!($row % 10000)) {
echo $this->ansiFormat('+', Console::FG_BLUE);
flush();
}*/
$ids[] = (int)Yii::$app->queue->push(new HostJob([
foreach ($hosts['data'] as $host) {
$res = Yii::$app->db->createCommand()->insert('{{%host}}', [
'domain' =>utf8($host),
'created_at' => new Expression('NOW()'),
'csv_date' => date("Y-m-d H:i:s",strtotime($hosts['csv_date'])),
])->execute();
/* $ids[] = (int)Yii::$app->queue->push(new HostJob([
'host' => $host,
'grabSource' => $grabUrls[array_rand($grabUrls)]
]));
break;
$row++;
]));*/
}
echo "\n".$this->ansiFormat('Количество полученных хостов', Console::FG_CYAN) . ' => ' . $this->ansiFormat(count($ids), Console::BOLD, Console::FG_GREEN) . "\n";
echo "\n".$this->ansiFormat('Количество полученных хостов', Console::FG_CYAN) . ' => ' . $this->ansiFormat(count($hosts['data']), Console::BOLD, Console::FG_GREEN) . "\n";
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ class Collection extends Component
{
public static function getHostsByCsv($csvFile, $separator = ";", $seporatorHosts = '|', $pos = 1, $unique = true)
{
$date = '';
$hosts = [];
$row = 1;
if (($handle = fopen($csvFile, "r")) !== FALSE) {
......@@ -20,6 +21,8 @@ class Collection extends Component
//$hosts = array_merge($hosts, $arr);
$hosts[] = str_replace('*.', '', $hostsString);
}
} else {
$date = $data[0];
}
$row++;
}
......@@ -29,7 +32,7 @@ class Collection extends Component
$hosts = array_unique($hosts);
}
return $hosts;
return ['csv_date' => str_replace('Updated: ', '', $date), 'data' => $hosts];
}
......
......@@ -19,7 +19,8 @@
"yiisoft/yii2-bootstrap": "~2.0.0",
"yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0",
"yiisoft/yii2-queue": "^2.3",
"paquettg/php-html-parser": "^3.1"
"paquettg/php-html-parser": "^3.1",
"yiisoft/yii2-jui": "^2.0"
},
"require-dev": {
"yiisoft/yii2-debug": "~2.1.0",
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "542c2f1c99ac709065f6dc54d45f640b",
"content-hash": "e72d3e667c852d7be29f899e98af1031",
"packages": [
{
"name": "bower-asset/bootstrap",
......@@ -67,6 +67,27 @@
]
},
{
"name": "bower-asset/jquery-ui",
"version": "1.12.1",
"source": {
"type": "git",
"url": "git@github.com:components/jqueryui.git",
"reference": "44ecf3794cc56b65954cc19737234a3119d036cc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/components/jqueryui/zipball/44ecf3794cc56b65954cc19737234a3119d036cc",
"reference": "44ecf3794cc56b65954cc19737234a3119d036cc"
},
"require": {
"bower-asset/jquery": ">=1.6"
},
"type": "bower-asset",
"license": [
"MIT"
]
},
{
"name": "bower-asset/punycode",
"version": "v1.3.2",
"source": {
......@@ -1942,6 +1963,59 @@
"time": "2020-06-24T00:04:01+00:00"
},
{
"name": "yiisoft/yii2-jui",
"version": "2.0.7",
"source": {
"type": "git",
"url": "https://github.com/yiisoft/yii2-jui.git",
"reference": "ce45c16d4fbbe7d1c516d8d0e8311e07f6138eed"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/yiisoft/yii2-jui/zipball/ce45c16d4fbbe7d1c516d8d0e8311e07f6138eed",
"reference": "ce45c16d4fbbe7d1c516d8d0e8311e07f6138eed",
"shasum": ""
},
"require": {
"bower-asset/jquery-ui": "~1.12.1",
"yiisoft/yii2": "~2.0.4"
},
"type": "yii2-extension",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"yii\\jui\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Qiang Xue",
"email": "qiang.xue@gmail.com"
}
],
"description": "The Jquery UI extension for the Yii framework",
"keywords": [
"jQuery UI",
"yii2"
],
"support": {
"forum": "http://www.yiiframework.com/forum/",
"irc": "irc://irc.freenode.net/yii",
"issues": "https://github.com/yiisoft/yii2-jui/issues",
"source": "https://github.com/yiisoft/yii2-jui",
"wiki": "http://www.yiiframework.com/wiki/"
},
"time": "2017-11-25T15:32:29+00:00"
},
{
"name": "yiisoft/yii2-queue",
"version": "2.3.2",
"source": {
......
......@@ -4,9 +4,9 @@ return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=rkn',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'prs_',
'password' => 'qwerty',
'charset' => 'utf8mb4',
'tablePrefix' => 'rkn_',
// Schema cache options (for production environment)
//'enableSchemaCache' => true,
......
......@@ -25,14 +25,14 @@ $config = [
'components' => [
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
/* 'yii\web\JqueryAsset' => [
'sourcePath' => null,
'basePath' => '@webroot',
'baseUrl' => '@web',
'js' => [
'js/jquery-1.12.4.min.js',
]
],
],*/
],
],
'queue' => [
......
......@@ -63,8 +63,8 @@ class SiteController extends Controller
*/
public function actionIndex()
{
//var_dump(Yii::$app->user->isGuest);
//var_dump(Yii::$app->user->identity);
var_dump(Yii::$app->user->isGuest);
var_dump(Yii::$app->user->identity);
return $this->render('index');
}
......
<?php
use yii\db\Migration;
/**
* Class m210602_105634_host
*/
class m210602_105634_host extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%host}}', [
'id' => $this->primaryKey(),
'domain' => $this->string(256)->notNull()->unique()->comment('Домен'),
'created_at' => $this->dateTime()->comment('Дата записи'),
'csv_date' => $this->dateTime()->comment('Дата CSV'),
'wis_date' => $this->dateTime()->null()->comment('Дата проверки домена'),
'wis_status' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('Статус'),
'tix' => $this->integer(11)->notNull()->defaultValue(0)->comment('Показатель'),
], 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%host}}');
}
}
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
* @param string $email the target email address
* @return bool whether the model passes validation
*/
public function contact($email)
{
if ($this->validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
->setReplyTo([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}
<?php
namespace app\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "{{%host}}".
*
* @property int $id
* @property string $domain Домен
* @property string|null $created_at Дата записи
* @property string|null $csv_date Дата CSV
* @property string|null $wis_date Дата проверки домена
* @property int|null $wis_status Статус
* @property int $tix Показатель
*/
class Host extends \yii\db\ActiveRecord
{
const STATUS_NONE = 0; //неизвестный
const STATUS_FREE = 1; //Свободный
const STATUS_BUSY = 2; //Занятый
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%host}}';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => null,
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['domain'], 'required'],
['status_id', 'in', 'range' => array_keys(self::getStatus())],
[['created_at', 'csv_date', 'wis_date'], 'safe'],
[['wis_status', 'tix'], 'integer'],
[['domain'], 'string', 'max' => 256],
[['domain'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'domain' => 'Домен',
'created_at' => 'Дата записи',
'csv_date' => 'Дата CSV',
'wis_date' => 'Дата проверки домена',
'wis_status' => 'Статус',
'tix' => 'Показатель',
];
}
/**
* {@inheritdoc}
* @return HostQuery the active query used by this AR class.
*/
public static function find()
{
return new HostQuery(get_called_class());
}
public static function getStatus($status = false)
{
$arr = [
self::STATUS_NONE => 'Неизвестный',
self::STATUS_FREE => 'Свободный',
self::STATUS_BUSY => 'Занятый',
];
if (is_numeric($status)) {
if (array_key_exists($status, $arr)) {
return $arr[$status];
}
return $status;
}
return $arr;
}
}
......@@ -2,46 +2,33 @@
namespace app\models;
use Yii;
use \yii\db\ActiveRecord;
/**
* This is the model class for table "plant_image".
* This is the ActiveQuery class for [[Host]].
*
* @property int $id
* @property int $image_id
* @property int $plant_id
* @see Host
*/
class PlantImage extends ActiveRecord
class HostQuery extends \yii\db\ActiveQuery
{
/**
* {@inheritdoc}
*/
public static function tableName()
/*public function active()
{
return '{{%flower_image}}';
}
return $this->andWhere('[[status]]=1');
}*/
/**
* {@inheritdoc}
* @return Host[]|array
*/
public function rules()
public function all($db = null)
{
return [
[['image_id', 'plant_id'], 'required'],
[['image_id', 'plant_id'], 'integer'],
];
return parent::all($db);
}
/**
* {@inheritdoc}
* @return Host|array|null
*/
public function attributeLabels()
public function one($db = null)
{
return [
'id' => 'ID',
'image_id' => 'Идентификатор картинки',
'plant_id' => 'Идентификато цветка',
];
return parent::one($db);
}
}
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "{{%image}}".
*
* @property int $id
* @property int $user_id
* @property string $title
* @property string $file
* @property string $ext
* @property int $size
* @property string $hash
*/
class Image extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%image}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id','title', 'file', 'ext', 'size', 'hash'], 'required'],
[['size'], 'integer'],
[['hash'], 'string', 'max' => 256],
[['title'], 'string', 'max' => 256],
[['file'], 'string', 'max' => 64],
[['ext'], 'string', 'max' => 6],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'user_id' => Yii::t('app', 'Пользователь'),
'title' => Yii::t('app', 'Название'),
'file' => Yii::t('app', 'Имя файла'),
'ext' => Yii::t('app', 'Расширение'),
'size' => Yii::t('app', 'Размер'),
'hash' => Yii::t('app', 'Хеш'),
];
}
}
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "{{%plant}}".
*
* @property int $id
* @property string $title
* @property string $description
* @property double $height
* @property int $bush_type
* @property int $flower_form
* @property int $flower_tint
* @property string $created_at
* @property int $cost
* @property int $top
* @property int $active
* @property string $edit_at
*/
class Plant extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%plant}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title'], 'required'],
[['description'], 'string'],
[['height'], 'number'],
[['bush_type', 'flower_form', 'flower_tint', 'cost'], 'integer'],
[['created_at', 'edit_at'], 'datetime'],
[['top', 'active'], 'integer', 'max' => 1],
[['title'], 'string', 'max' => 256],
[['title'], 'unique'],
[['title', 'description', 'height', 'bush_type', 'flower_form', 'flower_tint', 'cost', 'top', 'active'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Название',
'description' => 'Описание',
'height' => 'Высота куста',
'bush_type' => 'Тип куста',
'flower_form' => 'Форма цветка',
'flower_tint' => 'Оттенок цветка',
'created_at' => 'Дата создания',
'cost' => 'Цена',
'top' => 'показать в топ',
'active' => 'Показывать',
'edit_at' => 'Дата редактирования',
];
}
}
<?php
namespace app\modules\admin\controllers;
use Yii;
use app\models\Host;
use app\models\HostSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* HostController implements the CRUD actions for Host model.
*/
class HostController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Host models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new HostSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Host model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Host model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Host();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Host model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Host model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Host model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Host the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Host::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\Host */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="host-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'domain')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'created_at')->textInput() ?>
<?= $form->field($model, 'csv_date')->textInput() ?>
<?= $form->field($model, 'wis_date')->textInput() ?>
<?= $form->field($model, 'wis_status')->textInput() ?>
<?= $form->field($model, 'tix')->textInput() ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\HostSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="host-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'domain') ?>
<?= $form->field($model, 'created_at') ?>
<?= $form->field($model, 'csv_date') ?>
<?= $form->field($model, 'wis_date') ?>
<?php // echo $form->field($model, 'wis_status') ?>
<?php // echo $form->field($model, 'tix') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\Host */
$this->title = 'Create Host';
$this->params['breadcrumbs'][] = ['label' => 'Hosts', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="host-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\jui\DatePicker;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $searchModel app\models\HostSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Hosts';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="host-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Host', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'domain',
'created_at' => [
'attribute' => 'created_at',
'value' => 'created_at',
// 'format' => 'date',
/* 'filter' => DatePicker::widget([
'name' => 'created_at',
'clientOptions' => [
'format' => 'd.mm.yyyy',
'todayHighlight' => true,
]
]),*/
],
'csv_date',
'wis_status' =>
[
'attribute' => 'wis_status',
'filter' => \app\models\Host::getStatus(),
'format' => 'raw',
'value' => function ($model) {
return $model::getStatus($model->wis_status);
},
],
'wis_date',
'tix',
[
'class' => 'yii\grid\ActionColumn',
'header' => 'Actions',
'template' => '{view} {delete}',
]
],
]); ?>
<?php Pjax::end(); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\Host */
$this->title = 'Update Host: ' . $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Hosts', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="host-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\Host */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Hosts', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="host-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'domain',
'created_at',
'csv_date',
'wis_date',
'wis_status',
'tix',
],
]) ?>
</div>
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!