File indexing completed on 2025-01-26 05:26:14

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 Default_Form_GallerySubForm extends Zend_Form_SubForm
0024 {
0025 
0026     private $onlineGalleryPictureSources = array();
0027 
0028     private $numberOfUploadGalleryPictures = 1;
0029 
0030     private $maxGalleryPics = 5;
0031 
0032     /**
0033      * Constructs a new GallerySubForm object.
0034      * @param null|array $options
0035      */
0036     public function __construct($options = null)
0037     {
0038         if (isset($options['pictures'])) {
0039             $this->onlineGalleryPictureSources = $options['pictures'];
0040         }
0041 
0042         if (isset($options['nr_upload_pics'])) {
0043             $this->numberOfUploadGalleryPictures = $options['nr_upload_pics'];
0044         }
0045 
0046         parent::__construct($options);
0047     }
0048 
0049     /**
0050      * Returns the label of this sub-form.
0051      * @return string
0052      */
0053     public function getLabel()
0054     {
0055         return 'Product Gallery (max. ' . $this->getMaxGalleryPics() . ' pictures)';
0056     }
0057 
0058     /**
0059      * @return int
0060      */
0061     public function getMaxGalleryPics()
0062     {
0063         return $this->maxGalleryPics;
0064     }
0065 
0066     /**
0067      * @param int $maxGalleryPics
0068      */
0069     public function setMaxGalleryPics($maxGalleryPics)
0070     {
0071         $this->maxGalleryPics = $maxGalleryPics;
0072     }
0073 
0074     /**
0075      * Method if the sub-form should be displayed as required. Returns always false.
0076      * @return bool
0077      */
0078     public function isRequired()
0079     {
0080         return false;
0081     }
0082 
0083     /**
0084      * Sets the image sources of the existing online pictures in the gallery.
0085      * ATTENTION: This method triggers the reinitialization. Would be better to add the pictures with the option 'pictures' in the constructor
0086      * @param $onlineGalleryPictureSources
0087      */
0088     public function setOnlineGalleryImageSources($onlineGalleryPictureSources)
0089     {
0090         $this->onlineGalleryPictureSources = $onlineGalleryPictureSources;
0091         $this->init();
0092     }
0093 
0094     /**
0095      * Initializes the GalleryPicture-SubForm
0096      */
0097     public function init()
0098     {
0099         $this->addPrefixPath('Default_Form_Decorator', APPLICATION_PATH . '/modules/default/forms/decorators/',Zend_Form::DECORATOR);
0100         $this->addElementPrefixPath('Default_Form_Decorator', APPLICATION_PATH . '/modules/default/forms/decorators/',Zend_Form::DECORATOR);
0101 
0102         //General setups
0103         $this->setDisableLoadDefaultDecorators(true);
0104         $this->setDecorators(
0105             array(
0106                 'FormElements',
0107                 'Gallery',
0108                 'GalleryError',
0109                 array('HtmlTag', array('tag' => 'div', 'class' => 'field relative')),
0110                 'Label'
0111             ))
0112              ->setDecorators(
0113                  array(
0114                      'FormElements',
0115                      'Gallery',
0116                      'GalleryError',
0117                      array(
0118                          'ViewScript',
0119                          array(
0120                              'viewScript' => 'product/viewscripts/subform_gallery.phtml',
0121                              'placement'  => false
0122                          )
0123                      )
0124                  ))
0125              ->setElementsBelongTo('gallery')
0126              ->setIsArray(false)
0127              ->setElementsBelongTo(null);
0128 
0129         //SubForm for already uploaded - online - pictures
0130         $subFormOnline = new Zend_Form_SubForm();
0131         $subFormOnline->setDisableLoadDefaultDecorators(true)
0132                       ->setDecorators(array(
0133                           'FormElements'
0134                       ));
0135 
0136         $i = 0;
0137         foreach ($this->onlineGalleryPictureSources as $onlineGalleryImageSrc) {
0138 
0139             $onlineGalleryPicture = $this->createElement('hidden', '' . $i,
0140                 array('id' => 'gallery_picture_online_' . $i))
0141                                          ->setDisableLoadDefaultDecorators(true)
0142                                          ->setValue($onlineGalleryImageSrc)
0143                                          ->setDecorators(
0144                                              array(
0145                                                  'ViewHelper',
0146                                                  array('GalleryPicture', array('type' => 0))
0147                                              ));
0148 
0149             $subFormOnline->addElement($onlineGalleryPicture);
0150             $i++;
0151         }
0152 
0153 
0154         //Sub-Form for pics that should be uploaded
0155         $subFormUpload = new Zend_Form_SubForm();
0156         $subFormUpload->setDisableLoadDefaultDecorators(true)
0157                       ->setDecorators(array(
0158                           'FormElements'
0159                       ));
0160 
0161 
0162         $imageTable = new Default_Model_DbTable_Image();
0163         /** @var Zend_Form_Element_File $uploadGalleryPicture */
0164         $uploadGalleryPicture = $subFormUpload->createElement('file', 'upload_picture');
0165         $uploadGalleryPicture->addPrefixPath('Local_File_Transfer_Adapter', 'Local/File/Transfer/Adapter',Zend_Form_Element_File::TRANSFER_ADAPTER);
0166         $uploadGalleryPicture->setTransferAdapter('HttpMediaType');
0167         $uploadGalleryPicture->setDisableLoadDefaultDecorators(true)
0168                              ->setRequired(false)
0169                              ->setDecorators(
0170                                  array(
0171                                      array('File' => new Local_Form_Decorator_File()),
0172                                      array('GalleryPicture', array('type' => 1))
0173                                  ))
0174                              ->setAttrib('class', 'gallery-picture')
0175                              ->setAttrib('onchange', 'ProductGallery.previewImage(this);')
0176                              ->addValidator('Count', true, 5)
0177                              ->setMaxFileSize('29360128') //This setting affects the entire form, so here we also need to add the size of the logo.
0178                              ->addValidator('Size', true, array('max' => '5242880')) //max size of single uploaded file
0179                              ->addValidator('FilesSize', true, array('max' => '27262976')) //max size of all uploaded files
0180                              ->addValidator('Extension', true, $imageTable->getAllowedFileExtension())
0181                              ->addValidator('MimeType', true, $imageTable->getAllowedMimeTypes())
0182                              ->setMultiFile($this->getNumberOfUploadGalleryPictures())
0183                              ->addFilter('Rename',
0184                                  array(
0185                                      'target'    => IMAGES_UPLOAD_PATH . 'tmp/',
0186                                      'overwrite' => true,
0187                                      'randomize' => true
0188                                  ))
0189                              ->setIsArray(true);
0190 
0191         $subFormUpload->addElement($uploadGalleryPicture);
0192 
0193         //Adding Subforms to the form
0194         $this->addSubForm($subFormOnline, 'online_picture')
0195              ->addSubForm($subFormUpload, 'upload');
0196     }
0197 
0198     public function getNumberOfUploadGalleryPictures()
0199     {
0200         return $this->numberOfUploadGalleryPictures;
0201     }
0202 
0203     /**
0204      * Sets the number of displayed upload elements.
0205      * ATTENTION: This method triggers the reinitialization. Would be better to add the pictures with the option 'pictures' in the constructor
0206      * @param $number
0207      */
0208     public function setNumberOfUploadGalleryPictures($number)
0209     {
0210         $this->numberOfUploadGalleryPictures = $number;
0211         $this->init();
0212     }
0213 
0214     public function getOnlineGalleryImageSources()
0215     {
0216         return $this->onlineGalleryPictureSources;
0217     }
0218 
0219     public function isValid($data)
0220     {
0221         $valid = parent::isValid($data);
0222         //Validates, if the number of pictures exceed the specified number
0223         //Minus one because one of the uploaded elements is always null       
0224 
0225         $cntOnlinePicture = 0;
0226         foreach ($this->online_picture->getElements() as $el) {
0227             if ($el->getValue()) {
0228                 $cntOnlinePicture++;
0229             }
0230         }
0231         if ($cntOnlinePicture + count($this->upload->upload_picture->getValue()) > $this->getMaxGalleryPics()) {
0232             $this->markAsError();
0233             $this->addErrorMessage('projects.edit.gallery.max_number_files_exceeded');
0234             $valid = false;
0235         }
0236 
0237         return $valid;
0238     }
0239 
0240 }