Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/theyoungdesigners_com/modules/storeImage/model/ |
Upload File : |
<?php
// add images to products - add image to filesystem - and database
class Images_Model extends Database_Model {
const TH_WIDTH = 277;
private $image_id;
private $product_id;
private $DATA;
private $multiple;
public function ImageId() {
if (func_num_args()) {
$this->image_id = func_get_arg(0);
$this->multiple = is_array($this->image_id) && count($this->image_id) > 1;
$this->Load();
return $this;
}
return $this->image_id;
}
public function ProductId() {
if (func_num_args()) {
$this->product_id = func_get_arg(0);
$this->multiple = FALSE;
$this->Load(TRUE);
return $this;
}
return $this->product_id;
}
protected function Load($useProduct = FALSE) {
$this->Query("select * from `store_image`");
if ($useProduct) {
$this->Where('si_product=%d', $this->product_id)
->OrderBy('si_order');
} elseif (is_array($this->image_id)) {
$this->WhereIn('si_id', $this->image_id);
} else {
$this->Where('si_id=%d', $this->image_id);
}
$rows = $this->Get();
$this->DATA = [];
foreach($rows as $row) {
$basename = '/' . pathinfo($row->si_filename, PATHINFO_BASENAME);
$authhash = md5('media:' . $basename);
$node = [
'hash' => $row->si_hash,
'filename' => $row->si_filename,
'mime' => $row->si_mimetype,
'width' => $row->si_width,
'height' => $row->si_height,
'order' => (int) $row->si_order,
'uri' => "/~/storeimage/image/{$row->si_hash}/{$row->si_filename}",
'auth' => hook_execute('nonce.create', FALSE, 'mediaitm', $authhash, 1800),
];
if ($useProduct) {
$node['id'] = $row->si_id;
$this->DATA[] = $node;
} else {
$this->DATA[$row->si_id] = $node;
}
}
if ($useProduct) {
$this->DATA = hook_execute('mediamanager.validate', $this->DATA); // add icon and thumbnail
}
}
// add the stuff
public function addHandlebars(&$res) {
$param = [
'images' => [],
'product' => $this->product_id,
];
$output = [];
foreach($this->DATA as $id => $info) {
$param['images'][] = [
'mainUrl' => "/~/storeimage/image/" . $info['hash'] . '/' . $info['filename'],
'width' => (int) $info['width'],
'height' => (int) $info['height'],
// 'description' => $info['filename'],
];
}
if (!count($param['images'])) {
$param['images'][] = [
'mainUrl' => '/~/store/product-placeholder.jpg',
];
}
$res['param'] = json_encode($param, JSON_UNESCAPED_SLASHES);
}
public function getImageData() {
return [
'data' => $this->DATA,
'sorting' => 'list,drag',
'readonly' => 0,
'shownav' => 0,
'content' => '',
];
}
function escape($value) {
return '"' . str_replace(array("\\", '"', "\n", "\r", "\t", "\0"),
array("\\\\", '\\"', "\\n", "\\r", "\\t", "\\0"), $value) . '"';
}
protected function _reorder($data, $tablename, $idfield, $record_id, $keyfield, $order) {
$case = '';
foreach($data as $key => $item) {
$safevalue = $this->escape($key);
$case .= " when $keyfield = {$safevalue} then {$item['new']} ";
}
$sql = "update $tablename set $order = case $case else $order end";
$this->Query($sql)
->Where("{$idfield}=%d", $record_id)
->Get();
$this->DbErr();
}
public function setImageOrder($order) {
$reorder = [];
$rows = $this->Query('select * from `store_image`')
->Where('si_product=%d', $this->product_id)
->Get();
foreach($rows as $row) {
$reorder[$row->si_hash] = [ 'original' => (int) $row->si_order ];
}
foreach($order as $index => $value) {
$reorder[$value]['new'] = $index + 1;
}
$this->_reorder($reorder, 'store_image', 'si_product', $this->product_id, 'si_hash', 'si_order');
return 1;
}
public function CloneProduct($param) {
$this->TableName('store_image');
$order = 0;
foreach($this->DATA as $item) {
$order++;
$this->InsertOrUpdate(
[
'si_product' => $param['target'],
'si_hash' => $item['hash'],
],[
'si_filename' => $item['filename'],
'si_mimetype' => $item['mime'],
'si_width' => $item['width'],
'si_height' => $item['height'],
'si_order' => $order,
],[
'si_id' => NULL,
]
);
$this->DbErr();
}
}
public function AddCartContent(&$data) {
// get all the ids
$ids = [];
foreach($data['cart'] as $item) {
$sp_id = $item['product'] ?? $item['sp_id'];
$ids[$sp_id] = NULL;
}
unset($item);
// get the first image for each
$sql = <<<'BLOCK'
SELECT I.si_product,I.si_hash FROM `store_image` as I
left join `store_image` as I2 on I.si_product=I2.si_product and I.si_order>I2.si_order
BLOCK;
$rows = $this->Query($sql)
->WhereIn('I.si_product', array_keys($ids))
->Where('I2.si_order is NULL')
->Get();
// $this->DbErr();
foreach($rows as $row) {
$sp_id = (int) $row->si_product;
$ids[$sp_id] = $row->si_hash;
}
$thumbsize = $data['imagesize'] ?? static::TH_WIDTH;
foreach($data['cart'] as &$item) {
$sp_id = $item['product'] ?? $item['sp_id'];
if (isset($ids[$sp_id])) {
$item['hash'] = $ids[$sp_id];
$item['thumb'] = $this->_loadThumbnail($item['hash'], $thumbsize);
}
}
}
private function _loadThumbnail($hash, $thumbsize) {
$params = new \StdClass;
$params->name = $hash;
$params->width = $thumbsize;
$params = hook_execute('mediamanager.thumbnail', $params);
if (property_exists($params, 'image')) {
return $params->image;
}
return '';
}
public function AddOrderContent(&$orders) {
$ids = [];
foreach($orders as $order) {
foreach($order['cart'] as $item) {
$sp_id = $item['product'] ?? $item['sp_id'];
$ids[$sp_id] = '';
}
}
unset($item, $order);
// get the first image for each
$sql = <<<'BLOCK'
SELECT I.si_product,I.si_hash FROM `store_image` as I
left join `store_image` as I2 on I.si_product=I2.si_product and I.si_order>I2.si_order
BLOCK;
$rows = $this->Query($sql)
->WhereIn('I.si_product', array_keys($ids))
->Where('I2.si_order is NULL')
->Get();
// $this->DbErr();
$thumbsize = $data['imagesize'] ?? static::TH_WIDTH;
foreach($rows as $row) {
$sp_id = (int) $row->si_product;
$ids[$sp_id] = $this->_loadThumbnail($row->si_hash, $thumbsize);
}
foreach($orders as &$order) {
foreach($order['cart'] as &$item) {
$sp_id = $item['product'] ?? $item['sp_id'];
$item['thumb'] = $ids[$sp_id];
}
}
}
// add/update an image to the database
public function updateImageData(&$node) {
// get the next Order:
$row = $this->Query("select max(si_order) as mx from `store_image`")
->Where('si_product=%d', $this->product_id)
->Get()
->First();
$order = $row->mx ?? 0;
$node['target'] = $node['hash'];
$this->TableName('store_image')
->InsertOrUpdate(
[
'si_product' => $this->product_id,
'si_filename' => $node['name'],
],[
'si_hash' => $node['hash'],
'si_mimetype' => $node['mime'],
'si_width' => $node['width'],
'si_height' => $node['height'],
'si_order' => $order + 1,
],[
'si_id' => NULL,
]
);
$this->DbErr();
}
public function getPrimaryImage() {
$uri = FALSE;
foreach($this->DATA as $id => $info) {
$uri = "/~/storeimage/image/" . $info['hash'] . '/' . $info['filename'];
break;
}
return $uri;
}
public function getFileHashByName($filename) {
$row = $this->Query('select si_hash,si_id from store_image')
->Where('si_product=%d', $this->product_id)
->Where('si_filename=%s', $filename)
->Get()
->First();
if (!is_null($row) && !$row->empty()) {
$filename = $row->si_hash;
}
}
// add the first image to each product
public function addPrimaryImage(&$products) {
$key = array_column($products, 'sp_id');
$rows = $this->Query("select * from `store_image`")
->WhereIn('si_product', $key)
->Where('si_order=1')
->Get();
$lookup = [];
foreach($rows as $row) {
$product = $row->si_product;
$lookup[$product] = '/~/storeimage/image/' . $row->si_hash . '/' . $row->si_filename;
}
foreach($products as &$prod) {
$prod['image'] = $lookup[$prod['sp_id']] ?? NULL;
}
}
public function Rebuild() {
$rows = $this->Query('select si_id,si_hash from store_image')
->Get();
$dir = HOME . '/storage/images';
$this->TableName('store_image');
foreach($rows as $row) {
$filename = "{$dir}/{$row->si_hash}";
if (file_exists("{$filename}")) {
echo "Rebuilding $filename\n";
if (($info = getimagesize($filename)) !== FALSE) {
$this->InsertOrUpdate([
'si_id' => $row->si_id,
], [
'si_width' => $info[0],
'si_height' => $info[1],
]);
$this->DbErr();
}
}
}
}
}