Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3454 | 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\Db\Sql\Expression;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use Laminas\Log\LoggerInterface;
use Laminas\Paginator\Paginator;
use Laminas\Paginator\Adapter\DbSelect;
use LeadersLinked\Model\CompetencyType;
use LeadersLinked\Mapper\Common\MapperCommon;

class CompetencyTypeMapper extends MapperCommon {

    const _TABLE = 'tbl_competency_types';

    /**
     *
     * @var CompetencyTypeMapper
     */
    private static $_instance;

    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter) {
        parent::__construct($adapter);
    }

    /**
     *
     * @param AdapterInterface $adapter
     * @return CompetencyTypeMapper
     */
    public static function getInstance($adapter) {
        if (self::$_instance == null) {
            self::$_instance = new CompetencyTypeMapper($adapter);
        }
        return self::$_instance;
    }

    /**
     *
     * @param int $id
     * @return CompetencyType
     */
    public function fetchOne($id) {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        $select->limit(1);

        $prototype = new CompetencyType();
        return $this->executeFetchOneObject($select, $prototype);
    }

    /**
     *
     * @param int $company_id
     * @param int $competency_type_id_default
     * @return CompetencyType
     */
    public function fetchOneByCompanyId($company_id, $competency_type_id_default) {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('competency_type_id_default', $competency_type_id_default);
        $select->limit(1);

        $prototype = new CompetencyType();
        return $this->executeFetchOneObject($select, $prototype);
    }

    /**
     *
     * @return CompetencyType[]
     */
    public function fetchAllByDefault() {
        $select = $this->sql->select(self::_TABLE);
        $select->where->isNull('company_id');

        $prototype = new CompetencyType();
        return $this->executeFetchAllObject($select, $prototype);
    }

    /**
     *
     * @param string $uuid
     * @return CompetencyType
     */
    public function fetchOneByUuid($uuid) {
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        $select->limit(1);

        $prototype = new CompetencyType();
        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 fetchAllDataTableDefault($search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC') {
        $prototype = new CompetencyType();
        $select = $this->sql->select(self::_TABLE);
        $select->where->isNull('company_id');

        if ($search) {
            $select->where->like('name', '%' . $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;
    }

    /**
     *
     * @param int $company_id
     * @param string $search
     * @param int $page
     * @param int $records_per_page
     * @param string $order_field
     * @param string $order_direction
     * @return Paginator
     */
    public function fetchAllDataTableByCompanyId($company_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC') {
        $prototype = new CompetencyType();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);


        if ($search) {
            $select->where->NEST->like('name', '%' . $search . '%')->UNNEST;
        }
        $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;
    }

    /**
     *
     * @param CompetencyType $competencyType
     * @return boolean
     */
    public function insert($competencyType) {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($competencyType);

        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);


        $result = $this->executeInsert($insert);
        if ($result) {
            $competencyType->id = $this->lastInsertId;
        }

        return $result;
    }

    /**
     *
     * @param CompetencyType $competencyType
     * @return boolean
     */
    public function update($competencyType) {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($competencyType);

        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $competencyType->id);

        return $this->executeUpdate($update);
    }

    /**
     *
     * @param CompetencyType $competencyType
     * @return boolean
     */
    public function delete($competencyType) {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('id', $competencyType->id);

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

    /**
     *
     * @return boolean
     */
    public function truncate() {
        $sql = 'DELETE FROM ' . self::_TABLE;
        if ($this->executeSentenceWithParameters($sql)) {
            $sql = 'ALTER TABLE ' . self::_TABLE . ' AUTO_INCREMENT = 1 ';
            return $this->executeSentenceWithParameters($sql);
        }
        return false;
    }

    /**
     *
     * @return CompetencyType[]
     */
    public function fetchAllActive() {
        $prototype = new CompetencyType();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('status', CompetencyType::STATUS_ACTIVE);
        $select->order('name ASC');

        return $this->executeFetchAllObject($select, $prototype);
    }

    /**
     *
     * @return CompetencyType[]
     */
    public function fetchAllActiveByDefault() {
        $prototype = new CompetencyType();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('status', CompetencyType::STATUS_ACTIVE);
        $select->where->isNull('company_id');
        $select->order('name ASC');

        return $this->executeFetchAllObject($select, $prototype);
    }

    /**
     *
     * @return CompetencyType[]
     */
    public function fetchAllActiveByCompanyId($company_id) {
        $prototype = new CompetencyType();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('status', CompetencyType::STATUS_ACTIVE);
        $select->order('name ASC');

        return $this->executeFetchAllObject($select, $prototype);
    }

    /**
     *
     * @return CompetencyType[]
     */
    public function fetchAllByCompanyId($company_id) {
        $prototype = new CompetencyType();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        $select->order('name ASC');

        return $this->executeFetchAllObject($select, $prototype);
    }

}