public static loadAllDocumentFields ( $doc ) : mixed | ||
$doc | ||
리턴 | mixed |
/** * @param $id * @throws \Exception */ public function getDocumentSnippetById($id) { try { $snippet = Document::getById($id); if ($snippet instanceof Document\Snippet) { // load all data (eg. href, snippet, ... which are lazy loaded) Document\Service::loadAllDocumentFields($snippet); $className = Webservice\Data\Mapper::findWebserviceClass($snippet, "out"); $apiSnippet = Webservice\Data\Mapper::map($snippet, $className, "out"); return $apiSnippet; } throw new \Exception("Document Snippet with given ID (" . $id . ") does not exist."); } catch (\Exception $e) { \Logger::error($e); throw $e; } }
/** * @static * @param ElementInterface $element * @return ElementInterface */ public static function loadAllFields(ElementInterface $element) { if ($element instanceof Document) { Document\Service::loadAllDocumentFields($element); } else { if ($element instanceof Object\Concrete) { Object\Service::loadAllObjectFields($element); } else { if ($element instanceof Asset) { Asset\Service::loadAllFields($element); } } } return $element; }
/** end point for document related data. * - get document by id * GET http://[YOUR-DOMAIN]/webservice/rest/document/id/1281?apikey=[API-KEY] * returns json-encoded document data. * - delete document by id * DELETE http://[YOUR-DOMAIN]/webservice/rest/document/id/1281?apikey=[API-KEY] * returns json encoded success value * - create document * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/document?apikey=[API-KEY] * body: json-encoded document data in the same format as returned by get document by id * but with missing id field or id set to 0 * returns json encoded document id * - update document * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/document?apikey=[API-KEY] * body: same as for create document but with object id * returns json encoded success value * @throws \Exception */ public function documentAction() { $id = $this->getParam("id"); $success = false; try { if ($this->isGet()) { $doc = Document::getById($id); if (!$doc) { $this->encoder->encode(array("success" => false, "msg" => "Document does not exist", "code" => self::ELEMENT_DOES_NOT_EXIST)); return; } $this->checkPermission($doc, "get"); if ($doc) { $type = $doc->getType(); $getter = "getDocument" . ucfirst($type) . "ById"; if (method_exists($this->service, $getter)) { $object = $this->service->{$getter}($id); } else { // check if the getter is implemented by a plugin $class = "\\Pimcore\\Model\\Webservice\\Data\\Document\\" . ucfirst($type) . "\\Out"; if (Tool::classExists($class)) { Document\Service::loadAllDocumentFields($doc); $object = Webservice\Data\Mapper::map($doc, $class, "out"); } else { throw new \Exception("unknown type"); } } } if (!$object) { throw new \Exception("could not find document"); } @$this->encoder->encode(array("success" => true, "data" => $object)); return; } else { if ($this->isDelete()) { $doc = Document::getById($id); if ($doc) { $this->checkPermission($doc, "delete"); } $success = $this->service->deleteDocument($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; $typeUpper = ucfirst($type); $className = "\\Pimcore\\Model\\Webservice\\Data\\Document\\" . $typeUpper . "\\In"; if ($data["id"]) { $doc = Document::getById($data["id"]); if ($doc) { $this->checkPermission($doc, "update"); } $isUpdate = true; $setter = "updateDocument" . $typeUpper; if (!method_exists($this->service, $setter)) { throw new \Exception("method does not exist " . $setter); } $wsData = self::fillWebserviceData($className, $data); $success = $this->service->{$setter}($wsData); } else { $setter = "createDocument" . $typeUpper; if (!method_exists($this->service, $setter)) { throw new \Exception("method does not exist " . $setter); } $wsData = self::fillWebserviceData($className, $data); $doc = new Document(); $doc->setId($wsData->parentId); $this->checkPermission($doc, "create"); $id = $this->service->{$setter}($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) { $this->encoder->encode(array("success" => false, "msg" => (string) $e)); } $this->encoder->encode(array("success" => false)); }
/** end point for document related data. * - get document by id * GET http://[YOUR-DOMAIN]/webservice/rest/document/id/1281?apikey=[API-KEY] * returns json-encoded document data. * - delete document by id * DELETE http://[YOUR-DOMAIN]/webservice/rest/document/id/1281?apikey=[API-KEY] * returns json encoded success value * - create document * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/document?apikey=[API-KEY] * body: json-encoded document data in the same format as returned by get document by id * but with missing id field or id set to 0 * returns json encoded document id * - update document * PUT or POST http://[YOUR-DOMAIN]/webservice/rest/document?apikey=[API-KEY] * body: same as for create document but with object id * returns json encoded success value * @throws \Exception */ public function documentAction() { $id = $this->getParam("id"); $success = false; try { if ($this->isGet()) { /** * @api {get} /document Get document * @apiName getDocument * @apiGroup Document * @apiSampleRequest off * @apiParam {int} id The id of document you search * @apiParamExample {json} Request-Example: * { * "id": 4711 * "apikey": '2132sdf2321rwefdcvvce22' * } * @apiParam {string} apikey your access token * @apiSuccess {boolean} success Returns true if finished successfully * @apiSuccessExample {json} Succes-Response: * HTTP/1.1 200 OK * { * "success":true * } * @apiError {boolean} success Returns false if failed * @apiErrorExample {json} Error-Response: * {"success":false,"msg":"exception 'Exception' with message 'Document with given ID (712131243) does not exist.'"} */ $doc = Document::getById($id); if (!$doc) { $this->encoder->encode(["success" => false, "msg" => "Document does not exist", "code" => self::ELEMENT_DOES_NOT_EXIST]); return; } $this->checkPermission($doc, "get"); if ($doc) { $type = $doc->getType(); $getter = "getDocument" . ucfirst($type) . "ById"; if (method_exists($this->service, $getter)) { $object = $this->service->{$getter}($id); } else { // check if the getter is implemented by a plugin $class = "\\Pimcore\\Model\\Webservice\\Data\\Document\\" . ucfirst($type) . "\\Out"; if (Tool::classExists($class)) { Document\Service::loadAllDocumentFields($doc); $object = Webservice\Data\Mapper::map($doc, $class, "out"); } else { throw new \Exception("unknown type"); } } } if (!$object) { throw new \Exception("could not find document"); } @$this->encoder->encode(["success" => true, "data" => $object]); return; } elseif ($this->isDelete()) { /** * @api {delete} /document Delete document * @apiName deleteDocument * @apiGroup Document * @apiParam {int} id The id of document you delete * @apiSampleRequest off * @apiParamExample {json} Request-Example: * { * "id": 4711 * "apikey": '2132sdf2321rwefdcvvce22' * } * @apiParam {string} apikey your access token * @apiSuccess {boolean} success Returns true if finished successfully * @apiSuccessExample {json} Succes-Response: * HTTP/1.1 200 OK * { * "success":true * } * @apiError {boolean} success Returns false if failed * @apiErrorExample {json} Error-Response: * {"success":false,"msg":"exception 'Exception' with message 'Document with given ID (712131243) does not exist.'"} */ $doc = Document::getById($id); if ($doc) { $this->checkPermission($doc, "delete"); } $success = $this->service->deleteDocument($id); $this->encoder->encode(["success" => $success]); return; } elseif ($this->isPost() || $this->isPut()) { /** * @api {post} /document Update document * @apiName updateDocument * @apiGroup Document * @apiParam {int} id The id of document you delete * @apiSampleRequest off * @apiParamExample {json} Request-Example: * { * "id": 4711 * "apikey": '2132sdf2321rwefdcvvce22' * } * @apiParam {string} apikey your access token * @apiSuccess {boolean} success Returns true if finished successfully * @apiSuccessExample {json} Succes-Response: * HTTP/1.1 200 OK * { * "success":true * } * @apiError {boolean} success Returns false if failed * @apiErrorExample {json} Error-Response: * {"success":false,"msg":"exception 'Exception' with message 'Document with given ID (712131243) does not exist.'"} */ $data = file_get_contents("php://input"); $data = \Zend_Json::decode($data); $type = $data["type"]; $id = null; $typeUpper = ucfirst($type); $className = "\\Pimcore\\Model\\Webservice\\Data\\Document\\" . $typeUpper . "\\In"; if ($data["id"]) { $doc = Document::getById($data["id"]); if ($doc) { $this->checkPermission($doc, "update"); } $isUpdate = true; $setter = "updateDocument" . $typeUpper; if (!method_exists($this->service, $setter)) { throw new \Exception("method does not exist " . $setter); } $wsData = self::fillWebserviceData($className, $data); $success = $this->service->{$setter}($wsData); } else { $setter = "createDocument" . $typeUpper; if (!method_exists($this->service, $setter)) { throw new \Exception("method does not exist " . $setter); } $wsData = self::fillWebserviceData($className, $data); $doc = new Document(); $doc->setId($wsData->parentId); $this->checkPermission($doc, "create"); $id = $this->service->{$setter}($wsData); } if (!$isUpdate) { $success = $id != null; } if ($success && !$isUpdate) { $this->encoder->encode(["success" => $success, "id" => $id]); } else { $this->encoder->encode(["success" => $success]); } return; } } catch (\Exception $e) { $this->encoder->encode(["success" => false, "msg" => (string) $e]); } $this->encoder->encode(["success" => false]); }