<?php class containercategory extends baseobject { 
	
	protected static array $fields=array(
		'name'=>validator::REQUIRED,
		'userscancreate'=>validator::BOOLEAN
	);
	
	protected static array $helpTexts=array(
		'name'=>'A unique identifier for the container category',
		'userscancreate'=>'If false, only admins and technicians can create containers in this category',
	);

    /**
     * @param array $request
     * @return array
     * @throws BadRequestException
     * @throws ForbiddenException
     * @throws NotFoundException
     * @throws ServerException
     */
	public static function create(array $request=[]): array {
		if(!static::canCreate()){
			throw new ForbiddenException('Only administrators and technicians can create containers');
		}
		$project=project::getByName(baseproject::SHARED);
		if(!$project){
			throw new NotFoundException('Could not read shared project');
		}
		$request['projectid']=$project['id'];
        return parent::createByClassName($request,'containercategory');
	}

	/**
	 * @return bool
	 * @throws BadRequestException
	 * @throws NotFoundException
	 * @throws ServerException
	 */
	public static function canCreate(): bool {
		if(session::isAdmin()){ return true; }
		$grp=usergroup::getByName(usergroup::TECHNICIANS);
		return usergroup::userisingroup($grp['id']);
	}
	
	
}