<commit_before><?php
/**
 * Zym
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @author     <NAME>
 * @category   Zym
 * @package    Controller
 * @subpackage Action
 * @copyright Copyright (c) 2008 Zym. (http://www.assembla.com/wiki/show/zym)
 * @license http://www.assembla.com/wiki/show/dpEKouT5Gr3jP5abIlDkbG/License    New BSD License
 */

/**
 * @see Zend_Db_Table_Abstract
 */
require_once 'Zend/Db/Table/Abstract.php';

/**
 * @see Zym_Controller_Action_Abstract
 */
require_once 'Zym/Controller/Action/Abstract.php';

/**
 * @see Zym_Controller_Exception
 */
require_once 'Zym/Controller/Exception.php';

/**
 * @author     <NAME>
 * @category   Zym
 * @package    Controller
 * @subpackage Action
 * @copyright Copyright (c) 2008 Zym. (http://www.assembla.com/wiki/show/zym)
 * @license http://www.assembla.com/wiki/show/dpEKouT5Gr3jP5abIlDkbG/License    New BSD License
 */
abstract class Zym_Controller_Action_Crud_Abstract extends Zym_Controller_Action_Abstract
{
    /**
     * Get the table for this model
     *
     * @return Zend_Db_Table_Abstract
     */
    abstract protected function _getTable();

    /**
     * Get the form for this model
     *
     * @return Zend_Form
     */
    abstract protected function _getForm();

    /**
     * Get the primary id from the request
     *
     * @return int|null
     */
    protected function _getPrimaryId()
    {
        return $this->_getParam($this->_getPrimaryIdKey());
    }

    /**
     * Get the location to where the form needs to submit for a new entry
     *
     * @return array
     */
    protected function _getNewSubmitLocation()
    {
        return array('module'     => $this->getRequest()->getModuleName(),
                     'controller' => $this->getRequest()->getControllerName(),
                     'action'     => $this->_getAddEditAction());
    }

    /**
     * Get the location to where the form needs to submit for an edited entry
     *
     * @return array
     */
    protected function _getEditSubmitLocation()
    {
        $location = $this->_getNewSubmitLocation();
        $location[$this->_getPrimaryIdKey()] = $this->_getPrimaryId();

        return $location;
    }

    /**
     * Get the column name of the primary id
     *
     * @return string
     */
    protected function _getPrimaryIdKey()
    {
        $info = $this->_getTable()->info();
        // @TODO: decide if we want support for multiple primary keys
        return (string) $info[Zend_Db_Table_Abstract::PRIMARY][0];
    }

    /**
     * Get the model from the table
     *
     * @param int $id
     * @return Zend_Db_Table_Row_Abstract|null
     */
    protected function _getModel($id)
    {
        $table = $this->_getTable();

        return $table->find((int) $id)
                     ->current();
    }

    /**
     * Get the name of the action that takes care of the add/edit stuff
     *
     * @return string
     */
    protected function _getAddEditAction()
    {
        return 'addEdit';
    }

    /**
     * Get the name of the action that takes care of the listing
     *
     * @return string
     */
    protected function _getListAction()
    {
        return 'list';
    }

    /**
     * Index action. Forward to the list action
     */
    public function indexAction()
    {
        $this->_forward($this->_getListAction());
    }

    /**
     * Show a list with all available models
     * @TODO Add pagination support
     */
    public function listAction()
    {
        $table = $this->_getTable();
        $models = $table->fetchAll();

        $this->view->models = $models;
    }

    /**
     * View a model if it exists
     */
    public function viewAction()
    {
        $id = $this->_getPrimaryId();
        $model = null;

        if ($id) {
            $model = $this->_getModel($id);

            if ($model) {
                $this->view->model = $model;
            }
        }

        if (!$model) {
            $this->_goto($this->_getListAction());
        }
    }

