Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
15397 efrain 1
<?php
15663 anderson 2
 
15397 efrain 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
16768 efrain 9
 
15397 efrain 10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Log\LoggerInterface;
12
 
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Library\Functions;
16
use LeadersLinked\Mapper\AptitudeMapper;
17
use LeadersLinked\Form\AptitudeForm;
18
use LeadersLinked\Model\Aptitude;
19
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
20
use LeadersLinked\Mapper\DiscoveryContactMapper;
21
use LeadersLinked\Mapper\QueryMapper;
22
use LeadersLinked\Model\DiscoveryContactLog;
23
use LeadersLinked\Mapper\DiscoveryContactLogMapper;
24
use LeadersLinked\Mapper\UserMapper;
25
use Laminas\Hydrator\Iterator\HydratingArrayIterator;
26
use Laminas\Paginator\Adapter\DbSelect;
27
use Laminas\Paginator\Paginator;
28
 
29
 
30
class DiscoveryContactLogController extends AbstractActionController
31
{
16768 efrain 32
        /**
15397 efrain 33
     *
34
     * @var AdapterInterface
35
     */
36
    private $adapter;
15663 anderson 37
 
15397 efrain 38
    /**
39
     *
40
     * @var  LoggerInterface
41
     */
42
    private $logger;
15663 anderson 43
 
15397 efrain 44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
15663 anderson 49
 
15397 efrain 50
    /**
51
     *
52
     * @param AdapterInterface $adapter
53
     * @param LoggerInterface $logger
54
     * @param array $config
55
     */
16768 efrain 56
    public function __construct($adapter, $logger, $config)
15397 efrain 57
    {
16768 efrain 58
        $this->adapter = $adapter;
59
        $this->logger = $logger;
60
        $this->config = $config;
15397 efrain 61
    }
15663 anderson 62
 
15397 efrain 63
    public function indexAction()
64
    {
65
 
66
        $request = $this->getRequest();
15663 anderson 67
        if ($request->isGet()) {
68
 
15397 efrain 69
            $request = $this->getRequest();
70
            $uuid = $this->params()->fromRoute('id');
15663 anderson 71
 
72
 
15665 anderson 73
            if (!$uuid) {
74
                $data = [
75
                    'success'   => false,
76
                    'data'   => 'ERROR_INVALID_PARAMETER'
77
                ];
15663 anderson 78
 
15665 anderson 79
                return new JsonModel($data);
80
            }
15663 anderson 81
 
15665 anderson 82
            $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter);
83
            $discoveryContact = $discoveryContactMapper->fetchOneByUuid($uuid);
84
            if (!$discoveryContact) {
85
                $data = [
86
                    'success'   => false,
87
                    'data'   => 'ERROR_RECORD_NOT_FOUND'
88
                ];
15663 anderson 89
 
15665 anderson 90
                return new JsonModel($data);
91
            }
15663 anderson 92
 
15397 efrain 93
            $search = $this->params()->fromQuery('search');
16766 efrain 94
            $search = empty($search) ? '' : Functions::sanitizeFilterString($search);
15663 anderson 95
 
15397 efrain 96
            $page               = intval($this->params()->fromQuery('start', 1), 10);
97
            $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
15663 anderson 98
 
99
 
15397 efrain 100
            $queryMapper = QueryMapper::getInstance($this->adapter);
101
            $select = $queryMapper->getSql()->select();
15663 anderson 102
            $select->from(['tb1' => DiscoveryContactLogMapper::_TABLE]);
15397 efrain 103
            $select->columns(['activity', 'details', 'added_on']);
15663 anderson 104
            $select->join(['tb2' => UserMapper::_TABLE], 'tb1.user_id = tb2.id', ['first_name', 'last_name', 'email']);
15397 efrain 105
            $select->where->equalTo('company_id', $discoveryContact->company_id);
106
            $select->where->equalTo('contact_id', $discoveryContact->id);
15663 anderson 107
            if ($search) {
15397 efrain 108
                $select->where->nest()
15663 anderson 109
                    ->like('first_name', '%' . $search . '%')
110
                    ->or->like('last_name', '%' . $search . '%')
111
                    ->or->like('email', '%' . $search . '%')
112
                    ->or->like('activity', '%' . $search . '%')
113
                    ->unnest();
15397 efrain 114
            }
15663 anderson 115
 
15397 efrain 116
            $select->order('added_on DESC');
15663 anderson 117
 
15397 efrain 118
            $page               = intval($this->params()->fromQuery('start', 1), 10);
119
 
120
            $adapter = new DbSelect($select, $queryMapper->getSql());
121
            $paginator = new Paginator($adapter);
122
            $paginator->setItemCountPerPage($records_x_page);
123
            $paginator->setCurrentPageNumber($page);
124
 
15663 anderson 125
 
15397 efrain 126
            $items = [];
127
            $records = $paginator->getCurrentItems();
15663 anderson 128
            foreach ($records as $record) {
129
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record['added_on']);
130
 
15397 efrain 131
                $item = [
132
                    'first_name' => $record['first_name'],
133
                    'last_name' => $record['last_name'],
134
                    'email' => $record['email'],
135
                    'activity' => $record['activity'],
136
                    'details' => $record['details'],
137
                    'added_on' => $dt->format('d/m/Y H:i a'),
138
                ];
15663 anderson 139
 
15397 efrain 140
                array_push($items, $item);
141
            }
15663 anderson 142
 
15397 efrain 143
            $total_items = $paginator->getTotalItemCount();
15663 anderson 144
            if ($total_items <= 10) {
145
                $total_pages = 1;
15397 efrain 146
            } else {
15663 anderson 147
 
148
                $total_pages = ($total_items / 10);
149
 
150
 
151
                if (($total_pages * 10) <  $total_items) {
15397 efrain 152
                    $total_pages++;
153
                }
154
            }
15663 anderson 155
 
156
 
15397 efrain 157
            return new JsonModel([
158
                'success' => true,
159
                'data' => [
160
                    'current' => [
161
                        'items' => $items,
162
                        'page' => $paginator->getCurrentPageNumber()
15663 anderson 163
 
164
                    ],
15397 efrain 165
                    'total' => [
15663 anderson 166
 
15397 efrain 167
                        'items' => $total_items,
168
                        'pages' => $total_pages
15663 anderson 169
                    ],
15397 efrain 170
                ]
171
            ]);
172
        } else {
173
            return new JsonModel([
174
                'success' => false,
175
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
176
            ]);;
177
        }
178
    }
179
}