<?php 
class ControllerTooluniqueseokeywords extends Controller { 
	private $error = array();
	
	public function index() {

		$data['heading_title'] = 'Generare automata URL-uri SEO';
		
		$this->document->setTitle($data['heading_title']);
		
		
		if (isset($this->request->post['generate_for_prod'])) {
      			$data['generate_for_prod'] = $this->request->post['generate_for_prod'];
    		}
		
		if (isset($this->request->post['generate_for_categ'])) {
      			$data['generate_for_categ'] = $this->request->post['generate_for_categ'];
    		}
		
		if (isset($this->request->post['generate_for_info'])) {
      			$data['generate_for_info'] = $this->request->post['generate_for_info'];
    		}
		
		if (isset($this->request->post['generate_for_manu'])) {
      			$data['generate_for_manu'] = $this->request->post['generate_for_manu'];
    		}

    		
    		if (isset($this->request->post['upd_mode'])) {
      			$data['upd_mode'] = $this->request->post['upd_mode'];
		} else {
      			$data['upd_mode'] = 'ad';
    		}


    		////
    		


if ($this->request->server['REQUEST_METHOD'] == 'POST') { /// post

if (isset($this->request->post['generate'])) { /// generate button

if (isset($this->request->post['generate_for_prod']) OR isset($this->request->post['generate_for_categ']) OR isset($this->request->post['generate_for_info']) OR isset($this->request->post['generate_for_manu'])) { /// avem update

if ($this->user->hasPermission('modify', 'tool/uniqueseokeywords')) { /// modify permision

$this->load->model('tool/uniqueseokeywords');

$language_id_for_seo=$this->model_tool_uniqueseokeywords->default_lang();	

$this->session->data['success'] = '';


if (isset($this->request->post['generate_for_prod'])) { // generare pentru produse
if($query = $this->db->query('SELECT product_id, name FROM '.DB_PREFIX.'product_description WHERE language_id="'.$language_id_for_seo.'"')) { // avem produse
$count=0;
foreach ($query->rows as $row) { // scanare
$new_keyword='';
// cautare alias
$query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$row['product_id'] . "'");
if ($query->num_rows) { // alias existent
        
        if ($this->request->post['upd_mode']=='re') { // regenerate alias
        $this->db->query("DELETE FROM ".DB_PREFIX."url_alias WHERE query = 'product_id=".(int)$row['product_id']."'");
        $new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['name']);
        $this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'product_id=".(int)$row['product_id']."', '".$new_keyword."')");
        $count++;
        }
} else { // alias nou
	$new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['name']);
	$this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'product_id=".(int)$row['product_id']."', '".$new_keyword."')");
	$count++;
}
} // end scanare
} // end avem produse
$this->session->data['success'] = $this->session->data['success'].'Succes!: <strong>'.$count.'</strong> URL-urile SEO au fost generate pentru produse.<br />';
} // end generare pentru produse



if (isset($this->request->post['generate_for_categ'])) { // generare pentru category
if($query = $this->db->query('SELECT category_id, name FROM '.DB_PREFIX.'category_description WHERE language_id="'.$language_id_for_seo.'"')) { // avem category
$count=0;
foreach ($query->rows as $row) { // scanare
$new_keyword='';
// cautare alias
$query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'category_id=" . (int)$row['category_id'] . "'");
if ($query->num_rows) { // alias existent
        
        if ($this->request->post['upd_mode']=='re') { // regenerate alias
        $this->db->query("DELETE FROM ".DB_PREFIX."url_alias WHERE query = 'category_id=".(int)$row['category_id']."'");
        $new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['name']);
        $this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'category_id=".(int)$row['category_id']."', '".$new_keyword."')");
        $count++;
        }
} else { // alias nou
	$new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['name']);
	$this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'category_id=".(int)$row['category_id']."', '".$new_keyword."')");
	$count++;
}
} // end scanare
} // end avem produse
$this->session->data['success'] = $this->session->data['success'].'Success: <strong>'.$count.'</strong> URL-urile SEO au fost generate pentru Categorii.<br />';
} // end generare pentru category



