%PDF- %PDF-
Mini Shell

Mini Shell

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

<?php
/**
 * Created by PhpStorm.
 * User: juliangilles
 * Date: 29.07.14
 * Time: 14:32
 */

namespace Basis\Model\User;


use Basis\Entity\UserEntity;
use Basis\Model\Session;
use Basis\Repo\UserRepo;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use ReviewBase\Model\CleverReachManager;


class UserFactory
{
    /**
     * @var \Zend\ServiceManager\ServiceManager
     */
    protected $_serviceLocator;

    /**
     * @var \Basis\EventBus\EventBus
     */
    protected $_eventBus;

    /**
     * @var \Basis\Repo\UserRepo
     */
    protected $_repo;
    
    protected $_viewRepo;
    
    protected $_cleverReachManager;

    public function __construct($serviceLocator, \Basis\EventBus\EventBus $eventBus)
    {
        $this->_serviceLocator = $serviceLocator;
        $this->_eventBus = $eventBus;
        $this->_cleverReachManager = $this->_serviceLocator->get('ReviewBase\Model\CleverReachManager');
        $this->_repo = new UserRepo($eventBus, $this->_serviceLocator->get("systemDB"), $this->_cleverReachManager);
        $this->_viewRepo = new UserRepo($eventBus, $this->_serviceLocator->get("viewsDB"), $this->_cleverReachManager);
    }

    /**
     * @param $id
     * @return User
     */
    public function getUser($entityId)
    {
        $user = $this->_repo->load($entityId);
        return $user;
    }

    public function showUser($entityId)
    {
        $userEntity = $this->_viewRepo->showUser($entityId);
        return $userEntity;
    }
    
    public function getUserByEmail($email)
    {
        $userEntity = $this->_viewRepo->getUserByEmail($email);
        if($userEntity)
        {
           $user = $this->_repo->load($userEntity['entityid']);
           return $user;
        }
        return false;
    }
    
    public function getUserByExternalCustomerID($customerID)
    {
        $userEntity = $this->_viewRepo->getUserByExternalCustomerID($customerID);
        if($userEntity)
        {
           $user = $this->_repo->load($userEntity['entityid']);
           return $user;
        }
        return false;
    }
    
    public function getHomeCountForPharmacy($entityid)
    {
        $count = $this->_viewRepo->getHomeCountForPharmacy($entityid);
        return $count;
    }

    /**
     * @param $pharmacyid
     * @return ResultSet
     */
    public function getHomePharmacyFollower($pharmacyid)
    {
        $count = $this->_viewRepo->getHomePharmacyFollower($pharmacyid);
        return $count;
    }

    /**
     * @param $id
     * @return User
     */
    public function delete($entityId)
    {
        $user = $this->_repo->load($entityId);
        $user = $this->_repo->delete($user);
        return $user;
    }

    /**
     * @param $data
     * @return User
     */
    public function createUserFromForm($data)
    {
        $resultSet = new ResultSet(ResultSet::TYPE_ARRAYOBJECT, new \Basis\Entity\UserEntity());
        $eventStoreTable = new TableGateway('user', $this->_serviceLocator->get("viewsDB"), null, $resultSet);
        $result = $eventStoreTable->select(array("email" => $data['email']));

        if($result->count() === 0)
        {
            $user = $this->_repo->load($data['id']);
            $user->setName($data['name']);
            $user->setSurname($data['surname']);
//            $user->setUsername($data['username']);
            $user->setRole($data['role']);
            $user->setEmail($data['email']);
            $user->setPhone($data['phone']);
            $user->setSalutation($data['salutation']);
            $user->setTitle($data['title']);
            $user->setNewsletter($data['newsletter']);
            $user->setActive((int)$data['active']);
            $user->setPassword(\Login\Model\Password::generateHash($data['password']));

            $this->_repo->save($user);
            return $user;
        }
        else {
            throw new \Exception("Email already exists!");
        }

    }

    /**
     * @param $data
     * @return User
     */
    public function editUserFromForm($data)
    {
        $user = $this->_repo->load($data['id']);
        $user->setName($data['name']);
        $user->setSurname($data['surname']);
//        $user->setUsername($data['username']);
        $user->setRole($data['role']);

        if($data['email'] !== $user->getEmail()){
            $user->setVerifyMail($data['email']);
            $user->setHash($user->generateHash());
        }else{
            $user->setEmail($data['email']);
        }

        $user->setPhone($data['phone']);
        $user->setSalutation($data['salutation']);
        $user->setTitle($data['title']);
        $user->setNewsletter($data['newsletter']);
        $user->setActive((int)$data['active']);

        $this->_repo->save($user);
        return $user;
    }

    /**
     * @param $data
     * @return User
     */
    public function createFrontUser($data)
    {
        $now = new \DateTime();

        $user = $this->_repo->load($data['id']);
        //$user->setRole("owner");
        $user->setRole($data['role']);
        $user->setEmail($data['email']);
        $user->setName($data['name']);
        $user->setSurname($data['surname']);
//        $user->setUsername($data['username']);
        $user->setPhone($data['phone']);
        $user->setSalutation($data['salutation']);
        $user->setTitle($data['title']);
        $user->setPassword(\Login\Model\Password::generateHash($data['password']));
        $user->setHash($user->generateHash());
        $user->setNewsletter($data['newsletter']);

        $user->setActive(0);
        $user->setRemind(0);
        $user->setForgotten(0);
        $user->setCreatedAt($now->format("Y-m-d H:i:s"));


        $this->_repo->save($user);
        return $user;
    }

