TextController.php 6.44 KB
<?php
/**
 * @package  anc_text 
 * @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/>.
 * 
 */ 
class Anc_Text_Adminhtml_TextController extends Mage_Adminhtml_Controller_Action {


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

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

	public function indexAction() {
		$this->_initAction();
		$this->renderLayout();
		/**
		 * go to listAction
		 */
		$varparams = $this->getRequest()->getParams();
		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_text/nctext')->load($id);
		if ($model->getId() || $id == 0) {
			Mage::register('anc_text', $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_text/adminhtml_texts_edit'))
					->_addLeft($this->getLayout()
							->createBlock('anc_text/adminhtml_texts_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() {

		if ($this->getRequest()->getPost()) {
			try {
				$postData = $this->getRequest()->getPost();
				$varparams = $this->getRequest()->getParams();


				$testModel = Mage::getModel('anc_text/nctext');
				if ($this->getRequest()->getParam('id') <= 0)
					$testModel->setCreatedTime(
							Mage::getSingleton('core/date')
									->gmtDate()
					);
				$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)) {
					$postData['path'] = '/media/' . $postData['path'] . $filename;
					$postData['file'] = $filename;
				}
				/**
				 * Falls Keine Datei gespeichert wurde 
				 */
				if ($var_upload === false) {
					unset($postData['file']);
					unset($postData['path']);
				}
				$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_text/nctext');
				$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');
	}

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

}