<commit_before><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

namespace Mautic\LeadBundle\Helper;

use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\PointsChangeLog;

/**
 * Class FormEventHelper
 *
 * @package Mautic\LeadBundle\Helper
 */
class FormEventHelper
{

    /**
     * @param       $action
     * @param       $form
     * @param array $post
     * @param       $factory
     * @param array $fields
     *
     * @return array
     */
    public static function createLead($action, $form, array $post, MauticFactory $factory, array $fields)
    {
        /** @var \Mautic\LeadBundle\Model\LeadModel $model */
        $model      = $factory->getModel('lead');
        $em         = $factory->getEntityManager();
        $properties = $action->getProperties();

        //set the mapped data
        $leadFields = $factory->getModel('lead.field')->getEntities(
            array('filter' => array('isPublished' => true))
        );
        $data = array();

        $lead = $model->getCurrentLead();
        $currentFields = $lead->getFields();

        foreach ($leadFields as $f) {
            $id    = $f->getId();
            $alias = $f->getAlias();
            $type  = $f->getType();

            $data[$alias] = '';

            if (!empty($properties['mappedFields'][$id])) {
                $mappedTo = $properties['mappedFields'][$id];
                if (isset($fields[$mappedTo])) {
                    $fieldName = $fields[$mappedTo]['alias'];
                    if (isset($post[$fieldName])) {
                        $value        = is_array($post[$fieldName]) ? implode(', ', $post[$fieldName]) : $post[$fieldName];
                        $data[$alias] = $value;

                        //update the lead rather than creating a new one if there is for sure identifier match
                        if ($type == 'email') {
                            $leads = $em->getRepository('MauticLeadBundle:Lead')->getLeadsByFieldValue($alias, $value, $lead->getId());
                            if (count($leads)) {
                                //merge with current lead
                                $lead = $model->mergeLeads($lead, $leads[0]);
                            } else {
                                //create a new lead if details differ
                                $currentEmail = $currentFields['core']['email']['value'];
                                if (!empty($currentEmail) && $currentEmail != $value) {
                                    //for sure a different lead so create a new one
                                    $lead = new Lead();
                                    $lead->setNewlyCreated(true);
                                }
                            }
                        }
                    }
                }
            }
        }

        //check for existing IP address
        $ipAddress = $factory->getIpAddress();

        //no lead was found by a mapped email field so create a new one
        if ($lead->isNewlyCreated()) {
            $lead->setPoints($properties['points']);
            $lead->addIpAddress($ipAddress);

            //create a new points change event
            $lead->addPointsChangeLogEntry(
                'form',
                $form->getId() . ":" . $form->getName(),
                $action->getName(),
                $properties['points'],
                $ipAddress
            );
        } else {
            $leadIpAddresses = $lead->getIpAddresses();
            if (!$leadIpAddresses->contains($ipAddress)) {
                $lead->addIpAddress($ipAddress);
            }
        }

        //set the mapped fields
        $model->setFieldValues($lead, $data, false);

        if (!empty($event)) {
            $event->setIpAddress($ipAddress);
            $lead->addPointsChangeLog($event);
        }

        //create a new lead
        $model->saveEntity($lead, false);

        $model->setCurrentLead($lead);

        //return the lead so it can be used elsewhere
        return array('lead' => $lead);
    }

