<commit_before><?php
namespace cmsadmin\models;

class Nav extends \yii\db\ActiveRecord
{
    public $title;
    public $rewrite;
    public $nav_item_type;
    public $nav_item_type_id;
    public $lang_id;

    public static function tableName()
    {
        return 'cms_nav';
    }

    public function init()
    {
        parent::init();
        $this->on(self::EVENT_BEFORE_INSERT, [$this, 'beforeCreate']);
        $this->on(self::EVENT_AFTER_INSERT, [$this, 'afterCreate']);
    }

    public function rules()
    {
        return [
            [['cat_id', 'parent_nav_id', 'sort_index', 'is_deleted'], 'integer', 'on' => 'restcreate']
        ];
    }

    public function scenarios()
    {
        return [
            'restcreate' => ['cat_id', 'lang_id', 'parent_nav_id', 'nav_item_type', 'nav_item_type_id', 'sort_index', 'is_deleted', 'title', 'rewrite'],
            'restupdate' => []
        ];
    }

    public function beforeCreate()
    {
        $this->is_deleted = 0;
    }

    public function transactions()
    {
        return [
            'restcreate' => self::OP_ALL
        ];
    }

    public function afterCreate()
    {
        $navItem = new NavItem();
        $navItem->nav_item_type = $this->nav_item_type;
        $navItem->nav_item_type_id = $this->nav_item_type_id;
        $navItem->nav_id = $this->id;
        $navItem->lang_id = $this->lang_id;
        $navItem->title = $this->title;
        $navItem->rewrite = $this->rewrite;
        if ($navItem->validate()) {
            $navItem->save();
        } else {
            throw new \Exception(print_r($navItem->getErrors(), true));
        }
    }
    /*
    public function getNavItems()<commit_msg>[+] changed the create nav item before the nav entry has been created and update it afterwards with the corresponding nav_id<commit_after><?php
namespace cmsadmin\models;

class Nav extends \yii\db\ActiveRecord
{
    public $title;
    public $rewrite;
    public $nav_item_type;
    public $nav_item_type_id;
    public $lang_id;

    private $_navItem = null;
    
    public static function tableName()
    {
        return 'cms_nav';
    }

    public function init()
    {
        parent::init();
        $this->on(self::EVENT_AFTER_INSERT, [$this, 'eventAfterCreate']);
    }

    public function rules()
    {
        return [
            [['cat_id', 'parent_nav_id', 'sort_index', 'is_deleted'], 'integer', 'on' => 'restcreate']
        ];
    }

    public function scenarios()
    {
        return [
            'restcreate' => ['cat_id', 'lang_id', 'parent_nav_id', 'nav_item_type', 'nav_item_type_id', 'sort_index', 'is_deleted', 'title', 'rewrite'],
            'restupdate' => []
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($insert) {
                $this->is_deleted = 0;
                $this->_navItem = new NavItem();
                $this->_navItem->nav_item_type = $this->nav_item_type;
                $this->_navItem->nav_item_type_id = $this->nav_item_type_id;
                $this->_navItem->lang_id = $this->lang_id;
                $this->_navItem->title = $this->title;
                $this->_navItem->rewrite = $this->rewrite;
                if ($this->_navItem->validate()) {
                    $this->_navItem->save();
                } else {
                    foreach ($this->_navItem->getErrors() as $k => $v) {
                        $this->addError($k, $v[0]);
                    }
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }
    
    public function eventAfterCreate()
    {
        $this->_navItem->nav_id = $this->id;
        $resp = $this->_navItem->update();
    }
    /*
    public function getNavItems()
{'fraction_non_alphanumeric': 0.14442127215849843, 'fraction_numerical': 0.0007820646506777894, 'mean_word_length': 1.965224111282844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '1340628', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1169, 'n_words': 227}