    /**
     * Add or edit a model
     */
    public function addEditAction()
    {
        $idKey = $this->_getPrimaryIdKey();
        $form = $this->_getForm();

        $id = $this->_getPrimaryId();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $formValues = $form->getValues();

                if (!empty($formValues[$idKey])) {
                    $model = $this->_getModel($id);

                    if (!$model) {
                        throw new Zym_Controller_Exception('The model could not be loaded.'); // @TODO redirect to 'couldn't find model' page instead of an exception?
                    }
                } else {
                    $model = $this->_getTable()->createRow();
                }

                foreach ($formValues as $key => $value) {
                	if (isset($model->$key)) {
                	    $model->$key = $value;
                	}<commit_msg>Minor refactoring<commit_after><?php
/**
 * Zym
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @author     <NAME>
 * @category   Zym
 * @package    Controller
 * @subpackage Action
 * @copyright Copyright (c) 2008 Zym. (http://www.assembla.com/wiki/show/zym)
 * @license http://www.assembla.com/wiki/show/dpEKouT5Gr3jP5abIlDkbG/License    New BSD License
 */

/**
 * @see Zend_Db_Table_Abstract
 */
require_once 'Zend/Db/Table/Abstract.php';

/**
 * @see Zym_Controller_Action_Abstract
 */
require_once 'Zym/Controller/Action/Abstract.php';

/**
 * @see Zym_Controller_Exception
 */
require_once 'Zym/Controller/Exception.php';

/**
 * @author     <NAME>
 * @category   Zym
 * @package    Controller
 * @subpackage Action
 * @copyright Copyright (c) 2008 Zym. (http://www.assembla.com/wiki/show/zym)
 * @license http://www.assembla.com/wiki/show/dpEKouT5Gr3jP5abIlDkbG/License    New BSD License
 */
abstract class Zym_Controller_Action_Crud_Abstract extends Zym_Controller_Action_Abstract
{
    /**
     * Get the table for this model
     *
     * @return Zend_Db_Table_Abstract
     */
    abstract protected function _getTable();

    /**
     * Get the form for this model
     *
     * @return Zend_Form
     */
    abstract protected function _getForm();

    /**
     * Get the primary id from the request
     *
     * @return int|null
     */
    protected function _getPrimaryId()
    {
        return $this->_getParam($this->_getPrimaryIdKey());
    }

    /**
     * Get the location to where the form needs to submit for a new entry
     *
     * @return array
     */
    protected function _getNewSubmitLocation()
    {
        return array('module'     => $this->getRequest()->getModuleName(),
                     'controller' => $this->getRequest()->getControllerName(),
                     'action'     => $this->_getAddEditAction());
    }

    /**
     * Get the location to where the form needs to submit for an edited entry
     *
     * @return array
     */
    protected function _getEditSubmitLocation()
    {
        $location = $this->_getNewSubmitLocation();
        $location[$this->_getPrimaryIdKey()] = $this->_getPrimaryId();

        return $location;
    }

    /**
     * Get the column name of the primary id
     *
     * @return string
     */
    protected function _getPrimaryIdKey()
    {
        $info = $this->_getTable()->info();
        // @TODO: decide if we want support for multiple primary keys
        return (string) $info[Zend_Db_Table_Abstract::PRIMARY][0];
    }

    /**
     * Get the model from the table
     *
     * @param int $id
     * @return Zend_Db_Table_Row_Abstract|null
     */
    protected function _getModel($id)
    {
        $table = $this->_getTable();

        return $table->find((int) $id)
                     ->current();
    }

    /**
     * Get the name of the action that takes care of the add/edit stuff
     *
     * @return string
     */
    protected function _getAddEditAction()
    {
        return 'addEdit';
    }

    /**
     * Get the name of the action that takes care of the listing
     *
     * @return string
     */
    protected function _getListAction()
    {
        return 'list';
    }

    /**
     * Index action. Forward to the list action
     */
    public function indexAction()
    {
        $this->_forward($this->_getListAction());
    }

    /**
     * Show a list with all available models
     * @TODO Add pagination support
     */
    public function listAction()
    {
        $table = $this->_getTable();
        $models = $table->fetchAll();

        $this->view->models = $models;
    }

    /**
     * View a model if it exists
     */
    public function viewAction()
    {
        $id = $this->_getPrimaryId();
        $model = null;

        if ($id) {
            $model = $this->_getModel($id);

            if ($model) {
                $this->view->model = $model;
            }
        }

        if (!$model) {
            $this->_goto($this->_getListAction());
        }
    }

    /**
     * Add or edit a model
     */
    public function addEditAction()
    {
        $form = $this->_getForm();

        $id = $this->_getPrimaryId();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $formValues = $form->getValues();

                if (!empty($formValues[$this->_getPrimaryIdKey()])) {
                    $model = $this->_getModel($id);

                    if (!$model) {
                        throw new Zym_Controller_Exception('The model could not be loaded.'); // @TODO redirect to 'couldn't find model' page instead of an exception?
                    }
                } else {
                    $model = $this->_getTable()->createRow();
                }

                foreach ($formValues as $key => $value) {
                	if (isset($model->$key)) {
                	    $model->$key = $value;
                	}
{'fraction_non_alphanumeric': 0.1300780863892458, 'fraction_numerical': 0.0029653059207274883, 'mean_word_length': 1.9183732333429477, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '3312144', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2902, 'n_words': 731}