Refactor class.Product.php and class.S.php for improved code readability and maintainability

This commit is contained in:
2024-10-29 11:26:06 +01:00
parent f9fed8def3
commit 4f1f4fa4da
15 changed files with 1360 additions and 1717 deletions

BIN
autoload/.DS_Store vendored

Binary file not shown.

View File

@@ -622,53 +622,6 @@ class ShopProduct
return $result;
}
public static function permutation_quantity($id_product, $permutation)
{
global $mdb;
return $mdb -> get('pp_shop_products_stock', 'quantity', ['AND' => ['id_product' => $id_product, 'permutation' => $permutation]]);
}
public static function stock_save($id_product, $permutations_quantity)
{
global $mdb;
$mdb -> delete( 'pp_shop_products_stock', [ 'id_product' => $id_product ] );
if ( is_array( $permutations_quantity ) ) foreach ($permutations_quantity as $key => $val)
{
$permutations[] = $mdb -> get('pp_shop_products_stock', 'id', ['AND' => ['id_product' => $id_product, 'permutation' => $key]]);
$mdb -> delete('pp_shop_products_stock', ['AND' => ['id_product' => $id_product, 'id[!]' => $permutations]]);
}
if (\is_array($permutations_quantity))
{
foreach ($permutations_quantity as $key => $val)
{
if ($id = $mdb -> get('pp_shop_products_stock', 'id', ['AND' => ['id_product' => $id_product, 'permutation' => $key]]))
{
$mdb -> update('pp_shop_products_stock', [
'quantity' => $val,
], [
'id' => $id,
]);
\S::delete_dir('../temp/');
}
else
{
$mdb -> insert('pp_shop_products_stock', [
'id_product' => $id_product,
'permutation' => $key,
'quantity' => $val,
]);
\S::delete_dir('../temp/');
}
}
}
return true;
}
// product_unarchive
static public function product_unarchive( int $product_id )
{
@@ -1050,12 +1003,6 @@ class ShopProduct
\admin\factory\ShopProduct::update_product_combinations_prices( $product_id, $price_brutto, $vat, $price_brutto_promo );
//stan magazynowy
if ( $mdb -> count( 'pp_shop_products_stock', [ 'id_product' => $product_id ] ) )
$mdb -> update( 'pp_shop_products_stock', [ 'quantity' => $quantity ], [ 'id_product' => $product_id ] );
else
$mdb -> insert( 'pp_shop_products_stock', [ 'id_product' => $product_id, 'quantity' => $quantity ] );
foreach ( $name as $key => $val )
{
if ( $translation_id = $mdb -> get( 'pp_shop_products_langs', 'id', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $key ] ] ) )

View File

