ACC SHELL

Path : /srv/www/vhosts/tsisystem/app/controllers/
File Upload :
Current File : //srv/www/vhosts/tsisystem/app/controllers/products_controller.php

<?php // -*- mode: php; indent-tabs-mode: t -*-

/* $Id$ */


class ProductsController extends AppController
{
	var $name = 'Products';
	var $uses = array('Product',  'Article', 'ProdParameter', 'Config');
	var $helpers = array('Pagination', 'Filter', 'Html', 'Javascript', 'Cache');
	var $components = array('Pagination', 'Filter', 'MenuBuilder', 'Session', 'Search');

 	var $cacheAction = array(
		'produkty/hledat' => 0,
		'produkty/podrobnosti' => 0,
		'produkty/aplikace' => 0,
		'admin' => 0,
		'ke-stazeni' => 0,
		'' => WEEK,
	);

	/**
	 *
	 */
	function beforeFilter()
	{
		if (! empty($this->params['admin'])) {
			if (! $this->Session->read('logged_user')) {
				$this->Session->write('redir_to', '/admin/products/index');
				$this->redirect('/admin/login');
				exit();
			}
		}
	}

	/**
	 * Main page with categories - default page to display
	 */
	function home()
	{
		$this->pageTitle = 'TSI System';

		$data = $this->Article->findAll(
		   'article_type IN (0, 1) AND news = 1 AND published = 1 AND (publish_from IS NULL OR publish_from <= NOW())
			AND (publish_to IS NULL OR publish_to > NOW())', NULL, 'created_d DESC', 5
		);
		$this->set('scroll_news', $data);
	}

	/**
	 * Browse product tree or display product page
	 */
	function page($name = NULL)
	{
		$this->Product->bindModel(array('hasMany' => array('ProdParameter' => array(
			'className' => 'ProdParameter', 'foreignKey' => 'product', 'dependent' => true,
			'order' => 'ProdParameter.par_sort ASC'))));

		$this->product = $this->Product->findByName_idx($name);
		// is it already automatically unbinded?
		$this->Product->unBindModel(array('hasMany' => array('ProdParameter')));

		if (! $this->product) {
			$this->redirect('/');
			exit();
		}

		$this->MenuBuilder->base = $this->base;
		$this->MenuBuilder->current = $name;
		$this->set('menu', $this->MenuBuilder->buildMenu(
			$this->Product->menuTree($this->product)));
		$this->set('menu_current', $this->Product->current_path);

		$this->set('top', array(
			'Ruční bezkontaktní teploměry' => 'Bezkontaktní měření',
			'Termokamery a UV kamery' => 'Bezkontaktní měření',
			'Systémové bezkontaktní teploměry' => 'Bezkontaktní měření',
			'Metalografické stroje a materiály' => 'Zkoušky materiálů',
			'Přenosné a stacionární tvrdoměry' => 'Zkoušky materiálů',
			'Mikrotvrdoměry' => 'Zkoušky materiálů',
			'Povlakoměry, tloušťkoměry a porozimetry' => 'Nedestruktivní zkoušení',
			'Ultrazvukové NDT přístroje' => 'Nedestruktivní zkoušení',
			'Ultrazvukové diagnostické přístroje' => 'Nedestruktivní zkoušení',
		));

		$images = $this->Config->find('conf_name = \'images\'');
		$images = unserialize($images['Config']['conf_value']);

		if (isset($images[$this->product['Product']['img_name']])) {
			$this->set('images', $images[$this->product['Product']['img_name']]);
		}
		else {
			$this->set('images', array());
		}

		/* Display final product */
		if (! $this->product['Product']['is_cat']) {

			preg_match('/(.*?)\\--\d+$/', $this->product['Product']['name_idx'], $m);
			$idx = $m[1];

			$tmp = $this->Product->findById($this->product['Product']['parent']);
			$this->set('parent', $tmp['Product']);
			$this->set('product', $this->product['Product']);
			$this->set('product_params', $this->product['ProdParameter']);

			$downloads = $this->Config->find('conf_name = \'downloads\'');
			$downloads = unserialize($downloads['Config']['conf_value']);

			if (! empty($downloads[$this->product['Product']['pdf_name']])) {
				$this->set('pdf', $downloads[$this->product['Product']['pdf_name']]);
			}
			else if (isset($downloads[$idx])) {
				$this->set('pdf', $downloads[$idx]);
			}
			else {
				$this->set('pdf', NULL);
			}

			$software = array();
			foreach (explode(';', $this->product['Product']['sw_name']) as $sw) {
				if (! $sw) {
					continue;
				}
				$sw = preg_quote($sw);
				foreach ($downloads['software'] as $id => $ary) {
					if (preg_match(",$sw\$,", $ary['filename'])) {
						$software[$id] = $ary;
						break;
					}
				}
			}
			$this->set('software', $software);
		}

		/* Only category */
		else {
			$this->set('product', $this->product['Product']);
		}

		/* Bake page title :-) */
		$title = array_keys($this->Product->current_path);
		if (! in_array($this->product['Product']['name'], $title)) {
			array_unshift($title, $this->product['Product']['name']);
		}

		$title[] = 'TSI System';
		$title = array_reverse($title);

		$this->pageTitle = join(' &raquo; ', $title);
	}

