Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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