<?php
/**
 * ShashinAlbum class file.
 *
 * This file is part of Shashin. Please see the Shashin.phl file for
 * copyright and license information.
 *
 * @author Michael Toppa
 * @version 2.4
 * @package Shashin
 * @subpackage Classes
 */

/**
 * Instantiate this class and use its methods to manipulate Picasa albums in
 * Shashin. Also has a static population method, getAlbums() and a static
 * method for generating markup, getAlbumMarkup().
 *
 * @author Michael Toppa
 * @package Shashin
 * @subpackage Classes
 */
class ShashinAlbum {
    public $ref_data = array(
        'album_key' => array(
            'col_params' => array(
                'type' => 'smallint unsigned',
                'not_null' => true,
                'primary_key' => true,
                'other' => 'AUTO_INCREMENT')),
        'album_type' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '20',
                'not_null' => true)),
        'rss_url' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255',
                'not_null' => true),
            'input_type' => 'text',
            'input_size' => 100),
        'user' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255',
                'not_null' => true),
            'feed_params' => array(
                'picasa_ns' => array('gphoto'),
                'picasa_tag' => array('user'))),
        'name' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255',
                'not_null' => true),
            'feed_params' => array(
                'picasa_ns' => array('gphoto'),
                'picasa_tag' => array('nickname'))),
        'link_url' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255',
                'not_null' => true),
            'feed_params' => array(
                'picasa_tag' => array('link'),
                'flickr_tag' => array('link'),
                'twitpic_tag' => array('link'))),
        'title' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255',
                'not_null' => true),
            'feed_params' => array(
                'picasa_tag' => array('title'),
                'flickr_tag' => array('title'),
                'twitpic_tag' => array('title'))),
        'description' => array(
            'col_params' => array(
                'type' => 'text'),
            'feed_params' => array(
                'picasa_tag' => array('description'),
                'flickr_tag' => array('description'),
                'twitpic_tag' => array('description'))),
        'location' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255'),
            'feed_params' => array(
                'picasa_ns' => array('gphoto'),
                'picasa_tag' => array('location'))),
        'cover_photo_url' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '255'),
            'feed_params' => array(
                'picasa_tag' => array('image', 'url'),
                'flickr_tag' => array('image', 'url'))),
        'last_updated' => array(
            'col_params' => array(
                'type' => 'int unsigned')),
        'photo_count' => array(
            'col_params' => array(
                'type' => 'smallint unsigned',
                'not_null' => true),
            'feed_params' => array(
                'picasa_ns' => array('gphoto'),
                'picasa_tag' => array('numphotos'))),
        'pub_date' => array(
            'col_params' => array(
                'type' => 'int unsigned',
                'not_null' => true),
            'feed_params' => array(
                'picasa_ns' => array('gphoto'),
                'picasa_tag' => array('timestamp'),
                'flickr_tag' => array('pubDate'),
                'twitpic_tag' => array('pubDate'))),
        'geo_pos' => array(
            'col_params' => array(
                'type' => 'varchar',
                'length' => '25'),
            'feed_params' => array(
                'picasa_ns' => array('georss', 'gml', 'gml'),
                'picasa_tag' => array('where', 'Point', 'pos'))),
        'include_in_random' => array(
            'col_params' => array(
                'type' => 'char',
                'length' => '1',
                'other' => "default 'Y'"),
            'input_type' => 'radio',
            'input_subgroup' => array('Y' => SHASHIN_YES, 'N' => SHASHIN_NO)),
        );

    public $data;
    public $shashin;

    public function __construct(&$shashin) {
        if (!$shashin instanceof Shashin) {
            throw new Exception(__("You must pass a Shashin object", 'shashin'));
        }

        $this->shashin = $shashin;
    }

    /**
     * Inserts or updates a Picasa album in Shashin.
     *
     * Reads the user's Picasa feed of all albums and retieves data for the
     * specified album. Does not insert/update album photos. On success, calls
     * getAlbum() to (re)populate the album object.
     *
     * @access public
     * @param string $user_name The Picasa user_name of the album's owner
     * @param array $album_identifier A key-value pair (e.g. 'album_id' => 37)
     * @param array $local_data (optional) A hash of local album data (data not from a feed)
     * @uses ToppaWPFunctions::readFeed()
     * @uses ToppaWPFunctions::parseFeed()
     * @uses ToppaWPFunctions::sqlUpdate()
     * @uses ToppaWPFunctions::sqlInsert()
     * @uses ShashinAlbum::getAlbum()
     * @return array 0: true on success, false on failure; 1: message; 2: true if SQL error
     */
    public function addAlbum($album_data) {
        require_once(SHASHIN_DIR . '/ShashinParser.phl');

        if (!is_array($album_data)) {
            throw new Exception(__("album_data argument must be an array", 'shashin'));
        }

        if (!array_key_exists('rss_url', $album_data)) {
            throw new Exception(__("rss_url is required for adding an album", 'shashin'));
        }

        if (ShashinAlbum::albumExists(null, $album_data['rss_url'])) {
            throw new Exception(__("This album was previously added to Shashin. Please click its 'sync' icon to update it.", 'shashin'));
        }

        switch ($album_data['rss_url']) {
        case (strpos($album_data['rss_url'], 'picasaweb') !== false):
            $album_data['album_type'] = 'picasa';
            // replace 'base' feed with 'api' feed
            $album_data['rss_url'] = str_replace('/base/', '/api/', $album_data['rss_url']);
            break;
        case (strpos($album_data['rss_url'], 'flickr') !== false):
            $album_data['album_type'] = 'flickr';
            // make sure RSS 2 feed arg is included
            if (strpos($album_data['rss_url'], '&format=rss_2') === false) {
                $album_data['rss_url'] .= '&format=rss_200';
            }
            // get the user id from the rss url
            //preg_match("/\/photos\/([\w\@]+)\//", $album_);
            break;
        case (strpos($album_data['rss_url'], 'twitpic') !== false):
            $album_data['album_type'] = 'twitpic';
            break;
        default:
            throw new Exception(__("Invalid RSS feed URL", 'shashin'));
        }

        $photo = new ShashinPhoto($this->shashin);
        $feed_data = ShashinParser::parseFeed($album_data, $this->ref_data, $photo->ref_data);
        $album_data = array_merge($album_data, $feed_data);
/*
        // time-related updates
        $album_data['pub_date'] = strtotime($album_data['pub_date']);
        $album_data['last_updated'] = time();

        // if the album exists, update it
        if ($exists[0] === true) {
            $sql_result = ToppaWPFunctions::sqlUpdate(SHASHIN_ALBUM_TABLE, $album_data, $album_identifier);
        }

        // if the album does not exist, insert it
        else {
            $sql_result = ToppaWPFunctions::sqlInsert(SHASHIN_ALBUM_TABLE, $album_data);
        }

        if ($sql_result === false) {
            return array(false, __("ShashinAlbum::setAlbum - Failed to insert/update database record for album. SQL Error:", SHASHIN_L10N_NAME), true);
        }

        // update the object data
        $this->data = $album_data;

        return array(true, __("Album metadata synchronized", SHASHIN_L10N_NAME)); */
    }

    public static function albumExists($album_key = null, $rss_url = null) {
        if (!(is_numeric($album_key) || is_string($rss_url))) {
            throw new Exception(__("album_key or rss_url argument is required", 'shashin'));
        }

        if ($album_key) {
            $where = array('album_key' => $album_key);

        }

        else {
            $where = array('rss_url' => $rss_url);
        }

        return ToppaWPFunctions::sqlSelect(SHASHIN_ALBUM_TABLE, 'album_key', $where, null, 'get_var');
    }
}
?>
