Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

<?php

declare(strict_types=1);

namespace LeadersLinked\Mapper;

use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Paginator\Paginator;
use Laminas\Paginator\Adapter\DbSelect;
use LeadersLinked\Model\Network;
use LeadersLinked\Mapper\Common\MapperCommon;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;



class NetworkMapper extends MapperCommon
{
    const _TABLE = 'tbl_networks';

    
    /**
     *
     * @var NetworkMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return NetworkMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new NetworkMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     *
     * @return Network
     */
    public function fetchOneByDefault()
    {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('default', Network::DEFAULT_YES);
        $select->limit(1);
        
        $prototype = new Network();
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $id
     * @return Network
     */
    public function fetchOne($id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        $select->limit(1);
        
        $prototype = new Network();
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @return Network[]
     */
    public function fetchAllActives()
    {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('status', Network::STATUS_ACTIVE);
        $select->order('name');
        
        
        $prototype = new Network();
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @return Network[]
     */
    public function fetchAll()
    {
        $select = $this->sql->select(self::_TABLE);
        $select->order('name');
        
        
        $prototype = new Network();
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    
    
    /**
     *
     * @param string $hostname
     * @return Network
     */
    public function fetchOneByHostnameForFrontend($hostname)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('main_hostname', $hostname);
        $select->where->or->equalTo('alternative_hostname', $hostname);
        $select->limit(1);
        
        $prototype = new Network();
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param string $hostname
     * @return Network
     */
    public function fetchOneByHostnameForBackend($hostname)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('admin_hostname', $hostname);
        $select->limit(1);
        
        $prototype = new Network();
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param string $uuid
     * @return Network
     */
    public function fetchOneByUuid($uuid)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        $select->limit(1);
        
        $prototype = new Network();
        return $this->executeFetchOneObject($select, $prototype);
    }

   

    
    /**
     *
     * @param string $search
     * @param int $page
     * @param int $records_per_page
     * @param string $order_field
     * @param string $order_direction
     * @return Paginator
     */
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
    {
        $prototype = new Network();
        $select = $this->sql->select();
        $select->from(['d' => self::_TABLE]);
        $select->columns(['id', 'uuid', 'name', 'main_hostname', 'admin_hostname', 'status', 'default']);
        
        if($search) {
            $select->where->like('name', '%' . $search . '%');
        }
        $select->order($order_field . ' ' . $order_direction);
        
      // echo $select->getSqlString($this->adapter->platform); exit;
        
        $hydrator   = new ObjectPropertyHydrator();
        $resultset  = new HydratingResultSet($hydrator, $prototype);
        
        $adapter = new DbSelect($select, $this->sql, $resultset);
        $paginator = new Paginator($adapter);
        $paginator->setItemCountPerPage($records_per_page);
        $paginator->setCurrentPageNumber($page);
        
        
        return $paginator;
    }
    
    
   

    
    /**
     *
     * @param Network $network
     * @return boolean
     */
    public function insert($network)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($network);
        $values = $this->removeEmpty($values);
        $values['moodle_url'] = empty($values['moodle_url']) ? '' : $values['moodle_url'];
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        //echo $insert->getSqlString($this->adapter->platform); exit;
        $result =  $this->executeInsert($insert);
        if($result)  {
            $network->id = $this->lastInsertId;
        }
        
        return $result;
        
    }
    
    /**
     *
     * @param Network $network
     * @return boolean
     */
    public function update($network)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($network);
        $values = $this->removeEmpty($values);
        $values['moodle_url'] = empty($values['moodle_url']) ? '' : $values['moodle_url'];
        
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $network->id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     *
     * @param int $id
     * @return boolean
     */
    public function delete($id)
    {
        $sql = 'CALL sp_delete_network(?)';
        $parameters = [$id];
        
        return $this->executeDeleteUsingSentenceWithParameters($sql, $parameters);
        
        
       // $delete = $this->sql->delete(self::_TABLE);
       // $delete->where->equalTo('id', $id);
        
       // return $this->executeDelete($delete);
        
    }
    
    
    

    
    
   
}