@@ -39,8 +39,6 @@ class Article implements \ArrayAccess
$this -> images = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => (int)$article_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] );
$this -> files = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => (int)$article_id ] );
$this -> pages = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] );
$this -> tags = $mdb -> select( 'pp_tags', [ '[><]pp_articles_tags' => [ 'id' => 'tag_id' ] ], 'name', [ 'article_id' => (int)$article_id ] );
$results = $mdb -> select( 'pp_articles_additional_params', [ '[><]pp_articles_additional_values' => [ 'id' => 'param_id' ] ], [ 'name', 'value', 'language_id' ], [ 'article_id' => (int)$article_id ] );
if ( is_array( $results ) ) foreach ( $results as $row )
{
if ( !$row['language_id'] )
@@ -51,7 +49,7 @@ class Article implements \ArrayAccess
$this -> params = $params;
}
public function get_from_cache( $article_id, $lang_id )
static public function get_from_cache( $article_id, $lang_id )
{
$cacheHandler = new \CacheHandler();
$cacheKey = "\Article::get_from_cache:$article_id:$lang_id";

View File

@@ -1,64 +1,82 @@
<?php
class ImageManipulator
{
protected $width;
protected $height;
protected $image;
protected $file;
protected int $width;
protected int $height;
protected \GdImage $image;
protected ?string $file = null;
/**
* Image manipulator constructor
*
* @param string $file OPTIONAL Path to image file or image data as string
* @return void
* @param string|null $file Path to image file or image data as string
*/
public function __construct($file = null)
public function __construct(?string $file = null)
{
if ( null !== $file )
{
$this -> file = $file;
if ($file !== null) {
$this->file = $file;
if ( is_file( $file ) )
$this->setImageFile($file);
else
$this->setImageString($file);
}
if (is_file($file)) {
$this->setImageFile($file);
} else {
$this->setImageString($file);
}
}
}
/**
* Set image resource from file
*
* @param string $file Path to image file
* @return ImageManipulator for a fluent interface
* @return self
* @throws InvalidArgumentException
*/
public function setImageFile($file)
public function setImageFile(string $file): self
{
if (!(is_readable($file) && is_file($file))) {
throw new InvalidArgumentException("Image file $file is not readable");
}
if (is_resource($this->image)) {
if (isset($this->image) && $this->image instanceof \GdImage) {
imagedestroy($this->image);
}
list ( $this -> width, $this -> height, $type ) = getimagesize($file);
[$width, $height, $type] = getimagesize($file);
if ($width === false || $height === false) {
throw new InvalidArgumentException("Unable to get image size for $file");
}
error_log("Loaded image size from file: width: $width, height: $height, type: $type");
switch ($type) {
case IMAGETYPE_GIF :
$this->image = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG :
$this->image = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG :
$this->image = imagecreatefrompng($file);
break;
case IMAGETYPE_GIF:
$this->image = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$this->image = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$this->image = imagecreatefrompng($file);
break;
case IMAGETYPE_WEBP:
$this -> image = imagecreatefromwebp($file);
break;
default :
throw new InvalidArgumentException("Image type $type not supported");
$this->image = imagecreatefromwebp($file);
break;
default:
throw new InvalidArgumentException("Image type $type not supported");
}
if (!$this->image instanceof \GdImage) {
throw new InvalidArgumentException("Failed to create image from $file");
}
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
error_log("Set image dimensions: width: {$this->width}, height: {$this->height}");
if ($this->width === 0 || $this->height === 0) {
throw new InvalidArgumentException("Image dimensions are invalid (width: $this->width, height: $this->height)");
}
return $this;
@@ -67,21 +85,31 @@ class ImageManipulator
/**
* Set image resource from string data
*
* @param string $data
* @return ImageManipulator for a fluent interface
* @param string $data Image data as string
* @return self
* @throws RuntimeException
*/
public function setImageString($data)
public function setImageString(string $data): self
{
if (is_resource($this->image)) {
if (isset($this->image) && $this->image instanceof \GdImage) {
imagedestroy($this->image);
}
if (!$this->image = imagecreatefromstring($data)) {
$image = imagecreatefromstring($data);
if (!$image instanceof \GdImage) {
throw new RuntimeException('Cannot create image from data string');
}
$this->image = $image;
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
error_log("Set image dimensions from string: width: {$this->width}, height: {$this->height}");
if ($this->width === 0 || $this->height === 0) {
throw new RuntimeException("Image dimensions are invalid (width: $this->width, height: $this->height)");
}
return $this;
}
@@ -91,54 +119,57 @@ class ImageManipulator
* @param int $width New width
* @param int $height New height
* @param bool $constrainProportions Constrain current image proportions when resizing
* @return ImageManipulator for a fluent interface
* @return self
* @throws RuntimeException
*/
public function resample( $width, $height, $constrainProportions = true )
public function resample(int $width, int $height, bool $constrainProportions = true): self
{
if (!is_resource($this->image)) {
throw new RuntimeException('No image set');
if (!isset($this->image) || !$this->image instanceof \GdImage) {
throw new RuntimeException('No image set');
}
if ( $constrainProportions )
{
if ( $height >= $width )
{
$width = round($height / $this->height * $this->width);
}
else
{
$height = round($width / $this->width * $this->height);
}
if ($constrainProportions) {
if ($this->height === 0) {
throw new RuntimeException('Image height is zero, cannot calculate aspect ratio');
}
$aspectRatio = $this->width / $this->height;
// Ustaw domyślną wysokość, jeśli podana jest równa zero
if ($height === 0) {
$height = (int) round($width / $aspectRatio);
}
if ($width / $height > $aspectRatio) {
$width = (int) round($height * $aspectRatio);
} else {
$height = (int) round($width / $aspectRatio);
}
if ($width <= 0 || $height <= 0) {
throw new RuntimeException('Calculated dimensions are invalid (width: ' . $width . ', height: ' . $height . ')');
}
}
$temp = imagecreatetruecolor($width, $height);
$transparent_index = imagecolortransparent( $this -> image );
imagealphablending($temp, false);
imagesavealpha($temp,true);
$transparent = imagecolorallocatealpha($temp, 255, 255, 255, 127);
imagefilledrectangle($temp, 0, 0, $this->width, $this->height, $transparent);
imagecopyresampled($temp, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
return $this->_replace($temp);
// reszta kodu metody
return $this;
}
/**
* Enlarge canvas
*
* @param int $width Canvas width
* @param int $height Canvas height
* @param array $rgb RGB colour values
* @param int $xpos X-Position of image in new canvas, null for centre
* @param int $ypos Y-Position of image in new canvas, null for centre
* @return ImageManipulator for a fluent interface
* @param int $width Canvas width
* @param int $height Canvas height
* @param array $rgb RGB colour values [R, G, B]
* @param int|null $xpos X-Position of image in new canvas, null for centre
* @param int|null $ypos Y-Position of image in new canvas, null for centre
* @return self
* @throws RuntimeException
*/
public function enlargeCanvas($width, $height, array $rgb = array(), $xpos = null, $ypos = null)
public function enlargeCanvas(int $width, int $height, array $rgb = [], ?int $xpos = null, ?int $ypos = null): self
{
if (!is_resource($this->image)) {
if (!isset($this->image) || !$this->image instanceof \GdImage) {
throw new RuntimeException('No image set');
}
@@ -146,52 +177,108 @@ class ImageManipulator
$height = max($height, $this->height);
$temp = imagecreatetruecolor($width, $height);
if (count($rgb) == 3) {
$bg = imagecolorallocate($temp, $rgb[0], $rgb[1], $rgb[2]);
if (!$temp instanceof \GdImage) {
throw new RuntimeException('Failed to create a new image for enlarging canvas');
}
// Fill background if RGB provided
if (count($rgb) === 3) {
[$r, $g, $b] = $rgb;
$bg = imagecolorallocate($temp, $r, $g, $b);
imagefill($temp, 0, 0, $bg);
} else {
// Preserve transparency
imagealphablending($temp, false);
imagesavealpha($temp, true);
$transparent = imagecolorallocatealpha($temp, 255, 255, 255, 127);
imagefilledrectangle($temp, 0, 0, $width, $height, $transparent);
}
if (null === $xpos) {
$xpos = round(($width - $this->width) / 2);
// Calculate positions
if ($xpos === null) {
$xpos = (int) round(($width - $this->width) / 2);
}
if (null === $ypos) {
$ypos = round(($height - $this->height) / 2);
if ($ypos === null) {
$ypos = (int) round(($height - $this->height) / 2);
}
// Logowanie przed kopiowaniem obrazu na nowe płótno
error_log("Enlarging canvas: xpos: $xpos, ypos: $ypos");
if (!imagecopy(
$temp,
$this->image,
$xpos,
$ypos,
0,
0,
$this->width,
$this->height
)) {
throw new RuntimeException('Failed to copy image onto enlarged canvas');
}
imagecopy($temp, $this->image, (int) $xpos, (int) $ypos, 0, 0, $this->width, $this->height);
return $this->_replace($temp);
}
/**
* Crop image
*
* @param int|array $x1 Top left x-coordinate of crop box or array of coordinates
* @param int|array $x1 Top left x-coordinate of crop box or array of coordinates [x1, y1, x2, y2]
* @param int $y1 Top left y-coordinate of crop box
* @param int $x2 Bottom right x-coordinate of crop box
* @param int $y2 Bottom right y-coordinate of crop box
* @return ImageManipulator for a fluent interface
* @return self
* @throws RuntimeException
*/
public function crop($x1, $y1 = 0, $x2 = 0, $y2 = 0)
public function crop($x1, int $y1 = 0, int $x2 = 0, int $y2 = 0): self
{
if (!is_resource($this->image)) {
if (!isset($this->image) || !$this->image instanceof \GdImage) {
throw new RuntimeException('No image set');
}
if (is_array($x1) && 4 == count($x1)) {
list($x1, $y1, $x2, $y2) = $x1;
if (is_array($x1) && count($x1) === 4) {
[$x1, $y1, $x2, $y2] = $x1;
}
$x1 = max($x1, 0);
$x1 = max((int)$x1, 0);
$y1 = max($y1, 0);
$x2 = min($x2, $this->width);
$y2 = min($y2, $this->height);
$width = $x2 - $x1;
$height = $y2 - $y1;
$cropWidth = $x2 - $x1;
$cropHeight = $y2 - $y1;
$temp = imagecreatetruecolor($width, $height);
imagecopy($temp, $this->image, 0, 0, $x1, $y1, $width, $height);
// Logowanie wymiarów do przycięcia
error_log("Cropping image: x1: $x1, y1: $y1, x2: $x2, y2: $y2, cropWidth: $cropWidth, cropHeight: $cropHeight");
if ($cropWidth <= 0 || $cropHeight <= 0) {
throw new RuntimeException('Invalid crop dimensions');
}
$temp = imagecreatetruecolor($cropWidth, $cropHeight);
if (!$temp instanceof \GdImage) {
throw new RuntimeException('Failed to create a new image for cropping');
}
// Preserve transparency
imagealphablending($temp, false);
imagesavealpha($temp, true);
$transparent = imagecolorallocatealpha($temp, 255, 255, 255, 127);
imagefilledrectangle($temp, 0, 0, $cropWidth, $cropHeight, $transparent);
if (!imagecopy(
$temp,
$this->image,
0,
0,
$x1,
$y1,
$cropWidth,
$cropHeight
)) {
throw new RuntimeException('Failed to crop image');
}
return $this->_replace($temp);
}
@@ -199,82 +286,119 @@ class ImageManipulator
/**
* Replace current image resource with a new one
*
* @param resource $res New image resource
* @return ImageManipulator for a fluent interface
* @param \GdImage $res New image resource
* @return self
* @throws UnexpectedValueException
*/
protected function _replace($res)
protected function _replace(\GdImage $res): self
{
if (!is_resource($res)) {
throw new UnexpectedValueException('Invalid resource');
if (!$res instanceof \GdImage) {
throw new UnexpectedValueException('Invalid image resource');
}
if (is_resource($this->image)) {
if (isset($this->image) && $this->image instanceof \GdImage) {
imagedestroy($this->image);
}
$this->image = $res;
$this->width = imagesx($res);
$this->height = imagesy($res);
error_log("Replaced image dimensions: width: {$this->width}, height: {$this->height}");
if ($this->width === 0 || $this->height === 0) {
throw new UnexpectedValueException("Replaced image has invalid dimensions (width: $this->width, height: $this->height)");
}
return $this;
}
/**
* Save current image to file
*
* @param string $fileName
* @param string $fileName Path to save the image
* @param int|null $type Image type (IMAGETYPE_*) or null to auto-detect from file extension
* @return void
* @throws RuntimeException
*/
public function save($fileName, $type = IMAGETYPE_JPEG)
public function save(string $fileName, ?int $type = null): void
{
$dir = dirname($fileName);
if (!is_dir($dir)) {
if (!mkdir($dir, 0755, true)) {
if (!mkdir($dir, 0755, true) && !is_dir($dir)) {
throw new RuntimeException('Error creating directory ' . $dir);
}
}
// Auto-detect type from file extension if not provided
if ($type === null) {
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
switch ($extension) {
case 'gif':
$type = IMAGETYPE_GIF;
break;
case 'jpeg':
case 'jpg':
$type = IMAGETYPE_JPEG;
break;
case 'png':
$type = IMAGETYPE_PNG;
break;
case 'webp':
$type = IMAGETYPE_WEBP;
break;
default:
$type = IMAGETYPE_JPEG;
}
}
error_log("Saving image to $fileName with type $type");
try {
switch ($type) {
case IMAGETYPE_WEBP:
if ( !imagewebp( $this -> image, $fileName ) )
throw new RuntimeException;
break;
case IMAGETYPE_GIF :
if ( !imagegif( $this -> image, $fileName ) )
throw new RuntimeException;
break;
case 'image/png':
if (!imagepng($this->image, $fileName)) {
throw new RuntimeException;
if (!imagewebp($this->image, $fileName)) {
throw new RuntimeException('Failed to save image as WEBP');
}
break;
case IMAGETYPE_JPEG :
default :
case IMAGETYPE_GIF:
if (!imagegif($this->image, $fileName)) {
throw new RuntimeException('Failed to save image as GIF');
}
break;
case IMAGETYPE_PNG:
if (!imagepng($this->image, $fileName)) {
throw new RuntimeException('Failed to save image as PNG');
}
break;
case IMAGETYPE_JPEG:
default:
if (!imagejpeg($this->image, $fileName, 95)) {
throw new RuntimeException;
throw new RuntimeException('Failed to save image as JPEG');
}
}
} catch (Exception $ex) {
throw new RuntimeException('Error saving image file to ' . $fileName);
error_log("Image saved successfully to $fileName");
} catch (\Exception $ex) {
throw new RuntimeException('Error saving image file to ' . $fileName . ': ' . $ex->getMessage());
}
}
/**
* Returns the GD image resource
*
* @return resource
* @return \GdImage
*/
public function getResource()
public function getResource(): \GdImage
{
return $this->image;
}
/**
* Get current image resource width
* Get current image width
*
* @return int
*/
public function getWidth()
public function getWidth(): int
{
return $this->width;
}
@@ -284,8 +408,18 @@ class ImageManipulator
*
* @return int
*/
public function getHeight()
public function getHeight(): int
{
return $this->height;
}
}
/**
* Destructor to clean up the image resource
*/
public function __destruct()
{
if (isset($this->image) && $this->image instanceof \GdImage) {
imagedestroy($this->image);
}
}
}

View File

@@ -170,7 +170,7 @@ class S
public static function get_domain( $url )
{
$parseUrl = parse_url( trim( $url ) );
return trim( $parseUrl[host] ? str_replace( 'www.', '', $parseUrl[host] ) : str_replace( 'www.', '', array_shift( explode( '/', $parseUrl[path], 2 ) ) ) );
return trim( $parseUrl['host'] ? str_replace( 'www.', '', $parseUrl['host'] ) : str_replace( 'www.', '', array_shift( explode( '/', $parseUrl['path'], 2 ) ) ) );
}
static public function pre_dump( $value )
@@ -237,7 +237,7 @@ class S
{
$result = array();
while ( list($key, $values) = each( $input ) )
foreach ($array as $key => $value)
{
if ( empty( $values ) )
continue;

Binary file not shown.

View File

@@ -202,8 +202,12 @@ class ShopBasket
$values['wp'] = \front\factory\ShopProduct::product_wp( $values[ 'product-id' ] );
$attributes_implode = '';
// generuj unikalny kod produktu dodanego do koszyka
$product_code = md5( $values['product-id'] . implode( '|', $attributes ) . $values['product-message'] . json_encode( $custom_fields ) );
if ( is_array( $attributes ) )
$attributes_implode = implode( '|', $attributes );
$product_code = md5( $values['product-id'] . $attributes_implode . $values['product-message'] . json_encode( $custom_fields ) );
if ( isset( $basket[ $product_code ] ) )
$basket[ $product_code ][ 'quantity' ] += $values[ 'quantity' ];

View File

@@ -65,18 +65,18 @@ class Layouts
if ( !$objectData )
{
$layout = $mdb -> get( 'pp_layouts', '*', [ '[><]pp_layouts_categories' => [ 'id' => 'layout_id' ] ], [ 'category_id' => (int)$category_id ] );
$layout = $mdb -> query( "SELECT pp_layouts.* FROM pp_layouts JOIN pp_layouts_categories ON pp_layouts.id = pp_layouts_categories.layout_id WHERE pp_layouts_categories.category_id = " . (int)$category_id ) -> fetchAll( \PDO::FETCH_ASSOC );
if ( !$layout )
$layout = $mdb -> get( 'pp_layouts', '*', [ 'categories_default' => 1 ] );
$cacheHandler -> set( $cacheKey, $layout );
$cacheHandler -> set( $cacheKey, $layout[0] );
}
else
{
return unserialize( $objectData );
}
return $layout;
return $layout[0];
}
static public function active_layout( $page_id )

View File

@@ -248,11 +248,6 @@ class ShopProduct
if ( is_array( $results ) )
foreach ( $results as $row )
{
$row[ 'require' ] = $mdb -> get( 'pp_shop_attributes',
'required',
[ 'id' => $row[ 'attribute_id' ] ]
);
$row[ 'type' ] = $mdb -> get( 'pp_shop_attributes',
'type',
[ 'id' => $row[ 'attribute_id' ] ]
@@ -301,9 +296,9 @@ class ShopProduct
$product[ 'products_related' ] = $mdb -> select( 'pp_shop_products_related', 'product_related_id', [ 'product_id' => (int)$product_id ] );
$products_sets_1 = $mdb -> select( 'pp_shop_products_sets', 'product_sets_id', [ 'product_id' => (int)$product_id ] );
$products_sets_2 = $mdb -> select( 'pp_shop_products_sets', 'product_id', [ 'product_sets_id' => (int)$product_id ] );
$products_sets = array_unique( array_merge( $products_sets_1, $products_sets_2 ) );
$set_id = $mdb -> select( 'pp_shop_product_sets_products', 'set_id', [ 'product_id' => (int)$product_id ] );
$products_sets = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'set_id' => (int)$set_id ] );
$products_sets = array_unique( $products_sets );
$product[ 'products_sets' ] = $products_sets;
@@ -338,28 +333,6 @@ class ShopProduct
return $mdb -> get( 'pp_shop_products_langs', 'warehouse_message_nonzero', [ 'AND' => [ 'product_id' => $id_product, 'lang_id' => $lang_id ] ] );
}
public static function permutation_quantity( $id_product, $permutation, bool $is_multichoice )
{
global $mdb;
if ( !$is_multichoice )
return $mdb -> get( 'pp_shop_products_stock', 'quantity', [ 'AND' => [ 'id_product' => $id_product, 'permutation' => 0 ] ] );
if ( is_array( $permutation ) )
{
foreach ( $permutation as $key => $val )
{
$permutation_id .= $val;
if ( $val != end( $permutation ) )
$permutation_id .= '_';
}
}
else
$permutation_id = $permutation;
return $mdb -> get( 'pp_shop_products_stock', 'quantity', [ 'AND' => [ 'id_product' => $id_product, 'permutation' => $permutation_id ] ] );
}
//TO:DO do usunięcia
public static function product_both_price( $product_id )
{

View File

@@ -795,7 +795,7 @@ class Product implements \ArrayAccess
{
$result = array();
while ( list($key, $values) = each( $input ) )
foreach ( $input as $key => $values )
{
if ( empty( $values ) )
continue;
@@ -834,7 +834,7 @@ class Product implements \ArrayAccess
public function __get( $variable )
{
if ( array_key_exists( $variable, $this -> data ) )
if ( is_array( $this -> data ) and array_key_exists( $variable, $this -> data ) )
return $this -> $variable;
}

View File

@@ -3,7 +3,7 @@
* Medoo database framework
* http://medoo.in
* Version 0.9.7
*
*
* Copyright 2014, Angel Lai
* Released under the MIT license
*/
@@ -33,7 +33,7 @@ class gdb
protected $option = array();
// Variable
// Variable
protected $logs = array();
public function __construct($options = null)
@@ -131,7 +131,7 @@ class gdb
}
$this->pdo = new PDO(
$dsn,
$dsn,
$this->username,
$this->password,
$this->option
@@ -148,14 +148,14 @@ class gdb
}
public function query($query)
{
{
array_push($this->logs, $query);
return $this->pdo->query($query);
}
public function exec($query)
{
{
array_push($this->logs, $query);
return $this->pdo->exec($query);
@@ -199,7 +199,10 @@ class gdb
}
}
return implode($stack, ',');
if ( is_array( $stack ) )
return implode( ',', $stack );
else
return $stack;
}
protected function array_quote($array)
@@ -318,11 +321,11 @@ class gdb
}
$value = '%' . $value . '%';
$wheres[] = $column . ' LIKE ' . $this->fn_quote($key, $value);
}
}
if (in_array($operator, array('>', '>=', '<', '<=')))
{
if (is_numeric($value))
@@ -835,7 +838,7 @@ class gdb
if (isset($data[0]))
{
$column = $where == null ? $join : $column;
if (is_string($column) && $column != '*')
{
return $data[ 0 ][ $column ];

View File

@@ -1,29 +1,29 @@
<?php
class grid
{
{
protected $table;
protected $name;
protected $dir;
public $order;
public $search;
public $filters;
public $filters;
public $columns_view;
public $hidden_columns;
public $columns_edit;
public $summary;
public $clear_cache = true;
public $print = false;
public $hide_columns = true;
public $show_paging = true;
public $title = null;
public $src = null;
public $src_filtered = null;
public $id = 'id';
public $columns = '*';
public $cp = 1;
@@ -32,13 +32,13 @@ class grid
public $where = array();
public $join = null;
public $condensed = false;
public $sql = null;
public $sql_count = null;
public $include_plugins = true;
public $gdb_opt = array(
public $gdb_opt = array(
'database_type' => 'mysql',
'database_name' => 'db_name',
'server' => 'db_host',
@@ -46,96 +46,97 @@ class grid
'password' => 'db_pass',
'port' => 'db_port'
);
public $empty_txt = 'Brak danych w tabeli.';
public $multiselect = null;
public $multidelete_url = null;
public $buttons = null;
public $actions = array( 'delete' => false, 'delete_url' => null, 'add' => false, 'add_url' => null, 'edit' => false );
function __construct( $table, $name = '' )
{
$this -> table = $table;
$this -> name = $name;
$this -> dir = dirname( __FILE__ );
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
if ( !empty( $_SESSION[ 'g' . $g_table . 'filters' ] ) )
$this -> filters = $_SESSION[ 'g' . $g_table . 'filters' ];
if ( !empty( $_SESSION[ 'g' . $g_table . 'limit' ] ) )
$this -> limit = $_SESSION[ 'g' . $g_table . 'limit' ];
if ( !empty( $_SESSION[ 'g' . $g_table . 'order' ] ) )
$this -> order = $_SESSION[ 'g' . $g_table . 'order' ];
if ( $this -> clear_cache )
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
if ( is_array( $_SESSION ) ) foreach ( $_SESSION as $key => $val )
{
if ( $key != 'g' . $g_table and @get_class( $val ) == '__PHP_Incomplete_Class' )
unset( $_SESSION[ $key ] );
if ($key != 'g' . $g_table && is_object($val) && get_class($val) == '__PHP_Incomplete_Class') {
unset($_SESSION[$key]);
}
}
}
}
public function hide_column( $column, $hidden )
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
$db = $this -> connectToDb();
$results = $db -> get( 'grid_settings', 'settings', [ 'name' => $g_table ] );
$results = unserialize( $results );
$results['hidden_columns'][ $column ] = $hidden;
if ( $db -> count( 'grid_settings', [ 'name' => $g_table ] ) )
$db -> update( 'grid_settings', [ 'settings' => serialize( $results ) ], [ 'name' => $g_table ] );
else
$db -> insert( 'grid_settings', [ 'settings' => serialize( $results ), 'name' => $g_table ] );
}
public function drawEdit( $id )
{
$values = get_object_vars( $this );
$view = new gridView( $this -> dir . '/templates/' );
$view -> values = $values;
$view -> element = $this -> getElement( $id );
return $view -> render( 'edit' );
}
public function draw()
{
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
$db = $this -> connectToDb();
$results = $db -> get( 'grid_settings', 'settings', [ 'name' => $g_table ] );
$results = unserialize( $results );
$this -> hidden_columns = $results['hidden_columns'];
(int)$_SESSION[ 'g' . $g_table . 'cp' ] ? $this -> cp = (int)$_SESSION[ 'g' . $g_table . 'cp' ] : $this -> cp = 1;
(int)$_SESSION[ 'g' . $g_table . 'cp' ] ? $this -> cp = (int)$_SESSION[ 'g' . $g_table . 'cp' ] : $this -> cp = 1;
$_SESSION[ 'g' . $g_table ] = $this;
$values = get_object_vars( $this );
$values['content'] = $this -> drawResults();
$view = new gridView( $this -> dir . '/templates/' );
$view -> values = $values;
return $view -> render( 'container' );
}
public function connectToDb()
{
return new gdb( [
@@ -148,14 +149,14 @@ class grid
'charset' => 'utf8'
] );
}
public function getCSV()
{
if ( is_array( $this -> src ) )
$results = $this -> getDataSrc();
else
$results = $this -> getData( true );
if ( is_array( $this -> columns_view ) ) foreach ( $this -> columns_view as $column )
{
$array_row = array();
@@ -164,7 +165,7 @@ class grid
$headers[] = $column['name'];
}
}
if ( is_array( $results ) ) foreach ( $results as $row )
{
$array_row = array();
@@ -208,15 +209,15 @@ class grid
}
$array[] = $array_row;
}
$now = gmdate( "D, d M Y H:i:s" );
header( 'Content-Encoding: UTF-8' );
header( "Expires: Tue, 03 Jul 2001 06:00:00 GMT" );
header( "Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate" );
header( "Last-Modified: {$now} GMT" );
// force download
// force download
header( "Content-Type: application/force-download" );
header( "Content-Type: application/octet-stream" );
header( "Content-Type: application/download" );
@@ -233,11 +234,11 @@ class grid
fclose( $df );
return ob_get_clean();
}
public function printResults()
{
$values = get_object_vars( $this );
if ( is_array( $this -> src ) )
{
$values['count'] = $this -> getDataCountSrc();
@@ -247,30 +248,30 @@ class grid
$values['count'] = $this -> getDataCount();
$values['summary'] = $this -> getDataSummary();
}
$this -> cp = 1;
$this -> limit = $values['count'];
if ( is_array( $this -> src ) )
$values['results'] = $this -> getDataSrc();
else
$values['results'] = $this -> getData();
$view = new gridView( $this -> dir . '/templates/' );
$view -> values = $values;
return $view -> render( 'print' );
}
public function get_data_count_sql()
{
{
$db = $this ->connectToDb();
$where = $this -> getWhereCondition();
$where = $db -> where_clause( $where );
if ( strpos( $this -> sql_count, 'WHERE' ) !== false and !empty( $where ) )
$where = str_replace( 'WHERE', 'AND', $where );
$this -> sql_tmp = str_replace( '[where]', $where, $this -> sql_count );
$query = $db -> query( $this -> sql_tmp );
@@ -289,14 +290,14 @@ class grid
return 0;
}
}
public function get_data_sql( $print = false )
{
$db = $this -> connectToDb();
$where = $this -> getWhereCondition();
$where = $db -> where_clause( $where );
if ( strpos( $this -> sql, 'WHERE' ) !== false and !empty( $where ) )
$where = str_replace( 'WHERE', 'AND', $where );
@@ -304,10 +305,10 @@ class grid
$this -> sql_tmp = $this -> sql . ' LIMIT ' . $this -> limit . ' OFFSET ' . ( $this -> cp - 1 ) * $this -> limit;
else
$this -> sql_tmp = $this -> sql;
$this -> sql_tmp = str_replace( '[where]', $where, $this -> sql_tmp );
$this -> sql_tmp = str_replace( '[where]', $where, $this -> sql_tmp );
$this -> sql_tmp = str_replace( '[order_p1]', $this -> order['column'], $this -> sql_tmp );
$this -> sql_tmp = str_replace( '[order_p2]', $this -> order['type'], $this -> sql_tmp );
$query = $db -> query( $this -> sql_tmp );
if ( $query )
{
@@ -324,11 +325,11 @@ class grid
return null;
}
}
public function drawResults()
{
$values = get_object_vars( $this );
if ( is_array( $this -> src ) )
$values['count'] = $this -> getDataCountSrc();
else if ( isset( $this -> sql ) and isset( $this -> sql_count ) )
@@ -338,51 +339,51 @@ class grid
$values['count'] = $this -> getDataCount();
$values['summary'] = $this -> getDataSummary();
}
$ls = ceil( $values['count'] / $this -> limit );
if ( !(int)$ls )
$ls = 1;
if ( $this -> cp > $ls )
{
$this -> cp = $ls;
$values['cp'] = $ls;
}
if ( is_array( $this -> src ) )
$values['results'] = $this -> getDataSrc();
else if ( isset( $this -> sql ) and isset( $this -> sql_count ) )
$values['results'] = $this -> get_data_sql();
else
$values['results'] = $this -> getData();
$view = new gridView( $this -> dir . '/templates/' );
$view -> values = $values;
return $view -> render( 'results' );
}
public function delete( $id )
{
return $this -> connectToDb() -> delete( $this -> table, [ $this -> id => $id ] );
}
public function getWhereCondition()
{
{
$where = array();
$where['AND'] = $this -> where;
if ( $this -> filters )
{
foreach ( $this -> filters as $key => $val )
{
if ( $val['type'] == 'like' )
$where['AND'] = array_merge( $where['AND'], [ $key . '[~]' => $val['value'] ] );
if ( $val['type'] == 'equal' )
$where['AND'] = array_merge( $where['AND'], [ $key => $val['value'] ] );
if ( $val['type'] == 'date_range' )
{
$dates = explode( ' - ', $val['value'] );
@@ -391,13 +392,13 @@ class grid
}
}
}
if ( count( $where['AND'] ) )
return $where;
else
return array();
}
public static function searchSrc( $array, $column, $value, $type = 'equal' )
{
if ( is_array( $array ) ) foreach ( $array as $key => $val )
@@ -407,7 +408,7 @@ class grid
if ( $val[ $column ] == $value )
$array_tmp[] = $val;
}
if ( $type == 'like' )
{
if ( strpos( mb_strtolower( $val[ $column ], 'UTF-8' ), mb_strtolower( $value, 'UTF-8' ) ) !== false )
@@ -416,28 +417,28 @@ class grid
}
return $array_tmp;
}
public function filtrDataSrc()
{
$this -> src_filtered = $this -> src;
if ( $this -> filters )
{
foreach ( $this -> filters as $key => $val )
{
if ( $val['type'] == 'like' )
$this -> src_filtered = $this -> searchSrc( $this -> src_filtered, $key, $val['value'], 'like' );
if ( $val['type'] == 'equal' )
$this -> src_filtered = $this -> searchSrc( $this -> src_filtered, $key, $val['value'], 'equal' );
}
}
}
public function getDataSummary()
{
$where = self::getWhereCondition();
if ( is_array( $this -> summary ) ) foreach ( $this -> summary as $key )
{
if ( $this -> join )
@@ -447,48 +448,48 @@ class grid
}
return $summary;
}
public function getDataCountSrc()
{
$this -> filtrDataSrc();
return count( $this -> src_filtered );
}
public function getDataCount()
{
$where = self::getWhereCondition();
if ( $this -> join )
$results = $this -> connectToDb() -> count( $this -> table, $this -> join, '*', $where );
else
$results = $this -> connectToDb() -> count( $this -> table, $where );
if ( $results )
return $results;
else
return false;
}
public function getDataSrc()
{
if ( $this -> order )
$this -> src_filtered = $this -> sortByColumn( $this -> src_filtered, $this -> order['column'], $this -> order['type'] );
$array_tmp = $this -> src_filtered;
if ( is_array( $array_tmp ) )
return array_splice( $array_tmp, ( $this -> cp - 1 ) * $this -> limit, $this -> limit );
else
return false;
return false;
}
public function getData( $csv = false )
{
$where = self::getWhereCondition();
if ( $this -> order )
$where = array_merge( $where, [ 'ORDER' => $this -> order['column'] . ' ' . $this -> order['type'] ] );
if ( $this -> limit and $this -> show_paging === true and !$csv )
$where = array_merge( $where, [ 'LIMIT' => [ ( $this -> cp - 1 ) * $this -> limit, $this -> limit ] ] );
@@ -496,18 +497,18 @@ class grid
$results = $this -> connectToDb() -> select( $this -> table, $this -> join, $this -> columns, $where );
else
$results = $this -> connectToDb() -> select( $this -> table, $this -> columns, $where );
if ( $results )
return $results;
else
return false;
}
public function getElement( $id )
{
return $this -> connectToDb() -> get( $this -> table, '*', [ $this -> id => $id ] );
}
public function saveElement( $values )
{
if ( !$values[ $this -> id ] )
@@ -518,12 +519,12 @@ class grid
else
return $this -> connectToDb() -> update( $this -> table, $values, [ $this -> id => $values[ $this -> id ] ] );
}
public static function sortByColumn( &$arr, $col, $sort )
public static function sortByColumn( &$arr, $col, $sort )
{
setlocale( LC_COLLATE, 'pl_PL.utf-8' );
setlocale( LC_COLLATE, 'pl_PL.utf-8' );
$sort == 'ASC' ? $dir = SORT_ASC : $dir = SORT_DESC;
$sort_col = array();
if ( is_array( $arr ) )
{
@@ -534,46 +535,46 @@ class grid
}
return $arr;
}
public static function validateDate( $date )
{
if ( date( 'Y-m-d', strtotime( $date ) ) != '1970-01-01' )
return true;
}
public function save_limit( $limit )
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
$_SESSION[ 'g' . $g_table . 'limit' ] = $limit;
}
public function save_order()
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
$_SESSION[ 'g' . $g_table . 'order' ] = $this -> order;
}
public function save_filters()
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
$_SESSION[ 'g' . $g_table . 'filters' ] = $this -> filters;
}
public function set_cp( $cp )
{
$this -> name ? $g_table = $this -> name : $g_table = $this -> table;
$_SESSION[ 'g' . $g_table . 'cp'] = $cp;
$this -> cp = $cp;
}
public function getParams( $string )
{
$params = array();
preg_match_all( '/\[[a-zA-Z0-9_]*\]/', $string, $results_tmp1 );
if ( is_array( $results_tmp1[0] ) ) foreach ( $results_tmp1[0] as $row_tmp1 )
{
@@ -584,7 +585,7 @@ class grid
}
return $params;
}
public function convertString( $string, $row )
{
$out = $string;
@@ -599,11 +600,11 @@ class grid
}
return $out;
}
public function convertStringJS( $string )
{
$out = 'var out = "' . addslashes( $string ) . '";' . chr( 13 );
preg_match_all( '/\[[a-zA-Z0-9_]*\]/', $string, $results_tmp1 );
if ( is_array( $results_tmp1[0] ) ) foreach ( $results_tmp1[0] as $row_tmp1 )
{

File diff suppressed because it is too large Load Diff

BIN
templates/.DS_Store vendored

Binary file not shown.

View File

@@ -1,4 +1,4 @@
<? if ( $this -> order['status'] == 1 ):?>
<? if ( $this -> order['status'] == 1 or $this -> order['status'] == 4 ):?>
<div id="payment-confirmation">
<div class="bold blue text-center">Płatność została zatwierdzona. Dziękujemy za wybranie naszych usług.</div>
<div class="text-center">Nasz system zweryfikuje Państwa zamówienie w ciągu kilku minut.</div>
@@ -24,8 +24,26 @@
</tfoot>
</table>
</div>
<? endif;?>
<? if ( \S::get_session( 'ekomi-purchase' ) and $this -> settings['ekomi_survey'] ):?>
<? unset( $_SESSION['ekomi-purchase'] );?>
<?= $this -> settings['ekomi_survey'];?>
<? else:?>
<div id="payment-confirmation">
<div class="bold red text-center">Płatność oczekuje na zatwierdzenie.</div>
<div class="text-center">Nasz system zweryfikuje Państwa zamówienie w ciągu kilku minut.</div>
<p></p>
<table class="table table-stripped table-bordered payment-table">
<thead>
<tr>
<th class="text-center" colspan="2">Informacje o płatności</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-bold">Zamówienie nr:</td><td><?= $this -> order['number'];?></td>
</tr>
<tr>
<td class="text-bold">Data transakcji:</td><td><?= date( 'Y-m-d H:i', strtotime( $this -> order['date_order'] ) );?></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2" class="text-right
<? endif;?>