%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/bbw/farmaci/kritik-portale/module/ReviewBase/src/ReviewBase/Model/
Upload File :
Create Path :
Current File : /var/www/html/bbw/farmaci/kritik-portale/module/ReviewBase/src/ReviewBase/Model/AbstractReview.php

<?php

/**
 *
 * @author Florian Koller ( conlabz GmbH ) <florian.koller at conlabz.de>
 */

namespace ReviewBase\Model;

use Basis\Model\EventBasis;
use ReviewBase\Event\AbstractReview\ReviewApprovedMail;

use ReviewBase\Model\UtilityDate;

use ReviewBase\Event\AbstractReview\ReviewAverageCalculationEnd;
use ReviewBase\Event\AbstractReview\ReviewAverageCalculationStart;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswer;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswerAuthorID;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswerDeleteFrom;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswerModifyDate;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswerReviewDate;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswerReviewModeratorModified;
use ReviewBase\Event\AbstractReview\ReviewChangedAnswerStatus;
use ReviewBase\Event\AbstractReview\ReviewChangedComment;
use ReviewBase\Event\AbstractReview\ReviewChangedCommentDeleteFrom;
use ReviewBase\Event\AbstractReview\ReviewChangedCommentModifyDate;
use ReviewBase\Event\AbstractReview\ReviewChangedCommentReviewDate;
use ReviewBase\Event\AbstractReview\ReviewChangedCommentReviewModeratorModified;
use ReviewBase\Event\AbstractReview\ReviewChangedCommentStatus;
use ReviewBase\Event\AbstractReview\ReviewChangedCommentUseful;
use ReviewBase\Event\AbstractReview\ReviewChangedHelpfulCount;
use ReviewBase\Event\AbstractReview\ReviewChangedHelpfulVotes;
use ReviewBase\Event\AbstractReview\ReviewChangedLastModified;
use ReviewBase\Event\AbstractReview\ReviewChangedNotHelpfulCount;
use ReviewBase\Event\AbstractReview\ReviewChangedObjectID;
use ReviewBase\Event\AbstractReview\ReviewChangedRatings;
use ReviewBase\Event\AbstractReview\ReviewChangedUserID;
use ReviewBase\Event\AbstractReview\ReviewCreatedAt;
use ReviewBase\Event\AbstractReview\ReviewDeleted;
use Zend\Db\Adapter\Adapter;
use Zend\Json\Json;

abstract class AbstractReview  extends EventBasis
{
    protected $id;
    protected $objectid;
    protected $userid;
    protected $created_at;
    protected $last_modified;
    protected $ratings;
    protected $comment;
    protected $comment_status;
    protected $comment_modify_date;
    protected $comment_review_date;
    protected $comment_delete_from;
    protected $answer;
    protected $answer_author;
    protected $answer_status;
    protected $answer_modify_date;
    protected $answer_review_date;
    protected $answer_delete_from;
    protected $helpful_count;
    protected $not_helpful_count;
    protected $helpful_votes;

    public function __construct()
    {
    }

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function generateId()
    {
        $this->id = uniqid();
    }

    /**
     * @param array $events
     * @param \Basis\EventBus\EventBus $eventBus
     * @param Adapter $databaseAdapter
     */
    public function setFromEvents($events, \Basis\EventBus\EventBus $eventBus, Adapter $databaseAdapter)
    {
        foreach ($events as $event) {
            /* @var $event \Basis\Event\Event */
            $data = Json::decode($event->__get("domaindata"));
            switch ($event->__get("eventtype")) {
                case 'ReviewBase\Event\AbstractReview\ReviewApprovedMail':
                    break;
                default:
                    foreach ($data as $key => $value) {
                        $this->set($key, $value);
                    }
            }
        }
    }

    /**
     * @param $property
     * @param $value
     */
    protected function set($property, $value)
    {
        //$property = "_" . $property;
        $this->$property = $value;
    }

    /**
     * @param string $objectid
     */
    public function setObjectID($objectid)
    {
        if ($this->objectid !== $objectid) {
            $this->objectid = $objectid;
            $event = new ReviewChangedObjectID();
            $event->setParams(array(
                "objectid" => $objectid
            ));
            $this->addEvent($event);
        }
    }

    /**
     * @return string
     */
    public function getObjectID()
    {
        return $this->objectid;
    }

    public function setUserID($userid)
    {
        if ($this->userid !== $userid) {
            $this->userid = $userid;
            $event = new ReviewChangedUserID();
            $event->setParams(array(
                "userid" => $userid
            ));
            $this->addEvent($event);
        }
    }