if (isset($this->request->post['generate_for_info'])) { // generare pentru information
if($query = $this->db->query('SELECT information_id, title FROM '.DB_PREFIX.'information_description WHERE language_id="'.$language_id_for_seo.'"')) { // avem information
$count=0;
foreach ($query->rows as $row) { // scanare
$new_keyword='';
// cautare alias
$query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$row['information_id'] . "'");
if ($query->num_rows) { // alias existent
        
        if ($this->request->post['upd_mode']=='re') { // regenerate alias
        $this->db->query("DELETE FROM ".DB_PREFIX."url_alias WHERE query = 'information_id=".(int)$row['information_id']."'");
        $new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['title']);
        $this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'information_id=".(int)$row['information_id']."', '".$new_keyword."')");
        $count++;
        }
} else { // alias nou
	$new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['title']);
	$this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'information_id=".(int)$row['information_id']."', '".$new_keyword."')");
	$count++;
}
} // end scanare
} // end avem information
$this->session->data['success'] = $this->session->data['success'].'Success: <strong>'.$count.'</strong>URL-urile SEO au fost generate pentru Pagini Informatii.<br />';
} // end generare pentru information



if (isset($this->request->post['generate_for_manu'])) { // generare pentru manufacturer
if($query = $this->db->query('SELECT manufacturer_id, name FROM '.DB_PREFIX.'manufacturer')) { // avem manufacturer
$count=0;
foreach ($query->rows as $row) { // scanare
$new_keyword='';
// cautare alias
$query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'manufacturer_id=" . (int)$row['manufacturer_id'] . "'");
if ($query->num_rows) { // alias existent
        if ($this->request->post['upd_mode']=='re') { // regenerate alias
        $this->db->query("DELETE FROM ".DB_PREFIX."url_alias WHERE query = 'manufacturer_id=".(int)$row['manufacturer_id']."'");
        $new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['name']);
        $this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'manufacturer_id=".(int)$row['manufacturer_id']."', '".$new_keyword."')");
        $count++;
        }
} else { // alias nou
	$new_keyword=$this->model_tool_uniqueseokeywords->name_to_kw($row['name']);
	$this->db->query("INSERT INTO ".DB_PREFIX."url_alias (query, keyword) VALUES ( 'manufacturer_id=".(int)$row['manufacturer_id']."', '".$new_keyword."')");
	$count++;
}
} // end scanare
} // end avem manufacturer
$this->session->data['success'] = $this->session->data['success'].'Success: <strong>'.$count.'</strong> URL-urile SEO au fost generate pentru Producatori.<br />';
} // end generare pentru manufacturer


} else {
$this->session->data['error'] = 'Atentie: Nu aveti permisiunea sa modificati acest modul !';
} /// end modify permision


} else { /// nu avem update
$this->session->data['error'] = "Eroare: Nu ati selectat nimic pentru generare !";
} /// end avem update

} /// end generate button



if (isset($this->request->post['verify'])) { /// verify button

if($query = $this->db->query('SELECT query, keyword FROM '.DB_PREFIX.'url_alias')) { // avem aliasuri
$count=0;
$duplicated=array();
foreach ($query->rows as $row) { // scanare
$verif_query = $this->db->query("SELECT COUNT(*) as total FROM ".DB_PREFIX."url_alias WHERE keyword = '".$row['keyword']."'");
$find = $verif_query->row['total'];
if ($find>1) {
	$duplicated[$count]['query']=$row['query'];
	$duplicated[$count]['keyword']=$row['keyword'];
	$count++;
	}
} // end scanare
if ($count==0) {
$this->session->data['success'] = 'Succes!: Nu exista nici un URL SEO duplicat !';
} else {
$this->session->data['error'] = "Am gasit ".$count." URL-uri duplicate !";
$data['duplicated'] = $duplicated;
}

} // end avem aliasuri

} /// end verify button



} /// end post
		
		if (isset($this->session->data['error'])) {
    		$data['error_warning'] = $this->session->data['error'];
    
			unset($this->session->data['error']);
 		} elseif (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}
		
		
		$data['action'] = $this->url->link('tool/uniqueseokeywords', 'token=' . $this->session->data['token'], 'SSL');


		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('tool/uniqueseokeywords.tpl', $data));
		
	}

}
?>
