Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15737 | Rev 15739 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15670 anderson 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
15718 anderson 7
use LeadersLinked\Model\User;
15682 anderson 8
use Laminas\View\Model\JsonModel;
9
use Laminas\View\Model\ViewModel;
15718 anderson 10
use LeadersLinked\Mapper\UserMapper;
11
use LeadersLinked\Mapper\QueryMapper;
12
use LeadersLinked\Mapper\DiscoveryContactMapper;
15682 anderson 13
use LeadersLinked\Mapper\DiscoveryContactLogMapper;
15670 anderson 14
use Laminas\Mvc\Controller\AbstractActionController;
15727 anderson 15
use Laminas\Hydrator\ArraySerializableHydrator;
16
use Laminas\Db\ResultSet\HydratingResultSet;
17
use Laminas\Paginator\Adapter\DbSelect;
18
use Laminas\Paginator\Paginator;
15732 anderson 19
use LeadersLinked\Mapper\CompanyMapper;
15670 anderson 20
 
21
// Create an action controller.
22
class DiscoveryContactProgressController extends AbstractActionController
23
{
15682 anderson 24
    /**
25
     *
26
     * @var AdapterInterface
27
     */
28
    private $adapter;
29
 
30
 
31
    /**
32
     *
33
     * @var AbstractAdapter
34
     */
35
    private $cache;
36
 
37
    /**
38
     *
39
     * @var  LoggerInterface
40
     */
41
    private $logger;
42
 
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $config;
48
 
49
    /**
50
     *
51
     * @param AdapterInterface $adapter
52
     * @param AbstractAdapter $cache
53
     * @param LoggerInterface $logger
54
     * @param array $config
55
     */
56
    public function __construct($adapter, $cache, $logger, $config)
57
    {
58
        $this->adapter      = $adapter;
59
        $this->cache        = $cache;
60
        $this->logger       = $logger;
61
        $this->config       = $config;
62
    }
63
 
15670 anderson 64
    // Define an action "world".
65
    public function indexAction()
66
    {
15674 anderson 67
        $currentUserPlugin = $this->plugin('currentUserPlugin');
68
        $currentUser = $currentUserPlugin->getUser();
69
        $currentCompany = $currentUserPlugin->getCompany();
15670 anderson 70
 
15674 anderson 71
        $request = $this->getRequest();
72
        if ($request->isGet()) {
15677 anderson 73
            $headers  = $request->getHeaders();
74
            $isJson = false;
15678 anderson 75
 
15680 anderson 76
            if ($headers->has('Accept')) {
77
                $accept = $headers->get('Accept');
78
                $prioritized = $accept->getPrioritized();
15679 anderson 79
 
15680 anderson 80
                foreach ($prioritized as $key => $value) {
81
                    $raw = trim($value->getRaw());
15679 anderson 82
 
15680 anderson 83
                    if (!$isJson) {
84
                        $isJson = strpos($raw, 'json');
85
                    }
15679 anderson 86
                }
87
            }
88
 
15699 anderson 89
            //if ($isJson) {
90
            $search = $this->params()->fromQuery('search');
91
            $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
92
            $page               = intval($this->params()->fromQuery('start', 1), 10);
93
            $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
94
            $order =  $this->params()->fromQuery('order', []);
95
            $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
96
            $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
97
            $fields =  ['first_name', 'last_name', 'corporate_email', 'activity'];
98
            $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
15682 anderson 99
 
15699 anderson 100
            if (!in_array($order_direction, ['ASC', 'DESC'])) {
101
                $order_direction = 'ASC';
102
            }
15682 anderson 103
 
15687 anderson 104
 
15699 anderson 105
            //Quede aqui en el mapper
15727 anderson 106
            $discoveryContactLogMapper = DiscoveryContactLogMapper::getInstance($this->adapter);
15729 anderson 107
            $contactLogMappers = $discoveryContactLogMapper->fetchAll();
15717 anderson 108
 
15729 anderson 109
            foreach ($contactLogMappers as $contactLogMapper) {
15722 anderson 110
                $queryMapper = QueryMapper::getInstance($this->adapter);
111
                $sql = $queryMapper->getSql();
112
                $select = $sql->select();
15738 anderson 113
                $select->columns(['user_id', 'activity']);
15724 anderson 114
                $select->from(['tb1' => DiscoveryContactLogMapper::_TABLE]);
15731 anderson 115
                $select->join(['tb2' => UserMapper::_TABLE], 'tb1.user_id = tb2.id', ['uuid', 'first_name', 'last_name', 'email']);
15732 anderson 116
                $select->join(['tb3' => CompanyMapper::_TABLE], 'tb1.company_id = tb3.id', ['name']);
15731 anderson 117
                //$select->where->equalTo('tb1.company_id', $contactLogMapper->company_id);
15727 anderson 118
 
119
                if ($search) {
120
                    $select->where->nest()
121
                        ->like('first_name', '%' . $search . '%')
122
                        ->or->like('last_name', '%' . $search . '%')
123
                        ->or->like('email', '%' . $search . '%')
124
                        ->unnest();
125
                }
126
 
127
                $select->order($order_field . ' ' . $order_direction);
15728 anderson 128
                $hydrator   = new ArraySerializableHydrator();
15727 anderson 129
                $resultset  = new HydratingResultSet($hydrator);
130
                $adapter = new DbSelect($select, $sql, $resultset);
131
                $paginator = new Paginator($adapter);
132
                $paginator->setItemCountPerPage($records_x_page);
133
                $paginator->setCurrentPageNumber($page);
134
 
135
 
136
                $items = [];
137
                $records = $paginator->getCurrentItems();
15733 anderson 138
                foreach ($records as $record) {
139
 
15735 anderson 140
                    // $dt_added_on = \DateTime::createFromFormat('Y-m-d H:i:s', $record['added_on']);
141
                    // $added_on = $dt_added_on->format('d/m/Y h:i a');
15733 anderson 142
                    $item = [
143
                        'uuid' => $record['uuid'],
144
                        'first_name' => ucwords(strtolower($record['first_name'])),
145
                        'last_name' => ucwords(strtolower($record['last_name'])),
146
                        'email' => strtolower($record['email']),
15736 anderson 147
                        'company' => $record['name'],
15737 anderson 148
                        //'activity' => $record['activity'],
15734 anderson 149
                        //'text' => $record['text'],
150
                        //'added_on' => $added_on,
15733 anderson 151
                    ];
152
 
153
 
154
                    array_push($items, $item);
155
                    return new JsonModel([
156
                        'success' => true,
157
                        'message' => $items
158
                    ]);
159
                }
15711 anderson 160
            }
15699 anderson 161
            // } else {
162
            // }
15675 anderson 163
        }
15690 anderson 164
 
165
 
15701 anderson 166
        return new JsonModel([
167
            'success' => true,
168
            'message' => $currentCompany->id
169
        ]);
15670 anderson 170
    }
171
}