Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6319 | 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 $my_coach_answer_id
     * @return int
     */
    public function fetchCountByMyCoachAnswerId($my_coach_answer_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    
    
    /**
     *
     * @param int my_coach_question_id
     * @return int
     */
    public function fetchCountByMyCoachQuestionId($my_coach_question_id)
    {
        $selectIn = $this->sql->select(MyCoachAnswerMapper::_TABLE);
        $selectIn->columns(['id']);
        $selectIn->where->equalTo('question_id', $my_coach_question_id);

        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->in('my_coach_answer_id', $selectIn);
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
        $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 $my_coach_answer_id
     * @return Comment[]
     */
    public function fetchAllPublishedByMyCoachAnswerId($my_coach_answer_id)
    {
        $prototype = new Comment();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
        $select->where->IsNull('parent_id');
        $select->order('added_on DESC');
        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);
    }
}