Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
 
8
use Laminas\Db\Sql\Expression;
9
use LeadersLinked\Mapper\UserMapper;
10
use Laminas\Paginator\Adapter\DbSelect;
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Db\ResultSet\HydratingResultSet;
13
use LeadersLinked\Model\DiscoveryContactLog;
14
use LeadersLinked\Mapper\Common\MapperCommon;
15
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
16
use LeadersLinked\Model\DiscoveryContact;
17
use Laminas\Paginator\Paginator;
18
 
19
 
20
 
21
class DiscoveryContactLogMapper extends MapperCommon
22
{
23
    const _TABLE = 'tbl_discovery_contact_logs';
24
 
25
    /**
26
     *
27
     * @var DiscoveryContactLogMapper
28
     */
29
    private static $_instance;
30
 
31
    /**
32
     *
33
     * @param AdapterInterface $adapter
34
     */
35
    private function __construct($adapter)
36
    {
37
        parent::__construct($adapter);
38
    }
39
 
40
    /**
41
     *
42
     * @param AdapterInterface $adapter
43
     * @return DiscoveryContactLogMapper
44
     */
45
    public static function getInstance($adapter)
46
    {
47
        if (self::$_instance == null) {
48
            self::$_instance = new DiscoveryContactLogMapper($adapter);
49
        }
50
        return self::$_instance;
51
    }
52
 
53
    /**
54
     *
55
     * @return DiscoveryContact[]
56
     */
57
    public function fetchAll()
58
    {
59
        $prototype = new DiscoveryContactLog();
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->order('id ASC');
62
 
63
        return $this->executeFetchAllObject($select, $prototype);
64
    }
65
 
66
 
67
    public function fetchAllDataByDateRange($user_id, $start_date, $end_date)
68
    {
69
        $select = $this->sql->select();
70
        $select->columns([
71
            'added_on',
72
            'user_id' => new Expression('COUNT(user_id)'),
73
        ]);
74
        $select->from(['dcl' => DiscoveryContactLogMapper::_TABLE]);
75
        $select->join(['u' => UserMapper::_TABLE], 'u.id = dcl.user_id', ['first_name', 'last_name', 'email']);
76
        $select->where->between('dcl.added_on', $start_date, $end_date);
77
        $select->where->equalTo('dcl.activity', 'LABEL_RECORD_CONTACT_ADDED');
78
        $select->group('dcl.added_on');
79
        $select->order('dcl.added_on ASC');
80
 
81
        return $this->executeFetchAllArray($select);
82
    }
83
 
84
    public function anderson($start_date, $end_date)
85
    {
86
        $select = $this->sql->select(self::_TABLE);
87
        $select->columns([
88
            'user_id',
89
            'date' => new Expression('DATE(added_on)'),
90
            'total' => new Expression('COUNT(*)')
91
        ]);
92
 
93
        $select->where->between(new Expression('DATE(added_on)'), $start_date, $end_date);
94
        $select->where->equalTo('activity', 'LABEL_RECORD_CONTACT_ADDED');
95
        $select->group([
96
            'user_id',
97
            new Expression('DATE(added_on)'),
98
        ]);
99
        $select->order('added_on ASC');
100
 
101
        //echo $select->getSqlString($this->adapter->platform); exit;
102
 
103
 
104
        return $this->executeFetchAllArray($select);
105
    }
106
 
107
 
108
    /**
109
     *
110
     * @param int $contact_id
111
     * @return DiscoveryContactLog
112
     */
113
    public function fetchOneFirstByContactId($contact_id)
114
    {
115
 
116
        $select = $this->sql->select(self::_TABLE);
117
        $select->where->equalTo('contact_id', $contact_id);
118
        $select->order('id ASC');
119
 
120
        $prototype = new DiscoveryContactLog();
121
        return $this->executeFetchOneObject($select, $prototype);
122
    }
123
 
124
 
125
    /**
126
     *
127
     * @param int $id
128
     * @return DiscoveryContactLogMapper
129
     */
130
    public function fetchOne($id)
131
    {
132
 
133
        $select = $this->sql->select(self::_TABLE);
134
        $select->where->equalTo('id', $id);
135
 
136
        $prototype = new DiscoveryContactLog();
137
        return $this->executeFetchOneObject($select, $prototype);
138
    }
139
 
140
    /**
141
     *
142
     * @param string $uuid
143
     * @return DiscoveryContactLogMapper
144
     */
145
    public function fetchOneByUuid($uuid)
146
    {
147
 
148
        $select = $this->sql->select(self::_TABLE);
149
        $select->where->equalTo('uuid', $uuid);
150
 
151
        $prototype = new DiscoveryContactLog();
152
        return $this->executeFetchOneObject($select, $prototype);
153
    }
154
 
155
    /**
156
     *
157
     * @param int $company_id
158
     * @param int $contact_id
159
     * @param int $page
160
     * @param int $records_per_page
161
     * @return Paginator
162
     */
163
    public function fetchAllDataTableForCompanyIdAndContactId($company_id, $contact_id,  $page = 1, $records_per_page = 10)
164
    {
165
        $prototype = new DiscoveryContactLog();
166
        $select = $this->sql->select(self::_TABLE);
167
        $select->where->equalTo('company_id', $company_id);
168
        $select->where->equalTo('contact_id', $contact_id);
169
        $select->order('added_on DESC');
170
 
171
        $hydrator   = new ObjectPropertyHydrator();
172
        $resultset  = new HydratingResultSet($hydrator, $prototype);
173
 
174
        $adapter = new DbSelect($select, $this->sql, $resultset);
175
        $paginator = new Paginator($adapter);
176
        $paginator->setItemCountPerPage($records_per_page);
177
        $paginator->setCurrentPageNumber($page);
178
 
179
 
180
        return $paginator;
181
    }
182
 
183
 
184
    /**
185
     *
186
     * @param DiscoveryContactLog $record
187
     * @return boolean
188
     */
189
    public function insert($record)
190
    {
191
        $hydrator = new ObjectPropertyHydrator();
192
        $values = $hydrator->extract($record);
193
        $values = $this->removeEmpty($values);
194
 
195
        $insert = $this->sql->insert(self::_TABLE);
196
        $insert->values($values);
197
        $result = $this->executeInsert($insert);
198
 
199
        if ($result) {
200
            $record->id = $this->lastInsertId;
201
        }
202
 
203
        return $result;
204
    }
205
 
206
 
207
    /**
208
     *
209
     * @param array $values
210
     * @return boolean
211
     */
212
    public function insertRaw($values)
213
    {
214
        $insert = $this->sql->insert(self::_TABLE);
215
        $insert->values($values);
216
        return $this->executeInsert($insert);
217
 
218
    }
219
 
220
 
221
 
222
 
223
    /**
224
     *
225
     * @param DiscoveryContactLog $record
226
     * @return boolean
227
     */
228
    public function delete($record)
229
    {
230
        $delete = $this->sql->delete(self::_TABLE);
231
        $delete->where->equalTo('id', $record->id);
232
 
233
        return $this->executeDelete($delete);
234
    }
235
}