Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

<?php

declare(strict_types=1);

namespace LeadersLinked\Mapper;

use LeadersLinked\Model\Location;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use Laminas\Db\Sql\Expression;


class LocationMapper extends MapperCommon
{
    const _TABLE = 'tbl_locations';

    
    /**
     *
     * @var LocationMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return LocationMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new LocationMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     * 
     * @return Location[]
     */
    public function fetchAll()
    {
        $prototype = new Location;
        $select = $this->sql->select(self::_TABLE);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     * 
     * @param int $id
     * @return Location
     */
    public function fetchOne($id)
    {
        $prototype = new Location;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $uuid
     * @return Location
     */
    public function fetchOneBuUuid($uuid)
    {
        $prototype = new Location;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    
    
    /**
     *
     * @param int $company_id
     * @param double $latitude
     * @param double $longitude
     * @return int[]
     */
    public function fetchAllUserIdsByCompanyId($company_id, $latitude, $longitude)
    {
        
        
        $select = $this->sql->select();
        $select->columns(['user_id' => new Expression('DISTINCT(tb1.user_id)')]);
        $select->from(['tb1' => CompanyUserMapper::_TABLE]);
        $select->join(['tb2' => UserProfileMapper::_TABLE], 'tb1.user_id = tb2.user_id', []);
        $select->join(['tb3' => self::_TABLE], 'tb2.location_id  = tb3.id', []);
        $select->where->equalTo('tb1.company_id', $company_id);
        $select->where->equalTo('tb3.latitude', $latitude);
        $select->where->equalTo('tb3.longitude', $longitude);
        $select->where->isNotNull('tb2.location_id');
        
        $ids = [];
        $records = $this->executeFetchAllArray($select);
        foreach($records as $record)
        {
            array_push($ids, $record['user_id']);
        }
        
        return $ids;
    }
    
    
    /**
     * 
     * @param Location $location
     * @return boolean
     */
    public function insert($location)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($location);
        $values = $this->removeEmpty($values);

        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        $response = $this->executeInsert($insert);
        if($response) {
            $location->id = $this->lastInsertId;
        }
        
        return $response;
        
    }
    
    /**
     *
     * @param Location $location
     * @return location
     */
    public function update($location)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($location);
        $values = $this->removeEmpty($values);
        
        $update= $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $location->id);
        
        //echo $update->getSqlString($this->adapter->platform); exit;
        
        return $this->executeUpdate($update);
    }
    
    /**
     * 
     * @param int $id
     * @return boolean
     */
    public function delete($id)
    {
        
        $delete= $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('id', $id);

        return $this->executeDelete($delete);
    }
}