AdministratorController.php 7.47 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_AdministratorController extends Mage_Adminhtml_Controller_Action {
    protected function _initAction() {
		$this->loadLayout();
		return $this;
    }	
	/**
	 * Block/Template Funktion
	 * 
	 * fnc ist in "mein Benutzerkonto" laut @see app/design/frontend/layout/anc_text.xml als link aufrufbar,
	 * * durch selbige anc_text.xml bekommt die fnc das template @see app/design/frontend/template/anctext/customer_owngallery.phtml beigesteuert
	 */
	public function owngalleryAction() {
		$this->loadLayout();
		$this->getLayout()->getBlock('root')->setTemplate('page/1column.phtml');
		$this->renderLayout();
	}
	
	/**
	 * param - $this->getRequest()->getParams()['id'] -- id des zu bearbeiteten nctext
	 * 
	 * Block/Template Funktion
	 * 
	 */
	public function shownctextformAction() {
			
		if (Mage::getSingleton('customer/session')->isLoggedIn()) {
			$params=$this->getRequest()->getParams();
			if(is_array($params) && array_key_exists('id', $params)) {
				
				$nctext = Mage::helper('anc_text/ncmodel')->getNcText($params['id']);
//D::li($nctext, '$nctext'.$params['id']);
				if(is_object($nctext)) {
					
					if(array_key_exists('name', $params)) {
						$nctext->setName($params['name']);
					}					
					if(array_key_exists('comment', $params)) {
						$nctext->setComment($params['comment']);
					}	
					
					$nctext = Mage::helper('anc_text/ncmodel')->saveNcText($nctext);
//					D::compareFe($nctext,$params, '$nctext, $this->getParams()');

				}

			}
			
			$this->loadLayout();
			$this->getLayout()->getBlock('root')->setTemplate('page/empty.phtml');
//			$this->getLayout()->getBlock('content')->append(
//				$this->getLayout()->createBlock('anc_text/owngallery')->setTemplate('anc/image/customer_showNcTextForm.phtml')
//			);	
			$this->renderLayout();
		} else {
			echo('nicht eingeloggt');
		}
		
	}
	
	public function simpleowngalleryAction() {
//		D::li('testme',1);
		$this->loadLayout();
		$this->getLayout()->getBlock('root')->setTemplate('page/empty.phtml');
		$this->renderLayout();
	}
	
	/**
	 * @param _POST[id] => $this->getRequest()->getParams()[id] -- anc_text/nctext id
	 * 
	 */
	public function deleteNcTextAction() {
		$params=$this->getRequest()->getParams();
		$json_rueckgabe = array();		
		if(array_key_exists('id', $params) && $params['id']) {
			$param_id = $params['id'];

			$nctext = Mage::helper('anc_text/ncmodel')->getNcText($param_id);
			
			if(Mage::helper('anc_text/ncmodel')->deleteNcText($nctext)) {
				$json_rueckgabe['status'] = 'OK';
				$json_rueckgabe['message'] = 'Der Text ('.$nctext->getId().' wurde gelöscht)!';								

			} else {
				$json_rueckgabe['status'] = 'ERROR';
				$json_rueckgabe['message'] = 'Es ist beim Löschen ein Fehler aufgetreten!';								
			}			
			
		} else {
			$json_rueckgabe['status'] = 'ERROR';
			$json_rueckgabe['message'] = 'Keine ID angegeben!';								
		}

		echo json_encode($json_rueckgabe);	
	}
	
	/**
	 * @param optional _POST[id] => $this->getRequest()->getParams()[id] -- anc_text/nctext id
	 * 
	 * fnc wird übers formular von owngalleryAction() bzw customer_owngallery.phtml 
	 * bei einem datei upload aufgerufen und verarbeitet die post werte 
	 * * kopiert datei in entsprechendes kunden verzeichniss
	 * * speichert dateiname im model / in tabelle
	 */
	public function uploadTextAction() {
		$params=$this->getRequest()->getParams();
		if(array_key_exists('id', $params)) {
			$param_id = $params['id'];
		} else {
			$param_id = false;
		}
		if(array_key_exists('album_id', $params)) {
			$param_album_id = $params['album_id'];
		} else {
			$param_album_id = false;
		}
		$user = Mage::getSingleton('admin/session');
		$userId = $user->getUser()->getUserId();
//		D::ulli('Albumid: '.$param_album_id.' / Userid: '.$userId,1);
		$json_rueckgabe = array();

		/* Get the customer data */
		$customer = Mage::getSingleton('customer/session')->getCustomer();

		if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {

			try
			{      
				$path = Mage::helper('anc_text/nctext')->getPathForNewNcText('original');
				$fname = $_FILES['file']['name']; //file name                       
				$uploader = new Varien_File_Uploader('file'); //load class
				$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); //Allowed extension for file
				$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
				$uploader->setAllowRenameFiles(true); //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

				// den modifizierten namen, falls datei schon existierte
				$fname = $uploader->getUploadedFileName(); 

				if(is_file($path.DS.$fname)) {
					/**
					 * datei wird ersetzt
					 */
					if($param_id) {
						$nctext = Mage::helper('anc_text/ncmodel')->getNcText($param_id);

						if(Mage::helper('anc_text/ncmodel')->deleteNcTextFiles($nctext)) {

						} else {
							$json_rueckgabe['status'] = 'ERROR';
							$json_rueckgabe['message'] = 'Sie habe nicht die Berechtigung!';								
							echo json_encode($json_rueckgabe);
							return;
						}

					} else {
						$nctext = Mage::getModel('anc_text/nctext');
					}

//					$nctext->setData('customer_id',$customer->getId());
					$nctext->setData('admin_user_id',$userId);
					$nctext->setData('file',$fname);
					if($param_album_id){//ncalbum_id
						$nctext->setData('ncalbum_id',$param_album_id);
					}
					$nctext->save();

					$nctext->setData('path', Mage::helper('anc_text/nctext')->getNcTextPath($nctext->getId(), 'relative', 'original'));
					$nctext->save();

					if($nctext->getId()) {
						$json_rueckgabe['model'] = 'anc_text/nctext';
						$json_rueckgabe['file'] = $fname;
						$json_rueckgabe['id'] = $nctext->getId();						
						$json_rueckgabe['path'] = $nctext->getPath();
						$json_rueckgabe['status'] = 'OK';
						$json_rueckgabe['message'] = 'upload erfolgreich !! ('.$nctext->getId().'/'.$uploader->getUploadedFileName().')';
					} else {
						$json_rueckgabe['status'] = 'ERROR';
						$json_rueckgabe['message'] = 'Datei konnte nicht in die Datenbank eingetragen werden';
					}

				} else {
					$json_rueckgabe['status'] = 'ERROR';
					$json_rueckgabe['message'] = 'Datei konnte nicht auf dem Server gespeichert werden';						
				}

			} catch (Exception $e) {
				$json_rueckgabe['status'] = 'ERROR';
				$json_rueckgabe['message'] = $e->getMessage();					
			}
		} else {
			$json_rueckgabe['status'] = 'ERROR';
			$json_rueckgabe['message'] = 'Übermittlungsprobleme: Datei konnte nicht gespeichert werden (vielleicht war die Datei größer als '.@ini_get('post_max_size').' MB)';									
		}		 			

		echo json_encode($json_rueckgabe);
	}
}