Proyectos de Subversion LeadersLinked - Services

Rev

Rev 618 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php
declare(strict_types=1);

namespace LeadersLinked\Mapper;

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


class MicrolearningTopicCapsuleMapper extends MapperCommon
{
    const _TABLE = 'tbl_microlearning_capsules';
    
    /**
     *
     * @var MicrolearningTopicCapsuleMapper
     */
    private static $_instance;
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return MicrolearningTopicCapsuleMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new MicrolearningTopicCapsuleMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     * 
     * @param int $company_id
     * @param int $topic_id
     * @return int
     */
    public function fetchTotalCountByCompanyIdAndTopicId($company_id, $topic_id)
    {
        $select = $this->sql->select();
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->from(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('topic_id', $topic_id);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $company_id
     * @return int
     */
    public function fetchTotalCountAllActiveByCompanyId($company_id)
    {
        $select = $this->sql->select();
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->from(self::_TABLE);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $id
     * @return MicrolearningTopicCapsule
     */
    public function fetchOne($id)
    {
        $prototype = new MicrolearningTopicCapsule();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @param int $topic_id
     * @param int $capsule_id
     * @return MicrolearningTopicCapsule
     */
    public function fetchOneByTopicIdAndCapsuleId($topic_id, $capsule_id)
    {
        $prototype = new MicrolearningTopicCapsule();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('capsule_id', $capsule_id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @param int $company_id
     * @param int $topic_id
     * @return int
     */
    public function fetchCountByCompanyIdAndTopicId($company_id, $topic_id)
    {
       
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('topic_id', $topic_id);

        $record = $this->executeFetchOneArray($select);
        return $record['total'];
        
    }
    
    
    

    /**
     *
     * @param  int $topic_id  
     * @return MicrolearningTopicCapsule[]
     */
    public function fetchAllByTopicId($topic_id)
    {
        $prototype = new MicrolearningTopicCapsule();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('topic_id', 'topic_id');
        $select->where->order('added_on DESC');
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param  int $topic_id
     * @return MicrolearningTopicCapsule[]
     */
    public function fetchAllActiveByTopicId($topic_id)
    {
        $prototype = new MicrolearningTopicCapsule();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('topic_id', 'topic_id');
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
        $select->where->order('added_on DESC');
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param  int $topic_id
     * @return MicrolearningTopicCapsule[]
     */
    public function fetchAllActiveAndPublishByTopicId($topic_id)
    {
        $prototype = new MicrolearningTopicCapsule();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('topic_id', 'topic_id');
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
        $select->where->greaterThanOrEqualTo('publish_on', new Expression('CURRENT_DATE()'));
        $select->where->order('added_on DESC');
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    
    /**
     *
     * @param MicrolearningTopicCapsule $record
     * @return boolean
     */
    public function insert($record)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($record);
        $values = $this->removeEmpty($values);
        
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        $result = $this->executeInsert($insert);
        if($result) {
            $record->id = $this->lastInsertId;
        }
        return $result;
    }
    
    /**
     *
     * @param MicrolearningTopicCapsule $record
     * @return boolean
     */
    public function update($record)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($record);
        $values = $this->removeEmpty($values);
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $record->id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     *
     * @param MicrolearningTopicCapsule $record
     * @return boolean
     */
    public function delete($record)
    {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('id', $record->id);
        
        return $this->executeDelete($delete);
    }
    

    
}