| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Grocy\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 12:05:32 +02:00
										 |  |  | use Grocy\Controllers\Users\User; | 
					
						
							| 
									
										
										
										
											2020-09-01 19:59:40 +02:00
										 |  |  | use Slim\Exception\HttpBadRequestException; | 
					
						
							| 
									
										
										
										
											2020-08-29 12:05:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | class GenericEntityApiController extends BaseApiController | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-11 17:42:03 +01:00
										 |  |  | 	public function AddObject(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-29 16:41:27 +02:00
										 |  |  | 		User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); | 
					
						
							| 
									
										
										
										
											2020-08-29 12:05:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 16:41:27 +02:00
										 |  |  | 		if ($this->IsValidEntity($args['entity'])) | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 			if ($this->IsEntityWithEditRequiresAdmin($args['entity'])) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2020-09-06 10:00:49 +02:00
										 |  |  | 				User::checkPermission($request, User::PERMISSION_ADMIN); | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-09-06 10:00:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-14 22:49:29 +02:00
										 |  |  | 			$requestBody = $this->GetParsedAndFilteredRequestBody($request); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			try | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if ($requestBody === null) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)'); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 				$newRow = $this->getDatabase()->{$args['entity']} | 
					
						
							|  |  |  | 				()->createRow($requestBody); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 				$newRow->save(); | 
					
						
							|  |  |  | 				$success = $newRow->isClean(); | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 				return $this->ApiResponse($response, [ | 
					
						
							| 
									
										
										
										
											2020-03-01 23:47:47 +07:00
										 |  |  | 					'created_object_id' => $this->getDatabase()->lastInsertId() | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 				]); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			catch (\Exception $ex) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-01-19 14:51:51 +01:00
										 |  |  | 				return $this->GenericErrorResponse($response, $ex->getMessage()); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-19 14:51:51 +01:00
										 |  |  | 			return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed'); | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function DeleteObject(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ($this->IsValidEntity($args['entity'])) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 			if ($this->IsEntityWithEditRequiresAdmin($args['entity'])) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2020-09-06 10:00:49 +02:00
										 |  |  | 				User::checkPermission($request, User::PERMISSION_ADMIN); | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 			$row = $this->getDatabase()->{$args['entity']} | 
					
						
							|  |  |  | 			($args['objectId']); | 
					
						
							|  |  |  | 			$row->delete(); | 
					
						
							|  |  |  | 			$success = $row->isClean(); | 
					
						
							|  |  |  | 			return $this->EmptyApiResponse($response); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-09-14 11:20:29 +02:00
										 |  |  | 			return $this->GenericErrorResponse($response, 'Invalid entity'); | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 17:42:03 +01:00
										 |  |  | 	public function EditObject(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-29 16:41:27 +02:00
										 |  |  | 		User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); | 
					
						
							| 
									
										
										
										
											2020-08-29 12:05:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 16:41:27 +02:00
										 |  |  | 		if ($this->IsValidEntity($args['entity'])) | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 			if ($this->IsEntityWithEditRequiresAdmin($args['entity'])) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2020-09-06 10:00:49 +02:00
										 |  |  | 				User::checkPermission($request, User::PERMISSION_ADMIN); | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-10-14 22:49:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			$requestBody = $this->GetParsedAndFilteredRequestBody($request); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			try | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if ($requestBody === null) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)'); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 				$row = $this->getDatabase()->{$args['entity']} | 
					
						
							|  |  |  | 				($args['objectId']); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 				$row->update($requestBody); | 
					
						
							|  |  |  | 				$success = $row->isClean(); | 
					
						
							| 
									
										
										
										
											2019-01-19 14:51:51 +01:00
										 |  |  | 				return $this->EmptyApiResponse($response); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			catch (\Exception $ex) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-01-19 14:51:51 +01:00
										 |  |  | 				return $this->GenericErrorResponse($response, $ex->getMessage()); | 
					
						
							| 
									
										
										
										
											2019-01-05 20:39:22 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-19 14:51:51 +01:00
										 |  |  | 			return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed'); | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	public function GetObject(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 			$userfields = $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (count($userfields) === 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$userfields = null; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$object = $this->getDatabase()->{$args['entity']} | 
					
						
							|  |  |  | 			($args['objectId']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ($object == null) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				return $this->GenericErrorResponse($response, 'Object not found', 404); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$object['userfields'] = $userfields; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return $this->ApiResponse($response, $object); | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 			return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed'); | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	public function GetObjects(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							| 
									
										
										
										
											2019-08-10 13:07:08 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		$objects = $this->getDatabase()->{$args['entity']} | 
					
						
							|  |  |  | 		(); | 
					
						
							|  |  |  | 		$allUserfields = $this->getUserfieldsService()->GetAllValues($args['entity']); | 
					
						
							| 
									
										
										
										
											2020-08-29 12:05:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		foreach ($objects as $object) | 
					
						
							| 
									
										
										
										
											2019-08-10 13:07:08 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 			$userfields = FindAllObjectsInArrayByPropertyValue($allUserfields, 'object_id', $object->id); | 
					
						
							|  |  |  | 			$userfieldKeyValuePairs = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (count($userfields) > 0) | 
					
						
							| 
									
										
										
										
											2019-08-10 13:07:08 +02:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 				foreach ($userfields as $userfield) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					$userfieldKeyValuePairs[$userfield->name] = $userfield->value; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-08-10 13:07:08 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			$object->userfields = $userfieldKeyValuePairs; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->ApiResponse($response, $objects); | 
					
						
							| 
									
										
										
										
											2019-08-10 13:07:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 17:42:03 +01:00
										 |  |  | 	public function GetUserfields(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-29 16:41:27 +02:00
										 |  |  | 		try | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-03-01 23:47:47 +07:00
										 |  |  | 			return $this->ApiResponse($response, $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId'])); | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		catch (\Exception $ex) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->GenericErrorResponse($response, $ex->getMessage()); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function SearchObjects(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			try | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2020-09-01 19:59:40 +02:00
										 |  |  | 				return $this->FilteredApiResponse($response, $this->getDatabase()->{$args['entity']} | 
					
						
							|  |  |  | 					()->where('name LIKE ?', '%' . $args['searchString'] . '%'), $request->getQueryParams()); | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			catch (\PDOException $ex) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2020-09-01 19:59:40 +02:00
										 |  |  | 				throw new HttpBadRequestException($request, $ex->getMessage(), $ex); | 
					
						
							|  |  |  | 				//return $this->GenericErrorResponse($response, 'The given entity has no field "name"', $ex);
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 17:42:03 +01:00
										 |  |  | 	public function SetUserfields(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-29 16:41:27 +02:00
										 |  |  | 		User::checkPermission($request, User::PERMISSION_MASTER_DATA_EDIT); | 
					
						
							| 
									
										
										
										
											2020-08-29 12:05:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-14 22:49:29 +02:00
										 |  |  | 		$requestBody = $this->GetParsedAndFilteredRequestBody($request); | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		try | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if ($requestBody === null) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-01 23:47:47 +07:00
										 |  |  | 			$this->getUserfieldsService()->SetValues($args['entity'], $args['objectId'], $requestBody); | 
					
						
							| 
									
										
										
										
											2019-04-22 22:16:35 +02:00
										 |  |  | 			return $this->EmptyApiResponse($response); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		catch (\Exception $ex) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->GenericErrorResponse($response, $ex->getMessage()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	public function __construct(\DI\Container $container) | 
					
						
							| 
									
										
										
										
											2018-04-22 14:25:08 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		parent::__construct($container); | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-06 10:10:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 10:00:49 +02:00
										 |  |  | 	private function IsEntityWithEditRequiresAdmin($entity) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return !in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->EntityEditRequiresAdmin->enum); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-26 20:12:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private function IsEntityWithPreventedListing($entity) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-03-01 23:47:47 +07:00
										 |  |  | 		return !in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntityButNoListing->enum); | 
					
						
							| 
									
										
										
										
											2018-10-26 20:12:48 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private function IsValidEntity($entity) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntity->enum); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | } |