Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use LeadersLinked\Model\Location;
8
use LeadersLinked\Mapper\Common\MapperCommon;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Log\LoggerInterface;
11
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
7328 efrain 12
use Laminas\Db\Sql\Expression;
1 www 13
 
14
 
15
class LocationMapper extends MapperCommon
16
{
17
    const _TABLE = 'tbl_locations';
18
 
19
 
20
    /**
21
     *
22
     * @var LocationMapper
23
     */
24
    private static $_instance;
25
 
26
    /**
27
     *
28
     * @param AdapterInterface $adapter
29
     */
30
    private function __construct($adapter)
31
    {
32
        parent::__construct($adapter);
33
    }
34
 
35
    /**
36
     *
37
     * @param AdapterInterface $adapter
38
     * @return LocationMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new LocationMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
49
     *
50
     * @return Location[]
51
     */
52
    public function fetchAll()
53
    {
54
        $prototype = new Location;
55
        $select = $this->sql->select(self::_TABLE);
56
 
57
        return $this->executeFetchAllObject($select, $prototype);
58
    }
59
 
60
    /**
61
     *
62
     * @param int $id
63
     * @return Location
64
     */
65
    public function fetchOne($id)
66
    {
67
        $prototype = new Location;
68
        $select = $this->sql->select(self::_TABLE);
69
        $select->where->equalTo('id', $id);
70
 
71
        return $this->executeFetchOneObject($select, $prototype);
72
    }
73
 
74
 
75
    /**
76
     *
77
     * @param int $uuid
78
     * @return Location
79
     */
80
    public function fetchOneBuUuid($uuid)
81
    {
82
        $prototype = new Location;
83
        $select = $this->sql->select(self::_TABLE);
84
        $select->where->equalTo('uuid', $uuid);
85
 
86
        return $this->executeFetchOneObject($select, $prototype);
87
    }
88
 
7328 efrain 89
 
90
 
91
 
1 www 92
    /**
7328 efrain 93
     *
94
     * @param int $company_id
95
     * @param double $latitude
96
     * @param double $longitude
97
     * @return int[]
98
     */
99
    public function fetchAllUserIdsByCompanyId($company_id, $latitude, $longitude)
100
    {
101
 
102
 
103
        $select = $this->sql->select();
104
        $select->columns(['user_id' => new Expression('DISTINCT(tb1.user_id)')]);
105
        $select->from(['tb1' => CompanyUserMapper::_TABLE]);
106
        $select->join(['tb2' => UserProfileMapper::_TABLE], 'tb1.user_id = tb2.user_id', []);
107
        $select->join(['tb3' => self::_TABLE], 'tb2.location_id  = tb3.id', []);
108
        $select->where->equalTo('tb1.company_id', $company_id);
109
        $select->where->equalTo('tb3.latitude', $latitude);
110
        $select->where->equalTo('tb3.longitude', $longitude);
111
        $select->where->isNotNull('tb2.location_id');
112
 
113
        $ids = [];
114
        $records = $this->executeFetchAllArray($select);
115
        foreach($records as $record)
116
        {
117
            array_push($ids, $record['user_id']);
118
        }
119
 
120
        return $ids;
121
    }
122
 
123
 
124
    /**
1 www 125
     *
126
     * @param Location $location
127
     * @return boolean
128
     */
129
    public function insert($location)
130
    {
131
        $hydrator = new ObjectPropertyHydrator();
132
        $values = $hydrator->extract($location);
133
        $values = $this->removeEmpty($values);
134
 
135
 
136
        $insert = $this->sql->insert(self::_TABLE);
137
        $insert->values($values);
138
 
139
        $response = $this->executeInsert($insert);
140
        if($response) {
141
            $location->id = $this->lastInsertId;
142
        }
143
 
144
        return $response;
145
 
146
    }
147
 
148
    /**
149
     *
150
     * @param Location $location
151
     * @return location
152
     */
153
    public function update($location)
154
    {
155
        $hydrator = new ObjectPropertyHydrator();
156
        $values = $hydrator->extract($location);
157
        $values = $this->removeEmpty($values);
158
 
159
        $update= $this->sql->update(self::_TABLE);
160
        $update->set($values);
161
        $update->where->equalTo('id', $location->id);
162
 
163
        //echo $update->getSqlString($this->adapter->platform); exit;
164
 
165
        return $this->executeUpdate($update);
166
    }
167
 
168
    /**
169
     *
170
     * @param int $id
171
     * @return boolean
172
     */
173
    public function delete($id)
174
    {
175
 
176
        $delete= $this->sql->delete(self::_TABLE);
177
        $delete->where->equalTo('id', $id);
178
 
179
        return $this->executeDelete($delete);
180
    }
181
}