<commit_before><?php

class UserPostManagerController extends Ml_Controller_Action
{
    use Ml_Controller_People;

    public function newAction()
    {
        $signedUserInfo = $this->_registry->get("signedUserInfo");

        $router = Zend_Controller_Front::getInstance()->getRouter();

        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->viewRenderer("new-xhr");
            $this->_helper->layout->disableLayout();
        }

        $posts =  $this->_registry->get("sc")->get("posts");
        /** @var $posts \Ml_Model_Posts() */

        $form = new Ml_Form_UserPostNew(null, $this->_translatePosts);
        $this->view->assign("postForm", $form);

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

            $data = [];

            $formKeys = array("type", "make", "model", "price", "model_year", "engine");

            foreach ($formKeys as $key) {
                if (! isset($validatePost[$key])) {
                    if ($key == "price") {
                        $data["price"] = str_replace(array(",", "."), "", $values["price"]);
                    } else {
                        $data[$key] = $values[$key];
                    }
                }
            }

            $data["description"] = $signedUserInfo["post_template"];

            $id = $posts->create($this->_auth->getIdentity(), $data);

            $username = $signedUserInfo["username"];

            $this->_redirect($router->assemble(
                    array("username" => $username, "post_id" => $id), "user_post"), array("exit")
            );
        }
    }

    public function editAction()
    {
        $params = $this->getRequest()->getParams();

        $this->view->addJsParam("route", "user/post-manager");

        $people =  $this->_registry->get("sc")->get("people");
        /** @var $people \Ml_Model_People() */

        $posts =  $this->_registry->get("sc")->get("posts");
        /** @var $posts \Ml_Model_Posts() */

        $validatePost = $this->getRequest()->getPost();

        $post = $this->_post;
        $userInfo = $this->_userInfo;

        $type = $post["type"];

        $availableEquipment = $posts->getAvailableEquipment($type);

        $this->view->assign("availableEquipment", $availableEquipment);

        $form = new Ml_Form_UserPostEdit(
            null,
            $this->_translatePosts,
            $availableEquipment,
            $params["username"],
            $params["post_id"],
            $type
        );

        $form->setDefaults($post);

        $this->view->assign("postForm", $form);

        if ($this->getRequest()->isPost()) {
            $formKeys = ["make", "model", "name", "price", "model_year", "engine", "traction",
                "transmission", "fuel", "km", "armor", "handicapped", "equipment",
                "youtube_video", "description", "status"];

            foreach ($formKeys as $key) {
                if (! isset($validatePost[$key])) {
                    if ($key == "price") {
                        $filterCurrencyBr = new Ml_Filter_CurrencyBr();
                        $validatePost["price"] = $filterCurrencyBr->filter($post["price"] / 100);
                    } else {
                        $validatePost[$key] = $post[$key];
                    }
                }
            }


            if ($form->isValid($validatePost)) {
                $values = $form->getValues();

                $data = [];

                $data["type"] = $type;

                foreach ($formKeys as $key) {
                    $data[$key] = $values[$key];
                }

                $data["price"] = str_replace(array(",", "."), "", $data["price"]);

                $updatedPost = $posts->update($post["id"], $data);

                if (! $updatedPost) {
                    throw new Exception("Error trying to update post");
                }

                $post = $updatedPost;

                $this->_post = $post;

                $this->view->assign("post", $post);
            } else {
                $this->getResponse()->setHttpResponseCode(404);
                $this->_helper->json(["errors" => $form->getErrors()]);
            }
        }

        $this->_helper->layout->disableLayout();<commit_msg>refactor the post editing to simplify it and avoid updating data to the same values it already has<commit_after><?php

class UserPostManagerController extends Ml_Controller_Action
{
    use Ml_Controller_People;

    public function newAction()
    {
        $signedUserInfo = $this->_registry->get("signedUserInfo");

        $router = Zend_Controller_Front::getInstance()->getRouter();

        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->viewRenderer("new-xhr");
            $this->_helper->layout->disableLayout();
        }

        $posts =  $this->_registry->get("sc")->get("posts");
        /** @var $posts \Ml_Model_Posts() */

        $form = new Ml_Form_UserPostNew(null, $this->_translatePosts);
        $this->view->assign("postForm", $form);

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

            $data = [];

            $formKeys = array("type", "make", "model", "price", "model_year", "engine");

            foreach ($formKeys as $key) {
                if (! isset($validatePost[$key])) {
                    if ($key == "price") {
                        $data["price"] = str_replace(array(",", "."), "", $values["price"]);
                    } else {
                        $data[$key] = $values[$key];
                    }
                }
            }

            $data["description"] = $signedUserInfo["post_template"];

            $id = $posts->create($this->_auth->getIdentity(), $data);

            $username = $signedUserInfo["username"];

            $this->_redirect($router->assemble(
                    array("username" => $username, "post_id" => $id), "user_post"), array("exit")
            );
        }
    }

    public function editAction()
    {
        $params = $this->getRequest()->getParams();

        $this->view->addJsParam("route", "user/post-manager");

        $people =  $this->_registry->get("sc")->get("people");
        /** @var $people \Ml_Model_People() */

        $posts =  $this->_registry->get("sc")->get("posts");
        /** @var $posts \Ml_Model_Posts() */

        $validatePost = $this->getRequest()->getPost();

        $post = $this->_post;
        $userInfo = $this->_userInfo;

        $type = $post["type"];

        $availableEquipment = $posts->getAvailableEquipment($type);

        $this->view->assign("availableEquipment", $availableEquipment);

        $form = new Ml_Form_UserPostEdit(
            null,
            $this->_translatePosts,
            $availableEquipment,
            $params["username"],
            $params["post_id"],
            $type
        );

        $form->setDefaults($post);

        $this->view->assign("postForm", $form);

        if ($this->getRequest()->isPost()) {

            if ($form->isValid($validatePost)) {
                $values = $form->getValues();

                //unset / ignore the values of the form inputs not submitted
                $listOfValuesSent = filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW);
                $data = array_intersect_key($values, $listOfValuesSent);

                if (isset($data["price"])) {
                    $data["price"] = str_replace(array(",", "."), "", $data["price"]);
                }

                unset($data["hash"]);

                $updatedPost = $posts->update($post["id"], $data);

                if (! $updatedPost) {
                    throw new Exception("Error trying to update post");
                }

                $post = $updatedPost;

                $this->_post = $post;

                $this->view->assign("post", $post);
            } else {
                $this->getResponse()->setHttpResponseCode(404);
                $this->_helper->json(["errors" => $form->getErrors()]);
            }
        }

        $this->_helper->layout->disableLayout();
{'fraction_non_alphanumeric': 0.17850194552529183, 'fraction_numerical': 0.0010943579766536965, 'mean_word_length': 1.847005884389062, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 138, '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': '2270360', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2181, 'n_words': 367}