ACC SHELL

Path : /srv/www/vhosts/agroing/web/models/
File Upload :
Current File : /srv/www/vhosts/agroing/web/models/export_model.php

<?php
/**
 *  description?
 */
class Export_model extends Base_model {
	public $table = "def";
    public function __construct(){
		
	}
	
	/**
	 *
	 * @param string $zip_file path to zip file
	 * @param string $files_folder path to writeable folder with page attachments
	 * @param int $root_rel_id  id of root relation of this instance
	 */
	public function import($zip_file,$files_folder,$root_rel_id){
		if(!is_writable($files_folder)){
			Tools::alert("error in parametr \$files_folder in Export_model::import()");
			$this->uri->redirect("admin");
		}
		if(!$root_rel_id){
			Tools::critical("no \$parent_id in Export_model::import()");
			$this->uri->redirect("admin");
		}
		// import
		$this->load->library('unzip');
		$unziped = $this->unzip->extract($zip_file, $files_folder);
		if(!empty($unziped)){
			// sql
			$sql = file_get_contents($files_folder . "pre_export.sql");
			$sql = str_replace("#&root_rel_id&#", $root_rel_id ,$sql); // parent = root_relation

			// last ids (where to start)
			$last_rel = $this->db->query("select id from relations order by id desc limit 1")->row_array();
			$last_sites = $this->db->query("select id from sites order by id desc limit 1")->row_array();
			$last_parts = $this->db->query("select id from sites_parts order by id desc limit 1")->row_array();

			foreach(array("rel","sites","parts") as $table){
				$i = 1;
				$mimo = 0;
				while(strpos($sql,"#&".$table."_id")){
					$last_var = "last_" . $table; // naming variable
					$last_var = $$last_var;
					if(strpos($sql,"#&".$table."_id=$i&#")) $sql = str_replace("#&".$table."_id=$i&#",$last_var['id']+$i,$sql);
					elseif($mimo++>10000) die("fatal error - nebylo možné správně obměnit vkládané id relací");
					$i++;
				}
			}
			
			$sql = str_replace(",''",",NULL",$sql);
			$inserts = explode("#&query_separator&#",$sql);

			
			//die("<pre>".$sql);
			$this->db->trans_start();
			$this->db->query("SET FOREIGN_KEY_CHECKS=0");
			foreach($inserts as $insert){
				$this->db->query($insert);
			}
			$this->db->trans_complete();
		}
	}
	
	/**
	 * @param array $root_rel db result of one row relation
	 */
	public function export($root_rel = null){
		if(!$root_rel) $root_rel = Settings_model::$root_relation;
		
		// queries
		$q = " # relations for export
			select * from relations where lft>'".$root_rel['lft']."' && rgt<'".$root_rel['rgt']."' order by relations.id";
		$rels = $this->db->query($q)->result_array();
		$q = " # sites for export
			select sites.* from sites 
				join relations on sites.id = relations.id_sites
				where lft>'".$root_rel['lft']."' && rgt<'".$root_rel['rgt']."' group by sites.id order by sites.id";
		$sites = $this->db->query($q)->result_array();
		$q = " # parts for export
			select sites_parts.* from sites_parts 
				join sites on sites_parts.id_sites = sites.id
				join relations on sites.id = relations.id_sites
				where lft>'".$root_rel['lft']."' && rgt<'".$root_rel['rgt']."' order by sites_parts.id";
		$parts = $this->db->query($q)->result_array();
		
		// id offsets
		$rel_offset = $rels[0]['id']-1;
		$sites_offset = $sites[0]['id']-1;
		$parts_offset = $parts[0]['id']-1;
		
		$keys = array();
		foreach ($rels[0] as $ind=>$d) $keys[] = $ind; // make keys
		$sql_r = "insert into relations \n(`".implode("`,`",$keys)."`) \nvalues ";
		$c = 0;
		foreach($rels as $rel){
			foreach($rel as $i=>$r){ // prepare
				if(in_array($i,array("depth","lft","rgt","parent_site_id"))) $r = ""; // these will be rewriten
				$rel[$i] = str_replace(array("\\","'"),array("\\\\","\\'"),$r); // sanitize
			}
			$rel['id'] = "#&rel_id=" . ($rel['id']-$rel_offset) . "&#";
			$rel['id_sites'] = "#&sites_id=" . ($rel['id_sites']-$sites_offset) . "&#";
			if($rel['parent_id']==$root_rel['id']){
				$rel['parent_id'] = "#&root_rel_id&#";
			}else{
				$rel['parent_id'] = "#&rel_id=" . ($rel['parent_id']-$rel_offset) . "&#";
			}
			if($c) $sql_r .= ",";
			$sql_r .= "\n('" . implode("','",$rel) . "')";
			$c++;
		}
		$sql_r .= "#&query_separator&#\n\n";
		
		$keys = array();
		foreach ($sites[0] as $ind=>$d) $keys[] = $ind; // make keys
		$sql_s = "insert into sites \n(`".implode("`,`",$keys)."`) \nvalues ";
		$c = 0;
		foreach($sites as $site){
			foreach($site as $i=>$r){ // prepare
				$site[$i] = str_replace(array("\\","'"),array("\\\\","\\'"),$r); // sanitize
			}
			$site['id'] = "#&sites_id=" . ($site['id']-$sites_offset) . "&#";
			if($c) $sql_s .= ",";
			$sql_s .= "\n('" . implode("','",$site) . "')";
			$c++;
		}
		$sql_s .= "#&query_separator&#\n\n";
		
		$keys = array();
		foreach ($parts[0] as $ind=>$d) $keys[] = $ind; // make keys
		$sql_p = "insert into sites_parts \n(`".implode("`,`",$keys)."`) \nvalues ";
		$c = 0;
		foreach($parts as $part){
			foreach($part as $i=>$r){ // prepare
				$part[$i] = str_replace(array("\\","'"),array("\\\\","\\'"),$r); // sanitize
			}
			$part['id'] = "#&parts_id=" . ($part['id']-$parts_offset) . "&#";
			$part['id_sites'] = "#&sites_id=" . ($part['id_sites']-$sites_offset) . "&#";
			if($c) $sql_p .= ",";
			$sql_p .= "\n('" . implode("','",$part) . "')";
			$c++;
		}
		//$sql_p .= "#&query_separator&#\n\n";
		
		// order is important
		$sql = $sql_s . $sql_r . $sql_p;
		//die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><pre>' . $sql . '</pre>');
		$temp = tmpfile();
		fwrite($temp, $sql);
		$this->load->library('zip');
		$this->zip->add_data("pre_export.sql", $sql);
		$this->zip->download('export.zip');
		fclose($temp); // remove tmp file
	}

}

ACC SHELL 2018