	/**
	 *
	 */
	function download()
	{
        if (isset($_SERVER['HTTP_RANGE'])) {
            @ob_end_clean();
            header('HTTP/1.0 304 Not Modified');
            header('Date: ' . date('r'));
            header('Connection: close');
            exit();
        }

		$logged = false;

        // Client is logged as admin
        if ($this->Session->read('logged_user')) {
			$logged = true;
		}
		// Client is logged as customer
		else if (! empty($_SESSION['logged_customer'])) {
			$logged = true;
		}

		$map = $this->Config->find('conf_name = \'downloads\'');
		$map = unserialize($map['Config']['conf_value']);

		/* Application - check whether file exists and make sure that user is
		 * logged */
		if (empty($this->params['filename']) && isset($this->params['idx'])) {
			if (! $logged) {
				if (! empty($_SERVER['HTTP_REFERER'])
					&& strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])) {
					$_SESSION['redir_to'] = $_SERVER['HTTP_REFERER'];
				}
				else {
					$_SESSION['redir_to'] = '/' . $this->params['url']['url'];
				}
				$this->redirect('/login');
				exit;
			}
			@$file = $map['software'][$this->params['idx']];
			if (! $file) {
				$this->redirect('/');
				exit;
			}
		}
		else {
			@$file = $map[$this->params['filename']][$this->params['idx']];
			if (! $file) {
				$this->redirect('/');
				exit;
			}
		}

		@session_write_close();
		@ob_end_clean();

		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename="'
			. preg_replace('/^\d+_/', '', basename($file['filename'])) . '"');
		header('Content-Length: ' . filesize($file['filename']));
		header('Last-Modified: ' . date('r', filemtime($file['filename'])));

		$f = fopen($file['filename'], 'rb');
		while ($buf = fread($f, 65536)) {
			echo $buf;
			@flush();
		}
		fclose($f);

		exit;
	}

	/**
	 *
	 */
	function search()
	{
		if (empty($this->params['form']['search'])) {
			$this->home();
			return $this->render('/home');
		}

		$this->Search->init();
		$results = $this->Search->search($this->params['form']['search']);

		if (empty($results)) {
			$this->set('results', NULL);
			return $this->render('search');
		}

		if (count($results) == 1) {
			$this->redirect($results[0]['url']);
		}

		$this->set('results', $results);
	}

	/* Admin methods */

	/**
	 *
	 */
	function admin_index()
	{
		if (! empty($_REQUEST['up'])) {
			$this->Product->sortUp((int) $_REQUEST['up']);

		}
		else if (! empty($_REQUEST['down'])) {
			$this->Product->sortDown((int) $_REQUEST['down']);
		}

		$cats = array();
		foreach ($this->Product->query("
			SELECT t1.id, t1.name, t2.name FROM {$this->Product->table} t1
			LEFT JOIN {$this->Product->table} t2 ON (t1.parent = t2.id)
			WHERE t1.is_cat = 1
			ORDER BY t1.name
        ") as $ary) {
			$cats[$ary['t1']['id']] = sprintf('%s%s',
				strip_tags($ary['t1']['name']),
				empty($ary['t2']['name']) ? '' : ' (' . strip_tags($ary['t2']['name']) . ')');
		}

		$this->Filter->init($this);
		$this->Filter->setFilter(aa('parent', 'Kategorie'), $cats, a('='));
		$this->Filter->setFilter(aa('name', 'Jméno'), NULL, a('~', '!~'));
        $this->Filter->filter($f, $cond);
        $this->set('filters', $f);

		$this->Pagination->controller = &$this;
		$this->Pagination->show = 20;

		$this->Pagination->init(
			$cond,
			'Product',
			NULL,
			array('name', 'descr', 'is_cat', 'parent', 'id', 'prod_sort'),
			0
		);

		$this->Product->recursive = 1;
		$this->Product->bindModel(array('belongsTo' => array('Parent' => array(
			'className' => 'Product', 'foreignKey' => 'parent'))));

		$this->set('products', $this->Product->findAll(
			$cond,
			NULL,
			$this->Pagination->order,
			$this->Pagination->show,
			$this->Pagination->page
		));
	}

	function admin_edit($id = NULL)
	{
		$downloads = $this->Config->find('conf_name = \'downloads\'');
		$downloads = unserialize($downloads['Config']['conf_value']);
		$this->set('files', array_keys($downloads));

		$software = array();
		foreach ($downloads['software'] as $ary) {
			$software[] = $ary['name'] . '.' . $ary['ext'];
		}
		$this->set('software', $software);

		$tmp = (int)$id;
		$this->set('parents', $this->Product->generateList(
				"is_cat = 1 AND id <> $tmp",
				'parent asc', NULL, '{n}.Product.id', '{n}.Product.name'));

		$this->Product->bindModel(array('hasMany' => array('ProdParameter' => array(
			'className' => 'ProdParameter', 'foreignKey' => 'product', 'dependent' => true,
			'order' => 'ProdParameter.par_sort ASC'))));

		if (empty($this->data)) {
			if (! ($data = $this->Product->find('id = ' . (int) $id))) {
				$this->set('products', null);
			}
			else {
				$data['Product']['descr'] = _html2tmce($data['Product']['descr']);
				$this->set('products', $data);
				$this->data['Product']['is_cat'] = $data['Product']['is_cat'];
			}
		}
		else {
			$software = array();
			foreach (explode(';', $this->data['Product']['sw_name']) as $tmp) {
				$tmp = trim($tmp);
				if ($tmp) {
					$software[] = $tmp;
				}
			}
			$this->data['Product']['sw_name'] = join(';', $software);
			$this->data['Product']['pdf_name']
				= indexFromName($this->data['Product']['pdf_name']);
			$this->data['Product']['descr']
				= fix_local_links($this->data['Product']['descr'], $this->base);

			if($this->Product->save($this->data))
			{
				if ($pid = (int)$this->data['Product']['id']) {
					$this->ProdParameter->query("
						DELETE FROM {$this->ProdParameter->table} WHERE product = $pid
					");
				}

				if (! empty($this->data['ProdParameter']['par_text'])) {

					reset($this->data['ProdParameter']['par_text']);
					reset($this->data['ProdParameter']['par_sort']);
					foreach ($this->data['ProdParameter']['par_name'] as $name) {
						$name = trim($name);
						$value = trim(current($this->data['ProdParameter']['par_text']));
						$sort = (int) current($this->data['ProdParameter']['par_sort']);

						next($this->data['ProdParameter']['par_text']);
						next($this->data['ProdParameter']['par_sort']);

						if (empty($name)) {
							continue;
						}

						$this->ProdParameter->id = NULL;
						$this->ProdParameter->save(array(
							'par_name' => $name,
							'par_text' => $value,
							'par_sort' => $sort,
							'product' => $this->Product->id
						));
					}
				}

				if(is_object($this->Session))
				{
					$this->Session->setFlash('The Product has been saved');
					$this->redirect('/admin/products/index');
				}
				else
				{
					$this->flash('Product saved.', '/admin/products/index');
				}
			}
			else
			{
				if(is_object($this->Session))
				{
					$this->Session->setFlash('Please correct errors below.');
				}

				$data = $this->data;
				$data['ProdParameter'] = array();

				if (! empty($this->data['ProdParameter']['par_text'])) {

					reset($this->data['ProdParameter']['par_text']);
					reset($this->data['ProdParameter']['par_sort']);

					foreach ($this->data['ProdParameter']['par_name'] as $name) {
						$name = trim($name);
						$value = trim(current($this->data['ProdParameter']['par_text']));
						$sort = (int) current($this->data['ProdParameter']['par_sort']);

						next($this->data['ProdParameter']['par_text']);
						next($this->data['ProdParameter']['par_sort']);

						if (empty($name)) {
							continue;
						}

						$data['ProdParameter'][] = array(
							'par_name' => $name,
							'par_text' => $value,
							'par_sort' => $sort,
						);
					}
				}

				$this->set('products', $this->Product->defaultize($data));
			}
		}
	}

	function admin_delete($id)
	{
		$this->Product->bindModel(array('hasMany' => array('ProdParameter' => array(
			'className' => 'ProdParameter', 'foreignKey' => 'product', 'dependent' => true))));
		$this->Product->del($id);
		$this->redirect('/admin/products/index');
	}

	/**
	 *
	 */
	function admin_update_configs()
	{
		$this->Config->updateDownloads();
		$this->admin_index();
		$this->render('admin_index');
	}

	/**
	 *
	 */
	function admin_update_index()
	{
		$this->Search->init();
		$this->Search->reindex();
		$this->admin_index();
		$this->render('admin_index');
	}

	/**
	 *
	 */
	function admin_update_images()
	{
		$this->Config->updateImages();

		$this->admin_index();
		$this->render('admin_index');
	}

	/**
	 *
	 */
	function admin_toggle_html()
	{
		if (empty($_SESSION['tinymce_disabled'])) {
			$_SESSION['tinymce_disabled'] = 1;
		}
		else {
			unset($_SESSION['tinymce_disabled']);
		}
		$this->admin_index();
		$this->render('admin_index');
	}
}

?>

ACC SHELL 2018