<commit_before><?php

/*
 * This file is part of the Slim API skeleton package
 *
 * Copyright (c) 2016 <NAME>
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *   https://github.com/tuupola/slim-api-skeleton
 *
 */


use Exception\NotFoundException;
use Exception\ForbiddenException;
use Exception\PreconditionFailedException;
use Exception\PreconditionRequiredException;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Serializer\DataArraySerializer;
use Tuupola\Base62;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use Intervention\Image\ImageManager;
use App\Panoram;
use App\PropGroup;
use App\Prop;
use App\PanoramProp;
use App\Status;
use App\Tag;
use App\File;
use App\UserPanoram;
use App\Message;
use App\User;
use App\UserMessage;

$app->post("/perfil/reenviar-bienvenida", function ($request, $response, $arguments) {

    $body = $request->getParsedBody();

    $user = $this->spot->mapper("App\User")->first([
        "id" => $this->token->decoded->uid
    ]);

    if( $user ){
        $body['readable_password'] = $body["readable"];
        $body['email_encoded'] = Base62::encode($user->email);
        $body['email'] = $user->email;
        $body['first_name'] = $user->first_name;
        $body['last_name'] = $user->last_name;
        \send_email("Bienvenido a " . getenv('APP_TITLE'),$user,'welcome.html',$body);
        $data["status"] = "ok";        
        $data["message"] = "Reenviamos el email de bienvenida";

    } else {
        $data["status"] = "error";        
        $data["message"] = "No se encontró el usuario";
    }

    return $response->withStatus(200)
        ->withHeader("Content-Type", "application/json")
        ->write(json_encode($data));
});


$app->post("/perfil/panos", function ($request, $response, $arguments) {

    // publicaciones

    if (false === $this->token->decoded->uid) {
        throw new ForbiddenException("Token not allowed to list panorams.", 403);
    }
    
    $filter = [
        'user_id' => $this->token->decoded->uid,
        'deleted' => 0
    ];

    $order = [
        'paused' => "ASC",
        'updated' => "DESC"
    ];

    $mapper = $this->spot->mapper("App\Panoram")->all()
        ->where($filter)
        ->order($order);

    $fractal = new Manager();
    $fractal->setSerializer(new DataArraySerializer);
    $resource = new Collection($mapper, new Panoram);
    $data['listing'] = $fractal->createData($resource)->toArray();

    // favs & alerts
    $filter = [
        'user_id' => $this->token->decoded->uid
    ];

    $order = [
        'created' => "DESC"
    ];

    $mapper = $this->spot->mapper("App\UserPanoram")->all()
        ->where($filter)
        ->order($order);

    $pan_ids = $fav = $alert = [];

    foreach($mapper as $item){
        $pan_ids[$item->type][] = $item->pan_id;
    }

    if( ! empty($pan_ids['fav'])){
        $mapper = $this->spot->mapper("App\Panoram")->all()
            ->where(['id' => $pan_ids['fav']]);

        $fractal = new Manager();
        $fractal->setSerializer(new DataArraySerializer);
        $resource = new Collection($mapper, new Panoram);
        $fav = $fractal->createData($resource)->toArray();
    }

    if( ! empty($pan_ids['alert'])){
        $mapper = $this->spot->mapper("App\Panoram")->all()
            ->where(['id' => $pan_ids['alert']]);

        $fractal = new Manager();
        $fractal->setSerializer(new DataArraySerializer);
        $resource = new Collection($mapper, new Panoram);
        $alert = $fractal->createData($resource)->toArray();
    }

    $data['fav'] = $fav;
    $data['alert'] = $alert;

    return $response->withStatus(200)
        ->withHeader("Content-Type", "application/json")
        ->write(json_encode($data));    
});

