<commit_before><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2013 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 *
 * @author      JA
 */

class Webservice_RestController extends Pimcore_Controller_Action_Webservice {

    /**
     * the webservice
     * @var
     */
    private $service;

    /**
     * The output encoder (e.g. json)
     * @var
     */
    private $encoder;

    public function init() {
        parent::init();
        $this->disableViewAutoRender();
        $this->service = new Webservice_Service();
        // initialize json encoder by default, maybe support xml in the near future
        $this->encoder = new Webservice_JsonEncoder();
    }


    /** end point for object related data.
     * - get object by id
     *      GET http://[YOUR-DOMAIN]/webservice/rest/object/id/1281?apikey=[API-KEY]
     *      returns json-encoded object data.
     * - delete object by id
     *      DELETE http://[YOUR-DOMAIN]/webservice/rest/object/id/1281?apikey=[API-KEY]
     *      returns json encoded success value
     * - create object
     *      PUT or POST http://[YOUR-DOMAIN]/webservice/rest/object?apikey=[API-KEY]
     *      body: json-encoded object data in the same format as returned by get object by id
     *              but with missing id field or id set to 0
     *      returns json encoded object id
     * - update object
     *      PUT or POST http://[YOUR-DOMAIN]/webservice/rest/object?apikey=[API-KEY]
     *      body: same as for create object but with object id
     *      returns json encoded success value
     * @throws Exception
     */
    public function objectAction() {

        $id = $this->getParam("id");
        $success = false;

        try {
            if ($this->isGet()) {
                if ($id) {
                    $object = Object_Abstract::getById($id);
                    if ($object instanceof Object_Folder) {
                        $object = $this->service->getObjectFolderById($id);
                    } else {
                        $object = $this->service->getObjectConcreteById($id);
                    }

                    $this->encoder->encode($object);
                    return;
                }
            } else if ($this->isDelete()) {
                $success = $this->service->deleteObject($id);
                $this->encoder->encode(array("success" => $success));
                return;
            } else if ($this->isPost() || $this->isPut()) {
                $data = file_get_contents("php://input");
                $data = Zend_Json::decode($data);

                $type = $data["type"];
                $id = null;

                if ($data["id"]) {
                    $isUpdate = true;
                    if ($type == "folder") {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Folder_In", $data);
                        $success = $this->service->updateObjectFolder($wsData);
                    } else {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Concrete_In", $data);
                        $success = $this->service->updateObjectConcrete($wsData);
                    }
                } else {
                    if ($type == "folder") {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Folder_In", $data);
                        $id = $this->service->createObjectFolder($wsData);
                    } else {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Concrete_In", $data);
                        $id = $this->service->createObjectConcrete($wsData);
                    }
                }

                if (!$isUpdate) {
                    $success = $id != null;
                }

                if ($success && !$isUpdate) {
                    $this->encoder->encode(array("success" => $success, "id" => $id));
                } else {
                    $this->encoder->encode(array("success" => $success));
                }
                return;

            }
        } catch (Exception $e) {
            Logger::error($e);
            $this->encoder->encode(array("success" => false, "message" => $e));
        }

        throw new Exception("not implemented");
    }

    /** end point for object metadata
     * GET http://[YOUR-DOMAIN]/webservice/rest/object-meta/id/1281?apikey=[API-KEY]
     *      returns the json-encoded class definition for the given object
     *
     */
    public function objectMetaAction() {

        $id = $this->getParam("id");
        $success = false;<commit_msg> consolidated response<commit_after><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2013 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 *
 * @author      JA
 */

class Webservice_RestController extends Pimcore_Controller_Action_Webservice {

    /**
     * the webservice
     * @var
     */
    private $service;

    /**
     * The output encoder (e.g. json)
     * @var
     */
    private $encoder;

    public function init() {
        parent::init();
        $this->disableViewAutoRender();
        $this->service = new Webservice_Service();
        // initialize json encoder by default, maybe support xml in the near future
        $this->encoder = new Webservice_JsonEncoder();
    }


    /** end point for object related data.
     * - get object by id
     *      GET http://[YOUR-DOMAIN]/webservice/rest/object/id/1281?apikey=[API-KEY]
     *      returns json-encoded object data.
     * - delete object by id
     *      DELETE http://[YOUR-DOMAIN]/webservice/rest/object/id/1281?apikey=[API-KEY]
     *      returns json encoded success value
     * - create object
     *      PUT or POST http://[YOUR-DOMAIN]/webservice/rest/object?apikey=[API-KEY]
     *      body: json-encoded object data in the same format as returned by get object by id
     *              but with missing id field or id set to 0
     *      returns json encoded object id
     * - update object
     *      PUT or POST http://[YOUR-DOMAIN]/webservice/rest/object?apikey=[API-KEY]
     *      body: same as for create object but with object id
     *      returns json encoded success value
     * @throws Exception
     */
    public function objectAction() {

        $id = $this->getParam("id");
        $success = false;

        try {
            if ($this->isGet()) {
                if ($id) {
                    $object = Object_Abstract::getById($id);
                    if ($object instanceof Object_Folder) {
                        $object = $this->service->getObjectFolderById($id);
                    } else {
                        $object = $this->service->getObjectConcreteById($id);
                    }

                    $this->encoder->encode($object);
                    return;
                }
            } else if ($this->isDelete()) {
                $success = $this->service->deleteObject($id);
                $this->encoder->encode(array("success" => $success));
                return;
            } else if ($this->isPost() || $this->isPut()) {
                $data = file_get_contents("php://input");
                $data = Zend_Json::decode($data);

                $type = $data["type"];
                $id = null;

                if ($data["id"]) {
                    $isUpdate = true;
                    if ($type == "folder") {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Folder_In", $data);
                        $success = $this->service->updateObjectFolder($wsData);
                    } else {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Concrete_In", $data);
                        $success = $this->service->updateObjectConcrete($wsData);
                    }
                } else {
                    if ($type == "folder") {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Folder_In", $data);
                        $id = $this->service->createObjectFolder($wsData);
                    } else {
                        $wsData = self::fillWebserviceData("Webservice_Data_Object_Concrete_In", $data);
                        $id = $this->service->createObjectConcrete($wsData);
                    }
                }

                if (!$isUpdate) {
                    $success = $id != null;
                }

                if ($success && !$isUpdate) {
                    $this->encoder->encode(array("success" => $success, "id" => $id));
                } else {
                    $this->encoder->encode(array("success" => $success));
                }
                return;

            }
        } catch (Exception $e) {
            Logger::error($e);
            $this->encoder->encode(array("success" => false, "msg" => $e));
        }

        throw new Exception("not implemented");
    }

    /** end point for object metadata
     * GET http://[YOUR-DOMAIN]/webservice/rest/object-meta/id/1281?apikey=[API-KEY]
     *      returns the json-encoded class definition for the given object
     *
     */
    public function objectMetaAction() {

        $id = $this->getParam("id");
        $success = false;
{'fraction_non_alphanumeric': 0.1271195045182252, 'fraction_numerical': 0.0042643923240938165, 'mean_word_length': 1.6372155287817938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3399193', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2542, 'n_words': 613}