    public function getUserID()
    {
        return $this->userid;
    }

    public function setCreatedAt($createdAt)
    {
        $createdAt = UtilityDate::convertToUTC($createdAt);
        if ($this->created_at !== $createdAt) {
            $this->created_at = $createdAt;
            $event = new ReviewCreatedAt();
            $event->setParams(array(
                "created_at" => $createdAt
            ));
            $this->addEvent($event);
        }
    }

    public function getCreatedAt()
    {
        $createdAt = UtilityDate::convertFromUTC($this->created_at);
        return $createdAt;
//        return $this->created_at;
    }
    
    public function setLastModified($lastModified)
    {
        $lastModified = UtilityDate::convertToUTC($lastModified);
        if ($this->last_modified !== $lastModified) {
            $this->last_modified = $lastModified;
            $event = new ReviewChangedLastModified();
            $event->setParams(array(
                "last_modified" => $lastModified
            ));
            $this->addEvent($event);
        }
    }

    public function getLastModified()
    {
        $lastModified = UtilityDate::convertFromUTC($this->last_modified);
        return $lastModified;
//        return $this->last_modified;
    }

    public function setRatings($ratings)
    {

        foreach($ratings as $key => $rating){
            $ratings[$key] = $rating == 0 ? null : $rating;
        }

        $ratings = json_encode($ratings);

        if ($this->ratings !== $ratings) {
            $this->ratings = $ratings;
            $event = new ReviewChangedRatings();
            $event->setParams(array(
                "ratings" => $ratings
            ));
            $this->addEvent($event);
            return true;
        }

        return false;
    }

    public function getRatings($asJson = true)
    {
        $ratings = Json::decode($this->ratings);

        foreach( get_object_vars($ratings)  as $key => $rating){
            $ratings->$key = is_null($rating) ? 0 : $rating;
        }

        if ($asJson) {
            return Json::encode($this->ratings);
        }
        return $ratings;
        //return $this->ratings;
    }
    
    public function setComment($comment)
    {
        if ($this->comment !== $comment) {
            $this->comment = $comment;
            $event = new ReviewChangedComment();
            $event->setParams(array(
                "comment" => $comment
            ));
            $this->addEvent($event);
        }
    }

//    public function setComment($comment)
//    {
//        if(is_null($this->comment) && $comment === ""){
//            return false;
//        }
//
//        $comments = Json::decode($this->comment);
//        $lastComment = null;
//        $index = 0;
//
//        if(is_array($comments)){
//            $lastComment = end($comments);
//            $index = count($comments);
//        } else {
//            $comments = array();
//        }
//
//        if ($lastComment !== $comment) {
//            $comments[$index] = $comment;
//            $commentJson = Json::encode($comments);
//            $this->comment = $commentJson;
//            $event = new ReviewChangedComment();
//            $event->setParams(array(
//                "comment" => $commentJson
//            ));
//            $this->addEvent($event);
//
//            return true;
//        }
//
//        return false;
//    }

    public function getComment()
    {
        return $this->comment;
    }
    
    //return last comment
//    public function getComment($asJson = false)
//    {
//        if($asJson){
//            return $this->comment;
//        }
//        if ($this->comment !== null)
//        {
//            $comments = Json::decode($this->comment);
//            return  end($comments);
//        }
//        else
//        {
//            return null;
//        }
//    }

    public function setCommentStatus($comment_status)
    {
        if ($this->comment_status !== $comment_status) {
            $this->comment_status = $comment_status;
            $event = new ReviewChangedCommentStatus();
            $event->setParams(array(
                "comment_status" => $comment_status
            ));
            $this->addEvent($event);
        }
    }

    public function getCommentStatus()
    {
        return $this->comment_status;
    }

    public function setCommentModifyDate($comment_modify_date)
    {
        $comment_modify_date = UtilityDate::convertToUTC($comment_modify_date);
        if ($this->comment_modify_date !== $comment_modify_date) {
            $this->comment_modify_date = $comment_modify_date;
            $event = new ReviewChangedCommentModifyDate();
            $event->setParams(array(
                "comment_modify_date" => $comment_modify_date
            ));
            $this->addEvent($event);
        }
    }

    public function getCommentModifyDate()
    {
        $commentModifyDate = UtilityDate::convertFromUTC($this->comment_modify_date);
        return $commentModifyDate;
//        return $this->comment_modify_date;
    }

