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

/**
 * We'll use Snoopy to fetch the RSS feed - this is a safer bet across various
 * server configurations than fopen
 */
require_once(ABSPATH . WPINC . '/class-snoopy.php');

/**
 * For manipulating and displaying Picasa photos in Shashin.
 *
 * @author Michael Toppa
 * @package Shashin
 * @subpackage Classes
 */
class ShashinParser {

    /**
     * The function is for parsing the Picasa RSS feed. It assumes an
     * array of items, with keys that may have a : dividing the names
     * of related items, and containing an array with a 'data' value
     * and a possible 'attrs' array
     *
     * @static
     * @access public
     * @param array $feed_content should be supplied by the readFeed function
     * @param array $ref_data maps your local data structure to the feed's structure; it can handle arrays nested two levels below $item
     * @param string $match_field allows you to limit the results of the parse to a specific item
     * @param string $match_value the value to look for in $match_field
     * @param string $key allows you to use an item from the feed as the key for the returned array
     * @return boolean|array the parsed contents of the feed, or false on failure
     */
    public static function parseFeed($album_data, $album_ref_data, $photo_ref_data) {
        $snoopy = new Snoopy();
        $snoopy->fetch($album_data['rss_url']);
        $xml = new SimpleXmlElement($snoopy->results);
        $namespaces = $xml->channel->getNameSpaces(true);
        $type_tag = $album_data['album_type'] . '_tag';

        // album meta-data
        foreach ($album_ref_data as $k=>$v) {
            if ($v['feed_params'][$type_tag]) {
                $current_tag = $v['feed_params'][$type_tag];
                switch (count($current_tag)) {
                case 1:
                    $album_data[$k] = strval($xml->channel->{$current_tag[0]});
                    break;
                case 2:
                    $album_data[$k] = strval($xml->channel->{$current_tag[0]}->{$current_tag[1]});
                    break;
                case 3:
                    $album_data[$k] = strval($xml->channel->{$current_tag[0]}->{$current_tag[1]}->{$current_tag[2]});
                    break;
                }
            }
        }

        var_dump($album_data);
        exit;

        // photo data
        $album_data['photos'] = array();
        //foreach ($xml->channel->item as $item) {
        //}
    }

    public static function getTagData($xml, $namespaces, $params, $album_type) {

        $count = count($params[$album_type.'_tag']);
        $current_tag = array_shift($params[$album_type.'_tag']);

        if ($params[$album_type.'_ns']) {
            $ns_name = array_shift($params[$album_type.'_ns']);

            if ($current_ns) {
                $current_ns = $current_ns->children($namespaces[$ns_name]);
            }

            else {
                $current_ns = $xml->channel->children($namespaces[$ns_name]);
            }

            if ($count > 1) {
                ShashinParser::getTagData($xml, $namespaces, $params, $album_type, $current_ns, $current_tag);
            }

            return strval($current_ns->$current_tag);
        }

        if ($tag_path) {
            $tag_path .= '->' . $current_tag;
        }

        else {
            $tag_path = '$xml->channel->' . $current_tag;
        }

        if ($count > 1) {
            ShashinParser::getTagData($xml, $namespaces, $params, $album_type, null, $tag_path);
        }

// need to figure out why not returning image->url for album cover photo
        eval("\$return=" . $tag_path . ";" );
        return strval($return);
    }
}
?>