<?php

/**********************************************************************
 * Copyright (C) 2006 The Wikigraphe Team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * See also http://www.fsf.org
 *********************************************************************/
require_once('lib/validation.php');
require_once('model/Rectangle.inc.php');
include_once('db_connect.php');

error_reporting(E_ALL);
/* This is the control script that is run in order to save the links. */
if (isset($_POST['rect_save'])) {
  save_rectangles();
}
/* XXX: we assume the save was successful and output no feedback */
$_GET['title'] = $_POST['title'];
$_GET['mode'] = 'view'; /* change the mode so that we end up in view after save */

/** Unserializes a rectangle written in a hidden field. */
function &unserializeRectangle($str) {
  $str = str_replace('px','',$str);
  $arr = explode(',',$str);
  $rect =& new Rectangle($arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5]);
  return $rect;
}

function save_rectangles() {
	global $_POST;
	global $DB;
	
	 /**
       * Insert all zones into DB.
       */
      list($width, $height, $type, $attr) = getimagesize($_POST['image_src']);

      //$map =& new ImageMap($width, $height);

      $from = validate_name($_POST['title']);
      $node = DB_DataObject_Node::staticGet('title', $from);
      
      // Find the rlid.
      $result = $DB->query("SELECT MAX(rlid) as max FROM link WHERE nid = {$node->nid}");
      if (PEAR::isError($result)) {
        trigger_error($result->getUserInfo(), E_USER_ERROR);
      }
      $row =& $result->fetchRow();
      if ($row) {
      	$rlid = $row['max'] + 1;
      } else {
      	$rlid = 1;
      }
      
      foreach ($_POST as $k => $val) {
        if (substr($k, 0, 5) == "save_") {
        	
          $r =& unserializeRectangle($val);
          
          $lid = substr($k, 5);
          
          $link = new DB_DataObject_Link;
          $link->lid = $lid;
          $link->rlid = $rlid;
          $link->nid = $node->nid;
          $link->to_title = $r->getLink(RECTANGLE_RAW_LINK);
          
          $result = $link->insert();

          if (PEAR::isError($result)) {
          	trigger_error($result->getMessage(), E_USER_ERROR);
          }
        }
      }

//      /** Perform sql query */
//      $validated_title = validate_name($_POST['title']);
//      $sql = sprintf("SELECT COUNT(*) FROM `node` WHERE `title`='%s'", $validated_title);
//      $result = $DB->getOne($sql);
//
//      if ($result == 0) {
//        $sql = sprintf("INSERT INTO `%s` SET title='%s', map='%s'", 'node', $validated_title, mysql_escape_string($map->serialize()));
//      } else {
//        $sql = sprintf("UPDATE `%s` SET map='%s' WHERE title='%s'", 'node', mysql_escape_string($map->serialize()), $validated_title);
//      }
//      $result = $DB->query($sql);
}

?>