    public function setCommentReviewDate($comment_review_date)
    {
        $comment_review_date = UtilityDate::convertToUTC($comment_review_date);
        if ($this->comment_review_date !== $comment_review_date) {
            $this->comment_review_date = $comment_review_date;
            $event = new ReviewChangedCommentReviewDate();
            $event->setParams(array(
                "comment_review_date" => $comment_review_date
            ));
            $this->addEvent($event);
        }
    }

    public function getCommentReviewDate()
    {
        $commentReviewDate = UtilityDate::convertFromUTC($this->comment_review_date);
        return $commentReviewDate;
//        return $this->comment_review_date;
    }

    public function setCommentDeleteFrom($comment_delete_from)
    {
        if ($this->comment_delete_from !== $comment_delete_from) {
            $this->comment_delete_from = $comment_delete_from;
            $event = new ReviewChangedCommentDeleteFrom();
            $event->setParams(array(
                "comment_delete_from" => $comment_delete_from
            ));
            $this->addEvent($event);
        }
    }

    public function getCommentReviewDeleteFrom()
    {
        return $this->comment_delete_from;
    }
    
    public function setAnswer($answer)
    {
        if ($this->answer !== $answer) {
            $this->answer = $answer;
            $event = new ReviewChangedAnswer();
            $event->setParams(array(
                "answer" => $answer
            ));
            $this->addEvent($event);
        }
    }

//    public function setAnswer($answer)
//    {
//        if(is_null($this->answer) && $answer === ""){
//            return false;
//        }
//
//        $answers = Json::decode($this->answer);
//        $lastAnswer = null;
//        $index = 0;
//
//        if(is_array($answers)){
//            $lastAnswer = end($answers);
//            $index = count($answers);
//        } else {
//            $answers = array();
//        }
//
//        if ($lastAnswer !== $answer) {
//            $answers[$index] = $answer;
//            $answerJson = Json::encode($answers);
//            $this->answer = $answerJson;
//            $event = new ReviewChangedAnswer();
//            $event->setParams(array(
//                "answer" => $answerJson
//            ));
//            $this->addEvent($event);
//
//            return true;
//        }
//
//        return false;
//    }

    public function getAnswer()
    {
        return $this->answer;
    }
    
    //return last answer
//    public function getAnswer($asJson = false)
//    {
//        if($asJson){
//            return $this->answer;
//        }
//        if ($this->answer !== null)
//        {
//            $answers = Json::decode($this->answer);
//            return  end($answers);
//        }
//        else
//        {
//            return null;
//        }
//
//
//    }
    
    public function setAnswerAuthorID($authorid)
    {
        if ($this->answer_author !== $authorid) {
            $this->answer_author = $authorid;
            $event = new ReviewChangedAnswerAuthorID();
            $event->setParams(array(
                "answer_author" => $authorid
            ));
            $this->addEvent($event);
        }
    }

    public function getAnswerAuthorID()
    {
        return $this->answer_author;
    }

    public function setAnswerStatus($answer_status)
    {
        if ($this->answer_status !== $answer_status) {
            $this->answer_status = $answer_status;
            $event = new ReviewChangedAnswerStatus();
            $event->setParams(array(
                "answer_status" => $answer_status
            ));
            $this->addEvent($event);
        }
    }

    public function getAnswerStatus()
    {
        return $this->answer_status;
    }

    public function setAnswerModifyDate($answer_modify_date)
    {
        $answer_modify_date = UtilityDate::convertToUTC($answer_modify_date);
        if ($this->answer_modify_date !== $answer_modify_date) {
            $this->answer_modify_date = $answer_modify_date;
            $event = new ReviewChangedAnswerModifyDate();
            $event->setParams(array(
                "answer_modify_date" => $answer_modify_date
            ));
            $this->addEvent($event);
        }
    }

    public function getAnswerModifyDate()
    {
        $answerModifyDate = UtilityDate::convertFromUTC($this->answer_modify_date);
        return $answerModifyDate;
//        return $this->answer_modify_date;
    }

    public function setAnswerReviewDate($answer_review_date)
    {
        $answer_review_date = UtilityDate::convertToUTC($answer_review_date);
        if ($this->answer_review_date !== $answer_review_date) {
            $this->answer_review_date = $answer_review_date;
            $event = new ReviewChangedAnswerReviewDate();
            $event->setParams(array(
                "answer_review_date" => $answer_review_date
            ));
            $this->addEvent($event);
        }
    }

