Skip to content
Code-Schnipsel Gruppen Projekte
MembershipUser.php 42,2 KiB
Newer Older
                            'cancelUntilDate'  => $this->formatDate($CancelUntilDate),
                            'cycleEndDate'     => $cycleEndDate,
                            'nextCycleEndDate' => $nextCycleEndDate
                        ]
                    );
                } else {
                    $cancelInfoText = QUI::getLocale()->get(
                        'quiqqer/memberships',
                        'MembershipUser.cancel.info_text.period_of_notice_expired',
                        [
                            'addedDate'        => $addedDate,
                            'cancelUntilDate'  => $this->formatDate($CancelUntilDate),
                            'cycleBeginDate'   => $cycleBeginDate,
                            'cycleEndDate'     => $cycleEndDate,
                            'nextCycleEndDate' => $nextCycleEndDate
                        ]
                    );
                }
            } else {
                $cancelInfoText = QUI::getLocale()->get(
                    'quiqqer/memberships',
                    'MembershipUser.cancel.info_text.cycle_cancel_anytime',
                    [
                        'addedDate'        => $addedDate,
                        'cycleEndDate'     => $cycleEndDate,
                        'nextCycleEndDate' => $nextCycleEndDate
                    ]
                );
            }
        } elseif ($this->getMembership()->isInfinite()) {
            $cancelInfoText = QUI::getLocale()->get(
                'quiqqer/memberships',
                'MembershipUser.cancel.info_text.cancel_anytime'
            );
        } else {
            $cancelInfoText = QUI::getLocale()->get(
                'quiqqer/memberships',
                'MembershipUser.cancel.info_text.cycle_cancel_anytime',
                [
                    'addedDate'        => $addedDate,
                    'cycleEndDate'     => $cycleEndDate,
                    'nextCycleEndDate' => $nextCycleEndDate
                ]
            );
        }

            'id'                => $this->getId(),
            'userId'            => $QuiqqerUser->getId(),
            'membershipId'      => $Membership->getId(),
            'membershipTitle'   => $title,
            'membershipShort'   => $description,
            'membershipContent' => $content,
            'username'          => $QuiqqerUser->getUsername(),
            'fullName'          => $QuiqqerUser->getName(),
            'addedDate'         => $addedDate,
            'beginDate'         => $cycleBeginDate,
            'endDate'           => $cycleEndDate,
            'cancelEndDate'     => $this->formatDate($CurrentCancelEndDate),
            'cancelDate'        => $this->formatDate($this->getAttribute('cancelDate')),
//            'cancelUntilDate'   => $CancelUntilDate ? $this->formatDate($CancelUntilDate) : false,
            'cancelStatus'      => $this->getAttribute('cancelStatus'),
            'cancelAllowed'     => $cancelAllowed,
            'cancelInfoText'    => $cancelInfoText,
