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

//@todo - write merge action
//@todo - write export action

namespace Mautic\LeadBundle\Controller;

use Mautic\CoreBundle\Controller\FormController;
use Mautic\LeadBundle\LeadEvents;
use Mautic\LeadBundle\Event\LeadTimelineEvent;
use Mautic\SocialBundle\Helper\NetworkIntegrationHelper;
use Symfony\Component\HttpFoundation\JsonResponse;

class LeadController extends FormController
{

    /**
     * @param int    $page
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function indexAction($page = 1)
    {
        //set some permissions
        $permissions = $this->factory->getSecurity()->isGranted(array(
            'lead:leads:viewown',
            'lead:leads:viewother',
            'lead:leads:create',
            'lead:leads:editown',
            'lead:leads:editother',
            'lead:leads:deleteown',
            'lead:leads:deleteother'
        ), "RETURN_ARRAY");

        if (!$permissions['lead:leads:viewown'] || !$permissions['lead:leads:viewother']) {
            return $this->accessDenied();
        }

        /** @var \Mautic\LeadBundle\Model\LeadModel $model */
        $model   = $this->factory->getModel('lead.lead');
        $session = $this->factory->getSession();
        //set limits
        $limit = $session->get('mautic.lead.limit',$this->factory->getParameter('default_pagelimit'));
        $start = ($page === 1) ? 0 : (($page-1) * $limit);
        if ($start < 0) {
            $start = 0;
        }

        $search     = $this->request->get('search', $session->get('mautic.lead.filter', ''));
        $session->set('mautic.lead.filter', $search);

        //do some default filtering
        $orderBy     = $this->factory->getSession()->get('mautic.lead.orderby', 'l.date_added');
        $orderByDir  = $this->factory->getSession()->get('mautic.lead.orderbydir', 'ASC');

        $filter      = array('string' => $search, 'force' => '');
        $translator  = $this->factory->getTranslator();
        $isCommand   = $translator->trans('mautic.core.searchcommand.is');
        $anonymous   = $translator->trans('mautic.lead.lead.searchcommand.isanonymous');
        $listCommand = $translator->trans('mautic.lead.lead.searchcommand.list');
        $mine        = $translator->trans('mautic.core.searchcommand.ismine');

        if (strpos($search, "$isCommand:$anonymous") === false && strpos($search, "$listCommand:") === false) {
            //todo - maybe add this back?
            //remove anonymous leads unless requested to prevent clutter
            //$filter['force'] .= " !$isCommand:$anonymous";
        }
        if (!$permissions['lead:leads:viewother']) {
            $filter['force'] .= " $isCommand:$mine";
        }

        $results = $model->getEntities(
            array(
                'start'          => $start,
                'limit'          => $limit,
                'filter'         => $filter,
                'orderBy'        => $orderBy,
                'orderByDir'     => $orderByDir,
                'withTotalCount' => true
            ));
        $count = $results['count'];
        unset($results['count']);

        $leads = $results['results'];
        unset($results);

        // Get the quick add form
        $action = $this->generateUrl('mautic_lead_action', array('objectAction' => 'new'));

        $fields = $this->factory->getModel('lead.field')->getEntities(array(
            'filter' => array(
                'force' => array(
                    array(
                        'column' => 'f.isPublished',
                        'expr'   => 'eq',
                        'value'  => true
                    ),
                    array(
                        'column' => 'f.isShortVisible',
                        'expr'   => 'eq',
                        'value'  => true
                    )
                )
            ),
            'hydration_mode' => 'HYDRATE_ARRAY'
        ));

        $quickForm     = $model->createForm($model->getEntity(), $this->get('form.factory'), $action, array('fields' => $fields, 'isShortForm' => true));
        $quickFormView = $this->setFormTheme($quickForm, 'MauticLeadBundle:Lead:form.html.php', 'MauticLeadBundle:FormRow12');

        if ($count && $count < ($start + 1)) {
            //the number of entities are now less then the current page so redirect to the last page
            if ($count === 1) {
                $lastPage = 1;
            } else {
                $lastPage = (floor($limit / $count)) ? : 1;
            }
            $session->set('mautic.lead.page', $lastPage);
            $returnUrl   = $this->generateUrl('mautic_lead_index', array('page' => $lastPage));

            return $this->postActionRedirect(array(
                'returnUrl'       => $returnUrl,
                'viewParameters'  => array('page' => $lastPage),
                'contentTemplate' => 'MauticLeadBundle:Lead:index',
                'passthroughVars' => array(
                    'activeLink'    => '#mautic_lead_index',
                    'mauticContent' => 'lead'
                )
            ));
        }

        //set what page currently on so that we can return here after form submission/cancellation
        $session->set('mautic.lead.page', $page);

        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';

        $listArgs = array();
        if (!$this->factory->getSecurity()->isGranted('lead:lists:viewother')) {
            $listArgs["filter"]["force"] = " $isCommand:$mine";
        }

        $lists     = $this->factory->getModel('lead.list')->getUserLists();
        $indexMode = $this->request->get('view', $session->get('mautic.lead.indexmode', 'list'));
        $session->set('mautic.lead.indexmode', $indexMode);