    public function getAnswerReviewDate()
    {
        $answerReviewDate = UtilityDate::convertFromUTC($this->answer_review_date);
        return $answerReviewDate;
//        return $this->answer_review_date;
    }

    public function setAnswerDeleteFrom($answer_delete_from)
    {
        if ($this->answer_delete_from !== $answer_delete_from) {
            $this->answer_delete_from = $answer_delete_from;
            $event = new ReviewChangedAnswerDeleteFrom();
            $event->setParams(array(
                "answer_delete_from" => $answer_delete_from
            ));
            $this->addEvent($event);
        }
    }

    public function getAnswerDeleteFrom()
    {
        return $this->answer_delete_from;
    }
    
    public function addHelpfulVote($helpful = null, $userid = null)
    {
        $added = false;
        $helpful_votes = Json::decode($this->helpful_votes);
        $this->setHelpfulVote($userid, $helpful);
        
        return $added;
    }
    
    public function setHelpfulVote($userid = null, $helpful = null)
    {
        $helpful_votes = Json::decode($this->helpful_votes);
        $original_count_helpful = null;
        $original_count_not_helpful = null;
        
        $helpful = filter_var($helpful, FILTER_VALIDATE_BOOLEAN);
        if(is_object($helpful_votes))
        {
            $original_count_helpful = count(array_keys(get_object_vars($helpful_votes), true, false));
            $original_count_not_helpful = count(array_keys(get_object_vars($helpful_votes), false, false));
            
        } else {
            $helpful_votes = new \stdClass();
            $original_count_helpful = 0;
            $original_count_not_helpful = 0;
        }
        
        //add userid to helpful_votes
        if ( !empty($userid) )//&& !in_array($userid, $helpful_votes) )
        {
            $helpful_votes->$userid = $helpful;
        }
        
        $helpful_count = count(array_keys(get_object_vars($helpful_votes), true, false));
        $not_helpful_count = count(array_keys(get_object_vars($helpful_votes), false, false));
        if ( ($original_count_helpful !== $helpful_count) || ($original_count_not_helpful !== $not_helpful_count) )
        {
            $this->setHelpfulCount($helpful_count);
            $this->setNotHelpfulCount($not_helpful_count);
            
            $helpful_votes = Json::encode($helpful_votes);
            $this->helpful_votes = $helpful_votes;
            $event = new ReviewChangedHelpfulVotes();
            $event->setParams([
                "helpful_votes" => $helpful_votes
            ]);
            $this->addEvent($event);
        }
    }

    protected function setHelpfulCount($helpfulCount)
    {
        if ($this->helpful_count !== $helpfulCount) {
            $this->helpful_count = $helpfulCount;
            $event = new ReviewChangedHelpfulCount();
            $event->setParams(array(
                "helpful_count" => $helpfulCount
            ));
            $this->addEvent($event);
        }
    }
    
    public function getHelpfulCount()
    {
        return $this->helpful_count;
    }
    
    protected function setNotHelpfulCount($notHelpfulCount)
    {
        if ($this->not_helpful_count !== $notHelpfulCount) {
            $this->not_helpful_count = $notHelpfulCount;
            $event = new ReviewChangedNotHelpfulCount();
            $event->setParams(array(
                "not_helpful_count" => $notHelpfulCount
            ));
            $this->addEvent($event);
        }
    }
    
    public function getNotHelpfulCount()
    {
        return $this->not_helpful_count;
    }

    public function setAverageCalculationStart()
    {
        $event = new ReviewAverageCalculationStart();
        $event->setParams(array());
        $this->addEvent($event);
    }

    public function setAverageCalculationEnd()
    {
        $event = new ReviewAverageCalculationEnd();
        $event->setParams(array());
        $this->addEvent($event);
    }

    public function delete()
    {
        $event = new ReviewDeleted();
        $event->setParams(array(
            "entityid" => $this->getId()
        ));
        $this->addEvent($event);
    }

    /**
     * @return array
     */
    public function asArray()
    {
        return get_object_vars($this);
    }

    /**
     *
     * @return array returns result of get_object_vars($this)
     */
    public function getArrayCopy()
    {
        return get_object_vars($this);
    }

    /**
     * @param \Basis\Model\User\User $user
     * @param \ReviewPharmacy\Model\Pharmacy $pharmacy
     */
    public function setReviewApprovedMail($pharmacy)
    {
        $event = new ReviewApprovedMail();
        $event->setRateableObject($pharmacy);
        $event->setReview($this);
        $this->addEvent($event);
    }
}

Zerion Mini Shell 1.0