//            'archived'        => $this->isArchived(),
//            'archiveReason'   => $this->getAttribute('archiveReason'),
            'cancelled'         => $this->isCancelled(),
            'autoExtend'        => $Membership->isAutoExtend(),
            'infinite'          => $Membership->isInfinite()
    /**
     * Get membership data for backend view/edit purposes
     *
     * @return array
     */
    public function getBackendViewData()
    {
        $QuiqqerUser = $this->getUser();
        $Membership  = $this->getMembership();

            'userId'          => $this->getUserId(),
            'membershipId'    => $Membership->getId(),
            'membershipTitle' => $Membership->getTitle(),
            'username'        => $QuiqqerUser ? $QuiqqerUser->getUsername() : '-',
            'firstname'       => $QuiqqerUser ? $QuiqqerUser->getAttribute('firstname') : '-',
            'lastname'        => $QuiqqerUser ? $QuiqqerUser->getAttribute('lastname') : '-',
            'fullName'        => $QuiqqerUser ? $QuiqqerUser->getName() : '-',
            'addedDate'       => $this->getAttribute('addedDate'),
            'beginDate'       => $this->getAttribute('beginDate'),
            'endDate'         => $this->getAttribute('endDate'),
            'archived'        => $this->isArchived(),
            'archiveReason'   => $this->getAttribute('archiveReason'),
            'archiveDate'     => $this->getAttribute('archiveDate'),
            'cancelled'       => $this->isCancelled(),
            'extraData'       => $this->getExtraData(),
            'infinite'        => $Membership->isInfinite(),
            'contractId'      => $this->getContractId()
    /**
     * Get Verification object for MembershipUser cancellation
     *
     * @return CancelVerification
     */
    protected function getCancelVerification()
    {
        return new CancelVerification($this->id);
    }

    /**
     * Get Verification object for MembershipUser cancel abort
     *
     * @return AbortCancelVerification
     */
    protected function getAbortCancelVerification()
    {
        return new AbortCancelVerification($this->id);
    }

    /**
     * Send an email to the membership user
     *
     * @param string $subject - mail subject
     * @param string $templateFile
     * @param array $templateVars (optional) - additional template variables (besides $this)
     * @return void
    public function sendMail($subject, $templateFile, $templateVars = [])
Patrick Müller's avatar
Patrick Müller committed
        $User  = $this->getUser();
        $email = $User->getAttribute('email');

        if (empty($email)) {
            QUI\System\Log::addError(
                'Could not send mail to user #'.$User->getId().' because the user has'
                .' no email address!'
            );
        $Engine = QUI::getTemplateManager()->getEngine();

        $Engine->assign(array_merge(
                'MembershipUser' => $this,
                'Locale'         => $this->getUser()->getLocale(),
                'data'           => $this->getFrontendViewData()
            $templateVars
        ));

        $template = $Engine->fetch($templateFile);

        $Mailer = new Mailer();

Patrick Müller's avatar
Patrick Müller committed
        $Mailer->addRecipient($email, $User->getName());
        $Mailer->setSubject($subject);
        $Mailer->setBody($template);
        $Mailer->send();
    }

    /**
     * Set any extra text data to the MembershipUser
     *
     * This is meant for extra information that is not already covered by the history.
     *
     * @param string $key
     * @param string $value
     */
    public function setExtraData($key, $value)
    {
        $extraData = $this->getExtraData();

        $User       = QUI::getUserBySession();
        $userString = $User->getName().' ('.$User->getId().')';
        $editString = Utils::getFormattedTimestamp().' - '.$userString;

        if (isset($extraData[$key])) {
            $extraData[$key]['edit']  = $editString;
            $extraData[$key]['value'] = $value;
        } else {
                'value' => $value,
                'add'   => $editString,
                'edit'  => '-'
        }

        $this->setAttribute('extraData', json_encode($extraData));
    }

    /**
     * Get extra data of this MembershipUser
     *
     * @param string $key (optional) - If omitted return all extra data
     * @return array|string|false
     */
    public function getExtraData($key = null)
    {
        $extraData = $this->getAttribute('extraData');

        if (empty($extraData)) {
        } else {
            $extraData = json_decode($extraData, true);
        }

        if (is_null($key)) {
            return $extraData;
        }

        if (!array_key_exists($key, $extraData)) {
            return false;
        }

        return $extraData[$key]['value'];
    }
     * Get begin Date of the current cycle
    public function getCycleBeginDate()
        return \date_create($this->getAttribute('beginDate'));
    }

    /**
     * Get end Date of the current cycle
     *
     * @return \DateTime|false - DateTime of the cycle end or false if Membership has no cycle end (i.e. is infinite)
     */
    public function getCycleEndDate()
    {
        $Contract = $this->getContract();

        if ($Contract) {
            return $Contract->getCycleEndDate();
        }

        if ($this->getMembership()->isInfinite()) {
            return false;
        }

        return \date_create($this->getAttribute('endDate'));
    }

    /**
     * Get begin date of the (hypothetical) next cycle
     *
     * @return \DateTime|false - DateTime of the cycle end or false if Membership has no next cycle (i.e. is infinite)
     */
    public function getNextCycleBeginDate()
    {
        $Contract = $this->getContract();

        if ($Contract) {
            $EndDate       = $Contract->getCycleEndDate();
            $NextBeginDate = clone $EndDate;
            $NextBeginDate->add(\date_interval_create_from_date_string('1 day'));
            $NextBeginDate->setTime(0, 0, 0);

            return $NextBeginDate;
        }

        if ($this->getMembership()->isInfinite()) {
            return false;
        }

        $EndDate = $this->getCycleEndDate();
            return false;
        if (MembershipUsersHandler::getExtendMode() === MembershipUsersHandler::EXTEND_MODE_PROLONG) {
            return $this->getCycleBeginDate();
        }
        $NextBeginDate = clone $EndDate;

        switch (MembershipUsersHandler::getDurationMode()) {
            case MembershipUsersHandler::DURATION_MODE_EXACT:
                $NextBeginDate->add(\date_interval_create_from_date_string('1 second'));
                break;

            default:
                $NextBeginDate->add(\date_interval_create_from_date_string('1 day'));
                $NextBeginDate->setTime(0, 0, 0);
        return $NextBeginDate;
    }

    /**
     * Get the end Date of the (hypothetical) next cycle
     *
     * @return \DateTime|false - DateTime of the next cycle end or false if Membership has no next cycle end (i.e. is infinite)
     */
    public function getNextCycleEndDate()
    {
        $Contract = $this->getContract();

        if ($Contract) {
            return $Contract->getNextCycleEndDate();
        $Membership = $this->getMembership();

        if ($Membership->isInfinite()) {
            return false;
        $NextCycleBeginDate = $this->getNextCycleBeginDate();
        if (!$NextCycleBeginDate) {
            return false;
        }

        $start         = $NextCycleBeginDate->format('Y-m-d');
        $duration      = explode('-', $Membership->getAttribute('duration'));
        $durationCount = $duration[0];
        $durationScope = $duration[1];

        switch (MembershipUsersHandler::getDurationMode()) {
            case MembershipUsersHandler::DURATION_MODE_DAY:
                $endTime    = strtotime($start.' +'.$durationCount.' '.$durationScope);
                $beginOfDay = strtotime("midnight", $endTime);
                $end        = strtotime("tomorrow", $beginOfDay) - 1;
                break;

            default:
                $end = strtotime($start.' +'.$durationCount.' '.$durationScope);
        return \date_create('@'.$end);
    }

    /**
     * Calculates the date the membership for this user would end
     * if it was cancelled NOW
     *
     * @return \DateTime
     */
    public function getCurrentCancelEndDate()
    {
        /**
         * If a contract is connected to this MembershipUser
         * the contract cancel termination date has priority!
         */
        $Contract = $this->getContract();

        if ($Contract) {
                return $Contract->getCurrentCancelTerminationDate();
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
            }
        }

        return $this->getCycleEndDate();