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

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 class WidgetController extends Zend_Controller_Action
0023 {
0024 
0025     public function indexAction()
0026     {
0027         $this->_helper->viewRenderer('render');
0028         $this->renderAction();
0029     }
0030 
0031     public function renderAction()
0032     {
0033         $this->_helper->layout->disableLayout();
0034         $widgetProjectId = (int)$this->getParam('project_id');
0035         $userConfig = $this->getParamUserConfig();
0036 
0037         if (false == isset($widgetProjectId)) {
0038             throw new Zend_Controller_Action_Exception('This page does not exist', 404);
0039         }
0040 
0041         $widgetDefaultModel = new Default_Model_DbTable_ProjectWidgetDefault();
0042         $widgetDefault = $widgetDefaultModel->fetchConfig($widgetProjectId);
0043 
0044         $widgetConfig = $this->mergeConfig($userConfig, $widgetDefault);
0045 
0046         $this->view->widgetConfig = $widgetConfig;
0047         $productModel = new Default_Model_Project();
0048         $this->view->project = $productModel->fetchProductDataFromMV($widgetProjectId);
0049         if (is_null($this->view->project)) {
0050             throw new Zend_Controller_Action_Exception('This page does not exist', 404);
0051         }
0052         $this->view->supporting = $productModel->fetchProjectSupporterWithPlings($widgetProjectId);
0053         $plingModel = new Default_Model_DbTable_Plings();
0054         $this->view->comments = $plingModel->getCommentsForProject($widgetProjectId, 10);
0055         $websiteOwner = new Local_Verification_WebsiteProject();
0056         $this->view->authCode = '<meta name="ocs-site-verification" content="' . $websiteOwner->generateAuthCode(stripslashes($this->view->project->link_1)) . '" />';
0057         $this->view->paymentProvider = true;
0058         if (empty($this->view->project->paypal_mail) && empty($this->view->project->dwolla_id)) :
0059             $this->view->paymentProvider = false;
0060         endif;
0061     }
0062 
0063     private function getParamUserConfig()
0064     {
0065         $params = $this->getAllParams();
0066 
0067         if (!isset($params['ssu']) OR !isset($params['sco']) OR !isset($params['sda'])) {
0068             return null;
0069         }
0070 
0071         $userConfig = new stdClass();
0072         $userConfig->colors = new stdClass();
0073         $userConfig->colors->widgetBg = $params['wbg'];
0074         $userConfig->colors->widgetContent = $params['wco'];
0075         $userConfig->colors->headline = $params['whe'];
0076         $userConfig->colors->text = $params['wte'];
0077         $userConfig->colors->button = $params['wbu'];
0078         $userConfig->colors->buttonText = $params['wbt'];
0079 
0080         $userConfig->showSupporters = $params['ssu'] == 'true' ? true : false;
0081         $userConfig->showComments = $params['sco'] == 'true' ? true : false;
0082         $userConfig->logo = $params['log'];
0083         $userConfig->amounts = new stdClass();
0084         $userConfig->amounts->showDonationAmount = $params['sda'] == 'true' ? true : false;
0085 
0086         return $userConfig;
0087     }
0088 
0089     /**
0090      * @param object $userConfig
0091      * @param object $widgetDefault
0092      * @return object
0093      */
0094     protected function mergeConfig($userConfig, $widgetDefault)
0095     {
0096         if (false == is_null($userConfig)) {
0097             $widgetConfig = (object)array_merge((array)$widgetDefault, (array)$userConfig);
0098             $widgetConfig->text = $widgetDefault->text;
0099             $widgetConfig->amounts = (object)array_merge((array)$widgetDefault->amounts, (array)$userConfig->amounts);
0100             return $widgetConfig;
0101         } else {
0102             $widgetConfig = $widgetDefault;
0103             return $widgetConfig;
0104         }
0105     }
0106 
0107     public function saveAction()
0108     {
0109         $this->_helper->layout->disableLayout();
0110         $body = $this->getRequest()->getRawBody();
0111         $data = json_decode($body, true);
0112 
0113         //TODO: Filter Input !!!
0114 
0115         $modelProjectWidget = new Default_Model_DbTable_ProjectWidget();
0116 
0117         try {
0118             $result = $modelProjectWidget->save(array('uuid' => $data['uuid'], 'project_id' => (int)$data['project'], 'config' => json_encode($data['config'])));
0119 
0120             $embedCode = $this->view->externalWidgetSource((int)$data['project']);
0121 
0122             $this->_helper->json(array('status' => 'ok', 'data' => array('uuid' => $result->uuid, 'embedCode' => $embedCode, 'config' => $result->config)));
0123         } catch (Exception $e) {
0124             $this->getResponse()->setHttpResponseCode(500);
0125             $this->_helper->json(array('status' => 'error', 'data' => array('uuid' => '', 'embedCode' => '', 'config' => $result->config)));
0126         }
0127     }
0128 
0129     public function savedefaultAction()
0130     {
0131         $this->_helper->layout->disableLayout();
0132         $body = $this->getRequest()->getRawBody();
0133         $data = json_decode($body, true);
0134 
0135         //TODO: Filter Input !!!
0136 
0137         $modelProjectWidget = new Default_Model_DbTable_ProjectWidgetDefault();
0138 
0139         try {
0140             $result = $modelProjectWidget->save(array('uuid' => $data['uuid'], 'project_id' => (int)$data['project'], 'config' => json_encode($data['config'])));
0141 
0142             $externalWidget = new Default_View_Helper_ExternalWidgetSource();
0143             $embedCode = $this->view->externalWidgetSource((int)$data['project']);
0144 
0145             $this->_helper->json(array('status' => 'ok', 'data' => array('uuid' => $result->uuid, 'embedCode' => $embedCode, 'config' => $result->config)));
0146         } catch (Exception $e) {
0147             $this->getResponse()->setHttpResponseCode(500);
0148             $this->_helper->json(array('status' => 'error', 'data' => array('uuid' => '', 'embedCode' => '', 'config' => $result->config)));
0149         }
0150     }
0151 
0152     public function configAction()
0153     {
0154         $this->_helper->layout->disableLayout();
0155         $widgetProjectId = (int)$this->getParam('project_id');
0156 
0157         if (false == isset($widgetProjectId)) {
0158             throw new Zend_Controller_Action_Exception('This page does not exist', 404);
0159         }
0160 
0161         $widgetDefaultModel = new Default_Model_DbTable_ProjectWidgetDefault();
0162 
0163         $this->view->widgetConfig = $widgetDefaultModel->fetchConfig($widgetProjectId);
0164         $productModel = new Default_Model_Project();
0165         $this->view->product = $productModel->fetchProductInfo($widgetProjectId);
0166         if (is_null($this->view->product)) {
0167             throw new Zend_Controller_Action_Exception('This page does not exist', 404);
0168         }
0169         $this->view->supporting = $productModel->fetchProjectSupporterWithPlings($widgetProjectId);
0170         $plingModel = new Default_Model_DbTable_Plings();
0171         $this->view->comments = $plingModel->getCommentsForProject($widgetProjectId, 10);
0172         $websiteOwner = new Local_Verification_WebsiteProject();
0173         $this->view->authCode = '<meta name="ocs-site-verification" content="' . $websiteOwner->generateAuthCode(stripslashes($this->view->product->link_1)) . '" />';
0174 
0175         $supportersArray = array();
0176         $helperBuildMemberUrl = new Default_View_Helper_BuildMemberUrl();
0177         foreach ($this->view->supporting as $supporter) {
0178             $new_supporter = new stdClass();
0179             $new_supporter->username = $supporter->username;
0180             $new_supporter->img = $supporter->profile_image_url;
0181             $new_supporter->url = $helperBuildMemberUrl->buildMemberUrl($supporter->username);
0182             array_push($supportersArray, $new_supporter);
0183         }
0184         $commentsArray = array();
0185         foreach ($this->view->comments as $comment) {
0186             $new_comment = new stdClass();
0187             $new_comment->username = $comment->username;
0188             $new_comment->img = $comment->profile_image_url;
0189             $new_comment->amount = $comment->amount;
0190             $new_comment->comment = $comment->comment;
0191             array_push($commentsArray, $new_comment);
0192         }
0193 
0194         $result = array();
0195         $result['text'] = array(
0196             'content' => $this->view->widgetConfig->text->content,
0197             'headline' => $this->view->widgetConfig->text->headline,
0198             'button' => 'Pling it!'
0199         );
0200         $result['amounts'] = array(
0201             'donation' => $this->view->widgetConfig->amounts->donation,
0202             'showDonationAmount' => $this->view->widgetConfig->amounts->showDonationAmount,
0203             'current' => (float)$this->view->product->amount_received,
0204             'goal' => (false === empty($this->view->product->amount)) ? $this->view->product->amount : ''
0205         );
0206         $result['colors'] = array(
0207             'widgetBg' => $this->view->widgetConfig->colors->widgetBg,
0208             'widgetContent' => $this->view->widgetConfig->colors->widgetContent,
0209             'headline' => $this->view->widgetConfig->colors->headline,
0210             'text' => $this->view->widgetConfig->colors->text,
0211             'button' => $this->view->widgetConfig->colors->button,
0212             'buttonText' => $this->view->widgetConfig->colors->buttonText
0213         );
0214         $result['showSupporters'] = $this->view->widgetConfig->showSupporters ? 'true' : 'false';
0215         $result['supporters'] = $supportersArray;
0216 
0217         $result['showComments'] = $this->view->widgetConfig->showComments ? 'true' : 'false';
0218         $result['comments'] = $commentsArray;
0219 
0220         $result['logo'] = $this->view->widgetConfig->logo;
0221 
0222         $result['project'] = $this->view->product->project_id;
0223 
0224         $result['uuid'] = $this->view->widgetConfig->uuid;
0225 
0226         $result['embedCode'] = $this->view->externalWidgetSource($this->view->product->project_id);
0227 
0228         $this->_helper->json(array('status' => 'ok', 'message' => $result));
0229     }
0230 
0231 }