ImageController.php 8.25 KB
<?php
/**
 * @package  anc_image
 * @category magento
 * @mailto	code [at] netz.coop
 * @author	netz.coop eG* 
 * @copyright (c) 2014, netz.coop eG
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */ 

//    extends Mage_Adminhtml_Controller_Action
class Anc_Image_Adminhtml_ImageController extends Mage_Adminhtml_Controller_Action {

//	private static $customerid;

	public function listAction() {
		$albumBlock = $this->getLayout()->createBlock('anc_image/adminhtml_images');
		$this->loadLayout()
				->_addContent($albumBlock)
				->renderLayout();
	}

	protected function _initAction() {
		$this->loadLayout()->_setActiveMenu('anc_image/ANC')
				->_addBreadcrumb('test Manager', 'test Manager');
		return $this;
	}

	public function indexAction() {
		$this->_initAction();
		$this->renderLayout();
		/**
		 * go to listAction
		 */
		$varparams = $this->getRequest()->getParams();
		D::s($varparams, '$varparams Index', 5, 1);
		if ($varparams['albumid']) {
			$this->_redirect('*/*/list', array(
				'albumid' => $varparams['albumid'],
			));
		} else {
			$this->_redirect(
					'*/*/list', array(
					)
			);
		}
	}

	public function editAction() {
		$id = $this->getRequest()->getParam('id');
		$model = Mage::getModel('anc_image/ncimage')->load($id);
		if ($model->getId() || $id == 0) {
			Mage::register('anc_image', $model);
			$this->loadLayout();
			$this->_setActiveMenu('test/set_time');
			$this->_addBreadcrumb('test Manager', 'test Manager');
			$this->_addBreadcrumb('Test Description', 'Test Description');
			$this->getLayout()->getBlock('head')
					->setCanLoadExtJs(true);
			$this->_addContent($this->getLayout()
							->createBlock('anc_image/adminhtml_images_edit'))
					->_addLeft($this->getLayout()
							->createBlock('anc_image/adminhtml_images_edit_tabs')
			);
			$this->renderLayout();
		} else {
			Mage::getSingleton('adminhtml/session')
					->addError('Test does not exist');
			$this->_redirect('*/*/');
		}
	}

	public function newAction() {
		$this->_forward('edit');
	}

