/**
  * Finds the ShortLinksClick model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ShortLinksClick the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ShortLinksClick::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ShortLinksClick::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'short_links_id' => $this->short_links_id]);
     if (!empty($this->created_at)) {
         $date = strtotime($this->created_at);
         $query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]);
     }
     return $dataProvider;
 }
?>
    <div class="clearfix"></div>
    <div class="form-group margin-top-20">
        <?php 
echo Html::submitButton($model->isNewRecord ? 'Добавить' : 'Сохранить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
?>
    </div>

    <?php 
ActiveForm::end();
?>

</div>

<?php 
if (!$model->isNewRecord) {
    ?>
    <?php 
    $clicks = (new \yii\db\Query())->select(['DATE_FORMAT(FROM_UNIXTIME(`created_at`), \'%d.%m.%Y\') AS `date`, COUNT(*) as `count_click`'])->from(ShortLinksClick::tableName())->where(['short_links_id' => $model->id])->indexBy('date')->groupBy("date")->orderBy('created_at')->all();
    if ($clicks) {
        foreach ($clicks as $day => $click) {
            $temp = '';
            $temp['date'] = $day;
            $temp['click'] = empty($click['count_click']) ? 0 : $click['count_click'];
            $dataProvider[] = $temp;
        }
        $graphs = [['balloonText' => '<span>Переходов: <b>[[value]]</b></span>', 'title' => 'Переходов', 'valueField' => 'click', 'bulletSize' => 10, 'lineThickness' => 2, 'useLineColorForBulletBorder' => true, 'bullet' => 'round', 'bulletBorderAlpha' => 1, 'bulletColor' => '#FFFFFF', 'hideBulletsCount' => 100]];
        $chartConfiguration = ['dataProvider' => $dataProvider, 'type' => 'serial', 'language' => 'ru', 'dataDateFormat' => 'DD.MM.YYYY', 'decimalSeparator' => '.', 'thousandsSeparator' => ' ', 'valueAxes' => [['stackType' => 'none', 'gridAlpha' => 0.07000000000000001, 'position' => 'left']], 'graphs' => $graphs, 'legend' => ['periodValueText' => 'Всего: [[value.sum]]', 'equalWidths' => 'false', 'position' => 'top', 'valueAlign' => 'left', 'valueWidth' => 100], 'chartCursor' => ['cursorPosition' => 'mouse', 'pan' => true, 'valueLineEnabled' => true, 'valueLineBalloonEnabled' => true], 'chartScrollbar' => ['graph' => 'g1', 'scrollbarHeight' => 30], 'pathToImages' => 'http://www.amcharts.com/lib/3/images/', 'categoryField' => 'date', 'categoryAxis' => ['parseDates' => true, 'dashLength' => 1, 'minorGridEnabled' => true, 'position' => 'top'], 'exportConfig' => ['menuTop' => '10px', 'menuRight' => '10px', 'menuItems' => [['icon' => '/lib/3/images/export.png', 'format' => 'png']]]];
        echo mitrm\amcharts\AmChart::widget(['chartConfiguration' => $chartConfiguration, 'options' => ['id' => 'mitrm_chart_click'], 'width' => '100%', 'height' => '600px', 'language' => 'ru', 'zoomChart' => ['lenght_one' => 30, 'lenght_two' => 1]]);
    }
}
 public static function findByToken($token)
 {
     $model = ShortLinks::find()->where(['token' => $token])->one();
     if ($model) {
         $click = new ShortLinksClick();
         $click->short_links_id = $model->id;
         if (!$click->save()) {
             Yii::error(['msg' => 'Ошибка записи статистики клика', 'data' => ['error' => $model->errors, 'method' => __METHOD__]]);
         }
         $model->count_click += 1;
         $model->save();
         // Запишем ссылку в куки
         $cookies = Yii::$app->response->cookies;
         // добавление новой куки в HTTP-ответ
         $cookies->add(new \yii\web\Cookie(['name' => 'short_link_id', 'value' => $model->id, 'expire' => time() + 60 * 60 * 24 * 7]));
         return $model->link;
     }
     return false;
 }