File size: 4.73Kb
<?php
namespace Brivium\CustomNodeIcon\Service\Node;
use XF\Entity\Node;
use XF\Http\Upload;
use XF\Util\File;
class Icon extends \XF\Service\AbstractService
{
protected $node;
protected $error;
protected $readFileName;
protected $unreadFileName;
protected $allowedTypes = [
0 => 1,
1 => 2,
2 => 3,
];
protected $outputFiles = [];
public function __construct(\XF\App $app, Node $node)
{
parent::__construct($app);
$this->setNode($node);
}
protected function setNode(Node $node)
{
if (!$node->node_id)
{
throw new \LogicException("Node must be saved");
}
$this->node = $node;
}
public function getError()
{
return $this->error;
}
public function setImage($fileName, $isUnread = false)
{
if (!$this->validateImage($fileName, $error))
{
$this->error = $error;
$fileName = null;
return false;
}
if(!$isUnread)
{
$this->readFileName = $fileName;
}else
{
$this->unreadFileName = $fileName;
}
return true;
}
public function setIcon(Upload $upload, $isUnread = false)
{
$upload->requireImage();
if (!$upload->isValid($errors))
{
$this->error = reset($errors);
return false;
}
return $this->setImage($upload->getTempFile(), $isUnread);
}
public function validateImage($fileName, &$error = NULL)
{
$error = null;
if (!file_exists($fileName))
{
throw new \InvalidArgumentException("Invalid file '$fileName' passed to icon service");
}
if (!is_readable($fileName))
{
throw new \InvalidArgumentException("'$fileName' passed to icon service is not readable");
}
$imageInfo = filesize($fileName) ? getimagesize($fileName) : false;
if (!$imageInfo)
{
$error = \XF::phrase('provided_file_is_not_valid_image');
return false;
}
$type = $imageInfo[2];
if (!in_array($type, $this->allowedTypes))
{
$error = \XF::phrase('provided_file_is_not_valid_image');
return false;
}
$width = $imageInfo[0];
$height = $imageInfo[1];
if (!$this->app->imageManager()->canResize($width, $height))
{
$error = \XF::phrase('uploaded_image_is_too_big');
return false;
}
return true;
}
public function updateIcon($fileName, $type = 'read')
{
$imageManager = $this->app->imageManager();
$imageInfo = filesize($fileName) ? getimagesize($fileName) : false;
$width = $imageInfo[0];
$height = $imageInfo[1];
$image = $imageManager->imageFromFile($fileName);
if (!$image)
{
return false;
}
$defaultSize = $this->app->options()->brcniDefaultSize;
if(empty($defaultSize['width']))
{
$defaultSize['width'] = $width;
}
if(empty($defaultSize['height']))
{
$defaultSize['height'] = $height;
}
$image->resizeAndCrop($defaultSize['width'], $defaultSize['height']);
$newTempFile = \XF\Util\File::getTempFile();
if ($newTempFile && $image->save($newTempFile))
{
$this->outputFiles[$type] = $newTempFile;
}
return true;
}
public function updateIcons(array &$inputIconData = [])
{
if (!$this->readFileName && !$this->unreadFileName)
{
throw new \LogicException("No source file for icon set");
}
if (!$this->node->exists())
{
throw new \LogicException("Node does not exist, cannot update icons");
}
$this->updateIcon($this->readFileName);
$this->updateIcon($this->unreadFileName, 'unread');
$outputFiles = $this->outputFiles;
foreach ($outputFiles AS $code => $file)
{
$dataFile = $this->node->getAbstractedCustomNodeIconPath($code);
File::copyFileToAbstractedPath($file, $dataFile);
$newData[$code] = [
'type' => 'image',
'date' => \XF::$time
];
}
$inputIconData = $newData + $inputIconData;
return true;
}
public function deleteIcons($type = 'all', $isDeleteNode = false)
{
$node = $this->node;
$iconData = $node->brcni_icon_data;
$deleteIcons = [];
if($type == 'all')
{
$deleteIcons = $iconData;
$iconData = [];
}elseif(!empty($iconData[$type]))
{
$deleteIcons[$type] = $iconData[$type];
unset($iconData[$type]);
}
if(!empty($deleteIcons))
{
foreach ($deleteIcons as $_type => $time)
{
$this->deleteType($_type);
}
}
if($isDeleteNode)
{
return true;
}
if(empty($iconData))
{
$node->set('brcni_icon_type', '');
}
$node->set('brcni_icon_data', $iconData);
$node->save();
return true;
}
}