        //check to see if in a single list
        $inSingleList = (substr_count($search, "$listCommand:") === 1) ? true : false;
        $list         = array();
        if ($inSingleList) {
            preg_match("/$listCommand:(.*?)(?=\s|$)/", $search, $matches);

            if (!empty($matches[1])) {
                $alias = $matches[1];
                foreach ($lists as $l) {
                    if ($alias === $l['alias']) {
                        $list = $l;<commit_msg>Hide anonymous leads from card view.<commit_after><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic, NP. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.com
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

//@todo - write merge action
//@todo - write export action

namespace Mautic\LeadBundle\Controller;

use Mautic\CoreBundle\Controller\FormController;
use Mautic\LeadBundle\LeadEvents;
use Mautic\LeadBundle\Event\LeadTimelineEvent;
use Mautic\SocialBundle\Helper\NetworkIntegrationHelper;
use Symfony\Component\HttpFoundation\JsonResponse;

class LeadController extends FormController
{

    /**
     * @param int    $page
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function indexAction($page = 1)
    {
        //set some permissions
        $permissions = $this->factory->getSecurity()->isGranted(array(
            'lead:leads:viewown',
            'lead:leads:viewother',
            'lead:leads:create',
            'lead:leads:editown',
            'lead:leads:editother',
            'lead:leads:deleteown',
            'lead:leads:deleteother'
        ), "RETURN_ARRAY");

        if (!$permissions['lead:leads:viewown'] || !$permissions['lead:leads:viewother']) {
            return $this->accessDenied();
        }

        /** @var \Mautic\LeadBundle\Model\LeadModel $model */
        $model   = $this->factory->getModel('lead.lead');
        $session = $this->factory->getSession();
        //set limits
        $limit = $session->get('mautic.lead.limit',$this->factory->getParameter('default_pagelimit'));
        $start = ($page === 1) ? 0 : (($page-1) * $limit);
        if ($start < 0) {
            $start = 0;
        }

        $search     = $this->request->get('search', $session->get('mautic.lead.filter', ''));
        $session->set('mautic.lead.filter', $search);

        //do some default filtering
        $orderBy     = $this->factory->getSession()->get('mautic.lead.orderby', 'l.date_added');
        $orderByDir  = $this->factory->getSession()->get('mautic.lead.orderbydir', 'ASC');

        $filter      = array('string' => $search, 'force' => '');
        $translator  = $this->factory->getTranslator();
        $isCommand   = $translator->trans('mautic.core.searchcommand.is');
        $anonymous   = $translator->trans('mautic.lead.lead.searchcommand.isanonymous');
        $listCommand = $translator->trans('mautic.lead.lead.searchcommand.list');
        $mine        = $translator->trans('mautic.core.searchcommand.ismine');
        $indexMode   = $this->request->get('view', $session->get('mautic.lead.indexmode', 'list'));
        
        $session->set('mautic.lead.indexmode', $indexMode);

        // (strpos($search, "$isCommand:$anonymous") === false && strpos($search, "$listCommand:") === false)) ||  
        if ($indexMode != 'list') {
            //remove anonymous leads unless requested to prevent clutter
            $filter['force'] .= " !$isCommand:$anonymous";
        }
        
        if (!$permissions['lead:leads:viewother']) {
            $filter['force'] .= " $isCommand:$mine";
        }

        $results = $model->getEntities(
            array(
                'start'          => $start,
                'limit'          => $limit,
                'filter'         => $filter,
                'orderBy'        => $orderBy,
                'orderByDir'     => $orderByDir,
                'withTotalCount' => true
            ));
        $count = $results['count'];
        unset($results['count']);

        $leads = $results['results'];
        unset($results);

        // Get the quick add form
        $action = $this->generateUrl('mautic_lead_action', array('objectAction' => 'new'));

        $fields = $this->factory->getModel('lead.field')->getEntities(array(
            'filter' => array(
                'force' => array(
                    array(
                        'column' => 'f.isPublished',
                        'expr'   => 'eq',
                        'value'  => true
                    ),
                    array(
                        'column' => 'f.isShortVisible',
                        'expr'   => 'eq',
                        'value'  => true
                    )
                )
            ),
            'hydration_mode' => 'HYDRATE_ARRAY'
        ));

        $quickForm     = $model->createForm($model->getEntity(), $this->get('form.factory'), $action, array('fields' => $fields, 'isShortForm' => true));
        $quickFormView = $this->setFormTheme($quickForm, 'MauticLeadBundle:Lead:form.html.php', 'MauticLeadBundle:FormRow12');

        if ($count && $count < ($start + 1)) {
            //the number of entities are now less then the current page so redirect to the last page
            if ($count === 1) {
                $lastPage = 1;
            } else {
                $lastPage = (floor($limit / $count)) ? : 1;
            }
            $session->set('mautic.lead.page', $lastPage);
            $returnUrl   = $this->generateUrl('mautic_lead_index', array('page' => $lastPage));

            return $this->postActionRedirect(array(
                'returnUrl'       => $returnUrl,
                'viewParameters'  => array('page' => $lastPage),
                'contentTemplate' => 'MauticLeadBundle:Lead:index',
                'passthroughVars' => array(
                    'activeLink'    => '#mautic_lead_index',
                    'mauticContent' => 'lead'
                )
            ));
        }

        //set what page currently on so that we can return here after form submission/cancellation
        $session->set('mautic.lead.page', $page);

        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';

        $listArgs = array();
        if (!$this->factory->getSecurity()->isGranted('lead:lists:viewother')) {
            $listArgs["filter"]["force"] = " $isCommand:$mine";
        }

        $lists     = $this->factory->getModel('lead.list')->getUserLists();

        //check to see if in a single list
        $inSingleList = (substr_count($search, "$listCommand:") === 1) ? true : false;
        $list         = array();
        if ($inSingleList) {
            preg_match("/$listCommand:(.*?)(?=\s|$)/", $search, $matches);

            if (!empty($matches[1])) {
                $alias = $matches[1];
                foreach ($lists as $l) {
                    if ($alias === $l['alias']) {
                        $list = $l;
{'fraction_non_alphanumeric': 0.1661167804803192, 'fraction_numerical': 0.0033760454231566025, 'mean_word_length': 1.9656427758816837, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 167, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5006974', 'n_tokens_mistral': 3915, 'n_tokens_neox': 3709, 'n_words': 663}