File indexing completed on 2024-12-15 03:45:05
0001 <?php 0002 /* 0003 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0004 0005 SPDX-License-Identifier: MIT 0006 */ 0007 0008 require_once('datastore.php'); 0009 require_once('product.php'); 0010 0011 /** Represents a product aggregation setting. */ 0012 class Aggregation 0013 { 0014 public $type; 0015 public $name = ''; 0016 public $elements = array(); 0017 0018 /** Load aggregation settings for @p $product from storage. */ 0019 static public function aggregationsForProduct(DataStore $db, Product $product) 0020 { 0021 $sql = 'SELECT col_type, col_name, col_elements FROM tbl_aggregation WHERE col_product_id = :productId ORDER BY col_id'; 0022 $stmt = $db->prepare($sql); 0023 $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT); 0024 $db->execute($stmt); 0025 0026 $aggrs = array(); 0027 foreach ($stmt as $row) { 0028 $a = new Aggregation; 0029 $a->type = strval($row['col_type']); 0030 $a->name = strval($row['col_name']); 0031 $a->elements = json_decode(strval($row['col_elements'])); 0032 array_push($aggrs, $a); 0033 } 0034 return $aggrs; 0035 } 0036 0037 /** Update aggregation settings for @p $product to @p $aggregations. */ 0038 static public function update(DataStore $db, Product $product, $aggregations) 0039 { 0040 Aggregation::delete($db, $product); 0041 0042 $sql = 'INSERT INTO tbl_aggregation (col_product_id, col_type, col_name, col_elements) VALUES (:productId, :type, :name, :elements)'; 0043 $stmt = $db->prepare($sql); 0044 $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT); 0045 foreach ($aggregations as $a) { 0046 $stmt->bindValue(':type', $a->type, PDO::PARAM_STR); 0047 $stmt->bindValue(':name', $a->name, PDO::PARAM_STR); 0048 $stmt->bindValue(':elements', json_encode($a->elements), PDO::PARAM_STR); 0049 $db->execute($stmt); 0050 } 0051 } 0052 0053 /** Delete all aggregation settings for @p $product. */ 0054 static public function delete(DataStore $db, Product $product) 0055 { 0056 $sql = 'DELETE FROM tbl_aggregation WHERE col_product_id = :productId'; 0057 $stmt = $db->prepare($sql); 0058 $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT); 0059 $db->execute($stmt); 0060 } 0061 0062 /** Convert a JSON object into an array of Aggregation instances. */ 0063 static public function fromJson($jsonArray) 0064 { 0065 if (!is_array($jsonArray)) 0066 throw new RESTException('Wrong aggregation list format.', 400); 0067 0068 $aggrs = array(); 0069 foreach ($jsonArray as $jsonObj) { 0070 if (!is_object($jsonObj)) 0071 throw new RESTException('Wrong aggregation format.', 400); 0072 if (!property_exists($jsonObj, 'type') || !property_exists($jsonObj, 'name')) 0073 throw new RESTException('Incomplete aggregation object.', 400); 0074 0075 $a = new Aggregation; 0076 $a->type = strval($jsonObj->type); 0077 $a->name = strval($jsonObj->name); 0078 if (property_exists($jsonObj, 'elements')) 0079 $a->elements = $jsonObj->elements; 0080 array_push($aggrs, $a); 0081 } 0082 return $aggrs; 0083 } 0084 } 0085 0086 ?>