<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* PHP versions 4 and 5
*
* @category  Web Services
* @package   Gnavi
* @author    Kuroneko Square <http://www.kuroneko-square.net/>
* @copyright 2009 Kuroneko Square
* @license   http://www.php.net/license/3_0.txt  PHP License 3.0
* @see       http://www.kuroneko-square.net/services/gnavi/
* @filesource
*/
//-------------
define('GNAVI_ID''');        //Gnavi access key ID
//-------------
//require_once('api-config.php');
define('FEED2ONE_DIR','');        //path to feed2one.php
define('FEEDCREATOR_DIR','');    //path to feedcreator.class.php

/**
 * Class for accessing and retrieving information from Gnavi's Web service via Services_Gnavi.class and feedcreator.class.
 *
 * @package Gnavi2Feed
 * @author  kuroneko-square <http://www.kuroneko-square.net/>
 * @access  public
 * @version Release: 0.1.0
 * @uses    PEAR
 * @uses    HTTP_Request
 * @uses    XML_Unserializer
 * @uses    Services_Gnavi
 */
//-------------
/*
 * 0.12 function renamed
 * 0.11 bug fix (null data)
 * 0.10 stable
 * 0.05 output with feed2one
 * 0.04 bug fix
 * 0.03 Add api etc...
 * 0.02 Optimized
 * 0.01 Release
 */
//-------------
// Error report
#error_reporting(E_ERROR);

