<?php

// no direct access
defined('_JEXEC') or die ;

class K2ModelItemsMultiimages {
    function remove() {
        $cid = JRequest::getVar('cid');
        foreach ($cid as $id) {		
            //Delete multiimages
			$filecount = 1;
            while(
				JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$id)."_".$filecount.".jpg") || 
				JFile::exists(JPATH_SITE.'/media/k2/items/src/'.md5("Image".$id).".jpg")
			) {
				if($filecount == 1) {
					$k2_image_hash = md5("Image".$id);
				}
				else {
					$k2_image_hash = md5("Image".$id) . "_" . $filecount;
				}
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/src/'.$k2_image_hash.'.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/src/'.$k2_image_hash.'.jpg');
				}					
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_XS.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_XS.jpg');
				}					
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_S.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_S.jpg');
				}					
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_M.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_M.jpg');
				}					
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_L.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_L.jpg');
				}				
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_XL.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_XL.jpg');
				}					
				if (JFile::exists(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_Generic.jpg')) {
					JFile::delete(JPATH_ROOT.'/media/k2/items/cache/'.$k2_image_hash.'_Generic.jpg');
				}				
				$filecount++;
			}
        }
    }
	
    function copy() {
        $cid = JRequest::getVar('cid');
        foreach ($cid as $id) {
            //Source images	
			$filecount = 1;
            while(
				JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$id)."_".$filecount.".jpg") || 
				(JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$id).".jpg") && $filecount == 1)
			) {
				if($filecount == 1) {
					$sourceImage = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$id).'.jpg';
					$sourceImageXS = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id).'_XS.jpg';
					$sourceImageS = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id).'_S.jpg';
					$sourceImageM = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id).'_M.jpg';
					$sourceImageL = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id).'_L.jpg';
					$sourceImageXL = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id).'_XL.jpg';
					$sourceImageGeneric = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id).'_Generic.jpg';
				}
				else {
					${"sourceImage".$filecount} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$id)."_".$filecount.'.jpg';
					${"sourceImage".$filecount."XS"} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id)."_".$filecount.'_XS.jpg';
					${"sourceImage".$filecount."S"} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id)."_".$filecount.'_S.jpg';
					${"sourceImage".$filecount."M"} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id)."_".$filecount.'_M.jpg';
					${"sourceImage".$filecount."L"} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id)."_".$filecount.'_L.jpg';
					${"sourceImage".$filecount."XL"} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id)."_".$filecount.'_XL.jpg';
					${"sourceImage".$filecount."Generic"} = JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$id)."_".$filecount.'_Generic.jpg';
				}				
				$filecount++;
			}
			
			//Target images
			$newId = $this->getNewId();
			$filecount = 1;
            while(
				JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$id)."_".$filecount.".jpg") || 
				(JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$id).".jpg") && $filecount == 1)
			) {	
				if($filecount == 1) {		
					if (JFile::exists($sourceImage))
						JFile::copy($sourceImage, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$newId).'.jpg');
					if (JFile::exists($sourceImageXS))
						JFile::copy($sourceImageXS, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId).'_XS.jpg');
					if (JFile::exists($sourceImageS))
						JFile::copy($sourceImageS, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId).'_S.jpg');
					if (JFile::exists($sourceImageM))
						JFile::copy($sourceImageM, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId).'_M.jpg');
					if (JFile::exists($sourceImageL))
						JFile::copy($sourceImageL, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId).'_L.jpg');
					if (JFile::exists($sourceImageXL))
						JFile::copy($sourceImageXL, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId).'_XL.jpg');
					if (JFile::exists($sourceImageGeneric))
						JFile::copy($sourceImageGeneric, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId).'_Generic.jpg');
				}
				else {
					if (JFile::exists(${"sourceImage".$filecount}))
						JFile::copy(${"sourceImage".$filecount}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$newId)."_".$filecount.'.jpg');
					if (JFile::exists(${"sourceImage".$filecount."XS"}))
						JFile::copy(${"sourceImage".$filecount."XS"}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId)."_".$filecount.'_XS.jpg');
					if (JFile::exists(${"sourceImage".$filecount."S"}))
						JFile::copy(${"sourceImage".$filecount."S"}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId)."_".$filecount.'_S.jpg');
					if (JFile::exists(${"sourceImage".$filecount."M"}))
						JFile::copy(${"sourceImage".$filecount."M"}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId)."_".$filecount.'_M.jpg');
					if (JFile::exists(${"sourceImage".$filecount."L"}))
						JFile::copy(${"sourceImage".$filecount."L"}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId)."_".$filecount.'_L.jpg');
					if (JFile::exists(${"sourceImage".$filecount."XL"}))
						JFile::copy(${"sourceImage".$filecount."XL"}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId)."_".$filecount.'_XL.jpg');
					if (JFile::exists(${"sourceImage".$filecount."Generic"}))
						JFile::copy(${"sourceImage".$filecount."Generic"}, JPATH_ROOT.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$newId)."_".$filecount.'_Generic.jpg');
				}
				$filecount++;
			}
        }
    }
	
	function getNewId() {
		$db = JFactory::getDBO();
		$query = "SELECT id FROM #__k2_items ORDER BY id DESC LIMIT 1";
		$db->setQuery($query);
		return $db->loadResult() + 1;
	}
	
}
