<?php
/* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/markdown.php");

class Payment {
  public $title;
  public $type;
  public $paypalkey;
  
  public function write($fp)
  {
    /*
    fwrite($fp, "    <payment title=\"" .
	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "             type=\"" . $this->type . "\"\n");
    fwrite($fp, "             paypalkey=\"" . $this->paypalkey . "\">\n");
    fwrite($fp, "    </payment>\n");
    */
  }
  
  public function show()
  {
    $str = "";

		$str .= "      <div class=\"payment\">\n";
    $str .= "        <div class=\"payment_title\">".$this->title."</div>\n";
    $str .= "        <form  class=\"payment_button\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n";
		$str .= "          <div>\n";
    $str .= "            <input type=\"hidden\" name=\"cmd\" value=\"_s-xclick\"/>\n";
    $str .= "            <input type=\"hidden\" name=\"encrypted\" value=\"".$this->paypalkey."\"/>\n";
    $str .= "            <input class=\"payment_button_image\" value=\"\" type=\"submit\" name=\"submit\"\n";
		$str .= "                   alt=\"Make payments with PayPal - it's fast, free and secure!\"/>\n";
		$str .= "          </div>\n";
    $str .= "        </form>\n";
		$str .= "      </div>\n";

    return $str;
  }
	
  public function Payment($title, $type, $paypalkey)
  {
    $this->title = $title;
    $this->type = $type;
    $this->paypalkey = $paypalkey;
  }
}

class ShopItem {
	public $id;
  public $title;
  public $description;
  public $image;
	public $url;
  public $payments = array();

  public function write($fp)
  {
    /*
    fwrite($fp, "  <shopitem title=\"" .
	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        description=\"" .
	   htmlspecialchars($this->description, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        image=\"" . $this->image . "\">\n");
    
    fwrite($fp, "        type=\"" . $this->type . "\"\n");
    fwrite($fp, "        paypalkey=\"" . $this->paypalkey . "\">\n");
    fwrite($fp, "  </shopitem>\n");
    */
  }
  
  public function show()
  {
    $str = "";
    
    $str .= "  <div class=\"shopitem\">\n";
    $str .= "    <div class=\"shopitem_title\">".$this->title."</div>\n";
		if($this->url != "") $str .= "<a href=\"".$this->url."\">\n";
    $str .= "    <img class=\"shopitem_image\" alt=\"".$this->title."\" src=\"".$this->image."\"/>\n";
		if($this->url != "") $str .= "</a>\n";
    $str .= "    <div class=\"shopitem_description\">".$this->description."</div>\n";

    $str .= "    <div class=\"payments\">\n";
    foreach($this->payments as $p) {
      $str .= $p->show();
    }
    $str .= "    </div>\n";
    $str .= "  </div>\n";

    return $str;
  }

  public function addPayment($payment)
  {
    $key = $payment->title;
    $this->payments[$key] = $payment;
  }
	
  public function ShopItem($id, $title, $description, $image, $url)
  {
		$this->id = $id;
    $this->title = $title;
    $this->description = $description;
    $this->image = $image;
		$this->url = $url;
  }
}

class Shop {

  private $file;
  private $shopitems = array();

  // Admin config
  public $admin_title = "Shop";
  public $admin_submodules = array("Add Item" => "add",
				   "Edit Item" => "edit",
				   "Delete Item" => "delete");

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "add":
      break;
    case "edit":
      break;
    case "delete":
      break;
    }
  }

  public function run($params)
  {
    global $GLOBALS;

    $str = "<div class=\"shop\">\n";
    
    //foreach($params as $param => $value) {}
    
    if($this->shopitems) {
      foreach($this->shopitems as $shopitem) {
	$str .= $shopitem->show();
      }
    }
    
    $str .= "</div>\n";
    
    return $str;
  }

  public function add($shopitem) {
    $key = $shopitem->id;
    $this->shopitems[$key] = $shopitem;
  }
	
  public function write()
  {
    /*
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<discography>\n");
    foreach($this->discs as $disc) {
			$disc->write($fp);
    }
    fwrite($fp, "</discography>\n");

    fclose($fp);
    */
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $shopitems = $dom->getElementsByTagName('shopitem');

    if($shopitems) {
      foreach($shopitems as $i) {
				$shopitem = new ShopItem($i->getAttribute('id'),
																 $i->getAttribute('title'),
																 $i->getAttribute('description'),
																 $i->getAttribute('image'),
																 $i->getAttribute('url'));
				
				$payments = $i->getElementsByTagName('payment');
				if($payments) {
					foreach($payments as $p) {
						$payment = new Payment($p->getAttribute('title'),
																	 $p->getAttribute('type'),
																	 $p->getAttribute('paypalkey'));
						$shopitem->addPayment($payment);
					}
				}
				
				$this->add($shopitem);
      }
    }
		ksort($this->shopitems);
  }

  public function Shop($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }

}

function shop_init()
{
  global $DATA_DIR;
  return new Shop($DATA_DIR . "/shop.xml");
}

?>