<?php
defined('_VALID') or die('Restricted Access!');

if ( $config['photo_module'] == '0' ) {
    die();
}

require $config['BASE_DIR']. '/classes/filter.class.php';
require $config['BASE_DIR']. '/include/compat/json.php';
require $config['BASE_DIR']. '/include/adodb/adodb.inc.php';
require $config['BASE_DIR']. '/include/dbconn.php';
require $config['BASE_DIR']. '/include/sessions.php';

disableRegisterGlobals();

$data   = array('status' => 0, 'msg' => '');
if ( isset($_POST['photo_id']) && isset($_POST['album_id']) ) {
    $filter     = new VFilter();
    $photo_id   = $filter->get('photo_id', 'INTEGER');
    $album_id   = $filter->get('album_id', 'INTEGER');
    if ( isset($_SESSION['uid']) ) {
        $uid            = intval($_SESSION['uid']);
        $sql            = "SELECT PID FROM photo_favorites WHERE PID = " .$photo_id. " AND UID = " .$uid. " LIMIT 1";
        $conn->execute($sql);
        if ( $conn->Affected_Rows() == 1 ) {
            $data['msg']    = 'This photo is already in your favorite list!';
        } else {
            $sql        = "SELECT PID FROM photos WHERE PID = " .$photo_id. " AND UID = " .$uid. " LIMIT 1";
            $conn->execute($sql);
            if ( $conn->Affected_Rows() == 1 ) {
                $data['msg']    = 'You cannot add your own photo as favorite!';
            } else {
                $sql    = "INSERT INTO photo_favorites SET PID = " .$photo_id. ", UID = " .$uid;
                $conn->execute($sql);
                $sql    = "UPDATE photos SET total_favorites = total_favorites+1 WHERE PID = " .$photo_id. " LIMIT 1";
                $conn->execute($sql);
                $sql    = "UPDATE albums SET total_favorites = total_favorites+1 WHERE AID = " .$album_id. " LIMIT 1";
                $conn->execute($sql);
                $data['msg']    = 'Photo was added to your favorite list!';
                $data['status'] = 1;
            }
        }
    } else {
        $data['msg']        = 'You need to login to favorite photos!';
    }
}

echo json_encode($data);
die();
?>
