<commit_before><?php
abstract class Vps_Controller_Action_Auto_AssignGrid extends Vps_Controller_Action_Auto_Grid
{
    protected $_textAssignField = null;
    protected $_assignToReference;
    protected $_assignFromReference;

    public function jsonAssignAction()
    {
        $this->_checkNecessaryProperties();

        $ids = Zend_Json::decode($this->_getParam('foreign_keys'));
        if (!count($ids)) throw new Vps_ClientException(trlVps("There's no row selected"));

        $assignModel = $this->_getModel();

        $assignToRef = $assignModel->getReference($this->_assignToReference);
        $assignToColumn = $assignToRef['column'];
        $assignFromRef = $assignModel->getReference($this->_assignFromReference);
        $assignFromColumn = $assignFromRef['column'];

        ignore_user_abort(true);
        $this->_model->getAdapter()->beginTransaction();
        foreach ($ids as $id) {
            $row = $assignModel->getRow($assignModel->select()
                ->whereEquals($assignToColumn, $this->_getParam($assignToColumn))
                ->whereEquals($assignFromColumn, $id)
            );

            if (!$row) {
                $row = $assignModel->createRow();
                $row->$assignToColumn = $this->_getParam($assignToColumn);
                $row->$assignFromColumn = $id;
                $row->save();
            }
        }
        $this->_model->getAdapter()->commit();
    }

    public function jsonTextAssignAction()
    {
        $this->_checkNecessaryProperties();
        if (!$this->_textAssignField) {
            throw new Vps_Exception('$this->_textAssignField not set');
        }

        $text = $this->_getParam('assignText');
        if (!trim($text)) {
            throw new Vps_ClientException('Textinput was empty');
        }

        $assignModel = $this->_getModel();

        $assignToRef = $assignModel->getReference($this->_assignToReference);
        $assignToColumn = $assignToRef['column'];
        $assignFromRef = $assignModel->getReference($this->_assignFromReference);
        $assignFromColumn = $assignFromRef['column'];

        $dataModel = Vps_Model_Abstract::getInstance($assignFromRef['refModelClass']);

        ignore_user_abort(true);
        $this->_model->getAdapter()->beginTransaction();
        $items = preg_split("(\n|\r)", $text);
        foreach ($items as $item) {
            $item = trim($item);
            if (!$item) continue;

            $dataRow = $dataModel->getRow($dataModel->select()
                ->whereEquals($this->_textAssignField, $item)
            );
            if (!$dataRow) {
                $dataRow = $dataModel->createRow(array(
                    $this->_textAssignField => $item
                ));
                $dataRow->save();
            }

            if ($dataRow) {
                $assignRow = $assignModel->getRow($assignModel->select()
                    ->whereEquals($assignToColumn, $this->_getParam($assignToColumn))
                    ->whereEquals($assignFromColumn, $dataRow->id)
                );
                if (!$assignRow) {
                    $row = $assignModel->createRow();
                    $row->$assignToColumn = $this->_getParam($assignToColumn);
                    $row->$assignFromColumn = $dataRow->id;
                    $row->save();
                }
            }
        }
        $this->_model->getAdapter()->commit();
    }

    protected function _checkNecessaryProperties()
    {
        if (!$this->_modelName) {
            throw new Vps_Exception('$this->_modelName not set');
        }
        if (!$this->_assignToReference) {
            throw new Vps_Exception('$this->_assignToReference not set');
        }<commit_msg>AssignGrid cleanup + flexibler gemacht<commit_after><?php
abstract class Vps_Controller_Action_Auto_AssignGrid extends Vps_Controller_Action_Auto_Grid
{
    protected $_textAssignField = null;
    protected $_assignFromReference; //required
    protected $_assignToReference; //or overwrite _createAssignRow

    protected $_buttons = array('delete');

    public function jsonAssignAction()
    {
        $this->_checkNecessaryProperties();

        $ids = Zend_Json::decode($this->_getParam('foreign_keys'));
        if (!count($ids)) throw new Vps_ClientException(trlVps("There's no row selected"));

        $assignFromRef = $this->_getModel()->getReference($this->_assignFromReference);
        $assignFromColumn = $assignFromRef['column'];

        ignore_user_abort(true);
        $this->_model->getAdapter()->beginTransaction();
        foreach ($ids as $id) {
            $select = $this->_getSelect();
            $select->whereEquals($assignFromColumn, $id);
            $row = $this->_getModel()->getRow($select);
            if (!$row) {
                $this->_createAssignRow($id)->save();
            }
        }
        $this->_model->getAdapter()->commit();
    }

    protected function _createAssignRow($id)
    {
        if (!$this->_assignToReference) {
            throw new Vps_Exception('$this->_assignToReference not set');
        }

        $assignToRef = $this->_getModel()->getReference($this->_assignToReference);
        $assignToColumn = $assignToRef['column'];
        $assignFromRef = $this->_getModel()->getReference($this->_assignFromReference);
        $assignFromColumn = $assignFromRef['column'];

        $row = $this->_getModel()->createRow();
        $row->$assignToColumn = $this->_getParam($assignToColumn);
        $row->$assignFromColumn = $id;
        return $row;
    }

    public function jsonTextAssignAction()
    {
        $this->_checkNecessaryProperties();
        if (!$this->_textAssignField) {
            throw new Vps_Exception('$this->_textAssignField not set');
        }

        $text = $this->_getParam('assignText');
        if (!trim($text)) {
            throw new Vps_ClientException('Textinput was empty');
        }

        $assignModel = $this->_getModel();

        $assignToRef = $assignModel->getReference($this->_assignToReference);
        $assignToColumn = $assignToRef['column'];
        $assignFromRef = $assignModel->getReference($this->_assignFromReference);
        $assignFromColumn = $assignFromRef['column'];

        $dataModel = Vps_Model_Abstract::getInstance($assignFromRef['refModelClass']);

        ignore_user_abort(true);
        $this->_model->getAdapter()->beginTransaction();
        $items = preg_split("(\n|\r)", $text);
        foreach ($items as $item) {
            $item = trim($item);
            if (!$item) continue;

            $dataRow = $dataModel->getRow($dataModel->select()
                ->whereEquals($this->_textAssignField, $item)
            );
            if (!$dataRow) {
                $dataRow = $dataModel->createRow(array(
                    $this->_textAssignField => $item
                ));
                $dataRow->save();
            }

            if ($dataRow) {
                $assignRow = $assignModel->getRow($assignModel->select()
                    ->whereEquals($assignToColumn, $this->_getParam($assignToColumn))
                    ->whereEquals($assignFromColumn, $dataRow->id)
                );
                if (!$assignRow) {
                    $row = $assignModel->createRow();
                    $row->$assignToColumn = $this->_getParam($assignToColumn);
                    $row->$assignFromColumn = $dataRow->id;
                    $row->save();
                }
            }
        }
        $this->_model->getAdapter()->commit();
    }

    protected function _checkNecessaryProperties()
    {
        if (!$this->_modelName) {
            throw new Vps_Exception('$this->_modelName not set');
        }
{'fraction_non_alphanumeric': 0.14445455727473672, 'fraction_numerical': 0.0, 'mean_word_length': 2.270408163265306, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 132, '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': '1446346', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2002, 'n_words': 319}