Nctext.php 11.5 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_Helper_Nctext extends Mage_Core_Helper_Abstract {
	public function printAdminId() {
		$admin_id = Mage::getSingleton('admin/session')->getUser()->getId();
	}

	
	
	public function getNcUserAlbumWithTexts($param_customerId){
//		$nctext = Mage::helper('anc_text/ncmodel')->getNcText($param_nctext_id)->;
		
	}
	
	
	
	public function getNcTextMetaDataAsHtml($param_nctext_id) {
		$Quoteitemoption_Collection = Mage::helper('anc_text/ncmodel')->getNcTextMetaData($param_nctext_id);
		$nctext_quoteitemoptions = $Quoteitemoption_Collection->getItems();
		$return_html = 'Dieses Bild haben Sie bis jetzt '.count($nctext_quoteitemoptions).' mal verwendet!  &#10;';
		foreach($nctext_quoteitemoptions as $nctext_quoteitemoption) {
			$return_html .= $nctext_quoteitemoption->getOrdertime().' &#10;';
		}
		return $return_html;
	}
	
		/**
	 * return the path from the anc_text/nctext with the $param_nctext_id
	 * 
	 * @param int $param_nctext_id
	 * @param string $param_kindOfPath relative || absolute
	 * @param string $param_kindOfFile original || thumbnail
	 * @return string
	 */
	public function getNcTextPath($param_nctext_id, $param_kindOfPath, $param_kindOfFile, $param_NoPrivacy=false) {
		return null;
		$nctext = Mage::helper('anc_text/ncmodel')->getNcText($param_nctext_id);
		if(is_object($nctext)) {
			
			$mainpath = $this->getMainPath($nctext, $param_NoPrivacy);
			if($mainpath) {
				if($param_kindOfFile === 'original') {

				} else if($param_kindOfFile === 'thumbnail') {
					if($this->generateThumbnail($mainpath, $nctext)) {
					} else {
						$param_kindOfFile = 'original';
					}
					
				} else {
					return false;
				}
				
				if(is_file($this->getPath('absolute', $this->getAncTextPath($param_kindOfFile).$mainpath, $param_kindOfFile, $nctext))) {
					return $this->getPath($param_kindOfPath, $this->getAncTextPath($param_kindOfFile).$mainpath, $param_kindOfFile, $nctext);
				} else {
					return false;
				}
			} else {
//				D::ulli("getNcTextPath($param_nctext_id, $param_kindOfPath, $param_kindOfFile) FALSE: es gab getMainPath probleme");
			}
			
		} else {
			return false;
		}
	}	
	
	private function getPath($param_kindOfPath, $param_partOfPath, $param_kindOfFile, Anc_Text_Model_Nctext $param_nctext=null) {
		return null;
		if($param_kindOfPath==='relative') {
			$root = '';
		} else if($param_kindOfPath==='absolute') {
			$root = Mage::helper('anc_lib/ncfilepath')->getThisMagentoInstallationPath();
		}
		if(is_null($param_nctext)) {
			return $root.$param_partOfPath.DS.$param_kindOfFile.DS;
		} else {
			return $root.$param_partOfPath.DS.$param_kindOfFile.DS.$param_nctext->getFile();
		}
		
	}
	
	public function generateThumbnail($param_path, Anc_Text_Model_Nctext $param_nctext) {
return null;
		if(!file_exists($this->getPath('absolute', $this->getAncTextPath('thumbnail').$param_path, 'thumbnail', $param_nctext))) {
			$originalpath = $this->getPath('absolute', $this->getAncTextPath('original').$param_path, 'original', $param_nctext);
			$thumbnail_filepath = $this->getPath('absolute', $this->getAncTextPath('thumbnail').$param_path, 'thumbnail', $param_nctext);
			if(file_exists($originalpath)) {
				return mcImagick::makeThumbnail($originalpath, $thumbnail_filepath);
			} else {
				return false;
			}
		} else {
			return true;
		}
	}
	
	public function getPathForNewNcText($param_kindOfFile='original') {
		return null;
		$mainpath = $this->getMainPath();
		if($mainpath) {
			$path = Mage::helper('anc_lib/ncfilepath')->getThisMagentoInstallationPath().$this->getAncTextPath().$mainpath.DS.$param_kindOfFile;
			return $path;
		} else {
			return false;
		}

	}

	private $CropfilePath = array();
	
	/**
	 * wenn es die optionalen individuelle optionen gibt, und diese im frontend durch js:crop gesetzt wurden, gibt es ein gecroptest bild zurück
	 * 
	 * 
	 * @param int $param_item_id - AncText item id
	 * @return string
	 */
	public function getCropfilePath($param_item_id) {

		if(!array_key_exists($param_item_id, $this->CropfilePath) || !$this->CropfilePath[$param_item_id]) {
			$ItemProductOptionByOptionSku = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPAncText);
//			D::s($ItemProductOptionByOptionSku,$ItemProductOptionByOptionSku,5,1,1);
			return null;
			if ($ItemProductOptionByOptionSku) {
				$imgUrl = Mage::helper('anc_text/nctext')->getNcTextPath($ItemProductOptionByOptionSku, 'absolute', 'original');
				$nctext = Mage::helper('anc_text/ncmodel')->getNcText($ItemProductOptionByOptionSku);
				$mainpath = $this->getMainPath($nctext, false);
				$crop_filepath = $this->getPath('absolute', $this->getAncTextPath('crop').$mainpath, 'crop', null);
				$Cropdata = $this->getCropdata($param_item_id);
				if(is_array($Cropdata) && !empty($Cropdata)) {
					$returnpath = $this->crop($imgUrl, $crop_filepath, $Cropdata, $nctext->getFile());
				} else {
					$returnpath = $imgUrl;
				}
				$this->CropfilePath[$param_item_id] = $returnpath;			
			}
		}
		return $this->CropfilePath[$param_item_id];
	}
	
	public function getCropdata($param_item_id) {
		return null;
		$return = array();
		$return['cropH'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_cropH);
		$return['cropW'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_cropW);
		$return['imgH'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_imgH);
		$return['imgW'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_imgW);
		$return['imgInitH'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_imgInitH);
		$return['imgInitW'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_imgInitW);
		$return['imgX1'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_imgX1);
		$return['imgY1'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_text/ncconstant')->ancSPText_KEY_imgY1);
		if($return['imgInitW'] || $return['imgInitH'] || $return['imgW'] || $return['imgH'] || $return['imgY1'] || $return['imgX1'] || $return['cropW'] || $return['cropH']) {
			return $return;
		} else {
			return false;
		}
		
	}
	
	/**
	 * cropt das bild (schneiden, vergrößern etc) in ein temporäres verzeichnis
	 * 
	 * !!! THIS IS JUST AN EXAMPLE !!!, PLEASE USE TextMagick or some other quality text processing libraries
	 * 
	 * @param string $imgUrl
	 * @param string $targetpath
	 * @param array $Cropdata = array(
	 *		imgInitW  	// your text original width (the one we recieved after upload)
	 *		imgInitH 	// your text original height (the one we recieved after upload)
	 *		imgW 		// your new scaled text width
	 *		imgH 		// your new scaled text height
	 *		imgX1 		// top left corner of the cropped text in relation to scaled text
	 *		imgY1 		// top left corner of the cropped text in relation to scaled text
	 *		cropW 		// cropped text width
	 *		cropH 		// cropped text height
	 * )
	 * @param string $filenamepart=''
	 * @return string
	 */
	private function crop($imgUrl, $targetpath, $Cropdata, $filenamepart='') {
		return null;
		mcDir::create($targetpath);
		$jpeg_quality = 100;
		$output_filename = $targetpath."/".$filenamepart."cropped_".rand();

		$what = gettextsize($imgUrl);
		switch(strtolower($what['mime']))
		{
			case 'text/png':
				$img_r = textcreatefrompng($imgUrl);
				$source_text = textcreatefrompng($imgUrl);
				$type = '.png';
				break;
			case 'text/jpeg':
				$img_r = textcreatefromjpeg($imgUrl);
				$source_text = textcreatefromjpeg($imgUrl);
				$type = '.jpeg';
				break;
			case 'text/gif':
				$img_r = textcreatefromgif($imgUrl);
				$source_text = textcreatefromgif($imgUrl);
				$type = '.gif';
				break;
			default: die('text type not supported');
		}
			$resizedText = textcreatetruecolor($Cropdata['imgW'], $Cropdata['imgH']);
			textcopyresampled($resizedText, $source_text, 0, 0, 0, 0, $Cropdata['imgW'], 
						$Cropdata['imgH'], $Cropdata['imgInitW'], $Cropdata['imgInitH']);	

			$dest_text = textcreatetruecolor($Cropdata['cropW'], $Cropdata['cropH']);
			textcopyresampled($dest_text, $resizedText, 0, 0, $Cropdata['imgX1'], $Cropdata['imgY1'], $Cropdata['cropW'], 
						$Cropdata['cropH'], $Cropdata['cropW'], $Cropdata['cropH']);	


			textjpeg($dest_text, $output_filename.$type, $jpeg_quality);

			return $output_filename.$type ;
	}	
	
	

	
	
	/**
	 * gibt den pfad für das 
	 * 
	 * @return string
	 */
	private function getAncTextPath($param_kindOfFile) {
		
		if($param_kindOfFile==='thumbnail' || $param_kindOfFile==='crop') {
			$tmp = true;
		} else {
			$tmp = false;
		}
		
		return Mage::helper('anc_lib/ncfilepath')->getModuleFilepath('anc_text',$tmp);
	}

	/**
	 * gibt den wesentlichen Part zurück vom Pfad zur NcText datei ... ob benutzer, öffentlich oder so 
	 * 
	 * @param Anc_Text_Model_Nctext $param_nctext
	 * @return string|boolean
	 */
	private function getMainPath(Anc_Text_Model_Nctext $param_nctext=null, $param_NoPrivacy=false) {

		$admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();

		if(Mage::app()->getStore()->isAdmin()){
			$vartest='true';
		}
		
		if(is_object($param_nctext)
				&& (
						(
								$param_nctext->getCustomerId() == 0 
							&&	$param_nctext->getAdminUserId() > 0
						)
					)
				)
		{
			$path .= DS.'admin'.DS.$param_nctext->getAdminUserId(); 
		}
		
		
		
		// wenns ein objekt gibt, dann überprüf auch ob du darfst
		else if(is_object($param_nctext) 
			&&	(
					$param_NoPrivacy 
						|| 
					(
						($param_nctext->getCustomerId()==0 || $param_nctext->getCustomerId()===Mage::getSingleton('customer/session')->getCustomer()->getId()) 
							|| 
						Mage::app()->getStore()->isAdmin()
					)				
			)
		) {
			$path .= DS.'customer'.DS.$param_nctext->getCustomerId(); 
			
		// ansonsten bei keinem objekt einfach 
		} else if(!is_object($param_nctext)) {
			
			# Ensure we are in the admin session namespace for checking the admin user..
			
			if(Mage::getSingleton('customer/session')->isLoggedIn()) {
				$path .= DS.'customer'.DS.Mage::getSingleton('customer/session')->getCustomer()->getId(); 
			} else if($admin_logged_in){
				$path .= DS.'admin'.DS.Mage::getSingleton('admin/session')->getUser()->getId();; 
			}else{
				$path .= DS.'customer'.DS.'0'; 
			}
			
			
		} else {
			return false;
		}
		return $path;
	}	
}