Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional to use gallery for new model #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 136 additions & 21 deletions GalleryBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Yii;
use yii\base\Behavior;
use yii\base\Exception;
use yii\db\ActiveRecord;
Expand All @@ -17,6 +18,7 @@
* @author Bogdan Savluk <[email protected]>
*
* @property string $galleryId
* @property string $temporaryId
*/
class GalleryBehavior extends Behavior
{
Expand All @@ -25,6 +27,11 @@ class GalleryBehavior extends Behavior
* @var string
*/
public $pkGlue = '_';
/**
* The prefix of string for temporary id for new models
* @var string
*/
public $temporaryPrefix = 'temp';
/**
* @var string Type name assigned to model in image attachment action
* @see GalleryManagerAction::$types
Expand Down Expand Up @@ -133,6 +140,8 @@ public function events()
ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
ActiveRecord::EVENT_BEFORE_INSERT => 'afterFind',
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
];
}

Expand All @@ -142,8 +151,7 @@ public function beforeDelete()
foreach ($images as $image) {
$this->deleteImage($image->id);
}
$dirPath = $this->directory . '/' . $this->getGalleryId();
@rmdir($dirPath);
$this->removeDirectory($this->getDirectoryPath());
}

public function afterFind()
Expand All @@ -154,17 +162,45 @@ public function afterFind()
public function afterUpdate()
{
$galleryId = $this->getGalleryId();
if ($this->_galleryId != $galleryId) {
if ($this->_galleryId && ($this->_galleryId != $galleryId)) {
$dirPath1 = $this->directory . '/' . $this->_galleryId;
$dirPath2 = $this->directory . '/' . $galleryId;
rename($dirPath1, $dirPath2);
if (is_dir($dirPath1)) {
rename($dirPath1, $dirPath2);
}
}
}

/**
* Move dir and change id to actual
* @throws Exception
* @throws \yii\db\Exception
*/
public function afterInsert()
{
$galleryId = $this->getGalleryId();
if ($this->_galleryId && ($this->_galleryId != $galleryId)) {

\Yii::$app->db->createCommand()
->update(
$this->tableName,
['ownerId' => $galleryId],
['ownerId' => $this->_galleryId, 'type' => $this->type]
)->execute();

$dirPath1 = $this->directory . '/' . $this->_galleryId;
$dirPath2 = $this->directory . '/' . $galleryId;
if (is_dir($dirPath1)) {
rename($dirPath1, $dirPath2);
}
}
}

protected $_images = null;

/**
* @return GalleryImage[]
* @throws Exception
*/
public function getImages()
{
Expand Down Expand Up @@ -223,6 +259,39 @@ public function getFilePath($imageId, $version = 'original')
return $this->directory . '/' . $this->getFileName($imageId, $version);
}

public function getDirectoryPath()
{
return $this->directory . '/' . $this->getGalleryId();
}

/**
* Generate a temporary id for new models
* @return string
*/
public function getTemporaryId()
{
return $this->temporaryPrefix . Yii::$app->session->getId();
}

/**
* Get Gallery Id
*
* @return mixed as string or integer
* @throws Exception
*/
public function getGalleryId()
{
$pk = $this->owner->getPrimaryKey();
if ($pk === null) {
$pk = $this->temporaryId;
}
if (is_array($pk)) {
return implode($this->pkGlue, $pk);
} else {
return $pk;
}
}

/**
* Replace existing image by specified file
*
Expand Down Expand Up @@ -252,31 +321,48 @@ public function replaceImage($imageId, $path)
}
}

/**
* Remove single image file
* @param $fileName
* @return bool
*/
private function removeFile($fileName)
{
return FileHelper::unlink($fileName);
try {
return FileHelper::unlink($fileName);
} catch (\yii\base\ErrorException $exception) {
return false;
}
}

/**
* Get Gallery Id
*
* @return mixed as string or integer
* @throws Exception
* Remove a folders for gallery files
* @param $filePath string the filename of image
* @return bool
*/
public function getGalleryId()
private function removeDirectory($filePath)
{
$pk = $this->owner->getPrimaryKey();
if (is_array($pk)) {
return implode($this->pkGlue, $pk);
} else {
return $pk;
try {
FileHelper::removeDirectory(dirname($filePath));
} catch (\yii\base\ErrorException $exception) {
return false;
}
}

return true;
}

/**
* Create a folders for gallery files
* @param $filePath string the filename of image
* @return bool
*/
private function createFolders($filePath)
{
return FileHelper::createDirectory(FileHelper::normalizePath(dirname($filePath)), 0777);
try {
return FileHelper::createDirectory(FileHelper::normalizePath(dirname($filePath)), 0777);
} catch (\yii\base\Exception $exception) {
return false;
}
}

/////////////////////////////// ========== Public Actions ============ ///////////////////////////
Expand All @@ -287,10 +373,7 @@ public function deleteImage($imageId)
$this->removeFile($filePath);
}
$filePath = $this->getFilePath($imageId, 'original');
$parts = explode('/', $filePath);
$parts = array_slice($parts, 0, count($parts) - 1);
$dirPath = implode('/', $parts);
@rmdir($dirPath);
$this->removeDirectory($filePath);

$db = \Yii::$app->db;
$db->createCommand()
Expand All @@ -314,6 +397,35 @@ function ($image) use (&$removed) {
}
);
}
if (is_null($this->_images)) {
$this->removeDirectory($this->getDirectoryPath());
}
}

/**
* Remove images for expired session
* actions is a {'apiRoute'}?action=deleteOrphan&type={'type'}&behaviorName={'behaviorName'}
* @throws \yii\base\ErrorException
* @throws \yii\db\Exception
*/
public function deleteOrphanImages()
{
$toDelete = \Yii::$app->db->createCommand(
'SELECT DISTINCT `ownerId` FROM ' . $this->tableName . ' WHERE `ownerId` LIKE :ownerId AND `type` = :type',
[':ownerId' => $this->temporaryPrefix . '%', ':type' => $this->type]
)->queryColumn();

foreach ($toDelete as $item) {
\Yii::$app->db->createCommand()
->delete(
$this->tableName,
[
'type' => $this->type,
'ownerId' => $item,
]
)->execute();
FileHelper::removeDirectory($this->directory . '/' . $item);
}
}

public function addImage($fileName)
Expand Down Expand Up @@ -383,6 +495,8 @@ public function arrange($order)
* @param array $imagesData
*
* @return GalleryImage[]
* @throws Exception
* @throws \yii\db\Exception
*/
public function updateImagesData($imagesData)
{
Expand Down Expand Up @@ -432,6 +546,7 @@ public function updateImagesData($imagesData)
* Should be called in migration on every model after changes in versions configuration
*
* @param string|null $oldExtension
* @throws Exception
*/
public function updateImages($oldExtension = null)
{
Expand Down
Loading