<?php
/* ************************************************************************** *
 *																										*
 * Core mod - lib_mod_general.php v3.0 by Slodave, 2008,								*
 * http://www.zabec.net																			*
 * -------- -------- -------- -------- -------- -------- -------- --------		*
 * 																									*
 * Knjižnica, kjer so zbrane funkcije za kopiranje starega vnosa v novega		*
 * ************************************************************************** */

	/* Class skrbi za kopiranje obstoječega itema v nov item
	* klicano iz: @ - ajax.php
	* require: lib_file.php
	*/
	class copyNew extends fileHandling {
		function copyNew($id) {
			global $db;
			global $mod_settings;
			global $moreimages;

			$this->moreimages = $moreimages;
			$this->db = $db;
			$this->settings = $mod_settings;
			$this->id = $id;

			//osnovno
			$limit = array("id", "osnutek", "vrstni_red");
			$this->generateFandV($this->settings['tabela'], $limit);
			$this->execute("normal");

			//jeziki
			foreach($mod_settings['active_lang'] AS $lang) {
				$limit = array("item_id");
				$this->generateFandV($this->settings['tabela']."_".$lang, $limit, "item_id");
				$this->execute("lang", $this->settings['tabela']."_".$lang);
			}

			//datoteke
			if($mod_settings['datoteke']) {
				$this->prekopiraj("datoteka");
			}
			//galerija
			if($mod_settings['galerija']) {
				$this->prekopiraj("galerija");
			}

		}

		function generateFandV($tabela, $limit, $id_tip = "id") {
			$this->field = "";
			$this->values = "";
			$result = $this->db->query("SELECT * FROM ".$tabela." WHERE ".$id_tip." = '$this->id'");
			$r = $result->fetch(MYSQL_ASSOC);
			if($r) {
				foreach($r AS $key => $value) {
					if(!in_array($key, $limit)){
						if($this->settings['slika'] AND $key == "slika" AND $value != "") {
							$this->kopiraj("../../".$this->settings['slika_path'].$value, "../../".$this->settings['slika_path'].$value);
							//dodatne slike
							if($this->moreimages) {
								foreach($this->moreimages AS $imgvalue){
									$this->kopiraj("../../".$this->settings['slika_path'].$imgvalue[0]."/".$value, "../../".$this->settings['slika_path'].$imgvalue[0]."/".$this->newFileName);
								}
							}
							$this->field .= $key.", ";
							$this->values .= "'".$this->newFileName."', ";
						}
						else {
							$this->field .= $key.", ";
							$this->values .= "'$value', ";
						}
					}
				}
			}
		}

		function execute($mode, $tabela = "") {
			switch($mode) {
				case "normal":
					if($this->settings['osnutek']) {
						$this->field .= "osnutek, ";
						$this->values .= "'1', ";
					}
					if($this->settings['vrstni_red']) {
						$this->field .= "vrstni_red, ";
						$this->values .= "'0', ";
					}
					$this->render();
					$sql = "INSERT INTO ".$this->settings['tabela']." (".$this->field.") VALUES (".$this->values.")";
					$result = $this->db->query($sql);
					$this->insertid = $result->insertID();
				break;
				case "lang":
					$this->field .= "item_id";
					$this->values .= "'".$this->insertid."'";
					$sql = "INSERT INTO ".$tabela." (".$this->field.") VALUES (".$this->values.")";
					$result = $this->db->query($sql);
					break;
			}
		}

		function prekopiraj($tip) {
			switch($tip) {
				case "datoteka":
					$result = $this->db->query("SELECT datoteka, opis FROM ".$this->settings['datoteke_tabela']." WHERE item_id = '$this->id'");
					while($r = $result->fetch()) {
						$this->kopiraj("../../".$this->settings['datoteka_path'].$r['datoteka'], "../../".$this->settings['datoteka_path'].$r['datoteka']);
						$this->db->query("INSERT INTO ".$this->settings['datoteke_tabela']." (item_id, datoteka, opis) VALUES ('$this->insertid', '$this->newFileName', '$r[opis]')");
					}
					break;
				case "galerija":
					$result = $this->db->query("SELECT slika, opis FROM ".$this->settings['galerija_tabela']." WHERE item_id = '$this->id'");
					while($r = $result->fetch()) {
						$this->kopiraj("../../".$this->settings['slika_path'].$r['slika'], "../../".$this->settings['slika_path'].$r['slika']);
						//dodatne slike
						if($this->moreimages) {
							foreach($this->moreimages AS $imgvalue){
								$this->kopiraj("../../".$this->settings['slika_path'].$imgvalue[0]."/".$r['slika'], "../../".$this->settings['slika_path'].$imgvalue[0]."/".$this->newFileName);
							}
						}
						$this->db->query("INSERT INTO ".$this->settings['galerija_tabela']." (item_id, slika, opis) VALUES ('$this->insertid', '$this->newFileName', '$r[opis]')");
					}
					break;
			}
		}

		function render() {
			$this->field = substr($this->field, 0, -2);
			$this->values  = substr($this->values, 0, -2);
		}

		function output() {
			return $this->insertid;
		}
	}
?>