File indexing completed on 2024-05-12 05:58:47

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  *
0022  * Created: 13.09.2017
0023  */
0024 
0025 class Default_Model_Sponsor
0026 {
0027 
0028     /**
0029      * @inheritDoc
0030      */
0031     public function __construct()
0032     {
0033 
0034     }
0035 
0036     public function fetchSponsorHierarchy()
0037     {
0038         $sql = "
0039             SELECT section.name AS section_name, sponsor.sponsor_id,sponsor.name AS sponsor_name
0040             FROM section_sponsor
0041             JOIN sponsor ON sponsor.sponsor_id = section_sponsor.sponsor_id
0042             JOIN section ON section.section_id = section_sponsor.section_id
0043 
0044         ";
0045         $resultSet = $this->getAdapter()->fetchAll($sql);
0046         $optgroup = array();
0047         foreach ($resultSet as $item) {
0048             $optgroup[$item['section_name']][$item['sponsor_id']] = $item['sponsor_name'];
0049         }
0050 
0051         return $optgroup;
0052     }
0053     
0054     public function fetchAllSponsors()
0055     {
0056         $sql = "
0057             SELECT sponsor.name, sponsor.sponsor_id
0058             FROM sponsor
0059             WHERE is_active = 1
0060         ";
0061         $resultSet = $this->getAdapter()->fetchAll($sql);
0062 
0063         return $resultSet;
0064     }
0065 
0066     /**
0067      * @return Zend_Db_Adapter_Abstract
0068      */
0069     private function getAdapter()
0070     {
0071         return Zend_Db_Table::getDefaultAdapter();
0072     }
0073 
0074     /**
0075      * @param int $section_id
0076      *
0077      * @return array
0078      */
0079     public function fetchSectionItems($section_id)
0080     {
0081         $sql = "SELECT section_sponsor.section_sponsor_id
0082                     , section_sponsor.section_id
0083                     , sponsor.sponsor_id
0084                     , section_sponsor.percent_of_sponsoring
0085         , sponsor.name AS sponsor_name
0086                     , sponsor.fullname AS sponsor_fullname
0087                     , sponsor.description AS sponsor_description
0088                     , sponsor.amount AS sponsor_amount
0089                     , sponsor.is_active
0090              FROM section_sponsor 
0091              JOIN sponsor ON sponsor.sponsor_id = section_sponsor.sponsor_id AND sponsor.is_active = 1 
0092              WHERE section_id = :section_id";
0093         $resultSet = $this->getAdapter()->fetchAll($sql, array('section_id' => $section_id));
0094 
0095         return $resultSet;
0096     }
0097 
0098     /**
0099      * @param int    $sectionId
0100      * @param string $sponsorId
0101      * @param int    $percent
0102      *
0103      * @return array
0104      */
0105     public function assignSponsor($sectionId, $sponsorId, $percent)
0106     {
0107         $section_sponsor_id = $this->saveSectionSponsor($sectionId, $sponsorId, $percent);
0108         $resultSet = $this->fetchOneSectionItem($section_sponsor_id);
0109 
0110         return $resultSet;
0111     }
0112 
0113 
0114 
0115     /**
0116      * @param int $group_id
0117      * @param int $tag_id
0118      *
0119      * @return int
0120      */
0121     public function saveSectionSponsor($section_id, $sponsor_id, $percent)
0122     {
0123         $sql = "SELECT section_sponsor_id FROM section_sponsor WHERE section_id = :section_id AND sponsor_id = :sponsor_id";
0124         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_id' => $section_id, 'sponsor_id' => $sponsor_id));
0125         if (empty($resultSet)) {
0126             $this->getAdapter()->insert('section_sponsor', array('section_id' => $section_id, 'sponsor_id' => $sponsor_id, 'percent_of_sponsoring' => $percent));
0127             $resultId = $this->getAdapter()->lastInsertId();
0128         } else {
0129             $resultId = $resultSet['section_sponsor_id'];
0130         }
0131 
0132         return $resultId;
0133     }
0134 
0135     /**
0136      * @param int $section_sponsor_id
0137      *
0138      * @return array|false
0139      */
0140     public function fetchOneSectionItem($section_sponsor_id)
0141     {
0142         $sql = "SELECT section_sponsor.section_sponsor_id
0143                     , section_sponsor.section_id
0144                     , sponsor.sponsor_id
0145         , sponsor.name AS sponsor_name
0146                     , sponsor.fullname AS sponsor_fullname
0147                     , sponsor.description AS sponsor_description
0148                     , sponsor.is_active
0149              FROM section_sponsor 
0150              JOIN sponsor ON sponsor.sponsor_id = section_sponsor.sponsor_id  AND sponsor.is_active = 1
0151              WHERE section_sponsor_id = :section_sponsor_id";
0152         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_sponsor_id' => $section_sponsor_id));
0153 
0154         return $resultSet;
0155     }
0156 
0157     public function updateSectionSponsor($sectionSponsorId, $sectionId, $sponsorId, $percent)
0158     {        
0159             $updateValues = array(
0160                 'section_id' =>$sectionId,
0161                 'sponsor_id' => $sponsorId,
0162                 'percent_of_sponsoring' => $percent
0163             );
0164         
0165             $this->getAdapter()->update('section_sponsor', $updateValues, array('section_sponsor_id = ?' => $sectionSponsorId));        
0166     }
0167 
0168     public function deleteSectionSponsor($sectionSponsorId)
0169     {
0170         $this->getAdapter()->delete('section_sponsor', array('section_sponsor_id = ?' => $sectionSponsorId));
0171     }
0172 
0173 }