    /**
     * @param array $post
     * @param array $server
     * @param       $fields
     * @param MauticFactory $factory
     * @param       $action
     * @param       $form
     */
    public static function changePoints (array $post, array $server, $fields, MauticFactory $factory, $action, $form)
    {
        $properties = $action->getProperties();

        if (isset($fields[$properties['formField']])) {
            $fieldName = $fields[$properties['formField']]['alias'];
            if (isset($post[$fieldName])) {
                $model = $factory->getModel('lead.lead');
                $em    = $factory->getEntityManager();
                $leads = $em->getRepository('MauticLeadBundle:Lead')->getLeadsByFieldValue(
                    $properties['leadField'],
                    $fieldName
                );

                //check for existing IP address or add one if not exist
                $ipAddress = $factory->getIpAddress();

                //create a new points change event
                $event = new PointsChangeLog();
                $event->setType('form');
                $event->setEventName($form->getId() . ":" . $form->getName());
                $event->setActionName($action->getName());
                $event->setIpAddress($ipAddress);
                $event->setDateAdded(new \DateTime());

                if (count($leads) === 1) {
                    //good to go so update the points
                    $lead = $leads[0];
                } else {
                    switch ($properties['matchMode']) {
                        case 'strict':
                            //no points change since more than one lead matched
                            $lead = false;
                            break;
                        case 'newest':<commit_msg>Fixes a fatal error when submitting a form that changes the lead's points<commit_after><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic Contributors. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

namespace Mautic\LeadBundle\Helper;

use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\PointsChangeLog;

/**
 * Class FormEventHelper
 *
 * @package Mautic\LeadBundle\Helper
 */
class FormEventHelper
{

    /**
     * @param       $action
     * @param       $form
     * @param array $post
     * @param       $factory
     * @param array $fields
     *
     * @return array
     */
    public static function createLead($action, $form, array $post, MauticFactory $factory, array $fields)
    {
        /** @var \Mautic\LeadBundle\Model\LeadModel $model */
        $model      = $factory->getModel('lead');
        $em         = $factory->getEntityManager();
        $properties = $action->getProperties();

        //set the mapped data
        $leadFields = $factory->getModel('lead.field')->getEntities(
            array('filter' => array('isPublished' => true))
        );
        $data = array();

        $lead = $model->getCurrentLead();
        $currentFields = $lead->getFields();

        foreach ($leadFields as $f) {
            $id    = $f->getId();
            $alias = $f->getAlias();
            $type  = $f->getType();

            $data[$alias] = '';

            if (!empty($properties['mappedFields'][$id])) {
                $mappedTo = $properties['mappedFields'][$id];
                if (isset($fields[$mappedTo])) {
                    $fieldName = $fields[$mappedTo]['alias'];
                    if (isset($post[$fieldName])) {
                        $value        = is_array($post[$fieldName]) ? implode(', ', $post[$fieldName]) : $post[$fieldName];
                        $data[$alias] = $value;

                        //update the lead rather than creating a new one if there is for sure identifier match
                        if ($type == 'email') {
                            $leads = $em->getRepository('MauticLeadBundle:Lead')->getLeadsByFieldValue($alias, $value, $lead->getId());
                            if (count($leads)) {
                                //merge with current lead
                                $lead = $model->mergeLeads($lead, $leads[0]);
                            } else {
                                //create a new lead if details differ
                                $currentEmail = $currentFields['core']['email']['value'];
                                if (!empty($currentEmail) && $currentEmail != $value) {
                                    //for sure a different lead so create a new one
                                    $lead = new Lead();
                                    $lead->setNewlyCreated(true);
                                }
                            }
                        }
                    }
                }
            }
        }

        //check for existing IP address
        $ipAddress = $factory->getIpAddress();

        //no lead was found by a mapped email field so create a new one
        if ($lead->isNewlyCreated()) {
            $lead->setPoints($properties['points']);
            $lead->addIpAddress($ipAddress);

            //create a new points change event
            $lead->addPointsChangeLogEntry(
                'form',
                $form->getId() . ":" . $form->getName(),
                $action->getName(),
                $properties['points'],
                $ipAddress
            );
        } else {
            $leadIpAddresses = $lead->getIpAddresses();
            if (!$leadIpAddresses->contains($ipAddress)) {
                $lead->addIpAddress($ipAddress);
            }
        }

        //set the mapped fields
        $model->setFieldValues($lead, $data, false);

        if (!empty($event)) {
            $event->setIpAddress($ipAddress);
            $lead->addPointsChangeLog($event);
        }

        //create a new lead
        $model->saveEntity($lead, false);

        $model->setCurrentLead($lead);

        //return the lead so it can be used elsewhere
        return array('lead' => $lead);
    }

    /**
     * @param array $post
     * @param array $server
     * @param       $fields
     * @param MauticFactory $factory
     * @param       $action
     * @param       $form
     */
    public static function changePoints (array $post, array $server, $fields, MauticFactory $factory, $action, $form)
    {
        $properties = $action->getProperties();

        if (isset($fields[$properties['formField']])) {
            $fieldName = $fields[$properties['formField']]['alias'];
            if (isset($post[$fieldName])) {
                $model = $factory->getModel('lead.lead');
                $em    = $factory->getEntityManager();
                $leads = $em->getRepository('MauticLeadBundle:Lead')->getLeadsByFieldValue(
                    $fieldName,
                    $post[$fieldName]
                );

                //check for existing IP address or add one if not exist
                $ipAddress = $factory->getIpAddress();

                //create a new points change event
                $event = new PointsChangeLog();
                $event->setType('form');
                $event->setEventName($form->getId() . ":" . $form->getName());
                $event->setActionName($action->getName());
                $event->setIpAddress($ipAddress);
                $event->setDateAdded(new \DateTime());

                if (count($leads) === 1) {
                    //good to go so update the points
                    $lead = $leads[0];
                } else {
                    switch ($properties['matchMode']) {
                        case 'strict':
                            //no points change since more than one lead matched
                            $lead = false;
                            break;
                        case 'newest':
{'fraction_non_alphanumeric': 0.13427099517540272, 'fraction_numerical': 0.0016354567012838335, 'mean_word_length': 1.466223028836459, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809027', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3053, 'n_words': 703}