web/website/ops/oscommerce/DuneProductRules.php
2026-11-21 20:22:37 +01:00

37 lines
2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace frontend\components;
final class DuneProductRules {
public static function preferredName($value): string {
if (!is_string($value)) throw new \InvalidArgumentException('Please enter your preferred name for embroidery.');
$value=trim($value);
if ($value==='' || mb_strlen($value)>80 || preg_match('/[\x00-\x1F\x7F<>]/u',$value)) {
throw new \InvalidArgumentException('Enter a preferred name of 1–80 characters, without markup or control characters.');
}
return $value;
}
public static function validateCartRequest(): void {
if ((\Yii::$app->controller->action->id??'')==='error') return;
$action=$_GET['action']??'';
if (!in_array($action,['add_product','add_all','add_quote','add_quote_all','add_sample','buy_now'],true)) return;
$rows=\Yii::$app->db->createCommand('SELECT products_id,option_id,preview_only FROM dune_product_requirements')->queryAll();
foreach($rows as $row) {
$id=(int)$row['products_id'];
$single=(int)($_POST['products_id']??$_GET['products_id']??0)===$id;
$listed=isset($_POST['listqty'][$id]) || isset($_POST['listid'][$id]);
if (!$single && !$listed) continue;
$value=$single?($_POST['attr_text'][$row['option_id']]??null):($_POST['listattr_text'][$id][$row['option_id']]??null);
if((int)$row['option_id']>0){
try { self::preferredName($value); }
catch(\InvalidArgumentException $e) { self::reject(422,$e->getMessage()); }
}
if($row['preview_only']) self::reject(409,'Collection preview: ordering is currently unavailable.');
}
}
private static function reject(int $status,string $message): never {
$response=\Yii::$app->response;
$response->statusCode=$status;
$response->format=\yii\web\Response::FORMAT_JSON;
$response->data=['error'=>$message];
$response->send();\Yii::$app->end();exit;
}
}