<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( 'Not A Valid Entry Point' );

if (! $_REQUEST ['record'] || $_REQUEST ['record'] == '') {
	die ( 'Error!' );
}

$p = new EcmProduct ();
$p->retrieve ( $_REQUEST ['record'] );

// send files with SFTP
$code = preg_replace ( '/[^a-zA-Z0-9-_\.]/', '', $p->code );
$pic1 = "/var/www/html/crm/modules/EcmProducts/upload/images/big/" . $p->product_picture;
$pic2 = "/var/www/html/crm/modules/EcmProducts/upload/images/big/" . $p->packing_front_picture;
$remote_pic1 = "/var/www/html/e5.pl/www/allegro/products/" . $code . strrchr ( $p->product_picture, '.' );
$remote_pic2 = "/var/www/html/e5.pl/www/allegro/products/" . $code . "2" . strrchr ( $p->packing_front_picture, '.' );
$conn = ssh2_connect ( '192.168.1.205', 22 );
if (! ssh2_auth_password ( $conn, 'mz', '3x4z8123' )) {
	echo 'Błąd połaczenia SSH';
}
$pictures = 0;
$img=array();

if(file_exists ( $pic1 )==false){
	$pic1 = "/var/www/html/crm/modules/EcmProducts/upload/images/" . $p->product_picture;
}
if(file_exists ( $pic2 )==false){
	$pic2 = "/var/www/html/crm/modules/EcmProducts/upload/images/" . $p->packing_front_picture;
}
if (file_exists ( $pic1 ) && $p->product_picture!='') {
	ssh2_scp_send ( $conn, $pic1, $remote_pic1, 0644 );
	$img[0] = '<img id="prod-image" src="http://e5.pl/allegro/products/'.$code . strrchr ( $p->product_picture, '.' ).'"/>';
}
if (file_exists ( $pic2 && $p->packing_front_picture!='')) {
	ssh2_scp_send ( $conn, $pic2, $remote_pic2, 0644 );
	$img[1]= '<img id="prod-image" src="http://e5.pl/allegro/products/'.$code .'2'. strrchr ( $p->product_picture, '.' ).'"/>';
}
ssh2_exec ( $conn, 'exit' );

//opis
$db=$GLOBALS['db'];
$tmp = $db->fetchByAssoc($db->query("SELECT long_description as ld FROM ecmproduct_language WHERE language='pl' AND ecmproduct_id='$p->id'"));
//echo "SELECT long_description FROM ecmproduct_language WHERE language='pl' AND ecmproduct_id='$p->id'";
$ld = $tmp['ld'];
$tmp = explode('
', $ld);

$description_label = "Opis produktu";
$specification_label = "Specyfikacja";
$description = '';
$paste = false;
foreach ($tmp as $line) {
	if ($paste && strpos($line,'strong')!=false) break;
	if ($paste) $description.=$line; 
	if (strpos($line, trim($description_label))) $paste = true;
	
	
}

//var_dump($ld);

$specification = '';
$paste = false;
foreach ($tmp as $line) {
	if ($paste && strpos($line,'strong')!=false) break;
	if ($paste) $specification.=$line;
	if (strpos($line, trim($specification_label))) $paste = true;
	
}

//rysujemy ekran
echo '<div style="width: 50%; border: 2px solid black;">';
// create template

$ss = new Sugar_Smarty ();
$ss->assign('name', $p->name);
$ss->assign('pictures', $img);
$ss->assign('specification', html_entity_decode($specification));
$ss->assign('description', html_entity_decode($description));
$content = $ss->fetch ( 'modules/EcmProducts/tpls/allegroTemplate.tpl' );
echo $content;
//koniec ekranu
echo '</div>';
//html do skopiowania
echo '<br><textarea rows="50" cols="200">'.$content.'</textarea>';

?>