<?php
namespace App\Controller;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Doctrine\DBAL\Connection;
use App\Service\contentservice;
use App\Service\generalservice;
use App\Service\menuservice;
use \FileUploader;
use Symfony\Component\Security\Core\Security;
class EditContentController extends AbstractController
{
/**
* @var contentservice
*/
/**
* @var generalservice
*/
/**
* @var menuservice
*/
private $apiContent;
private $apiGeneral;
private $apiMenu;
public $rootDir;
public $security;
public function __construct(contentservice $contentservice, generalservice $generalservice,menuservice $menuservice, KernelInterface $kernel, Security $security)
{
$this->apiContent = $contentservice;
$this->apiGeneral = $generalservice;
$this->apiMenu = $menuservice;
$this->rootDir = $kernel->getProjectDir();
$this->security = $security;
}
/**
* @Route("/editcontent/{id}", name="edit_content")
*/
public function index($id)
{
$lUser = $this->getUser();
$lUserID = $lUser->getId();
$lModul = explode(",",$lUser->getModul());
if (!in_array("Content", $lModul))
{
return $this->redirectToRoute('app_homepage');
}
$content = $this->apiContent->getContentByID($id);
$lLogsContent = $this->apiContent->getLogsContent($id);
$lResponseLogs = new JsonResponse(array('data' => $lLogsContent));
file_put_contents('api/dataLogsContent_'.$id.'.json', $lResponseLogs->getContent());
$menu = $this->apiMenu->generateMenu(0,$content[0]['language_id'], $id,$lUser->getCompany());
file_put_contents("MENU.log", print_r($menu, true));
return $this->render('edit_content/edit_content.html.twig', [
'controller_name' => 'EditContentController',
'content' => $content,
'id' => $id,
'menu' => $menu
]);
}
/**
* @Route("/content/refreshlogs", name="editcontent_refreshlogs")
*/
public function editContentRefreshLogs(Request $request)
{
$lcontent_id = $_POST['content_id'];
$lLogsContent = $this->apiContent->getLogsContent($lcontent_id);
$lResponseLogs = new JsonResponse(array('data' => $lLogsContent));
file_put_contents('api/dataLogsContent_'.$lcontent_id.'.json', $lResponseLogs->getContent());
return new JsonResponse($lcontent_id);
}
/**
* @Route("/content/savecontent", name="editcontent_savecontent")
*/
public function editContentSaveContent(Request $request)
{
file_put_contents("DREVO.log", "1" );
$lUser = $this->getUser();
$lUserID = $lUser->getId();
$lcontent_id = $_POST['content_id'];
$lcontent_title = $_POST['content_title'];
$lcontent_notes = $_POST['content_notes'];
$llanguage_id = $_POST['language_id'];
$lengine_id = $_POST['engine_id'];
$lcontent_image = $_POST['content_image'];
$llog_decription = $_POST['log_decription'];
$lTreemenu = $_POST['treemenu'];
$this->apiContent->content_id = $lcontent_id;
$this->apiContent->content_title = $lcontent_title;
$this->apiContent->content_notes = $lcontent_notes;
$this->apiContent->language_id = $llanguage_id;
$this->apiContent->engine_id = $lengine_id;
$this->apiContent->content_image = $lcontent_image;
$this->apiContent->treemenu = $lTreemenu;
$this->apiContent->saveContent();
$this->apiGeneral->ClearLog();
$this->apiGeneral->content_id = $lcontent_id;
$this->apiGeneral->action_id = 3;
$this->apiGeneral->log_description = $llog_decription;
$this->apiGeneral->user_id = $lUserID;
$this->apiGeneral->AddLog();
return new JsonResponse(array('data' => "ok"));
}
/**
* @Route("/content/showform", name="editcontent_showform")
*/
public function editContentShowForm(Request $request)
{
$lcontent_id = $_POST['content_id'];
$this->apiContent->content_id = $lcontent_id;
$lForm = $this->apiContent->showForm();
return new JsonResponse($lForm);
}
/**
* @Route("/content/getpreloadedphotos", name="editcontent_getpreloadedphotos")
*/
public function GetPreloadedPhotos(Request $request)
{
$lcontent_id = $_POST['content_id'];
$this->apiContent->content_id = $lcontent_id;
$lPhotos = $this->apiContent->getPreloadedPhotos($lcontent_id);
return new Response(json_encode($lPhotos));
}
/**
* @Route("/content/getpreloadeddocs", name="editcontent_getpreloadeddocs")
*/
public function GetPreloadedDocs(Request $request)
{
$lcontent_id = $_POST['content_id'];
$this->apiContent->content_id = $lcontent_id;
$lDocs = $this->apiContent->getPreloadedDocs($lcontent_id);
return new Response(json_encode($lDocs));
}
/**
* @Route("/content/uploadimage", name="editcontent_uploadimage")
*/
public function editContentUploadImage()
{
$folder = '';
//dobimo mapo v kateri folder naj naložimo sliko
$user = $this->security->getUser();
$folder = $user->getFolder();
if ($user->getFolder() == 'mki')
{
$folder = 'strah';
}
//test
$lcontent_id = $_POST['content_id'];
$FileUploader = new FileUploader('content_image', array(
'limit' => null,
'fileMaxSize' => null,
'extensions' => null,
'uploadDir' => $this->rootDir .'/public/uploads/' . $folder .'/',
'title' => 'name'
));
// call to upload the files
$data = $FileUploader->upload();
$lfilefrom = $data['files'][0]['file'];
$path_parts = pathinfo($lfilefrom);
$ldirname = $path_parts['dirname'];
$lbasename = $path_parts['basename'];
$lextension = $path_parts['extension'];
$lfilename = $path_parts['filename'];
$from = $this->rootDir .'/public/uploads/' . $folder .'/' . $lfilename . '.'. $lextension;
$to = $this->rootDir .'/public/uploads/' . $folder .'/' . '1000-' .$lfilename .'.'.$lextension;
$to800 = $this->rootDir .'/public/uploads/' . $folder .'/' . '800-' .$lfilename .'.'.$lextension;
copy($from, $to);
copy($from, $to800);
//copy($this->rootDir .'/public/uploads/' . $folder .'/','/home/telefonika/domains/telefonika.si/public_html/admin_backend/uploads/zff_images/' . $lfile300);
//copy($lfilefrom,'/home/telefonika/domains/telefonika.si/public_html/admin_backend/uploads/zff_images/' . $lfile350);
return new JsonResponse(array($data));
}
/**
* @Route("/content/getpreloadedimage", name="editcontent_getpreloadedimage")
*/
public function editContentGetPreloadedImage(Request $request)
{
$lcontent_id = $request->get('id');
$this->apiContent->content_id = $lcontent_id;
$lImage = $this->apiContent->getPreloadedImage();
return new JsonResponse($lImage);
}
/**
* @Route("/content/savefield", name="editcontent_savefield")
*/
public function editContentSaveField(Request $request)
{
$lUser = $this->getUser();
$lUserID = $lUser->getId();
$lcontent_id = $_POST['content_id'];
$lcontentform_id = $_POST['contentform_id'];
$lcontentform_value = $_POST['contentform_value'];
$loldvalue = $_POST['oldvalue'];
if ($loldvalue != "")
{
$loldvalue = '(stara vrednost: '.$loldvalue.')';
}
$lTreemenu = $_POST['treemenu'];
$this->apiContent->content_id = $lcontent_id;
$this->apiContent->contentform_id = $lcontentform_id;
$this->apiContent->contentform_value = $lcontentform_value;
$this->apiContent->treemenu = $lTreemenu;
$this->apiContent->saveField();
$this->apiGeneral->ClearLog();
$this->apiGeneral->content_id = $lcontent_id;
$this->apiGeneral->action_id = 4;
$this->apiGeneral->log_description = $lcontentform_value;
$this->apiGeneral->user_id = $lUserID;
$this->apiGeneral->AddLog();
return new JsonResponse(array('data' => "Borut"));
}
/**
* @Route("/content/publishcontent", name="editcontent_publishcontent")
*/
public function editContentPublishContent(Request $request)
{
$luser = $this->getUser()->getId();
$lcontent_id = $_POST['content_id'];
$this->apiContent->content_id = $lcontent_id;
$lok = $this->apiContent->publishContent();
return new JsonResponse($lok);
}
/**
* @Route("/content/savetinymce", name="editcontent_savetinymce")
*/
public function editContensavetinymce(Request $request)
{
$content_id = $_POST['content_id'];
$content_tinymce = $_POST['content_tinymce'];
$this->apiContent->content_id = $content_id;
$this->apiContent->content_tinymce = $content_tinymce;
$lok = $this->apiContent->savetinymce();
return new JsonResponse($lok);
}
/**
* @Route("/content/uploadphotos", name="editcontent_uploadphotos")
*/
public function uploadPhotos(Request $request)
{
$lcontent_id = $_POST['content_id'];
$folder = '';
//dobimo mapo v kateri folder naj naložimo sliko
$user = $this->security->getUser();
$folder = $user->getFolder();
if ($user->getFolder() == 'mki')
{
$folder = 'strah';
}
//$folder = $this->security->getUser()->getFolder();
// initialize FileUploader
$FileUploader = new FileUploader('datotekaMultipleInput', array(
'limit' => 1,
'fileMaxSize' => 20,
'extensions' => array('image/*', 'video/*', 'audio/*', 'pdf'),
'uploadDir' => $this->rootDir . '/public/uploads/' . $folder . '/',
'required' => true,
'title' => 'name',
'editor' => array(
'maxWidth' => 1980,
'maxHeight' => 1980,
'crop' => false,
'quality' => 90
)
));
$upload = $FileUploader->upload();
if (count($upload['files']) == 1) {
$item = $upload['files'][0];
$title = $item['name'];
$type = $item['type'];
$size = $item['size'];
$file = $this->rootDir . '/public/uploads/' . $folder . '/' . $item['name'];
$fileName = $item['name'];
$this->apiContent->content_id = $lcontent_id;
$this->apiContent->photo_name = $fileName;
$this->apiContent->photo_file = $fileName;
$this->apiContent->photo_type = $type;
$this->apiContent->photo_size = $size;
$lLastInsertID = $this->apiContent->writePhotoDB();
if ($lLastInsertID > 0) {
$upload['files'][0] = array(
'title' => $item['title'],
'name' => $item['name'],
'size' => $item['size'],
'size2' => $item['size2'],
'url' => $file,
'id' => $lLastInsertID
);
} else {
if (is_file($this->rootDir . '/public/uploads/' . $folder . '/' .$item['file']))
@unlink($this->rootDir . '/public/uploads/' . $folder . '/' .$item['file']);
unset($upload['files'][0]);
$upload['hasWarnings'] = true;
$upload['warnings'][] = 'Prišlo je do napake.';
}
}
echo json_encode($upload);
exit;
//----------------------------------
$this->apiContent->content_id = $lcontent_id;
$lPhotos = $this->apiContent->getPreloadedPhotos($lcontent_id);
return new Response(json_encode($lPhotos));
}
/**
* @Route("/content/sortphotos", name="editcontent_sortphotos")
*/
public function sortPhotos(Request $request)
{
$lcontent_id = $_POST['content_id'];
$this->apiContent->content_id = $lcontent_id;
$this->apiContent->list = $_POST['listsort'];
$this->apiContent->sortPhotos();
exit;
}
}