$app->post("/perfil/panos/eliminar/{code}", function ($request, $response, $arguments) {

    if (false === $this->token->decoded->uid) {
        throw new ForbiddenException("Token not allowed to update panorams.", 403);
    }

    $mapper =  $this->spot->mapper("App\Panoram")->first([
        "code" => $request->getAttribute('code'),
        "user_id" => $this->token->decoded->uid
    ]);

    if( ! $mapper){
        throw new NotFoundException("Panoram not found.", 404);        
    }

    // drop burden

    $photos = $this->spot->mapper("App\File")->where([
        "pan_id" => $mapper->id
    ]);

    $path = getenv('BUCKET_PATH') . '/cams/' . $this->token->decoded->uid . '/' . $mapper->code . '/';

    foreach($photos as $photo) {
        $fn = substr($photo->photo_url, strrpos($photo->photo_url, '/') + 1);

        unlink($path . $fn);

        $resolutions = explode(',',getenv('S3_RESOLUTIONS'));

        foreach($resolutions as $res){
            $parts = explode('x',$res);
            unlink($path . $parts[0] . 'x' . $parts[1] . $fn);
        }

        $this->spot->mapper("App\File")->delete($photo);        
    }

    $vehicle = $mapper->data(['deleted' => 1]);
    $this->spot->mapper("App\Panoram")->save($vehicle);

    $data['status'] = 'ok';
    $data['message'] = 'El vehículo fue eliminado';<commit_msg>remove panos fix<commit_after><?php

/*
 * This file is part of the Slim API skeleton package
 *
 * Copyright (c) 2016 <NAME>
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *   https://github.com/tuupola/slim-api-skeleton
 *
 */


use Exception\NotFoundException;
use Exception\ForbiddenException;
use Exception\PreconditionFailedException;
use Exception\PreconditionRequiredException;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Serializer\DataArraySerializer;
use Tuupola\Base62;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use Intervention\Image\ImageManager;
use App\Panoram;
use App\PropGroup;
use App\Prop;
use App\PanoramProp;
use App\Status;
use App\Tag;
use App\File;
use App\UserPanoram;
use App\Message;
use App\User;
use App\UserMessage;

$app->post("/perfil/reenviar-bienvenida", function ($request, $response, $arguments) {

    $body = $request->getParsedBody();

    $user = $this->spot->mapper("App\User")->first([
        "id" => $this->token->decoded->uid
    ]);

    if( $user ){
        $body['readable_password'] = $body["readable"];
        $body['email_encoded'] = Base62::encode($user->email);
        $body['email'] = $user->email;
        $body['first_name'] = $user->first_name;
        $body['last_name'] = $user->last_name;
        \send_email("Bienvenido a " . getenv('APP_TITLE'),$user,'welcome.html',$body);
        $data["status"] = "ok";        
        $data["message"] = "Reenviamos el email de bienvenida";

    } else {
        $data["status"] = "error";        
        $data["message"] = "No se encontró el usuario";
    }

    return $response->withStatus(200)
        ->withHeader("Content-Type", "application/json")
        ->write(json_encode($data));
});


$app->post("/perfil/panos", function ($request, $response, $arguments) {

    // publicaciones

    if (false === $this->token->decoded->uid) {
        throw new ForbiddenException("Token not allowed to list panorams.", 403);
    }
    
    $filter = [
        'user_id' => $this->token->decoded->uid,
        'deleted' => 0
    ];

    $order = [
        'paused' => "ASC",
        'updated' => "DESC"
    ];

    $mapper = $this->spot->mapper("App\Panoram")->all()
        ->where($filter)
        ->order($order);

    $fractal = new Manager();
    $fractal->setSerializer(new DataArraySerializer);
    $resource = new Collection($mapper, new Panoram);
    $data['listing'] = $fractal->createData($resource)->toArray();

    // favs & alerts
    $filter = [
        'user_id' => $this->token->decoded->uid
    ];

    $order = [
        'created' => "DESC"
    ];

    $mapper = $this->spot->mapper("App\UserPanoram")->all()
        ->where($filter)
        ->order($order);

    $pan_ids = $fav = $alert = [];

    foreach($mapper as $item){
        $pan_ids[$item->type][] = $item->pan_id;
    }

    if( ! empty($pan_ids['fav'])){
        $mapper = $this->spot->mapper("App\Panoram")->all()
            ->where(['id' => $pan_ids['fav']]);

        $fractal = new Manager();
        $fractal->setSerializer(new DataArraySerializer);
        $resource = new Collection($mapper, new Panoram);
        $fav = $fractal->createData($resource)->toArray();
    }

    if( ! empty($pan_ids['alert'])){
        $mapper = $this->spot->mapper("App\Panoram")->all()
            ->where(['id' => $pan_ids['alert']]);

        $fractal = new Manager();
        $fractal->setSerializer(new DataArraySerializer);
        $resource = new Collection($mapper, new Panoram);
        $alert = $fractal->createData($resource)->toArray();
    }

    $data['fav'] = $fav;
    $data['alert'] = $alert;

    return $response->withStatus(200)
        ->withHeader("Content-Type", "application/json")
        ->write(json_encode($data));    
});

$app->post("/perfil/panos/eliminar/{code}", function ($request, $response, $arguments) {

    if (false === $this->token->decoded->uid) {
        throw new ForbiddenException("Token not allowed to update panorams.", 403);
    }

    $mapper =  $this->spot->mapper("App\Panoram")->first([
        "code" => $request->getAttribute('code'),
        "user_id" => $this->token->decoded->uid
    ]);

    if( ! $mapper){
        throw new NotFoundException("Panoram not found.", 404);        
    }

    // drop burden

    $photos = $this->spot->mapper("App\File")->where([
        "pan_id" => $mapper->id
    ]);

    $path = getenv('BUCKET_PATH') . '/cams/' . $mapper->code . '/';

    foreach($photos as $photo) {
        $fn = substr($photo->file_url, strrpos($photo->file_url, '/') + 1);

        unlink($path . $fn);

        $resolutions = explode(',',getenv('S3_RESOLUTIONS'));

        foreach($resolutions as $res){
            $parts = explode('x',$res);
            unlink($path . $parts[0] . 'x' . $parts[1] . $fn);
        }

        $this->spot->mapper("App\File")->delete($photo);        
    }

    rmdir($path);

    $vehicle = $mapper->data(['deleted' => 1]);
    $this->spot->mapper("App\Panoram")->save($vehicle);

    $data['status'] = 'ok';
    $data['message'] = 'El vehículo fue eliminado';
{'fraction_non_alphanumeric': 0.19455926175141786, 'fraction_numerical': 0.0063443237527636255, 'mean_word_length': 2.8690963183339533, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 198, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '770632', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3439, 'n_words': 605}