File indexing completed on 2024-12-22 05:33:33

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 
0023 class SectionController extends Local_Controller_Action_DomainSwitch
0024 {
0025 
0026 
0027     public function indexAction()
0028     {
0029         $isAdmin = false;
0030         if(Zend_Auth::getInstance()->hasIdentity() AND Zend_Auth::getInstance()->getIdentity()->roleName == 'admin') {
0031             $isAdmin = true;
0032         }
0033 
0034         $section_id = (int)$this->getParam('id',null);
0035 
0036         $products = self::fetchProducts($section_id, $isAdmin);
0037         $creators = self::fetchCreators($section_id, $isAdmin);
0038         $supporters = self::fetchSupporters($section_id);
0039 
0040         $model = new Default_Model_Section();
0041         $section = null;
0042         if($section_id)
0043         {
0044             $section = $model->fetchSection($section_id);
0045             $this->view->section = $section;
0046             $this->view->section_id = $section_id;
0047         }
0048 
0049         $amount = $model->fetchProbablyPayoutLastMonth($section_id);
0050         $amount_factor = $amount;
0051         $sectionStats = $model->fetchSectionStatsLastMonth($section_id);
0052         if($sectionStats['factor'] != null) {
0053             $amount_factor = $amount * $sectionStats['factor'];
0054         }
0055 
0056         $this->view->supporters = $supporters;
0057         $this->view->products = $products;
0058         $this->view->creators = $creators;
0059         if($isAdmin) {
0060             $this->view->probably_payout_amount = number_format($amount, 2, '.', '');
0061         }else{
0062             $this->view->probably_payout_amount = -1;
0063         }
0064         $this->view->probably_payout_amount_factor = number_format($amount_factor, 2, '.', '');
0065         $goal = ceil( $amount_factor / 500 ) * 500;
0066         $this->view->probably_payout_goal = ($goal ==0 ? 500: $goal);
0067 
0068         $title = 'Section';
0069         if($section){
0070             $title = 'Section '.$section['name'];
0071         }
0072         $this->view->headTitle($title . ' - ' . $this->getHeadTitle(), 'SET');
0073     }
0074 
0075 
0076     public function topAction()
0077     {
0078         $this->_helper->layout->disableLayout();
0079         $this->_helper->viewRenderer->setNoRender(true);
0080         $isAdmin = false;
0081         if(Zend_Auth::getInstance()->hasIdentity() AND Zend_Auth::getInstance()->getIdentity()->roleName == 'admin') {
0082             $isAdmin = true;
0083         }
0084         $section_id = (int)$this->getParam('id',null);
0085         $products = self::fetchProducts($section_id, $isAdmin);
0086         $creators = self::fetchCreators($section_id, $isAdmin);
0087         $this->_helper->json(array('status' => 'ok', 'products' => $products,'creators' => $creators));
0088     }
0089 
0090     public function fetchCreators($section_id,$isAdmin)
0091     {
0092         $model = new Default_Model_Section();
0093         $helpPrintDate = new Default_View_Helper_PrintDate();
0094         $helperImage = new Default_View_Helper_Image();
0095         $sectionStats = $model->fetchSectionStatsLastMonth($section_id);
0096 
0097         $creators = $model->fetchTopCreatorPerSection($section_id);
0098 
0099         foreach ($creators as &$p) {
0100           $p['profile_image_url'] = $helperImage->Image($p['profile_image_url'], array('width' => 100, 'height' => 100));
0101 
0102           $p['probably_payout_amount_factor'] = number_format($p['probably_payout_amount']*($sectionStats['factor']?$sectionStats['factor']:1), 2, '.', '');
0103 
0104           $p['section_factor'] = $sectionStats['factor'];
0105 
0106           if($isAdmin) {
0107             $p['probably_payout_amount'] = number_format($p['probably_payout_amount'], 2, '.', '');
0108           }else{
0109             $p['probably_payout_amount'] = -1;
0110           }
0111         }
0112 
0113         return $creators;
0114 
0115     }
0116 
0117     public function fetchProducts($section_id, $isAdmin)
0118     {
0119         $model = new Default_Model_Section();
0120         $helpPrintDate = new Default_View_Helper_PrintDate();
0121         $helperImage = new Default_View_Helper_Image();
0122         $sectionStats = $model->fetchSectionStatsLastMonth($section_id);
0123         $products=$model->fetchTopProductsPerSection($section_id);
0124         foreach ($products as &$p) {
0125           $p['image_small'] = $helperImage->Image($p['image_small'], array('width' => 200, 'height' => 200));
0126           $p['updated_at'] = $helpPrintDate->printDate(($p['changed_at']==null?$p['created_at']:$p['changed_at']));
0127 
0128           $p['probably_payout_amount_factor'] = number_format($p['probably_payout_amount']*($sectionStats['factor']?$sectionStats['factor']:1), 2, '.', '');
0129 
0130           $p['section_factor'] = $sectionStats['factor'];
0131 
0132           if($isAdmin) {
0133             $p['probably_payout_amount'] =  number_format($p['probably_payout_amount'], 2, '.', '') ;
0134           }else{
0135             $p['probably_payout_amount'] = -1;
0136           }
0137         }
0138 
0139         return $products;
0140     }
0141 
0142     public function fetchSupporters($section_id)
0143     {
0144 
0145         $info = new Default_Model_Info();
0146         $helperImage = new Default_View_Helper_Image();
0147         if($section_id)
0148         {
0149             $supporters = $info->getNewActiveSupportersForSection($section_id,1000);
0150         }else{
0151             $supporters = $info->getNewActiveSupportersForSectionAll(1000);
0152         }
0153 
0154         $s= array();
0155         foreach ($supporters as &$p) {
0156             
0157             $s[] = array('profile_image_url' => $helperImage->Image($p['profile_image_url'], array('width' => 100, 'height' => 100)),
0158                 'member_id' => $p['member_id'],
0159                 'username' => $p['username'],
0160                 'section_support_tier' => $p['section_support_tier']
0161         );
0162 
0163           /*$p['profile_image_url'] = $helperImage->Image($p['profile_image_url'], array('width' => 100, 'height' => 100));*/
0164         }
0165 
0166         return $s;
0167 
0168     }
0169 
0170     public function topcatAction()
0171     {
0172         $this->_helper->layout->disableLayout();
0173         $this->_helper->viewRenderer->setNoRender(true);
0174         $model = new Default_Model_Section();
0175         $helpPrintDate = new Default_View_Helper_PrintDate();
0176         $helperImage = new Default_View_Helper_Image();
0177         $cat_id = $this->getParam('cat_id');
0178         $products=$model->fetchTopProductsPerCategory($cat_id);
0179         $section = $model->fetchSectionForCategory($cat_id);
0180         $sectionStats = $model->fetchSectionStatsLastMonth($section['section_id']);
0181         $isAdmin = false;
0182         if(Zend_Auth::getInstance()->hasIdentity() AND Zend_Auth::getInstance()->getIdentity()->roleName == 'admin') {
0183             $isAdmin = true;
0184         }
0185 
0186         foreach ($products as &$p) {
0187           $p['image_small'] = $helperImage->Image($p['image_small'], array('width' => 200, 'height' => 200));
0188           $p['updated_at'] = $helpPrintDate->printDate(($p['changed_at']==null?$p['created_at']:$p['changed_at']));
0189 
0190             $p['probably_payout_amount_factor'] = number_format($p['probably_payout_amount']*($sectionStats['factor']?$sectionStats['factor']:1), 2, '.', '');
0191 
0192           $p['section_factor'] = $sectionStats['factor'];
0193 
0194           if($isAdmin) {
0195             $p['probably_payout_amount'] =  number_format($p['probably_payout_amount'], 2, '.', '') ;
0196           }else{
0197             $p['probably_payout_amount'] = -1;
0198           }
0199         }
0200 
0201         $creators = $model->fetchTopCreatorPerCategory($cat_id);
0202         foreach ($creators as &$p) {
0203           $p['profile_image_url'] = $helperImage->Image($p['profile_image_url'], array('width' => 100, 'height' => 100));
0204           $p['probably_payout_amount_factor'] = number_format($p['probably_payout_amount']*($sectionStats['factor']?$sectionStats['factor']:1), 2, '.', '');
0205 
0206           $p['section_factor'] = $sectionStats['factor'];
0207 
0208           if($isAdmin) {
0209             $p['probably_payout_amount'] = number_format($p['probably_payout_amount'], 2, '.', '');
0210           }else{
0211             $p['probably_payout_amount'] = -1;
0212           }
0213 
0214         }
0215         $this->_helper->json(array('status' => 'ok', 'cat_id'=>$cat_id,'products' => $products,'creators' => $creators));
0216     }
0217 
0218 
0219     protected function setLayout()
0220     {
0221         $this->_helper->layout()->setLayout('layout_pling_home');
0222     }
0223 
0224 }