<?php

/**
 * API service to allow remote access to document information.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2015 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * contentApiV1 Class
 *
 * Class to implement the doc API endpoint.
 *
 * @todo check against the document permissions
 */
class contentApiV1 extends flApiService {

	/**
	 * The content object.
	 *
	 * @var cfContent
	 */
	protected $content;

	/**
	 * Construct the service object.
	 *
	 * @param flDb $db The database object.
	 * @param flApiServer $apiServer The API server object.
	 * @param flRequest $request The request object.
	 * @param flResponse $response The response object.
	 */
	function __construct($db, $apiServer, $request, $response) {
		parent::__construct($db, $apiServer, $request, $response);

		// Create the content object
		$this->content = new cfContent($this->db, clearFusionCMS::getSite());
	}

	/**
	 * Get the children of a given node.
	 *
	 * @throws flExceptionBadRequest
	 */
	function childrenGet() {
		// Get the parameters
		$id = $this->request->getQuery('id');
		$fields = $this->request->getQuery('fields', '["id"]');
		if($id === null || !($fields = json_decode($fields)))
			throw new flExceptionBadRequest();

		// Test access permission
		if(!$this->apiServer->hasPermission($this->content->getViewPermissions($id)))
			throw new flExceptionForbidden();

		// Ensure accessperm being returned
		if(!in_array('accessperm', $fields))
			$fields[] = 'accessperm';

		// Get the children checking permissions
		$results = array();
		$children = $this->content->getChildren($id, $fields);
		foreach($children as $c) {
			if(empty($c['accessperm']) || $this->apiServer->hasPermission($c['accessperm']))
				$results[] = $c;
		}

		// Send reply
		$this->response->setStatusCode(200)
			->setBody(json_encode($results))
			->sendResponse();
	}

	/**
	 * Get the custom fields for the given node.
	 *
	 * @throws flExceptionBadRequest
	 */
	function customfieldsGet() {
		// Get the parameters
		$id = $this->request->getQuery('id');
		if($id === null)
			throw new flExceptionBadRequest();

		// Test access permission
		if(!$this->apiServer->hasPermission($this->content->getViewPermissions($id)))
			throw new flExceptionForbidden();

		// Send reply
		$customfields = $this->content->getCustomFields($id);
		$this->response->setStatusCode(200)
			->setBody(json_encode($customfields))
			->sendResponse();
	}

	/**
	 * Get data from the content node.
	 *
	 * @throws flExceptionBadRequest
	 */
	function dataGet() {
		// Get the parameters
		$id = $this->request->getQuery('id');
		$fields = $this->request->getQuery('fields', '["id"]');
		if($id === null || !($fields = json_decode($fields)))
			if($id === null)
			throw new flExceptionBadRequest();

		// Test access permission
		if(!$this->apiServer->hasPermission($this->content->getViewPermissions($id)))
			throw new flExceptionForbidden();

		// Send reply
		$this->response->setStatusCode(200)
			->setBody(json_encode($this->content->getNodeData($id, $fields)))
			->sendResponse();
	}
}
