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

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  *    Created: 14.09.2016
0024  **/
0025 class Backend_RssController extends Local_Controller_Action_CliAbstract
0026 {
0027 
0028     public function runAction()
0029     {
0030         $this->initGlobalApplicationVars();
0031         foreach (Zend_Registry::get('application_store_category_list') as $hostname => $listCategory) {
0032             $latestProducts = $this->getLatestProductsForHost($listCategory, $hostname);
0033 
0034             $rssWriter = Zend_Feed::importArray($latestProducts);
0035             $resultXml = $rssWriter->saveXml();
0036             $config = Zend_Registry::get('application_store_config_list')[$hostname];
0037             $config_name = $config['config_id_name'];
0038             $this->saveXmlFile($resultXml, $hostname);
0039         }
0040     }
0041 
0042     private function initGlobalApplicationVars()
0043     {
0044         $modelDomainConfig = new Default_Model_DbTable_ConfigStore();
0045         Zend_Registry::set('application_store_category_list', $modelDomainConfig->fetchAllStoresAndCategories());
0046         Zend_Registry::set('application_store_config_list', $modelDomainConfig->fetchAllStoresConfigArray());
0047     }
0048 
0049     private function getLatestProductsForHost($listCategories, $hostname)
0050     {
0051         $filter['category'] = $listCategories;
0052         $filter['order'] = 'latest';
0053         $pageLimit = 10;
0054         $offset = 0;
0055         
0056         $tagFilter  = Zend_Registry::isRegistered('config_store_tags') ? Zend_Registry::get('config_store_tags') : null;
0057         if ($tagFilter) {
0058             $filter['tag'] = $tagFilter;
0059         }
0060 
0061         $modelProject = new Default_Model_Project();
0062         $requestedElements = $modelProject->fetchProjectsByFilter($filter, $pageLimit, $offset);
0063 
0064         $dataArray = $this->createBaseInformation('Latest Products', $hostname);
0065         $dataArray['entries'] = $this->generateFeedData($requestedElements['elements'], $hostname);
0066 
0067         return $dataArray;
0068     }
0069 
0070     private function createBaseInformation($title, $hostname)
0071     {
0072         $storeData = Zend_Registry::get('application_store_config_list')[$hostname];
0073         $storeConfig = $this->getStoreTemplate($storeData['config_id_name']);
0074 
0075         return $importArray = array(
0076             'title'      => $storeConfig['head']['browser_title_prepend'] . ' ' . $title,
0077             // required
0078             'link'       => 'https://' . $hostname . '/content.rdf',
0079             // required
0080             'lastUpdate' => time(),
0081             // optional
0082             //            'published'   => time(),                                                          // optional
0083             'charset'    => 'utf-8',
0084             // required
0085             //            'description' => 'short description of the feed',                                 // optional
0086             'author'     => $storeConfig['head']['meta_author'],
0087             // optional
0088             'email'      => 'contact@opendesktop.org',
0089             // optional
0090             'copyright'  => 'All rights reserved. All trademarks are copyright by their respective owners. All contributors are responsible for their uploads.',
0091             // optional
0092             'image'      => 'https://' . $hostname . $storeConfig['logo'],
0093             // optional
0094             'generator'  => $hostname . ' atom feed generator',
0095             // optional
0096             'language'   => 'en-us',
0097             // optional
0098             'ttl'        => '15'
0099             // optional, ignored if atom is used
0100         );
0101     }
0102 
0103     private function getStoreTemplate($storeConfigName)
0104     {
0105         $storeTemplate = array();
0106 
0107         $fileNameConfig = APPLICATION_PATH . '/configs/client_' . $storeConfigName . '.ini.php';
0108 
0109         if (file_exists($fileNameConfig)) {
0110             $storeTemplate = require APPLICATION_PATH . '/configs/client_' . $storeConfigName . '.ini.php';
0111         } else {
0112             Zend_Registry::get('logger')->warn(__METHOD__ . ' - ' . $storeConfigName
0113                 . ' :: can not access config file for store context.')
0114             ;
0115         }
0116 
0117         return $storeTemplate;
0118     }
0119 
0120     private function generateFeedData($requestedElements, $hostname)
0121     {
0122         $helperTruncate = new Default_View_Helper_Truncate();
0123         $returnValues = array();
0124         foreach ($requestedElements as $requestedElement) {
0125             $returnValues[] = array(
0126                 'title'       => $requestedElement->title . ' [' . $requestedElement->cat_title . ']',
0127                 // required
0128                 'link'        => 'https://' . $hostname . '/p/' . $requestedElement->project_id,
0129                 // required
0130                 'description' => $helperTruncate->truncate(strip_tags($requestedElement->description)),
0131                 // only text, no html, required
0132                 //                'guid' => 'id of the article, if not given link value will used',             // optional
0133                 'content'     => $this->createContent($requestedElement, $hostname),
0134                 // can contain html, optional
0135                 'lastUpdate'  => strtotime($requestedElement->project_changed_at),
0136                 // optional
0137                 //                'comments' => 'comments page of the feed entry',                              // optional
0138                 //                'commentRss' => 'the feed url of the associated comments',                    // optional
0139                 'category'    => array(
0140                     array(
0141                         'term' => $requestedElement->cat_title,                                 // required,
0142                     ),
0143                 ),
0144                 // list of the attached categories                                              // optional
0145                 //                'enclosure'    => array(
0146                 //                array(
0147                 //                    'url' => 'url of the linked enclosure',                                   // required
0148                 //                    'type' => 'mime type of the enclosure',                                   // optional
0149                 //                    'length' => 'length of the linked content in octets',                     // optional
0150                 //                ),
0151                 //                ) // list of the enclosures of the feed entry                                 // optional
0152             );
0153         }
0154 
0155         return $returnValues;
0156     }
0157 
0158     private function createContent($requestedElement, $hostname)
0159     {
0160         $link = 'https://' . $hostname . '/p/' . $requestedElement->project_id;
0161         $helperImage = new Default_View_Helper_Image();
0162         $image = $helperImage->Image($requestedElement->image_small, array('height' => 40, 'width' => 40));
0163 
0164         return '<a href="' . $link . '"><img src="' . $image
0165             . '" alt="Thumbnail" class="thumbnail" align="left" hspace="10" vspace="10" border="0" /></a><b><big><a href="' . $link
0166             . '" style="font-weight:bold;color:#333333;text-decoration:none;">' . $requestedElement->title . '</a></big></b><br /> ('
0167             . $requestedElement->cat_title . ')<br />' . $requestedElement->description . '<br /><br /><a href="' . $link
0168             . '" style="font-weight:bold;color:#333333;text-decoration:none;">[read more]</a><br /><br />';
0169     }
0170 
0171     private function saveXmlFile($resultXml, $hostname)
0172     {
0173         $filename = str_replace('.', '_', $hostname);
0174         $path = APPLICATION_PATH . '/../httpdocs/rss/' . $filename . '-content.rss';
0175         if (is_dir(dirname($path)) AND is_writable(dirname($path))) {
0176             file_put_contents($path, $resultXml);
0177         } else {
0178             throw new Zend_Exception('directory for rss feed files doesn`t exist or is not writable: ' . $path);
0179         }
0180     }
0181 
0182 }