File indexing completed on 2024-06-23 05:51:18

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 class Backend_Commands_DeleteProductExtended implements Local_Queue_CommandInterface
0024 {
0025 
0026     /** @var Zend_Db_Table_Row_Abstract */
0027     protected $product;
0028 
0029     /**
0030      * PHP 5 allows developers to declare constructor methods for classes.
0031      * Classes which have a constructor method call this method on each newly-created object,
0032      * so it is suitable for any initialization that the object may need before it is used.
0033      *
0034      * Note: Parent constructors are not called implicitly if the child class defines a constructor.
0035      * In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
0036      *
0037      * param [ mixed $args [, $... ]]
0038      *
0039      * @param Zend_Db_Table_Row_Abstract $product
0040      *
0041      * @link http://php.net/manual/en/language.oop5.decon.php
0042      */
0043     public function __construct($product)
0044     {
0045         $this->product = $product;
0046     }
0047 
0048     public function doCommand()
0049     {
0050         $this->deleteProductFromIndex();
0051         $this->deleteCollectionFromPPload();
0052         $this->deleteImagesFromCdn();
0053     }
0054 
0055     protected function deleteProductFromIndex()
0056     {
0057         if (empty($this->product->project_id) OR empty($this->product->project_category_id)) {
0058             Zend_Registry::get('logger')->warn(__METHOD__ . ' - no productId or catId was set.');
0059             return;
0060         }
0061 
0062         $modelSearch = new Default_Model_Search_Lucene();
0063         $modelSearch->deleteDocument($this->product->toArray());
0064     }
0065 
0066     private function deleteCollectionFromPPload()
0067     {
0068         // ppload
0069         // Delete collection
0070         if ($this->product->ppload_collection_id) {
0071             $pploadApi = new Ppload_Api(array(
0072                 'apiUri'   => PPLOAD_API_URI,
0073                 'clientId' => PPLOAD_CLIENT_ID,
0074                 'secret'   => PPLOAD_SECRET
0075             ));
0076 
0077             $collectionResponse = $pploadApi->deleteCollection($this->product->ppload_collection_id);
0078 
0079             Zend_Registry::get('logger')->info(__METHOD__ . ' - product delete request for ppload: ' . $this->product->project_id
0080                 . ' response: ' . print_r($collectionResponse,
0081                     true));
0082         }
0083     }
0084     
0085     private function deleteImagesFromCdn()
0086     {
0087         //Remove Logo
0088         $imgPath = $this->product->image_small;
0089         $newPath = $this->deleteImageFromCdn($imgPath);
0090         
0091         //save renamed images
0092         $this->product->image_small = $newPath;
0093         $projectTable = new Default_Model_DbTable_Project();
0094         $galleryPictureTable = new Default_Model_DbTable_ProjectGalleryPicture();
0095 
0096         
0097         //$this->product->save();
0098         $projectTable->update(array('image_small' => $newPath), "image_small = '".$imgPath."'");
0099         $galleryPictureTable->update(array('picture_src' => $newPath), "picture_src = '".$imgPath."'");
0100         
0101         //Remove Gallery Pics
0102         $stmt = $galleryPictureTable->select()->where('project_id = ?', $this->product->project_id)->order(array('sequence'));
0103 
0104         foreach ($galleryPictureTable->fetchAll($stmt) as $pictureRow) {
0105             $imgPath = $pictureRow['picture_src'];
0106             $newPath = $this->deleteImageFromCdn($imgPath);
0107 
0108             //save renamed images
0109             //$galleryPictureTable->update(array('picture_src' => $newPath), 'project_id = '.$pictureRow['project_id'].' AND sequence = '.$pictureRow['sequence']);
0110             $galleryPictureTable->update(array('picture_src' => $newPath), "picture_src = '".$imgPath."'");
0111             $projectTable->update(array('image_small' => $newPath), "image_small = '".$imgPath."'");
0112         }
0113         
0114     }
0115     
0116     private function deleteImageFromCdn($imgPath) {
0117         $config = Zend_Registry::get('config');
0118         $url = $config->images->media->delete;
0119         $secret = $config->images->media->privateKey;
0120         
0121         $postString = '--'.md5(rand()).md5(rand());
0122         $url .= '?path='.urlencode($imgPath).'&post='.$postString.'&key='.$secret;
0123         
0124         $client = new Zend_Http_Client($url);
0125         $response = $client->request('POST');
0126 
0127         if ($response->getStatus() > 200) {
0128             throw new Default_Model_Exception_Image('ERROR: Could not remove images from CD-Server: ' . $url . ' - server response: ' . $response->getBody());
0129         }
0130         
0131         Zend_Registry::get('logger')->info(__METHOD__ . ' - Result fromCN-Server: ' . $response->getBody());
0132         
0133         //save renamed images
0134         return $imgPath.$postString;
0135     }
0136 
0137 }