Skip to content
Code-Schnipsel Gruppen Projekte
Handler.php 5,68 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    namespace QUI\Memberships;
    
    use QUI\CRUD\Factory;
    use QUI\Utils\Grid;
    use QUI;
    
    class Handler extends Factory
    {
    
    Patrick Müller's avatar
    Patrick Müller committed
        const PERMISSION_CREATE     = 'quiqqer.memberships.create';
        const PERMISSION_EDIT       = 'quiqqer.memberships.edit';
        const PERMISSION_DELETE     = 'quiqqer.memberships.delete';
        const PERMISSION_FORCE_EDIT = 'quiqqer.memberships.force_edit';
    
    
    Patrick Müller's avatar
    Patrick Müller committed
        /**
         * @inheritdoc
    
    Patrick Müller's avatar
    Patrick Müller committed
         * @throws QUI\Memberships\Exception
    
    Patrick Müller's avatar
    Patrick Müller committed
         */
        public function createChild($data = array())
        {
    
    Patrick Müller's avatar
    Patrick Müller committed
            $data['createDate'] = Utils::getFormattedTimestamp();
    
    Patrick Müller's avatar
    Patrick Müller committed
            $data['createUser'] = QUI::getUserBySession()->getId();
    
    Patrick Müller's avatar
    Patrick Müller committed
    
            // title
            $title = trim($data['title']);
    
            if (empty($title)) {
                throw new QUI\Memberships\Exception(array(
                    'quiqqer/memberships',
                    'exception.handler.no.title'
                ));
            }
    
            $data['title'] = array();
    
    Patrick Müller's avatar
    Patrick Müller committed
    
            foreach (QUI::availableLanguages() as $lang) {
                $data['title'][$lang] = $title;
            }
    
            $data['title'] = json_encode($data['title']);
    
    
    Patrick Müller's avatar
    Patrick Müller committed
            // groupIds
            $Groups   = QUI::getGroups();
            $groupIds = $data['groupIds'];
    
            if (empty($groupIds)
                || !is_array($groupIds)
            ) {
                throw new QUI\Memberships\Exception(array(
                    'quiqqer/memberships',
                    'exception.handler.no.groups'
                ));
            }
    
            foreach ($groupIds as $groupId) {
                // check if group exist by getting them
                $Groups->get((int)$groupId);
            }
    
            $data['groupIds'] = ',' . implode(',', $groupIds) . ',';
            $data['duration'] = '1-month';
    
    
    Patrick Müller's avatar
    Patrick Müller committed
            return parent::createChild($data);
        }
    
    
    Patrick Müller's avatar
    Patrick Müller committed
        /**
         * Get membership
         *
         * @param int $id
         * @return Membership
         */
        public function getChild($id)
        {
            return parent::getChild($id);
        }
    
    
        /**
         * @inheritdoc
         * @return string
         */
        public function getDataBaseTableName()
        {
            return 'quiqqer_memberships';
        }
    
        /**
         * @inheritdoc
         * @return string
         */
        public function getChildClass()
        {
            return Membership::class;
        }
    
        /**
         * @inheritdoc
         * @return array
         */
        public function getChildAttributes()
        {
            return array(
                'title',
                'description',
                'content',
                'duration',
                'groupIds',
                'autoRenew',
                'editDate',
                'editUser',
    
    Patrick Müller's avatar
    Patrick Müller committed
    //            'createDate',
    //            'createUser',
    
    
                // these fields require quiqqer/order
                'paymentInterval',
                'netPrice',
                'netPriceOffer',
    //            'grossPrice',
                'paymentMethodIds'
    
                // @todo additional fields for quiqqer/contracts
            );
        }
    
        /**
         * Search memberships
         *
         * @param array $searchParams
         * @param bool $countOnly (optional) - get count for search result only [default: false]
    
    Patrick Müller's avatar
    Patrick Müller committed
         * @return array - membership IDs
    
         */
        public function search($searchParams, $countOnly = false)
        {
            $memberships = array();
            $Grid        = new Grid($searchParams);
            $gridParams  = $Grid->parseDBParams($searchParams);
    
            $whereOr = array();
    
            if (!empty($searchParams['search'])) {
                $search = $searchParams['search'];
    
                $whereOr['title'] = array(
                    'type'  => '%LIKE%',
                    'value' => $search
                );
    
                $whereOr['description'] = array(
                    'type'  => '%LIKE%',
                    'value' => $search
                );
    
                $whereOr['content'] = array(
                    'type'  => '%LIKE%',
                    'value' => $search
                );
            }
    
            if ($countOnly) {
                $result = QUI::getDataBase()->fetch(array(
                    'count'    => 1,
                    'from'     => $this->getDataBaseTableName(),
                    'where_or' => $whereOr
                ));
    
                return current(current($result));
            }
    
            $result = QUI::getDataBase()->fetch(array(
                'select'   => array(
                    'id'
                ),
                'from'     => $this->getDataBaseTableName(),
                'where_or' => $whereOr,
                'limit'    => $gridParams['limit']
            ));
    
            foreach ($result as $row) {
    
    Patrick Müller's avatar
    Patrick Müller committed
                $memberships[] = $row['id'];
    
            }
    
            return $memberships;
        }
    
    Patrick Müller's avatar
    Patrick Müller committed
    
        /**
         * Get list of all packages that are relevant for quiqqer/memberships
         * and that are currently installed
         *
         * @return array
         */
        public function getInstalledMembershipPackages()
        {
    
    Patrick Müller's avatar
    Patrick Müller committed
            $packages         = array();
            $relevantPackages = array(
                'quiqqer/products',
                'quiqqer/contracts'
            );
    
    Patrick Müller's avatar
    Patrick Müller committed
            foreach ($relevantPackages as $package) {
                try {
                    QUI::getPackage($package);
                    $packages[] = $package;
                } catch (\Exception $Exception) {
                }
    
    Patrick Müller's avatar
    Patrick Müller committed
            return $packages;
        }
    
        /**
         * Get IDs of all memberships that have a specific group assigned
         *
         * @param int $groupId
         * @return int[]
         */
        public function getMembershipIdsByGroupId($groupId)
        {
            $ids = array();
    
            $result = QUI::getDataBase()->fetch(array(
                'select' => array(
                    'id'
                ),
                'from'   => self::getDataBaseTableName(),
                'where'  => array(
                    'groupIds' => array(
                        'type'  => '%LIKE%',
                        'value' => ',' . $groupId . ','
                    )
                )
            ));
    
            foreach ($result as $row) {
                $ids[] = $row['id'];
    
    Patrick Müller's avatar
    Patrick Müller committed
            return $ids;