<commit_before><?php
namespace Chiliec\pager;

use yii\base\Widget;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use Yii;

/**
 * Class Pager
 * @package Chiliec\pager
 * @author  <NAME> <<EMAIL>>
 */
class Pager extends Widget {
    /**
     * Имя таблицы
     * @var string
     */
    public $tableName;

    /**
     * Значение текущего первичного ключа
     * @var int
     */
    public $id;

    /**
     * Имя столбца с первичным ключом
     * @var string
     */
    public $primaryKey = 'id';

    /**
     * Имя столбца с заголовком
     * @var string
     */
    public $title = 'title';

    /**
     * Время кеширования результатов в секундах
     * @var int
     */
    public $cacheTime = 3600;

    /**
     * Дополнительное SQL-условие
     * @var string
     */
    public $additionalСondition = '1=1';

    /**
     * Путь для формирования ссылки
     * @var string
     */
    public $path = 'action/view';

    /**
     * @var array
     */
    protected $closetLinks;

    public function init(){
        parent::init();
        if($this->tableName == null)
            throw new InvalidConfigException('Table name is not configured!');
        $this->closetLinks = Yii::$app->cache->get($this->tableName.'closestLinks'.$this->id);
        if($this->closetLinks === false) {
            $nextQuery = Yii::$app->db->createCommand("SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->primaryKey} > {$this->id} AND {$this->additionalСondition} LIMIT 1");
            if(($next = $nextQuery->queryOne()) == false) {
                $next = Yii::$app->db->createCommand("SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->additionalСondition} ORDER BY {$this->primaryKey} ASC LIMIT 1")->queryOne();
            }
            $prevQuery = Yii::$app->db->createCommand("SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->primaryKey} < {$this->id} AND {$this->additionalСondition} ORDER BY {$this->primaryKey} DESC LIMIT 1");
            if(($prev = $prevQuery->queryOne()) == false) {
                $prev = Yii::$app->db->createCommand("SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->additionalСondition} ORDER BY {$this->primaryKey} DESC LIMIT 1")->queryOne();
            }
            $this->closetLinks = ['next'=>$next, 'prev'=>$prev];
            Yii::$app->cache->set($this->tableName.'closestLinks'.$this->id, $this->closetLinks, $this->cacheTime);
        }
    }

    public function run(){
        return "
        <nav>
            <ul class='pager'>
                <li class='previous'>"
                    .Html::a('&larr; '.$this->closetLinks['prev'][$this->title], [$this->path, 'id' => $this->closetLinks['prev'][$this->primaryKey]], ['class'=>'pull-left', 'rel'=>'prev']).
                "</li>
                <li class='next'>"
                    .Html::a($this->closetLinks['next'][$this->title].' &rarr;', [$this->path, 'id' => $this->closetLinks['next'][$this->primaryKey]], ['class'=>'pull-right', 'rel'=>'next']).
                "</li>
            </ul>
        </nav>";
    }
}<commit_msg>Update Pager.php<commit_after><?php
namespace Chiliec\pager;

use yii\base\Widget;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use Yii;

/**
 * Class Pager
 * @package Chiliec\pager
 * @author  <NAME> <<EMAIL>>
 */
class Pager extends Widget
{
    /**
     * Имя таблицы
     * @var string
     */
    public $tableName;

    /**
     * Значение текущего первичного ключа
     * @var int
     */
    public $id;

    /**
     * Имя столбца с первичным ключом
     * @var string
     */
    public $primaryKey = 'id';

    /**
     * Имя столбца с заголовком
     * @var string
     */
    public $title = 'title';

    /**
     * Время кеширования результатов в секундах
     * @var int
     */
    public $cacheTime = 3600;

    /**
     * Дополнительное SQL-условие
     * @var string
     */
    public $additionalСondition = '1=1';

    /**
     * Путь для формирования ссылки
     * @var string
     */
    public $path = 'action/view';

    /**
     * @var array
     */
    protected $closetLinks;


    /**
     * @inheritdoc
     */
    public function init()
    {        
        if ($this->tableName === null) {
            throw new InvalidConfigException('Table name is not configured!');
        }
        
        $this->closetLinks = Yii::$app->cache->get($this->tableName . 'closestLinks' . $this->id);
        
        if ($this->closetLinks === false) {
            $nextQuery = Yii::$app->db->createCommand(
                "SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->primaryKey} > {$this->id} AND {$this->additionalСondition} LIMIT 1"
            );
            if (($next = $nextQuery->queryOne()) === false) {
                $next = Yii::$app->db->createCommand(
                    "SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->additionalСondition} ORDER BY {$this->primaryKey} ASC LIMIT 1"
                )->queryOne();
            }
            $prevQuery = Yii::$app->db->createCommand(
                "SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->primaryKey} < {$this->id} AND {$this->additionalСondition} ORDER BY {$this->primaryKey} DESC LIMIT 1"
            );
            if (($prev = $prevQuery->queryOne()) === false) {
                $prev = Yii::$app->db->createCommand(
                    "SELECT {$this->primaryKey},{$this->title} FROM {$this->tableName} WHERE {$this->additionalСondition} ORDER BY {$this->primaryKey} DESC LIMIT 1"
                )->queryOne();
            }
            $this->closetLinks = ['next' => $next, 'prev' => $prev];
            Yii::$app->cache->set($this->tableName . 'closestLinks' . $this->id, $this->closetLinks, $this->cacheTime);
        }
    }

    /**
     * @inheritdoc
     */
    public function run()
    {
        return '
        <nav>
            <ul class="pager">
                <li class="previous">'
        . Html::a(
            '&larr; ' . $this->closetLinks['prev'][$this->title],
            [$this->path, 'id' => $this->closetLinks['prev'][$this->primaryKey]],
            ['class' => 'pull-left', 'rel' => 'prev']
        ) .
        '</li>
                <li class="next">'
        . Html::a(
            $this->closetLinks['next'][$this->title] . ' &rarr;',
            [$this->path, 'id' => $this->closetLinks['next'][$this->primaryKey]],
            ['class' => 'pull-right', 'rel' => 'next']
        ) .
        '</li>
            </ul>
        </nav>';
    }
}
{'fraction_non_alphanumeric': 0.1865526039321627, 'fraction_numerical': 0.0030016509079994, 'mean_word_length': 2.1419141914191417, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3119565', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2169, 'n_words': 418}