<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS 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
 * (at your option) any later version.
 * 
 * easy-CMS 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 easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @package    Core
 * @subpackage Forms
 */
 
include_once('classes/core/versions/ContentVersionsManager.class.php');
 

class ContentForm extends ObjectForm {

	var $contentId;
	var $contentType;
	var $containerId;
  var $mailingListId;

	function ContentForm(&$site) {
		parent::ObjectForm($site);
		$this->containerId    = $this->site->getSubmittedVar('container_id', 0);
		$this->contentId		  = $this->site->getSubmittedVar('content_id', 0);
		$this->contentType    = $this->site->getSubmittedVar('type_name');
		$this->mailingListId  = $this->site->getSubmittedVar('list_id', 0);
		$mode                 = $this->site->getSubmittedVar('mode');
		$deleted              = $this->site->getSubmittedVar('deleted', false);

//todo : check if all needed vars are found : otherwise it should generate errors

		if (!($lid = $site->getSubmittedVar('lid', 0))) {
			if (!($lid = $site->getPersistentVar('lid', 0))) {
				$lid = $this->site->getDefaultLangId();
			}
		}

		$this->objectManager = &$this->site->getContentsManager();
		$this->action = $mode;

		if (!$this->contentId) {
			$containersManager = &$this->site->getContainersManager();
			if($this->containerId && ($container = &$containersManager->getContainer($this->containerId, false))) {
				$aclId = $container->getContainerAclId();
			}
			else {
				$aclId = $this->site->getRootAclId();
			}
			if(!($content = &$this->objectManager->getNewContent($this->contentType, $aclId))) {
				$this->debug("ContentForm::ContentForm(), error: unable to load specified content (id='".$this->contentId."'", 1);
        die();
      }
			$this->objectManager->addContent($content);
		}
		else {
			$content = &$this->objectManager->getContent($this->contentId, $this->contentType, LOAD_ALL_LANGUAGES, $deleted);
		}
		$this->object = &$content;
	}
	
	function processForm() {
		// check user rights on current object
		$aclManager = &$this->site->getACLManager();
		$user = &$this->site->getCurrentUser();
		$aclId = $this->object->getAclId();
		$acl = &$aclManager->getAcl($aclId, true, true);
		switch($this->action) {
			case ASK_MODIF_CONTENT:
				break;
			case MODIF_CONTENT:
				if($acl->userHasEditRight($user)) {
					$this->processFormModif();
				}
				break;
			case DELETE_CONTENT:
				if($acl->userHasRemoveRight($user)) {
					$this->processFormDelete();
				}
				break;
		}
	}

	/**
	 * @return boolean
	 */
	function processFormModif() {
    $langsArray = $this->objectManager->getLangsArray();

		if(!$this->contentId) {
    	// save content fields
			$this->objectManager->setContent($this->contentId, $this->contentType, $_POST);
			$contentId = $this->objectManager->storeContent($this->contentId, $this->contentType);

      if($this->mailingListId) {
  			$dbManipulator = &$this->objectManager->getDBManipulator();
        $dbManipulator->setRecord(TYPE_TABLES_PREFIX.$this->contentType, $contentId, array('mailing_list_id'=>$this->mailingListId));
      }

      if($this->containerId) {
        $containersManager = &$this->site->getContainersManager();
        $containersManager->addContent($this->containerId, $contentId);
      }
		}
		else {
			$contentVersionsManager = &$this->site->getContentVersionsManager();
			$user = &$this->site->getCurrentUser();
      // check for changes, if no change : stop here
			$newContent = &$this->objectManager->getNewContent($this->contentType);
			$this->objectManager->addContent($newContent);
			$this->objectManager->setContent(0, $this->contentType, $_POST);
      $noChange = true;
			foreach ($langsArray as $langId => $langName) {
        if($this->objectManager->compareContents($this->object, $newContent, $langId) != 0) {
          $noChange = false;
          break;
        }
	    }
      if($noChange) {
        return true;
      }

      // backup the current version to a new ContentVersion object
			if (!$contentVersionsManager->createNewContentVersion($this->contentId, $this->contentType, $user->getUserId())) {
				$this->debug("ContentForm::processFormModif(), error: unable to create new content version.", 3);
			}
			if (!$contentVersionsManager->save($this->contentId, $this->contentType)) {
				$this->debug("ContentForm::processFormModif(), error: unable to save new content version.", 3);
			}
	    // save new content fields values
			$this->objectManager->setContent($this->contentId, $this->contentType, $_POST);
			$this->objectManager->storeContent($this->contentId, $this->contentType);
		}
    // tell parents objects that changes have been done
		$pid = &$this->site->getCurrentPageId();
		if ($pid) {
			$pagesManager = &$this->site->getPagesManager();
			$pagesManager->setPageForCacheRenewalAndStore($pid);
		}
		return true;
	}

	/**
	 * @return boolean
	 */
	function processFormDelete() {
		return $this->objectManager->removeContent($this->object->getContentId(), $this->object->getContentTypeName());
	}
	
	function spreadAcl($aclId) {
		return $this->objectManager->updateContentACL($this->object->getContentId(), $this->object->getContentTypeName(), $aclId);
	}

}
?>