class gnavi2feed {
    var 
$outputfeed;
    var 
$max_age;
    var 
$format;
    var 
$entries;
    var 
$items;
    var 
$sortorder;
//------------
function gnavi2feed() {
    
//default
    
$this->output_as_func false;    //Return the data(act as function)
    
$this->charset 'UTF-8';        //character code (ex. SJIS,EUC-JP)
    
$this->cache false;            //cache enabled or disabled
    
$this->max_age 1800;            //Maximum cache available time(sec)
    
$this->entries 30;            //Number of outputting entries
    
$this->format 'rss2';            //Output feed format [RSS 1.0:rss10 / RSS 2.0:rss20 / atom 0.3:atom03]
    #$this->tz = '+900';             //timezone of gnavi.co.jp @ Asia/Tokyo
    
    //Publisher options
    
$this->feed['title'] = 'GNaviFeed';
    
$this->feed['link'] = "http://www.kuroneko-square.net/services/gnavi/";
    
$this->feed['feedurl'] = "http://www.kuroneko-square.net/services/gnavi/feed";
    
$this->feed['description'] = '[Powered by ぐるなび]';
    
$this->feed['language'] = 'ja';
    
$this->feed['creator'] = 'kurosquare';
    
    
//image options
    
$this->feed['image']['title'] = 'グルメ情報検索サイト ぐるなび';
    
$this->feed['image']['link'] = 'http://www.gnavi.co.jp/';
    
$this->feed['image']['url'] = 'http://apicache.gnavi.co.jp/image/rest/b/api_90_35.gif';
    
$this->feed['image']['description'] = 'グルメ情報検索サイト ぐるなび';
    
$this->feed['image']['width'] = '90';
    
$this->feed['image']['height'] = '35';
    
    
//filter options
    
$this->feed['unwantedWords'] = '/^((AD|PR):|[(AD|PR)]) /i';  //any of these unwanted words
}
//------------
function main(){
    require_once(
'Services/Gnavi.php');
    
/**
     * Send Gnavi operation
     */
    
    
$Gnavi = new Services_Gnavi(GNAVI_ID);
    
    
//execute
    
$options $this->validate_options($this->Gnavi_api['options']);
    
$Gnavi->searchRestaurant($options);
    
    
//get array
    
$Result $this->parse_Services_Gnavi($Gnavi->getResult());
    
    
//output
    
$this->output_feed($Result,$this->format);
}
//------------
function parse_Services_Gnavi($Services_Gnavi_ResultData){
    
//single data
    
if($Services_Gnavi_ResultData['total_hit_count']==1|$Services_Gnavi_ResultData['hit_per_page']==1){
        
$arrayItems = array($Services_Gnavi_ResultData['rest']);
    
//multiple data
    
}elseif($Services_Gnavi_ResultData['total_hit_count']>=2&&$Services_Gnavi_ResultData['hit_per_page']>=2){
        
$arrayItems $Services_Gnavi_ResultData['rest'];
    }else{
        
$arrayItems NULL;
    }
    return 
$arrayItems;
}
//----------
private function output_feed($arrayItems,$format){
    if(
is_array($arrayItems)){
        
//message
        
$message_empty_date 'A refix date is unacquirable.';
        
$this->feed['title'] = 'ぐるなび - 検索結果 (検索キー: '.$this->Gnavi_api['options']['freeword'].')';
        
$this->feed['description'] = 'ぐるなびの情報から、ご希望の条件に合ったリストを提供します。 [Powered by ぐるなび]';
        foreach (
$arrayItems as $item) {
            if(!
preg_match($this->feed['unwantedWords'],$item['ProductName'])){ //remove unwanted words
                
$items[] = array(
                    
'date' => !empty($item['update_date'])? $this->parse_dtf(htmlspecialchars($item['update_date'])):$message_empty_date,
                    
'creator' => $item['id'],
                    
'title' => $item['name'],
                    
'link' => $item['url'],
                    
'description' => '<img src="'.$item['image_url']['shop_image1'].'" alt="" />'.$item['pr']['pr_short'],
                    
'content_encoded' => '<p><img src="'.$item['image_url']['shop_image1'].'" alt="" /></p><p>'.$item['pr']['pr_long'].'</p><p><img src="'.$item['image_url']['qrcode'].'" alt="" /></p>',
                );
            }
        }
    }
    
//output with feed2one
    
require_once(FEED2ONE_DIR.'/feed2one.class.php');
    
$ObjF21 = new feed2one();
    
$ObjF21->feed2one $this->feed;
    
$ObjF21->format $this->format;
    
$ObjF21->entries $this->entries;
    echo 
$ObjF21->output_feed($items);
}
//------------
// validate parameter
function validate_options($options = array()){
    
$parameter_available = array(
        
'api',
        
'id',
        
'name',
        
'name_kana',
        
'tel',
        
'address',
        
'area',
        
'pref',
        
'category_l',
        
'category_s',
        
'input_coordinates_mode',
        
'equipment',
        
'coordinates_mode',
        
'latitude',
        
'input_coordinates_mode',
        
'longitude',
        
'input_coordinates_mode',
        
'range',
        
'sort',
        
'offset',
        
'hit_per_page',
        
'integer',
        
'offset_page',
        
'freeword',
        
'freeword_condition'
    
);
    if(
is_array($options)){
        foreach(
$options as $key => $value){
            if(!
in_array($key,$parameter_available)|empty($value)){
                unset(
$options[$key]);
            }
        }
    }
    
//when not freeword, remove 'word_condition'
    
if (empty($options['freeword'])) {
        unset(
$options['freeword_condition']);
    }
    return 
$options;
}
//------------
function parse_dtf($date_str) {
    
# regex to match jpdtf
    //seperaters are「/」「-」「.」「年」「月」「日」,and  multibyte is avilable
    
$pat "/([0-90-9]{4})[\/\-\./−.年]{1}([0-90-9]{1,2})[\/\-\./−.月]{1}([0-90-9]{1,2})[\/\-\./−.日]{1}/u";
    if (
preg_match($pat$date_str$match)) {
        list(
$year$month$day$hours$minutes$seconds$tz) =
        array(
$match[1], sprintf("%02d",$match[2]), sprintf("%02d",$match[3]), '00''00''00'$tz);
        
$date_str $year '-' $month '-' $day 'T' $hours ':' $minutes ':' $seconds $this->tz;
    }
    return 
$date_str;
}
//------------
}
/*
$debug=1;
if($debug){
    //Set Feed
    $ObjF = new gnavi2feed();
    $ObjF->gnavi2feed;
    
    //Set Feed option
    $ObjF->entries = is_numeric($_GET['entries'])? $_GET['entries']:10;
    $ObjF->format = isset($_GET['feed'])? htmlspecialchars($_GET['feed']):NULL;
    
    //Select Service
    #$ObjF->api = 'searchRestaurant';
    
    //Set Service option
    foreach($_GET as $key => $value){
        $ObjF->Gnavi_api['options'][$key] = !empty($value)? htmlspecialchars($value):NULL;
    }
    $ObjF->main();
}*/
?>