<?php
require_once 'src/facebook.php';

try {
    $facebook = new Facebook(array(
                'appId' => '495811327106485',
                'secret' => 'b5d0c4862fe1cfab1b1c6fc09ee0cbab',
                'cookie' => true
            ));


    if (!$facebook->getUser()) {
        header("Location:{$facebook->getLoginUrl(array('scope' => 'user_status,publish_stream,user_photos,user_videos'))}");
        exit;
    }
    $me = $facebook->api('/me');
} catch (Exception $e) {
    echo $e->getMessage();
    echo '<p>Please try clearing your browser cookies.</p>';
    die;
}
?>
<html>
    <head>
        <title>Facebook Album</title>
        <script>
            function validate()
            {
                
                var chkelements = document.getElementsByName('albumphotodata[]');
                var ids = "";
                var countChecked = 0;
                for(var i = 0; i < chkelements.length; i++)
                {
                    if(chkelements.item(i).checked == true)
                    {
                        countChecked++;
                        if(ids != "")
                            ids += ",";
                        ids += chkelements.item(i).value;
                    }
                }
                if(countChecked == 0)
                {
                    alert(" Please select atleast one album to be uploade.");
                    return false;
                }
                if(!confirm('Caution! The selected album will be uploaded. Press OK to confirm.'))
                {
                    return false;
                }
                
            }
            function validate1()
            {
                
                var chkelements = document.getElementsByName('photodata[]');
                var ids = "";
                var countChecked = 0;
                for(var i = 0; i < chkelements.length; i++)
                {
                    if(chkelements.item(i).checked == true)
                    {
                        countChecked++;
                        if(ids != "")
                            ids += ",";
                        ids += chkelements.item(i).value;
                    }
                }
                if(countChecked == 0)
                {
                    alert(" Please select atleast one photo to be uploade.");
                    return false;
                }
                if(!confirm('Caution! The selected photo will be uploaded. Press OK to confirm.'))
                {
                    return false;
                }
                
            }
        </script>
    </head>
    <body>


        <?php
     
            echo '<a href="index.php?logout=t">Logout</a>';
            if ($_REQUEST['logout'] == 't') {
                $token = $facebook->getAccessToken();
                $url = 'https://www.facebook.com/logout.php?next=http://netcluesdemo.com/facebookalbum/index.php&access_token=' . $token;
                session_destroy();
                header('Location: ' . $url);
            }
     

        if (isset($_GET['album_id']) && $_GET['album_id'] != '') {
            $album_id = $_GET['album_id'];
            $photos = $facebook->api("/{$album_id}/photos");
            $i = 0;
            ?>
            <form action="upload.php" method="post" onsubmit="return validate1();" enctype="multipart/form-data"/>
            <?php
            foreach ($photos['data'] as $photo) {
                echo "<input type='checkbox' name='photodata[]' value='{$photos['data'][$i]['source']}' id='photodata' />
            <input type='hidden' name='album_id' value='{$album_id}' id='album_id' />
            <img width='100' height='100' src='{$photos['data'][$i]['source']}' />";
                $i++;
            }
            ?>
            <input type="submit" value="upload image" />
            <br/><a href="index.php">Back</a>
        </form>
    <?php
}

if (!isset($_GET['album_id']) && $_GET['album_id'] == '') {
    ?>
        <form action="upload.php" method="post" onsubmit="return validate();" enctype="multipart/form-data"/>
        <?php
        $albums = $facebook->api('/me/albums');

        foreach ($albums['data'] as $album) {
            $albumCover = $facebook->api("/{$album['id']}?fields=picture", "get");
            $p = $albumCover['picture']['data']['url'];
            echo "<br><b>Album Name:</b><a href='index.php?album_id=" . $album['id'] . "'>" . $album['name'] . "";
            echo "<img src='" . $p . "' /></a><input type='checkbox' name='albumphotodata[]' value='{$album['id']}' id='albumphotodata' /><br><br/>";
        }
        ?>
        <input type="submit" value="upload album" />
    </form>
    <?php
}
?>
</body>
</html>