Nccategory.php 6.46 KB
<?php
/**
 * @package  anc_album
 * @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_Album_Helper_Nccategory extends Mage_Core_Helper_Abstract {

	/**
	 * gibt den request_path zurück, 
	 * falls $param_ncalbum übergeben und 'core/url_rewrite' noch nicht exisitert wird diese in getUrlRewrite angelegt
	 * 
	 * @param Mage_Catalog_Model_Category $param_ncalbumjokercategory
	 * @param array $param_ncalbum
	 * @return string - request_path
	 */
	public function getCategoryUrl (Mage_Catalog_Model_Category $param_ncalbumjokercategory, $param_ncalbum) {
//		D::compareFe($param_ncalbumjokercategory, $param_ncalbum, '$param_ncalbumjokercategory, $param_ncalbum');
		if(is_object($param_ncalbumjokercategory)) {
			if(array_key_exists('url_rewrite', $param_ncalbum) && $param_ncalbum['url_rewrite']) {
				$basepath = dirname($param_ncalbumjokercategory->getRequestPath());
				$new_request_path = $basepath.'/'.$param_ncalbum['url_rewrite'].'.html';
				return $this->getUrlRewrite($new_request_path, $param_ncalbumjokercategory);

			} else if($param_ncalbumjokercategory->getParentId() 
						&& $param_ncalbumjokercategory->getParentCategory()->getName()==='ncalbumjokercategory') {

//				$parent_url_rewrite = basename(Mage::app()->getRequest()->getOriginalPathInfo(), ".html");
				$SelectedCategoryNcAlbumModel = $this->getSelectedCategoryNcAlbumModel();
				if(is_object($SelectedCategoryNcAlbumModel)) {
					$parent_url_rewrite = $SelectedCategoryNcAlbumModel->getUrlRewrite();
					$ncalbum = Mage::getModel('anc_album/ncalbum')->load($parent_url_rewrite, 'url_rewrite');

					if($ncalbum->getUrlRewrite() === $parent_url_rewrite) {
						$new_request_path = str_replace("ncalbumjokercategory", $parent_url_rewrite, $param_ncalbumjokercategory->getRequestPath());
						return $this->getUrlRewrite($new_request_path, $param_ncalbumjokercategory);
					}					
				}


			} else {
				return $param_ncalbumjokercategory->getRequestPath();
			}			
		}


	}
	
	private $SelectedCategoryNcAlbumModel = null;
	private $SelectedCategoryNcAlbumModelByUrl = null;
	
	public function getSelectedCategoryNcAlbumModel() {
		$SelectedCategoryNcAlbumModel =  Mage::getSingleton('checkout/session')->getSelectedCategoryNcAlbumModel();
//		D::fe($SelectedCategoryNcAlbumModel, 'getSelectedCategoryNcAlbumModel');
		return $SelectedCategoryNcAlbumModel;
	}
	
	/**
	 * return Anc_Album_Model_Ncalbum
	 */
	public function getSelectedCategoryNcAlbumModelByUrl() {

		// umständlicher Vergleich zwischen current_category und getRequest
		if(is_null($this->SelectedCategoryNcAlbumModelByUrl)) {
			$current_category = Mage::registry('current_category');
			
			if(is_object($current_category) 
					&& strpos($current_category->getUrlPath(), 'ncalbumjokercategory') !==false
					&& Mage::app()->getRequest()->getParam('id') === $current_category->getId()
			) {
				$OriginalPathInfo = explode('/', Mage::app()->getRequest()->getOriginalPathInfo());
				if(!$OriginalPathInfo[0]) {
					array_shift($OriginalPathInfo);
				}			
				$UrlPath =  explode('/', $current_category->getUrlPath());
				if(!$UrlPath[0]) {
					array_shift($UrlPath);
				}
				
				if(array_search('ncalbumjokercategory', $UrlPath) !== false) {
					$SelectedNcAlbumCategory = $OriginalPathInfo[array_search('ncalbumjokercategory', $UrlPath)];
				} else if(array_search('ncalbumjokercategory.html', $UrlPath) !== false) {
					$SelectedNcAlbumCategory = $OriginalPathInfo[array_search('ncalbumjokercategory.html', $UrlPath)];
					$SelectedNcAlbumCategory = basename($SelectedNcAlbumCategory, ".html");
				} else {
					D::compareFe($OriginalPathInfo, $UrlPath, '$OriginalPathInfo, $UrlPath');
				}
				
				if($SelectedNcAlbumCategory) {
					$ncalbum = Mage::getModel('anc_album/ncalbum')->load($SelectedNcAlbumCategory, 'url_rewrite');
					if(is_object($ncalbum) && $ncalbum->getId()) {
						$this->SelectedCategoryNcAlbumModelByUrl = $ncalbum;
					}					
				}
			} else {
				$current_product = Mage::registry('current_product');
//				D::comparefe($current_product, 'no $current_category!!!!',1,1);

			}
		}
		if(!$this->SelectedCategoryNcAlbumModelByUrl) {
//			D::li('Achtung keine $this->SelectedCategoryNcAlbumModelByUrl = $ncalbum; !!! ',1,1);
		}
		
		return $this->SelectedCategoryNcAlbumModelByUrl;
	}
	/**
	 * schaut nach ob schon exisitert, falls nein wirds erstellt 
	 * und gibt dann den richtigen (durch anc_album/ncalbum.url_rewrite ersetzten) request_path zurück
	 * 
	 * @param string $new_request_path
	 * @param Mage_Catalog_Model_Category $param_ncalbumjokercategory
	 * @return string - request_path
	 */
	private function getUrlRewrite($new_request_path, Mage_Catalog_Model_Category $param_ncalbumjokercategory) {
			$already_url_rewrite = Mage::getModel('core/url_rewrite')->load($new_request_path, 'request_path');
			if($already_url_rewrite->getRequestPath() === $new_request_path) {
				return $already_url_rewrite->getRequestPath();
				
			} else {

				$url_rewrite = Mage::getModel('core/url_rewrite')->load($param_ncalbumjokercategory->getRequestPath(), 'request_path');
				$new_url_rewrite = Mage::getModel('core/url_rewrite');
				$new_url_rewrite->setRequestPath($new_request_path);
				$new_url_rewrite->setStoreId($url_rewrite->getStoreId());
				$new_url_rewrite->setIdPath(str_replace('.html', '', $new_request_path));			
				$new_url_rewrite->setTargetPath($url_rewrite->getTargetPath());
				$new_url_rewrite->setOptions($url_rewrite->getOptions());
				$new_url_rewrite->setDescription($url_rewrite->getDescription());
				$new_url_rewrite->setCategoryId($url_rewrite->getCategoryId());
				$new_url_rewrite->setProductId($url_rewrite->getProductId());
				$new_url_rewrite->save();

				$new_url_rewrite = Mage::getModel('core/url_rewrite')->load($new_request_path, 'request_path');
				return $new_url_rewrite->getRequestPath();
			}			
	}
}