Skip to content

Commit

Permalink
[FIX] Implementing sGallery::initialiseView() and mark sGallery::init…
Browse files Browse the repository at this point in the history
…ialise() as deprecated.
  • Loading branch information
Seiger committed Sep 3, 2024
1 parent f5486f5 commit 951bd7a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 127 deletions.
4 changes: 2 additions & 2 deletions plugins/sGalleryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
if (in_array($currentTemplate, $templateIDs) && $params['id'] > 0) {
if (is_array(is_array($templates[$currentTemplate])) && is_array($templates[$currentTemplate][$currentTemplate]) && count($templates[$currentTemplate][$currentTemplate]) > 0) {
foreach ($templates[$currentTemplate][$currentTemplate] as $block) {
echo sGallery::initialise('tab', 'resource', 'id', $block);
echo sGallery::initialiseView()->blockName($block);
}
} else {
echo sGallery::initialise();
echo sGallery::initialiseView();
}
}
});
84 changes: 31 additions & 53 deletions src/Builders/sGalleryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Seiger\sGallery\Builders;

use Seiger\sGallery\Controllers\sGalleryController;
use Seiger\sGallery\Models\sGalleryModel;
use Illuminate\Support\Facades\View;

Expand All @@ -10,11 +11,12 @@
*/
class sGalleryBuilder
{
protected string $viewType = 'default';
protected string $resourceType = 'resource';
protected string $viewType = sGalleryModel::VIEW_TAB;
protected string $itemType = 'resource';
protected string $idType = 'id';
protected string $blockName = '1';
protected int|null $documentId = null;
protected string|null $lang = null;
protected string|null $blockName = null;
protected array $resizeParams = [];

/**
Expand All @@ -25,31 +27,37 @@ class sGalleryBuilder
*/
public function viewType(string $viewType): self
{
$this->viewType = $viewType;
if (in_array($viewType, [
sGalleryModel::VIEW_TAB,
sGalleryModel::VIEW_SECTION,
sGalleryModel::VIEW_SECTION_DOWNLOADS
])) {
$this->viewType = $viewType;
}
return $this;
}

/**
* Set the resource type.
*
* @param string $resourceType
* @param string $itemType
* @return self
*/
public function resourceType(string $resourceType): self
public function itemType(string $itemType): self
{
$this->resourceType = $resourceType;
$this->itemType = $itemType;
return $this;
}

/**
* Set the document ID.
* Set the document ID type.
*
* @param int $documentId
* @param string $idType
* @return self
*/
public function idType(int $documentId): self
public function idType(string $idType): self
{
$this->documentId = $documentId;
$this->idType = $idType;
return $this;
}

Expand Down Expand Up @@ -92,56 +100,26 @@ public function resizeParams(array $params): self
/**
* Initialize and retrieve the gallery view.
*
* @return object|View
* @return self
*/
public function initialise()
public function initialise(): self
{
// Determine document ID if not set
if (is_null($this->documentId)) {
$this->documentId = evo()->documentObject['id'] ?? 0;
}

// Determine language if not set
if (is_null($this->lang)) {
$this->lang = evo()->getConfig('lang', 'base');
}

// Build query to the sGalleryModel
$query = sGalleryModel::lang($this->lang)
->whereParent($this->documentId)
->whereResourceType($this->resourceType);

if (!is_null($this->blockName) && trim($this->blockName) !== '') {
$query->whereBlock($this->blockName);
}

// Get the result
$galleries = $query->orderBy('position')->get();

// You may want to return a View or another object
// For example:
// return View::make('sgallery::view.' . $this->viewType, ['galleries' => $galleries]);

// For example purposes, return the collection
return $galleries;
return $this;
}

/**
* Perform image resizing based on the set parameters.
* Render the view as a string when the object is treated like a string.
*
* @param string $input
* @return string
*/
public function resize(string $input): string
public function __toString(): string
{
// Use methods from the sGallery class or other logic to resize
// For example, using the resizeImage method from sGallery

// If you need to use an instance of sGallery, you may need to inject it or use a static method
// For simplicity, let's assume we're calling a static method

return \Seiger\sGallery\sGallery::initialise()->resizeImage($input, $this->resizeParams);
try {
$sGalleryController = new sGalleryController($this->viewType, $this->itemType, $this->idType, $this->blockName);
return $sGalleryController->index(); // Assuming this returns a View object
} catch (\Exception $e) {
// Handle any exceptions and return an error message as a string
return "Error initializing gallery: " . $e->getMessage();
}
}

// Additional methods can be added as needed
}
2 changes: 1 addition & 1 deletion src/Controllers/sGalleryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function addYoutube(Request $request)
* @param Request $request The current HTTP request
* @return void
*/
public function resortGallery(Request $request)
public function resortGallery(Request $request): void
{
$validator = Validator::make($request->all(), [
'cat' => 'required|integer|min:1',
Expand Down
152 changes: 81 additions & 71 deletions src/sGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ public function __construct()
$this->builder = new sGalleryBuilder();
}

/**
* Initialize the view builder.
*
* @return sGalleryBuilder
*/
public function initialiseView()
{
return $this->builder->initialise();
}

/**
* Resize an image using the new Builder pattern.
*
* @param string $input The path of the input image file.
* @param array $params An array of parameters for resizing the image.
* @return string The URL of the resized image.
*/
public function resizeImage(string $input, array $params = []): string
{
return $this->builder->resize($input);
}

/**
* Initialize the Gallery with the specified parameters.
*
Expand All @@ -35,10 +57,20 @@ public function __construct()
* @return View|string The initialized view or error string
* @deprecated Use the new Builder pattern with initialiseView() instead.
*/
public function initialise(string $viewType = 'tab', string $resourceType = 'resource', string $idType = 'id', string $blockName = '1')
public function initialise(string $viewType = '', string $resourceType = 'resource', string $idType = 'id', string $blockName = '1')
{
trigger_error('Method initialise() is deprecated. Use the new Builder pattern with initialiseView() instead.', E_USER_DEPRECATED);

$viewTypeDef = sGalleryModel::VIEW_TAB;
if (in_array($viewType, [
sGalleryModel::VIEW_TAB,
sGalleryModel::VIEW_SECTION,
sGalleryModel::VIEW_SECTION_DOWNLOADS
])) {
$viewTypeDef = $viewType;
}
$viewType = $viewTypeDef;

try {
$sGalleryController = new sGalleryController($viewType, $resourceType, $idType, $blockName);
return $sGalleryController->index(); // Assuming this returns a View object
Expand All @@ -48,64 +80,6 @@ public function initialise(string $viewType = 'tab', string $resourceType = 'res
}
}

/**
* Set the view type.
*
* @param string $viewType
* @return self
*/
public function viewType(string $viewType): self
{
$this->builder->viewType($viewType);
return $this;
}

/**
* Set the resource type.
*
* @param string $resourceType
* @return self
*/
public function resourceType(string $resourceType): self
{
$this->builder->resourceType($resourceType);
return $this;
}

/**
* Set the ID type.
*
* @param string $idType
* @return self
*/
public function idType(string $idType): self
{
$this->builder->idType($idType);
return $this;
}

/**
* Set the block name.
*
* @param string $blockName
* @return self
*/
public function blockName(string $blockName): self
{
$this->builder->blockName($blockName);
return $this;
}

/**
* Initialize the view with the current settings.
*
* @return View
*/
public function initialiseView(): View
{
return $this->builder->initialise();
}

/**
* Retrieves all galleries of a given type and language for a given document using the new Builder pattern.
*
Expand Down Expand Up @@ -155,18 +129,6 @@ public function firstGallery(string $resourceType = 'resource', int $documentId
->initialiseView();
}

/**
* Resize an image using the new Builder pattern.
*
* @param string $input The path of the input image file.
* @param array $params An array of parameters for resizing the image.
* @return string The URL of the resized image.
*/
public function resizeImage(string $input, array $params = []): string
{
return $this->builder->resize($input);
}

/**
* Crop an image using the new Builder pattern.
*
Expand Down Expand Up @@ -447,6 +409,54 @@ public function langTabs(): array
return $tabs;
}

/**
* Set the view type.
*
* @param string $viewType
* @return self
*/
public function viewType(string $viewType): self
{
$this->builder->viewType($viewType);
return $this;
}

/**
* Set the resource type.
*
* @param string $itemType
* @return self
*/
public function itemType(string $itemType): self
{
$this->builder->itemType($itemType);
return $this;
}

/**
* Set the ID type.
*
* @param string $idType
* @return self
*/
public function idType(string $idType): self
{
$this->builder->idType($idType);
return $this;
}

/**
* Set the block name.
*
* @param string $blockName
* @return self
*/
public function blockName(string $blockName): self
{
$this->builder->blockName($blockName);
return $this;
}

/**
* Gets the default image width.
*
Expand Down

0 comments on commit 951bd7a

Please sign in to comment.