```
<?php
namespace ZeroPlugin\Controller\AdminV2;
use AppBundle\Common\ArrayToolkit;
use AppBundle\Controller\BaseController;
use Biz\Content\Service\FileService;
use Biz\System\Service\SettingService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class AdvertisementSettingController extends BaseController
{
public function setAdvertisementAction(Request $request)
{
$advertisementSetting = $this->setting('zeroAdvertisement', array());
if ($request->getMethod() == 'POST') {
$data = $request->request->all();
$data['startTime'] = strtotime($data['startTime']);
$data['endTime'] = strtotime($data['endTime']);
$this->getSettingService()->set('zeroAdvertisement', $data);
}
return $this->render('ZeroPlugin:ZeroAdminV2:advertisement-setting.html.twig', array(
'advertisementSetting' => $advertisementSetting,
));
}
public function showUploadModalAction()
{
return $this->render('ZeroPlugin:ZeroAdminV2:advertisement-upload.html.twig', array(
'pictureUrl' => '',
));
}
public function cropAction(Request $request)
{
if ($request->getMethod() == 'POST') {
$data = $request->request->all();
$files = $this->changePicture($data['images']);
foreach ($files as $key => $file) {
$files[$key]['file']['url'] = $this->get('web.twig.extension')->getFilePath($file['file']['uri']);
}
return new JsonResponse($files);
}
$fileId = $request->getSession()->get('fileId');
list($pictureUrl, $naturalSize, $scaledSize) = $this->getFileService()->getImgFileMetaInfo($fileId, 270, 270);
return $this->render('ZeroPlugin:ZeroAdminV2:advertisement-crop.html.twig', array(
'pictureUrl' => $pictureUrl,
'naturalSize' => $naturalSize,
'scaledSize' => $scaledSize,
));
}
protected function changePicture($data)
{
$fileIds = ArrayToolkit::column($data, 'id');
$files = $this->getFileService()->getFilesByIds($fileIds);
$data = ArrayToolkit::index($data, 'type');
$files = ArrayToolkit::index($files, 'id');
foreach ($data as $key => $value) {
if ($key == 'origin') {
$file = $this->getFileService()->getFileObject($value['id']);
$file = $this->getFileService()->uploadFile('default', $file);
$data[$key]['file'] = $file;
$this->getFileService()->deleteFileByUri($files[$value['id']]['uri']);
} else {
$data[$key]['file'] = $files[$value['id']];
}
}
return $data;
}
/**
* @return SettingService
*/
protected function getSettingService()
{
return $this->createService('System:SettingService');
}
/**
* @return FileService
*/
protected function getFileService()
{
return $this->createService('Content:FileService');
}
}
```