	public function saveAction() {
		$var_deleted=false;
		if ($this->getRequest()->getPost()) {
			try {
				$postData = $this->getRequest()->getPost();
				$varparams = $this->getRequest()->getParams();


				$testModel = Mage::getModel('anc_image/ncimage');
				if ($this->getRequest()->getParam('id') <= 0)
					$testModel->setCreatedTime(
							Mage::getSingleton('core/date')
									->gmtDate()
					);
				
//				D::s($_FILES,'$_FILES',5,1);

				if($postData['file']['delete'] === '1'){
//					D::s($postData, '$postData', 5, 1);
//					D::s($varparams, '$varparams', 5, 1);
//					D::ulli("PATH: media/".$postData['file']['value'],1);
					
//					$path= Mage::helper('anc_lib/data')->realPathToWebPath("media/".$postData['file']['value'],false);
//					D::ulli("$path:". $path,1);
					unlink( "media/".$postData['file']['value']);
					
					$var_deleted = true;
//					$postData['path']= dirname($postData['file']);
					unset($postData['file']);
				}
				
				
				
				$path = "";


				if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
					try {
						if (!(strpos($varparams['path'], "/media/") === 0)) {
							$varparams['path'] = 'media/' . $varparams['path'];
						}

						$filename = $_FILES['file']['name'];
						$path = $varparams['path'];
						D::ulli('Pfad: ' . $path, 1);
						$fname = $_FILES['file']['name']; //file name                       
						$uploader = new Varien_File_Uploader('file'); //load class
						$uploader->setAllowedExtensions(array('png', 'jpg', 'gif')); //Allowed extension for file
						$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
						$uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory.
						$uploader->setFilesDispersion(false);
						$uploader->save($path, $fname); //save the file on the specified path
						$postData['file'] = $_FILES['file']['name'];
//						D::ulli('$path:'.$path.' / $fname:'.$fname);
						$var_upload = true;
					} catch (Exception $e) {
						$var_upload = false;
						echo 'Error Message: ' . $e->getMessage();
					}
				} else {
					$var_upload = false;
				}

				if (is_array($varparams['file'])) {
//					unset($postData['file']);
				}



				if (!(strpos($postData['path'], "/media/") === 0) && $var_deleted !== true) {
//					D::ulli('inside: fn: '.$filename.' /'.$postData['path'],1);
//					$postData['path']=dirname($postData['path']).'/';
					$postData['path'] = '/media/' . $postData['path'] . $filename;
//					$postData['path']=$data['path'];
					$postData['file'] = $filename;
//					D::ulli('inside after : fn: '.$postData['file'].' /'.$postData['path'],1);
				}
				/**
				 * Falls Keine Datei gespeichert wurde 
				 */
				if ($var_upload === false && $var_deleted === false) {
					unset($postData['file']);
					unset($postData['path']);
				}else{
					D::ulli('Upload Font'.$postData['issignature'].' / '. $postData['customer_id'] .' / '. $postData['path'],1);
					if($postData['issignature']=='A' && $postData['customer_id'] && $postData['path']){
						$fontname = Mage::helper('anc_image/ncfonts')->nccreateFont($postData['path'],$postData['customer_id'],$postData['issignature']);
						//issignature
						if($fontname){
							$postData['font']=$fontname;
							Mage::getSingleton('adminhtml/session')
								->addSuccess('Font wurde erstellt er muss jetzt dem System zur Verfügung gestellt werden');
						}else{
							Mage::getSingleton('adminhtml/session')
								->addError('Der Font konnte nicht erstellt werden');
						}
					}
					
					
					
					
					
					
					
				}

//				D::s($postData,'$postData',5,1);

				$testModel
						->addData($postData)
						->setUpdateTime(
								Mage::getSingleton('core/date')
								->gmtDate())
						->setId($this->getRequest()->getParam('id'))
						->save();
				Mage::getSingleton('adminhtml/session')
						->addSuccess('successfully saved');
				Mage::getSingleton('adminhtml/session')
						->settestData(false);
				/**
				 * Für save and continue
				 */
				if ($this->getRequest()->getParam('back')) {
					if ($varparams['albumid']) {
						$this->_redirect(
								'*/*/edit', array(
							'id' => $testModel->getId(),
							'albumid' => $varparams['albumid'],
								)
						);
					} else {
						$this->_redirect(
								'*/*/edit', array(
							'id' => $testModel->getId(),
								)
						);
					}
					return;
				}
				if ($varparams['albumid']) {
					$this->_redirect('*/*/list', array(
						'albumid' => $varparams['albumid'],
					));
				} else {
					$this->_redirect('*/*/list');
				}



				return;
			} catch (Exception $e) {
				Mage::getSingleton('adminhtml/session')
						->addError($e->getMessage());
				Mage::getSingleton('adminhtml/session')
						->settestData($this->getRequest()
								->getPost()
				);
				$this->_redirect('*/*/edit', array('id' => $this->getRequest()
							->getParam('id')));
				return;
			}
		}
		$this->_redirect('*/*/');
	}

	public function deleteAction() {
		if ($this->getRequest()->getParam('id') > 0) {
			try {
				$testModel = Mage::getModel('anc_image/ncimage');
				$testModel->setId($this->getRequest()
								->getParam('id'))
						->delete();
				Mage::getSingleton('adminhtml/session')
						->addSuccess('successfully deleted');
				$this->_redirect('*/*/');
				$this->_redirect('*/*/list');
			} catch (Exception $e) {
				Mage::getSingleton('adminhtml/session')
						->addError($e->getMessage());
				$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
			}
		}
		$this->_redirect('*/*/list');
//		$this->_redirect('*/*/');
	}

	protected function _isAllowed() {
		return Mage::getSingleton('admin/session')->isAllowed('image/form');
	}

}