63 lines
8.9 KiB
PHP
63 lines
8.9 KiB
PHP
<?php
|
|
namespace frontend\controllers;
|
|
use Yii;
|
|
use yii\helpers\Html;
|
|
final class DuneShopController extends Sceleton {
|
|
public function actionSalesFeed() {
|
|
Yii::$app->response->format=\yii\web\Response::FORMAT_JSON;
|
|
Yii::$app->response->headers->set('Cache-Control','public, max-age=60');
|
|
if(!Yii::$app->request->isGet){Yii::$app->response->statusCode=405;return ['message'=>'Read-only shop offers.'];}
|
|
$rows=Yii::$app->db->createCommand("SELECT DISTINCT p.products_id,p.products_price,p.products_tax_class_id,pd.products_name FROM products p JOIN platforms_products pp ON pp.products_id=p.products_id AND pp.platform_id=7 JOIN products_description pd ON pd.products_id=p.products_id AND pd.language_id=1 AND pd.platform_id=7 JOIN specials s ON s.products_id=p.products_id WHERE p.products_status=1 AND p.products_price>0 AND s.status=1 AND s.specials_new_products_price>=0 AND s.specials_new_products_price<p.products_price AND (s.start_date IS NULL OR s.start_date='0000-00-00 00:00:00' OR s.start_date<=NOW()) AND (s.expires_date IS NULL OR s.expires_date='0000-00-00 00:00:00' OR s.expires_date>NOW()) ORDER BY p.products_id DESC LIMIT 12")->queryAll();
|
|
$currencies=Yii::$container->get('currencies');$items=[];
|
|
foreach($rows as $p){
|
|
$id=(int)$p['products_id'];$normal=\common\helpers\Product::get_products_price($id,1,$p['products_price']);$special=\common\helpers\Product::get_products_special_price($id,1);
|
|
if(!is_numeric($normal)||!is_numeric($special)||(float)$normal<=0||(float)$special<0||(float)$special>=(float)$normal)continue;
|
|
$tax=\common\helpers\Tax::get_tax_rate($p['products_tax_class_id']);
|
|
$price=fn($value)=>html_entity_decode(strip_tags($currencies->format($currencies->getPriceTaxable($value,$tax,1),true,'EUR','',false,false)),ENT_QUOTES|ENT_HTML5,'UTF-8');
|
|
$items[]=['id'=>$id,'name'=>$p['products_name'],'url'=>\yii\helpers\Url::to(['catalog/product','products_id'=>$id],true),'image'=>\common\classes\Images::getImageUrl($id,'Small',1),'regularPrice'=>$price($normal),'salePrice'=>$price($special),'savingPercent'=>max(1,(int)round((1-(float)$special/(float)$normal)*100))];
|
|
}
|
|
return ['source'=>'oscommerce','brand'=>'Imperial Heralds','currency'=>'EUR','items'=>$items];
|
|
}
|
|
public function actionIndex() {
|
|
$this->view->title='Imperial Heralds · DuneRealms';
|
|
$rows=Yii::$app->db->createCommand("SELECT p.products_id,p.products_price,p.products_tax_class_id,pd.products_name,pd.products_description_short,d.kind FROM products p JOIN platforms_products pp ON pp.products_id=p.products_id AND pp.platform_id=7 JOIN products_description pd ON pd.products_id=p.products_id AND pd.language_id=1 AND pd.platform_id=7 LEFT JOIN dune_catalog_import d ON d.products_id=p.products_id WHERE p.products_status=1 ORDER BY p.products_date_added DESC,p.products_id DESC")->queryAll();
|
|
$saleIds=Yii::$app->db->createCommand("SELECT DISTINCT products_id FROM specials WHERE EXISTS(SELECT 1 FROM products active JOIN platforms_products pp ON pp.products_id=active.products_id AND pp.platform_id=7 WHERE active.products_id=specials.products_id AND active.products_status=1 AND specials.specials_new_products_price<active.products_price) AND status=1 AND (start_date IS NULL OR start_date='0000-00-00 00:00:00' OR start_date<=NOW()) AND (expires_date IS NULL OR expires_date='0000-00-00 00:00:00' OR expires_date>NOW())")->queryColumn();
|
|
$featuredIds=Yii::$app->db->createCommand("SELECT DISTINCT products_id FROM featured WHERE EXISTS(SELECT 1 FROM products active JOIN platforms_products pp ON pp.products_id=active.products_id AND pp.platform_id=7 WHERE active.products_id=featured.products_id AND active.products_status=1) AND status=1 AND (start_date IS NULL OR start_date='0000-00-00 00:00:00' OR start_date<=NOW()) AND (expires_date IS NULL OR expires_date='0000-00-00 00:00:00' OR expires_date>NOW())")->queryColumn();
|
|
$sashes=count(array_filter($rows,fn($r)=>$r['kind']==='sash'));$ranks=count(array_filter($rows,fn($r)=>$r['kind']==='rank'));
|
|
$base=fn($route)=>Html::encode(\yii\helpers\Url::to([$route]));
|
|
$out='<section class="dune-store-welcome"><div><small>IMPERIAL HERALDS · DUNEREALMS</small><h1>Carry a piece of the realms.</h1><p>Ceremonial sashes, personal keepsakes and ways to support the community.</p><a class="dune-store-button" href="'.$base('catalog/all-products').'">Browse the collection</a></div><img src="https://www.dunerealms.com/assets/imperial-heralds-logo-v1.png" alt=""></section>';
|
|
$tiles=[['Sale',count($saleIds)?'Explore current offers':'No sales active right now','catalog/sales','nav-exchange-v2.webp',''],['Ceremonial sashes',$sashes.' designs · made to order','catalog/all-products','nav-clan-v2.webp','sash'],['Donor ranks',$ranks.' ways to support the realms','catalog/all-products','nav-account-v2.webp','rank']];
|
|
$out.='<nav class="dune-store-categories" aria-label="Shop collections">';
|
|
foreach($tiles as [$title,$text,$route,$icon,$kind]){
|
|
$category=$kind?Yii::$app->db->createCommand('SELECT c.categories_id FROM categories c JOIN categories_description d ON d.categories_id=c.categories_id WHERE d.language_id=1 AND d.categories_name=:name LIMIT 1',[':name'=>$kind==='sash'?'Ceremonial Sashes':'Donor Ranks'])->queryScalar():null;
|
|
$url=$category?\yii\helpers\Url::to(['catalog/index','cPath'=>$category]):\yii\helpers\Url::to([$route]);
|
|
$out.='<a class="'.($title==='Sale'?'dune-store-category--sale':'').'" href="'.Html::encode($url).'"><img src="https://www.dunerealms.com/assets/'.$icon.'" alt=""><div><h2>'.Html::encode($title).'</h2><p>'.Html::encode($text).'</p></div><span aria-hidden="true">'.($title==='Sale'?'SALE':'↗').'</span></a>';
|
|
}
|
|
$out.='</nav>';
|
|
foreach([['Sale','Current offers from the emporium',array_values(array_filter($rows,fn($r)=>in_array($r['products_id'],$saleIds))),'catalog/sales'],['Featured','Selected pieces from the realms',array_values(array_filter($rows,fn($r)=>in_array($r['products_id'],$featuredIds))),'catalog/featured-products'],['New arrivals','Recently added to the collection',array_slice($rows,0,6),'catalog/products-new']] as [$title,$sub,$items,$route]){
|
|
$sectionClass=$title==='Sale'?' dune-store-section--sale':($title==='Featured'?' dune-store-section--featured':'');
|
|
$out.='<section class="dune-store-section'.$sectionClass.'"><header><div><h2>'.($title==='Sale'?'<span class="dune-store-sale-label">SALE</span> ':'').$title.'</h2><p>'.$sub.'</p></div><a href="'.$base($route).'">View all ↗</a></header>';
|
|
if(!$items)$out.='<div class="dune-store-empty">'.($title==='Sale'?'There are no active sale offers.':'Featured products have not been selected yet.').'</div>';
|
|
else{
|
|
$out.='<div class="dune-store-products">';
|
|
foreach(array_slice($items,0,6) as $p){
|
|
$url=Html::encode(\yii\helpers\Url::to(['catalog/product','products_id'=>$p['products_id']]));
|
|
$image=Html::encode(\common\classes\Images::getImageUrl($p['products_id'],'Small',1));
|
|
$currencies=Yii::$container->get('currencies');$tax=\common\helpers\Tax::get_tax_rate($p['products_tax_class_id']);
|
|
$normal=\common\helpers\Product::get_products_price($p['products_id'],1,$p['products_price']);
|
|
$special=\common\helpers\Product::get_products_special_price($p['products_id'],1);
|
|
$price=$currencies->display_price($normal,$tax);
|
|
if($special!==false&&$special!==null&&(float)$special>=0&&(float)$special<(float)$normal){
|
|
$saving=(int)round((1-(float)$special/(float)$normal)*100);
|
|
$price='<del>'.$price.'</del><strong>'.$currencies->display_price($special,$tax).'</strong><span class="dune-store-saving">Save '.$saving.'%</span>';
|
|
}
|
|
$out.='<article><a class="dune-store-product-art" href="'.$url.'"><img loading="lazy" src="'.$image.'" alt="'.Html::encode($p['products_name']).'"></a><small>'.($p['kind']==='sash'?'MADE TO ORDER':'CATALOGUE PREVIEW').'</small><h3><a href="'.$url.'">'.Html::encode($p['products_name']).'</a></h3><div class="dune-store-price">'.$price.'</div><a class="dune-store-button" href="'.$url.'">View details</a></article>';
|
|
}
|
|
$out.='</div>';
|
|
}
|
|
$out.='</section>';
|
|
}
|
|
$out.='<aside class="dune-store-preview"><strong>Test ordering</strong><p>The Season sash is available as a test order by bank transfer; do not transfer money for the test. Other products remain collection previews. Sashes are made to order, with a required preferred name. Standard side edging is included; silver side edging is +€50 and gold side edging is +€75. The bottom fringe is unchanged.</p></aside>';
|
|
return $this->renderContent($out);
|
|
}
|
|
}
|

Staff