<?php

//------------------------------------------------------------------------------/
// UPDATE EVENT
//------------------------------------------------------------------------------/
/**
 * Tim service, it's a tim success if the update is done,
 * and a tim failure otherwise.
 */
use QuickPdo\QuickPdo;
use Tim\TimServer\TimServer;
use Tim\TimServer\TimServerInterface;
require_once __DIR__ . "/../../init.php";
TimServer::create()->start(function (TimServerInterface $server) {
    if (isset($_POST['id']) && isset($_POST['title']) && isset($_POST['description']) && isset($_POST['start_date']) && isset($_POST['end_date'])) {
        if (true === QuickPdo::update('the_events', ['title' => $_POST['title'], 'description' => $_POST['description'], 'start_date' => $_POST['start_date'], 'end_date' => $_POST['end_date']], [['id', '=', $_POST['id']]])) {
            $server->success('ok');
        } else {
            appLog("[app]/www/service/update-event: pdo error: {pdoError}", ['pdoError' => QuickPdo::getLastError()]);
            $server->error('An error occurred with the database, please retry later.');
        }
    }
})->output();
 $arr = array_replace($defaultValues, $arr);
 $idf = $mc->getFormDataProcessor()->getIdentfyingFields();
 $nac = $mc->getFormDataProcessor()->getNonAutoIncrementedFields();
 $nac2Values = array_intersect_key($arr, array_flip($nac));
 try {
     $mode = 'insert';
     if (false === ($missing = ArrayTool::getMissingKeys($arr, $idf))) {
         $mode = 'update';
         // update
         if (true === MeredithSupervisor::inst()->isGranted($formId, 'update')) {
             $idf2Values = array_intersect_key($arr, array_flip($idf));
             $where = [];
             foreach ($idf2Values as $k => $v) {
                 $where[] = [$k, '=', $v];
             }
             if (true === QuickPdo::update($table, $nac2Values, $where)) {
                 $msg = $mc->getFormDataProcessor()->getSuccessMessage($formId, 'update');
                 if (false === $msg) {
                     $msg = MeredithSupervisor::inst()->translate("The record has been successfully updated");
                 }
                 $server->success(['msg' => $msg]);
             } else {
                 $server->error(MeredithSupervisor::inst()->translate("An error occurred with the database, please retry later"));
             }
         } else {
             throw new MeredithException("Permission not granted to update with {$formId}");
         }
     } else {
         // insert
         if (true === MeredithSupervisor::inst()->isGranted($formId, 'insert')) {
             if (false !== ($id = QuickPdo::insert($table, $nac2Values))) {