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 require_once('sample.php'); 0011 require_once('survey.php'); 0012 require_once('utils.php'); 0013 0014 /** Command handler for the admin interface. */ 0015 class Admin 0016 { 0017 0018 /** Add a new product. */ 0019 public function post_products() 0020 { 0021 $rawPostData = file_get_contents('php://input'); 0022 $product = Product::fromJson($rawPostData); 0023 0024 $db = new DataStore(); 0025 $db->beginTransaction(); 0026 $product->insert($db); 0027 $db->commit(); 0028 0029 echo('Product ' . $product->name . " added."); 0030 } 0031 0032 /** Update a given product. */ 0033 public function put_products($productName) 0034 { 0035 $raw = file_get_contents('php://input'); 0036 $newProduct = Product::fromJson($raw); 0037 0038 $db = new DataStore(); 0039 $db->beginTransaction(); 0040 $oldProduct = Product::productByName($db, $productName); 0041 if (is_null($oldProduct)) 0042 throw new RESTException('Product not found.', 404); 0043 0044 $oldProduct->update($db, $newProduct); 0045 $db->commit(); 0046 echo('Product ' . $productName . ' updated.'); 0047 } 0048 0049 /** Delete product and associated data. */ 0050 public function delete_products($productName) 0051 { 0052 $db = new DataStore(); 0053 $db->beginTransaction(); 0054 $product = Product::productByName($db, $productName); 0055 if (is_null($product)) 0056 throw new RESTException('Product not found.', 404); 0057 $product->delete($db); 0058 $db->commit(); 0059 echo('Product ' . $productName . ' deleted.'); 0060 } 0061 0062 /** Import data for a product. */ 0063 public function post_data($productName) 0064 { 0065 $db = new DataStore(); 0066 $db->beginTransaction(); 0067 $product = Product::productByName($db, $productName); 0068 if (is_null($product)) 0069 throw RESTException('Unknown product.', 404); 0070 Sample::import($db, file_get_contents('php://input'), $product); 0071 $db->commit(); 0072 echo('Data imported.'); 0073 } 0074 0075 /** Add new survey. */ 0076 public function post_surveys($productName) 0077 { 0078 if ($productName == "") 0079 Utils::httpError(400, "No product id specified."); 0080 0081 $rawPostData = file_get_contents('php://input'); 0082 $survey = Survey::fromJson($rawPostData); 0083 0084 $db = new DataStore(); 0085 $db->beginTransaction(); 0086 $product = Product::productByName($db, $productName); 0087 if (is_null($product)) 0088 Utils::httpError(404, "Invalid product identifier."); 0089 0090 $survey->insert($db, $product); 0091 $db->commit(); 0092 echo('Survey created for product ' . $product->name . '.'); 0093 } 0094 0095 /** Edit an existing survey. */ 0096 public function put_surveys($surveyId) 0097 { 0098 $surveyId = strval($surveyId); 0099 if (strlen($surveyId) <= 0) 0100 throw new RESTException('Invalid survey id.', 400); 0101 0102 $surveyData = file_get_contents('php://input'); 0103 $survey = Survey::fromJson($surveyData); 0104 $survey->uuid = $surveyId; 0105 0106 $db = new DataStore(); 0107 $db->beginTransaction(); 0108 $survey->update($db); 0109 $db->commit(); 0110 echo("Survey updated."); 0111 } 0112 0113 /** Delete survey. */ 0114 public function delete_surveys($surveyId) 0115 { 0116 $survey = new Survey; 0117 $survey->uuid = strval($surveyId); 0118 if (strlen($survey->uuid) <= 0) 0119 throw new RESTException('Invalid survey id.', 400); 0120 0121 $db = new DataStore(); 0122 $db->beginTransaction(); 0123 $survey->delete($db); 0124 $db->commit(); 0125 echo("Survey deleted."); 0126 } 0127 0128 } 0129 0130 ?>