<?php
namespace App\Service;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;
use FileUploader;
use Doctrine\DBAL\DBALException;
use Symfony\Component\Security\Core\Security;
class contentservice
{
public $content_image;
public $security;
public function __construct(Connection $connection, LoggerInterface $logger, KernelInterface $kernel, Security $security)
{
$this->dbconnection = $connection;
$this->logger = $logger;
$this->security = $security;
$user = $this->security->getUser();
//$userID = $user->getId();
//$companyID = $user->getCompany();
//$folder = $user->getFolder();
//file_put_contents("session1.log", "/var/www/nota/public/moxiemanager/companies/". $folder ."/files/");
//file_put_contents("session2.log", print_r($_SESSION, true));
//$_SESSION['filesystem.rootpath']="/var/www/nota/public/moxiemanager/companies/". $folder ."/files/";
try
{
$this->rootDir = $kernel->getProjectDir();
$this->logger->info($this->rootDir);
}
catch(Exception $e)
{
$this->logger->info($e);
}
}
private function friendlyName($Aname) {
$crke = array('Č', 'Š', 'Ž', 'č', 'š', 'ž', 'ć', 'Ć', '%', '_', ';', '€');
$zamenjava = array('C', 'S', 'Z', 'c', 's', 'z', 'c', 'C', '-procentov', '-', '-', 'EUR');
$name = str_replace($crke, $zamenjava, $Aname);
preg_match_all('/\w+/', strtolower($name), $m);
return implode('-', $m[0]);
}
public function getAllContent($Amaterial_id,$AUserCompany)
{
$lContenti = array();
$lquery = "select *, concat(u.user_name,' ',u.user_surname) as created
from contents c, users u, engines e, objecttypes t, objects o, languages l, materials m, catalogues g where
c.engine_id = e.engine_id and t.objecttype_id = e.objecttype_id and o.material_id = m.material_id and t.catalogue_id = g.catalogue_id and
o.object_id = t.object_id and c.user_id = u.user_id and c.language_id = l.language_id and m.material_id = ".$Amaterial_id . " and c.company_id = " . $AUserCompany;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $content)
{
$ldateraw = $content['content_date'];
$ldate = date('d.m.Y', strtotime($ldateraw));
$lstatusdescriptive ="";
if ($content['content_status'] =="0")
{
$lstatusdescriptive = "izbrisana";
}
else if ($content['content_status'] =="1")
{
$lstatusdescriptive = "v pripravi";
}
else if ($content['content_status'] =="2")
{
$lstatusdescriptive = "objavljena";
}
$lContenti[] = array('content_id' => $content['content_id'],
'content_date' => $ldate,
'content_title' => $content['content_title'],
'content_dateraw' => $ldateraw,
'content_status' => $content['content_status'],
'content_status_descriptive' => $lstatusdescriptive,
'content_notes' => $content['content_notes'],
'language_title' => $content['language_title'],
'catalogue_title' => $content['catalogue_title'],
'breadcrumbs' => "content/".$this->friendlyName($content['content_title']),
'user_id' => $content['user_id'],
'created' => $content['created']
);
}
return $lContenti;
}
public function getContentByID($Acontent_id)
{
$lContent = array();
$lquery = "select *, concat(u.user_name,' ',u.user_surname) as created
from contents c, users u, engines e, objecttypes t, objects o, languages l, materials m, catalogues g where
c.engine_id = e.engine_id and t.objecttype_id = e.objecttype_id and o.material_id = m.material_id and t.catalogue_id = g.catalogue_id and
o.object_id = t.object_id and c.user_id = u.user_id and c.language_id = l.language_id and c.content_id = ".$Acontent_id;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $content)
{
$ldate = date( 'd.m.Y', strtotime($content['content_date']) );
$lquery = "SELECT count(*) as fields from contentforms where content_id = ".$Acontent_id;
$lfields = 0;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $fields)
{
$lfields = $fields['fields'];
}
$lContent[] = array('content_id' => $content['content_id'],
'content_title' => $content['content_title'],
'content_date' => $ldate,
'engine_id' => $content['engine_id'],
'catalogue_title' => $content['catalogue_title'],
'content_status' => $content['content_status'],
'content_notes' => $content['content_notes'],
'user_id' => $content['user_id'],
'created' => $content['created'],
'language_id' => $content['language_id'],
'content_image' => $content['content_image'],
'fields' => $lfields
);
}
return $lContent;
}
public function getPreloadedPhotos($AcontentID)
{
$lPhotos = array();
$preloadedFiles = array();
$folder = $this->security->getUser()->getFolder();
$lquery = "select * from photos WHERE content_id = " . $AcontentID;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $photo)
{
$lPhotos[] = array('photo_id' => $photo['photo_id'],
'photo_name' => $photo['photo_name'],
'photo_description' => $photo['photo_description'],
'photo_file' => $photo['photo_file'],
'photo_dateadded' => $photo['photo_dateadded'],
'photo_ordernum' => $photo['photo_ordernum'],
'photo_type' => $photo['photo_type'],
'photo_size' => $photo['photo_size']
);
$preloadedFiles[] = array(
'name' => $photo['photo_file'],
'type' => $photo['photo_type'],
'size' => $photo['photo_size'],
'file' => $this->rootDir . '/public/uploads/' . $folder . '/' . $photo['photo_file'],
'data' => array(
'readerForce' => true,
'thumbnail' => $this->rootDir . '/public/uploads/' . $folder . '/' . $photo['photo_file'],
'url' => $this->rootDir . '/public/uploads/' . $folder . '/' . $photo['photo_file'],
'date' => $photo['photo_dateadded'],
'isMain' => 0,
'listProps' => array(
'id' => $photo['photo_id'],
)
),
);
}
return $preloadedFiles;
}
public function getPreloadedDocs($AcontentID)
{
$lPhotos = array();
$lquery = "select * from docs WHERE content_id = " . $AcontentID;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $doc)
{
$lPhotos[] = array('doc_id' => $doc['doc_id'],
'doc_name' => $doc['doc_name'],
'doc_description' => $doc['photo_doc_descriptiondescription'],
'doc_file' => $doc['doc_file'],
'doc_dateadded' => $doc['doc_dateadded'],
'doc_ordernum' => $doc['doc_ordernum'],
'doc_type' => $doc['doc_type'],
'doc_size' => $doc['doc_size']
);
}
return $lPhotos;
}
public function addContent()
{
$lcontent_id = 0;
$lToday = date('Y-m-d H:i');
$SqlInsert = "INSERT INTO contents (content_date,engine_id,user_id,content_status,company_id,language_id) VALUES (?,?,?,?,?,?)";
$stmt = $this->dbconnection->prepare($SqlInsert);
$stmt->bindValue(1, $lToday);
$stmt->bindValue(2, $this->engine_id);
$stmt->bindValue(3, $this->user_id);
$stmt->bindValue(4, 1);
$stmt->bindValue(5, $this->company_id);
$stmt->bindValue(6, 1);
try
{
$stmt->execute();
$lcontent_id = $this->dbconnection->lastInsertId();
}
catch(DBALException $e)
{
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $lcontent_id;
}
public function deleteMenuContents($AContentID, $AMenuID)
{
$lSQLStatus = false;
$lSqlDeleteMenuContents = "DELETE FROM menucontents WHERE content_id = ? AND menu_id = ?";
file_put_contents("errorTest.log", $AMenuID);
$stmt = $this->dbconnection->prepare($lSqlDeleteMenuContents);
$stmt->bindValue(1, $AContentID);
$stmt->bindValue(2, $AMenuID);
file_put_contents("errorTest.log","OK");
try
{
$stmt->execute();
$lSQLStatus = true;
}
catch(DBALException $e)
{
$lSQLStatus = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $lSQLStatus;
}
public function insertMenuContents($AContentID, $AMenuID)
{
$lSQLStatus = false;
$lSqlInsertMenuContents = "INSERT INTO menucontents (content_id, menu_id) VALUES (?,?)";
$stmt = $this->dbconnection->prepare($lSqlInsertMenuContents);
$stmt->bindValue(1, $AContentID);
$stmt->bindValue(2, $AMenuID);
try
{
$stmt->execute();
$lSQLStatus = true;
}
catch(DBALException $e)
{
$lSQLStatus = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $lSQLStatus;
}
public function saveContent()
{
$lSqlUpdate = "UPDATE contents set engine_id = ?, content_title = ?, content_notes = ?, content_image = ?, language_id = ? WHERE content_id = ?";
$stmt = $this->dbconnection->prepare($lSqlUpdate);
$stmt->bindValue(1, $this->engine_id);
$stmt->bindValue(2, $this->content_title);
$stmt->bindValue(3, $this->content_notes);
$stmt->bindValue(4, $this->content_image);
$stmt->bindValue(5, $this->language_id);
$stmt->bindValue(6, $this->content_id);
file_put_contents("tree1.log", print_r($this->treemenu, true) );
foreach($this->treemenu as $node)
{
$this->deleteMenuContents($this->content_id, $node['id']);
if ($node['state']['selected'] == 'true')
{
$this->insertMenuContents($this->content_id, $node['id']);
file_put_contents("tree1-.log", $node['id'] . ":" . $node['state']['selected'] . PHP_EOL, FILE_APPEND);
}
//grem skozi drevo in pogledam katera vozlišča so označena. tista zapišem v tabelo menucontents
/*if ($node['id'] = $this->content_id)
{
file_put_contents("tree1-.log", print_r($node['id'] . ":" . $node['state']['selected'], true), FILE_APPEND );
}*/
}
try
{
$stmt->execute();
$lok = true;
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $lok;
}
public function savetinymce()
{
$contentform_id = 0;
$lok = false;
//najprej dobimo contentform id da lahko udejtamo tabelo
//prequery - domino iz bate za katero vrsto vsebine gre?
$lprequery = "select * from contentforms c, enginefields e WHERE c.content_id = " . $this->content_id . " and c.enginefield_id = e.enginefield_id and e.enginefield_key = 'contents'";
$lSqlpre = $this->dbconnection->fetchAllAssociative($lprequery);
foreach ($lSqlpre as $res)
{
$preid = $res['enginefield_id'];
}
//***************************************************** */
$lquery = "select * from contentforms WHERE content_id = " . $this->content_id ." and enginefield_id = " . $preid ;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $res)
{
$contentform_id = $res['contentform_id'];
}
$lSqlUpdate = "UPDATE contentforms set contentform_value = ? WHERE content_id = ? and contentform_id = ?";
if ($contentform_id != 0)
{
$stmt = $this->dbconnection->prepare($lSqlUpdate);
$stmt->bindValue(1, $this->content_tinymce);
$stmt->bindValue(2, $this->content_id);
$stmt->bindValue(3, $contentform_id);
file_put_contents("tiny.log", "UPDATE contentforms set contentform_value = $this->content_tinymce WHERE content_id = $this->content_id and enginefield_id =$contentform_id");
try
{
$stmt->execute();
$lok = true;
}
catch(\DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
}
return $lok;
}
public function deleteContent()
{
$lSqlUpdate = "UPDATE contents set content_status = 0 WHERE content_id = ?";
$stmt = $this->dbconnection->prepare($lSqlUpdate);
$stmt->bindValue(1, $this->content_id);
try
{
$stmt->execute();
$lok = true;
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $lok;
}
public function getLogsContent($Acontent_id)
{
$logArray = array();
$lquery = "SELECT * FROM logs l, actions a, users u WHERE a.action_id = l.action_id and u.user_id = l.user_id and l.content_id =" . $Acontent_id;
$lSql = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSql as $log)
{
$logArray[] = array('log_datetime' => $log['log_datetime'],
'user_name' => $log['user_name'],
'user_surname' => $log['user_surname'],
'action_title' => $log['action_title'],
'log_description' => $log['log_description'],
);
}
return $logArray;
}
public function createForm()
{
$lok = true;
$fields = array();
$lquery = "SELECT * FROM enginefields where engine_id = ".$this->engine_id;
$lSqlEngineFields = $this->dbconnection->fetchAllAssociative($lquery);
foreach ($lSqlEngineFields as $EngineField)
{
$lqueryInsert = "INSERT INTO contentforms (content_id,enginefield_id) VALUES (?,?)";
$stmt = $this->dbconnection->prepare($lqueryInsert);
$stmt->bindValue(1, $this->content_id);
$stmt->bindValue(2, $EngineField['enginefield_id']);
try
{
$stmt->execute();
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
$fields[] = array('field_id' => $EngineField['enginefield_id']);
}
return $fields;
}
public function showForm()
{
$fields = array();
$lquery = "SELECT * FROM contentforms c, enginefields e where
c.enginefield_id = e.enginefield_id and
c.content_id = ".$this->content_id." order by e.enginefield_number";
$lSqlEnginefields = $this->dbconnection->fetchAllAssociative($lquery);
$lanchor = "#";
foreach ($lSqlEnginefields as $EngineField)
{
$lvrednost = $EngineField['contentform_value'];
if ($EngineField['contentform_value'] == "" && $lanchor != 'prazna')
{
$lanchor = "prazna";
}
else
{
$lanchor = "#";
}
$fields[] = array('contentform_id' => $EngineField['contentform_id'],
'contentform_value' => $EngineField['contentform_value'],
'enginefield_instruction' => $EngineField['enginefield_instruction'],
'enginefield_datatype' => $EngineField['enginefield_datatype'],
'enginefield_key' => $EngineField['enginefield_key'],
'enginefield_value' => $EngineField['enginefield_value'],
'enginefield_label' => $EngineField['enginefield_label'],
'enginefield_objecttype' => $EngineField['enginefield_objecttype'],
'enginefield_number' => $EngineField['enginefield_number'],
'anchor' => $lanchor
);
}
return $fields;
}
public function getPreloadedImage()
{
$folder = '';
//dobimo mapo v kateri folder naj naložimo sliko
$user = $this->security->getUser();
$folder = $user->getFolder();
if ($user->getFolder() == 'mki')
{
$folder = 'strah';
}
$uploadDir = $this->rootDir .'/public/uploads/' . $folder .'/';
$uploadDirReturn = $this->rootDir .'/public/uploads/' . $folder .'/';
//$folder = $this->security->getUser()->getFolder();
$preloadedFiles = array();
$lSql = $this->dbconnection->fetchAllAssociative("SELECT content_image from contents where content_id = " . $this->content_id);
foreach ($lSql as $image)
{
if ( $image['content_image'] != null)
{
file_put_contents("preloadcontentimage1.log", $this->rootDir . '/public/uploads' . $folder . '/' . $image['content_image']);
$preloadedFiles[] = array(
"name" => $image['content_image'],
"type" => mime_content_type($this->rootDir . '/public/uploads/' . $folder . '/' . $image['content_image']),
"size" => filesize($this->rootDir . '/public/uploads/' . $folder . '/' . $image['content_image']),
"file" => '/uploads/' . $folder . '/' . $image['content_image'],
"local" => $this->rootDir . '/public/uploads/' . $folder . '/' . $image['content_image'], // same as in form_upload.php
"data" => array(
"url" => $this->rootDir . '/public/uploads/' . $folder . '/' . $image['content_image'], // (optional)
"thumbnail" => file_exists($this->rootDir . '/public/uploads/' . $folder . '/' . $image['content_image']) ? $uploadDirReturn . 'thumbs/' . $image['content_image'] : null, // (optional)
"readerForce" => true // (optional) prevent browser cache
),
);
}
}
return $preloadedFiles;
}
public function saveField()
{
$lSqlUpdate = "UPDATE contentforms set contentform_value = ? where contentform_id= ?";
$stmt = $this->dbconnection->prepare($lSqlUpdate);
$stmt->bindValue(1, $this->contentform_value);
$stmt->bindValue(2, $this->contentform_id);
try
{
$stmt->execute();
$lok = true;
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
foreach($this->treemenu as $node)
{
$this->deleteMenuContents($this->content_id, $node['id']);
if ($node['state']['selected'] == 'true')
{
$this->insertMenuContents($this->content_id, $node['id']);
}
}
return $lok;
}
public function publishContent()
{
$lSqlUpdate = "UPDATE contents set content_status = 2 WHERE content_id = ?";
$stmt = $this->dbconnection->prepare($lSqlUpdate);
$stmt->bindValue(1,$this->content_id);
try
{
$lok = true;
$stmt->execute();
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $lok;
}
public function writePhotoDB()
{
$maxOrdernum = 0;
$lSql = $this->dbconnection->fetchAllAssociative("SELECT MAX(photo_ordernum) AS photo_ordernum FROM photos WHERE content_id = $this->content_id");
foreach ($lSql as $result)
{
$maxOrdernum = $result['photo_ordernum'] +1;
}
$sqlInsertPhoto = "INSERT INTO photos (content_id, photo_name, photo_file, photo_dateadded, photo_ordernum, photo_type, photo_size) VALUES (?,?,?,NOW(),?,?,?)";
$stmt = $this->dbconnection->prepare($sqlInsertPhoto);
$stmt->bindValue(1,$this->content_id);
$stmt->bindValue(2,$this->photo_name);
$stmt->bindValue(3,$this->photo_file);
$stmt->bindValue(4,$maxOrdernum);
$stmt->bindValue(5,$this->photo_type);
$stmt->bindValue(6,$this->photo_size);
try
{
$lok = true;
$stmt->execute();
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
return $this->dbconnection->lastInsertId();
}
public function sortPhotos()
{
$listsort = json_decode($this->list, true);
$index = 0;
foreach($listsort as $val)
{
if (!isset($val['id']) || !isset($val['name']) || !isset($val['index']))
{
break;
}
$photo_id = $val['id'];
$sqlSortPhotos = "UPDATE photos SET photo_ordernum = ? WHERE photo_id = ? and content_id = ?";
$stmt = $this->dbconnection->prepare($sqlSortPhotos);
$stmt->bindValue(1,$index);
$stmt->bindValue(2,$photo_id);
$stmt->bindValue(3,$this->content_id);
$argJson = '{"workOrderNum":"' .$ldelovninalogispir_sifra . '","monolit1":"'. $lkoda_monolita1 . '","monolit2":"'. $lkoda_monolita2 . '","monolit3":"'. $lkoda_monolita3 . '","monolit4":"'. $lkoda_monolita4 . '","kalup1":"'. $lkoda_ohisja1 . '","kalup2":"'. $lkoda_ohisja2 . '","kalup3":"'. $lkoda_ohisja1 . '","kalup4":"' . $lkoda_ohisja4 . '"}';
$argJsonArray = array("workOrderNum" => "$ldelovninalogispir_sifra",
"monolit1" => "$lkoda_monolita1",
"monolit2" => "$lkoda_monolita2",
"monolit3" => "$lkoda_monolita3",
"monolit4" => "$lkoda_monolita4",
"kalup1" => "$lkoda_ohisja1",
"kalup2" => "$lkoda_ohisja1",
"kalup3" => "$lkoda_ohisja1",
"kalup4" => "$lkoda_ohisja1");
$argJson = json_encode($argJsonArray);
try
{
$lok = true;
$stmt->execute();
}
catch(DBALException $e)
{
$lok = false;
file_put_contents("error.log",$e->getMessage().PHP_EOL,FILE_APPEND);
}
$index++;
}
return $this->dbconnection->lastInsertId();
}
}