Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4808 | Rev 6521 | 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\Model\Comment;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Insert;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Model\Feed;


class CommentMapper extends MapperCommon
{
    const _TABLE = 'tbl_comments';

    
    /**
     *
     * @var CommentMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @param LoggerInterface $logger
     * @param int $user_id
     * @return \LeadersLinked\Mapper\CommentMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new CommentMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     *
     * @param int $uuid
     * @return Comment
     */
    public function fetchOneByUuid($uuid)
    {
        $prototype = new Comment;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     * 
     * @param int $feed_id
     * @return int
     */
    public function fetchCountCommentByFeedId($feed_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)') ]);
        $select->where->equalTo('feed_id', $feed_id);
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $knowledge_area_id
     * @return int
     */
    public function fetchCountCommentByKnowledgeAreaId($knowledge_area_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)') ]);
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
        
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $post_id
     * @return int
     */
    public function fetchCountCommentByPostId($post_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)') ]);
        $select->where->equalTo('post_id', $post_id);
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
        
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $feed_id
     * @return Comment[]
     */
    public function fetchAllByFeedId($feed_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('feed_id', $feed_id);
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param int $post_id
     * @return Comment[]
     */
    public function fetchAllByPostId($post_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('post_id', $post_id);
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param int $feed_id
     * @return Comment[]
     */
    public function fetchAllPublishedByFeedId($feed_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('feed_id', $feed_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
        $select->where->IsNull('parent_id');
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $post_id
     * @return Comment[]
     */
    public function fetchAllPublishedByKnowledgeAreaId($knowledge_area_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
        $select->where->IsNull('parent_id');
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $post_id
     * @return Comment[]
     */
    public function fetchAllPublishedByPostId($post_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('post_id', $post_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
        $select->where->IsNull('parent_id');
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    public function fetchAllPublishedByCommentId($comment_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('parent_id', $comment_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param int $id
     * @return Comment
     */
    public function fetchOne($id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     * 
     * @param Comment $comment
     * @return boolean
     */
    public function insert($comment)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($comment);
        $values = $this->removeEmpty($values);
        
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        $result = $this->executeInsert($insert);
        if($result) {
           $comment->id = $this->getLastInsertId();
        }
        
        return $result;
        
    }
    
    
    /**
     *
     * @param Comment $comment
     * @return boolean
     */
    public function update($comment)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($comment);
        $values = $this->removeEmpty($values);
        
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $comment->id);
        
        

        
        return $this->executeUpdate($update);
    }
    
    
    /**
     *
     * @param int $id
     * @return boolean
     */
    public function delete($id)
    {
       
        
        $update = $this->sql->update(self::_TABLE);
        $update->set([
            'status' => Comment::STATUS_DELETED
        ]);
        $update->where->equalTo('id', $id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     * 
     * @param int $feed_id
     * @return boolean
     */
    public function deleteAllByFeedId($feed_id)
    {
        
        $update = $this->sql->update(self::_TABLE);
        $update->set([
            'status' => Comment::STATUS_DELETED
        ]);
        $update->where->equalTo('feed_id', $feed_id);
        
        return $this->executeUpdate($update);
    }
}