    /**
     * @param $data
     * @return User
     */
    public function editOwner($data)
    {
        $now = new \DateTime();

        $user = $this->_repo->load($data['id']);

        if($data['email'] !== $user->getEmail()){
            $user->setVerifyMail($data['email']);
            $user->setHash($user->generateHash());
        }else{
            $user->setEmail($data['email']);
        }

        $user->setName($data['name']);
        $user->setSurname($data['surname']);
//        $user->setUsername($data['username']);
        $user->setPhone($data['phone']);
        $user->setSalutation($data['salutation']);
        $user->setTitle($data['title']);
        $user->setNewsletter($data['newsletter']);

        if(!empty($data['password'])){
            $user->setPassword(\Login\Model\Password::generateHash($data['password']));
        }

        $this->_repo->save($user);
        return $user;
    }

    /**
     * @param string $hash
     * @return User
     */
    public function activateFrontendUser($hash)
    {
        $userEntity = $this->_viewRepo->getUserByHash($hash);

        if($userEntity){
            $user = $this->_repo->load($userEntity['entityid']);

            // if mail was verified use it as new main mail
            if(!is_null($user->getVerifyMail())){
                $user->setEmail($user->getVerifyMail());
                $user->setVerifyMail(null);
            }

            $user->setHash("");
            $user->setActive(1);

            $this->_repo->save($user);
            return $user;
        }
    }

    /**
     * @param string $email
     * @return boolean
     */
    public function passwordForgotten($data)
    {
        $userEntity = $this->_viewRepo->getUserByEmail($data['email']);
//        if(!$userEntity)
//        {
//            $userEntity = $this->_viewRepo->getUserByUsername($data['email']);
//        }
        if($userEntity){
            $user = $this->_repo->load($userEntity['entityid']);

            $user->setHash($user->generateHash());
            $user->setForgotten(1);
            $user->setNewsletter(isset($data['newsletter']) ? $data['newsletter'] : 0);

            $this->_repo->save($user);
            return true;
        }
        return false;
    }

    /**
     * @param string $email
     * @return User
     */
    public function setNewPassword($data)
    {
        $userEntity = $this->_viewRepo->getUserByHash($data['hash']);

        if($userEntity){
            $user = $this->_repo->load($userEntity['entityid']);

            $user->setHash("");
            $user->setForgotten(0);
            $user->setPassword(\Login\Model\Password::generateHash($data['password']));
            $user->setNewsletter($data['newsletter']);

            $this->_repo->save($user);
            return true;
        }
        return false;
    }

    /*
     * @param string $email
     */
    public function emailExists($email){

        return $this->_viewRepo->emailExists($email);
    }
    
    public function userExists($userID)
    {
        $userExists = false;
        $user = $this->getUser($userID);
        if ($this->emailExists($user->getEmail()))
        {
            $userExists = true;
        }
        return $userExists;
    }
    
    public function usernameExists($username)
    {
        return $this->_viewRepo->usernameExists($username);
    }

    /*
     * @param string $query
     */
    public function findUser($query){
        return $this->_viewRepo->findUser($query);
    }

    /*
     * @param string $email
     */
    public function newEmailExists($newEmail, $userId){

        return $this->_viewRepo->newEmailExists($newEmail, $userId);
    }
    
    public function newUsernameExists($newUsername, $userId){

        return $this->_viewRepo->newUsernameExists($newUsername, $userId);
    }

    public function remindAllFrontendUser()
    {
        $userEntities = $this->_viewRepo->getUserForReminds();

        if($userEntities){
            foreach($userEntities as $userEntity){
                $user = $this->_repo->load($userEntity['entityid']);

                $user->setRemind(1);

                $this->_repo->save($user);
            }
        }
    }

    public function removeInactiveUser()
    {
        $userEntities = $this->_viewRepo->getUserForRemove();

        if($userEntities){
            foreach($userEntities as $userEntity){
                $user = $this->_repo->load($userEntity['entityid']);
                $this->_repo->delete($user);
            }
        }
    }

    /**
     * @param $data
     * @return User
     */
    public function setUserHash($data)
    {
        $user = $this->_repo->load($data['entityid']);
        $user->setHash($data['hash']);
        $this->_repo->save($user);
        return $user;
    }

    /**
     * @param $data
     * @return User
     */
    public function setUserPassword($data)
    {
        $user = $this->_repo->load($data['entityid']);
        $user->setHash($data['hash']);
        $user->setPassword(\Login\Model\Password::generateHash($data['password']));
        $this->_repo->save($user);
        return $user;
    }


    /**
     * @param User $user
     */
    public function save(User $user)
    {
        $this->_repo->save($user);
    }
} 

Zerion Mini Shell 1.0