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

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 DiscoveryController extends Zend_Controller_Action
0023 {
0024 
0025     /**
0026      * @var int project_id
0027      */
0028     protected $project_id;
0029 
0030     /**
0031      * @param $project_id
0032      */
0033     public function init($project_id)
0034     {
0035         parent::init();
0036         $this->project_id = $project_id;
0037         $this->_discovery = new Default_Model_Discovery();
0038         /*
0039         $this->_helper->contextSwitch()
0040              ->addActionContext('index', 'json')
0041              ->addActionContext('youtube', 'json')
0042              ->addActionContext('general', 'json')
0043              ->initContext();*/
0044         //$this->_helper->layout->disableLayout();
0045     }
0046 
0047     public function indexAction()
0048     {
0049         $this->_helper->layout->disableLayout();
0050         if ($this->_request->isPost() && $this->getParam('next')) {
0051 
0052 
0053             $url = $_REQUEST['url'];
0054             try {
0055                 $data = $this->_discovery->guessGeneralData($url);
0056                 $form = $this->getForm($data);
0057             } catch (Exception $e) {
0058                 $form = $this->getForm(array());
0059             }
0060             $this->view->form = $form;
0061 
0062         }
0063     }
0064 
0065     /**
0066      * @param $data
0067      * @return Zend_Form
0068      */
0069     private function getForm($data)
0070     {
0071         $form = new Zend_Form();
0072         $helperBuildProductUrl = new Default_View_Helper_BuildProductUrl();
0073         $form->setAction($helperBuildProductUrl->buildProductUrl($this->project_id, 'additem'));
0074         return $form;
0075     }
0076 
0077     public function generalAction()
0078     {
0079         $url = $_REQUEST['url'];
0080         $result = $this->_discovery->getGeneralData($url);
0081         $response = array(
0082             'result' => true,
0083             'message' => 'Do you like this content?',
0084             'data' => $result
0085         );
0086         $this->view->response = $response;
0087     }
0088 
0089     public function youtubeAction()
0090     {
0091         $url = $_REQUEST['url'];
0092         $code = $this->_discovery->getYoutubeCode($url);
0093         if ($code) {
0094             try {
0095                 $data = $this->_discovery->getYoutubeData($code);
0096                 $response = array(
0097                     'result' => true,
0098                     'message' => 'This looks like a youtube video',
0099                     'data' => $data
0100                 );
0101             } catch (Exception $e) {
0102                 $response = false;
0103             }
0104         } else {
0105             $response = false;
0106         }
0107         if (!$response) {
0108             $response = array(
0109                 'result' => false,
0110                 'message' => 'This does not look like a youtube video'
0111             );
0112         }
0113         $this->view->response = $response;
0114     }
0115 }