Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | 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\Adapter\DbSelect;
use Laminas\Paginator\Paginator;
use Laminas\Log\LoggerInterface;
use Laminas\Hydrator\ArraySerializableHydrator;


use LeadersLinked\Model\Page;
use LeadersLinked\Mapper\Common\MapperCommon;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;

class PageMapper extends MapperCommon
{
    const _TABLE = 'tbl_pages';

    
    /**
     *
     * @var PageMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return PageMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new PageMapper($adapter);
        }
        return self::$_instance;
    }
    
    
    /**
     *
     * @param int $id
     * @return  Page
     */
    public function fetchOne($id)
    {
        $prototype = new Page();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        $select->limit(1);
        
        $record = $this->executeFetchOneObject($select, $prototype);
        if($record && $record->content) {
            $record->content = html_entity_decode($record->content);
        }
        return $record;
    }
    
    /**
     *
     * @param string $code
     * @param int $network_id
     * @return  Page
     */
    public function fetchOneByCodeAndNetworkId($code, $network_id)
    {
        $prototype = new Page();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('code', $code);
        $select->where->equalTo('network_id', $network_id);
        $select->limit(1);
        
        $record = $this->executeFetchOneObject($select, $prototype);
        if($record && $record->content) {
            $record->content = html_entity_decode($record->content);
        }
        return $record;
    }
    
    /**
     *
     * @param string $search
     * @param int $network_id
     * @param int $page
     * @param int $records_per_page
     * @param string $order_field
     * @param string $order_direction
     * @return Paginator
     */
    public function fetchAllDataTable($search, $network_id, $page = 1, $records_per_page = 10, $order_field= 'title', $order_direction = 'ASC')
    {
        $prototype = new Page();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('network_id', $network_id);
        
        if($search) {
            $select->where->like('id', '%' . $search . '%')->or->like('title', '%' . $search . '%');
        }
        $select->order($order_field . ' ' . $order_direction);
        
        
        
        $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;
    }
    
    /**
     *
     * @return Page[]
     */
    public function fetchAllDefault()
    {
        $prototype = new Page();
        $select = $this->sql->select(self::_TABLE);
        $select->where->isNull('page_default_id');
        
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    
    /**
     *
     * @param Page $page
     * @return boolean
     */
    public function insert($page)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($page);
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        $result = $this->executeInsert($insert);
        
        if($result) {
            $page->id = $this->lastInsertId;
            return true;
        }
        return false;
    }
    
    /**
     *
     * @param Page $page
     * @return boolean
     */
    public function update($page)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($page);
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $page->id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     *
     * @param Page $page
     * @return boolean
     */
    public function delete($page)
    {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('id', $page->id);
        
        return $this->executeDelete($delete);
        
    }
    
    
    /**
     *
     * @return boolean
     */
    public function truncate()
    {
        $sql = sprintf('TRUNCATE TABLE `%s` ', self::_TABLE);
        return $this->executeSentenceWithParameters($sql);
    }

}