<%pre>

#include <exception.h>
#include <tools.h>
#include <users.h>
#include <recman.h>

using namespace vdrlive;

</%pre>
<%args>
  std::string param;
  std::string async;
</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<%cpp>
  if (!logged_in && LiveSetup().UseAuth()) {
    cToSvConcat targetUrl = "/login.html?redirect=";
    targetUrl.appendUrlEscaped(request.getQuery());
    return reply.redirect(targetUrl.data());
  }

  if (!cUser::CurrentUserHasRightTo(UR_DELRECS)) {
    esyslog("live: WARNING in delete_recording.ecpp, no permission to delete recording");
    throw HtmlError( tr("Sorry, no permission. Please contact your administrator!") );
  }

  bool ajaxReq = !async.empty() && (parse_int<int>(async) != 0);
  std::string referrer;

  if (ajaxReq) {
    reply.setContentType( "application/xml" );
  } else {
    referrer = request.getHeader("Referer:");
  }

  std::string name;
  const char *error_message = "";
  int result = RecordingsManager::DeleteRecording(param, &name);
  switch (result) {
    case 0: isyslog("live: INFO in delete_recording.ecpp, deleted recording with id %s name %s", param.c_str(), name.c_str() );
            break;
    case 1: error_message = tr("Couldn't find recording or no recordings available."); break;
    case 2: error_message = tr("Attempt to delete recording currently in playback."); break;
    case 3: error_message = "other error"; break;
    default: break;
  }
  if (result != 0)
    esyslog("live: ERROR in delete_recording.ecpp, cannot delete recording with id %s name %s, error %s", param.c_str(), name.c_str(), error_message);

  if (!ajaxReq) {
    if (!referrer.empty() && referrer.find("login") == std::string::npos) {
      return reply.redirect(referrer);
    }
</%cpp>
<b>Delete-Recording Request</b>
<hr/>
Recording: <$ name $><br/>
Result:    <$ result==0?"success":"error" $><br/>
Error:     <$ error_message $>
<hr/>
<%cpp>
  } else {
</%cpp>
<& xmlresponse.ajax name=("delete_recording") pname=("recording") value=(name) result=(result==0) error=(error_message) &>
<%cpp>
  }
</%cpp>
