/** * @param $object * @param null $options * @throws \Exception */ public function map($object, $options = null) { $keys = get_object_vars($this); $blockedKeys = array("childs"); foreach ($keys as $key => $value) { $method = "get" . $key; if (method_exists($object, $method) && !in_array($key, $blockedKeys)) { if ($object->{$method}()) { $this->{$key} = $object->{$method}(); // check for a pimcore data type if ($this->{$key} instanceof Element\ElementInterface) { $this->{$key} = $this->{$key}->getId(); } // if the value is an object or array call the mapper again for the value if (is_object($this->{$key}) || is_array($this->{$key})) { $type = "out"; if (strpos(get_class($this), "_In") !== false) { $type = "in"; } $className = Webservice\Data\Mapper::findWebserviceClass($this->{$key}, "out"); $this->{$key} = Webservice\Data\Mapper::map($this->{$key}, $className, $type); } } } } }
/** * @static * @param Object\ClassDefinition $class * @return string */ public static function generateClassDefinitionJson($class) { $data = Webservice\Data\Mapper::map($class, "\\Pimcore\\Model\\Webservice\\Data\\ClassDefinition\\Out", "out"); unset($data->id); unset($data->name); unset($data->creationDate); unset($data->modificationDate); unset($data->userOwner); unset($data->userModification); unset($data->fieldDefinitions); //add propertyVisibility to export data $data->propertyVisibility = $class->propertyVisibility; $json = \Zend_Json::encode($data); $json = \Zend_Json::prettyPrint($json); return $json; }
/** * @param $id * @throws \Exception */ public function getClassById($id) { try { $class = Object\ClassDefinition::getById($id); if ($class instanceof Object\ClassDefinition) { $apiClass = Webservice\Data\Mapper::map($class, "\\Pimcore\\Model\\Webservice\\Data\\ClassDefinition\\Out", "out"); unset($apiClass->fieldDefinitions); return $apiClass; } throw new \Exception("Class with given ID (" . $id . ") does not exist."); } catch (\Exception $e) { \Logger::error($e); throw $e; } }
/** 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)); }
/** * Returns the current tag's data for web service export * @param mixed $params * @abstract * @return array */ public function getForWebserviceExport($document = null, $params = []) { $keys = get_object_vars($this); $el = []; foreach ($keys as $key => $value) { if ($value instanceof Model\Element\ElementInterface) { $value = $value->getId(); } $className = Webservice\Data\Mapper::findWebserviceClass($value, "out"); $el[$key] = Webservice\Data\Mapper::map($value, $className, "out"); } unset($el["dao"]); unset($el["documentId"]); unset($el["controller"]); unset($el["view"]); unset($el["editmode"]); $el = Webservice\Data\Mapper::toObject($el); return $el; }
/** * @param $asset * @return mixed|null|string * @throws Exception * @throws \Exception */ public function createAsset($asset) { if ($asset->getType() == "folder") { $documentType = "\\Pimcore\\Model\\Webservice\\Data\\Asset\\Folder\\Out"; } else { $documentType = "\\Pimcore\\Model\\Webservice\\Data\\Asset\\File\\Out"; } $wsDocument = Webservice\Data\Mapper::map($asset, $documentType, "out"); $encodedData = json_encode($wsDocument); $response = $this->doRequest($this->buildEndpointUrl("asset/"), "PUT", $encodedData); $response = $response->data; return $response; }
/** 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]); }
/** * See http://www.pimcore.org/issues/browse/PIMCORE-2358 * Add option to export/import all class definitions/brick definitions etc. at once */ public function bulkExportAction() { $result = []; $this->removeViewRenderer(); $fieldCollections = new Object\Fieldcollection\Definition\Listing(); $fieldCollections = $fieldCollections->load(); foreach ($fieldCollections as $fieldCollection) { $key = $fieldCollection->key; $fieldCollectionJson = json_decode(Object\ClassDefinition\Service::generateFieldCollectionJson($fieldCollection)); $fieldCollectionJson->key = $key; $result["fieldcollection"][] = $fieldCollectionJson; } $classes = new Object\ClassDefinition\Listing(); $classes->setOrder("ASC"); $classes->setOrderKey("id"); $classes = $classes->load(); foreach ($classes as $class) { $data = Model\Webservice\Data\Mapper::map($class, "\\Pimcore\\Model\\Webservice\\Data\\ClassDefinition\\Out", "out"); unset($data->fieldDefinitions); $result["class"][] = $data; } $objectBricks = new Object\Objectbrick\Definition\Listing(); $objectBricks = $objectBricks->load(); foreach ($objectBricks as $objectBrick) { $key = $objectBrick->key; $objectBrickJson = json_decode(Object\ClassDefinition\Service::generateObjectBrickJson($objectBrick)); $objectBrickJson->key = $key; $result["objectbrick"][] = $objectBrickJson; } $customLayouts = new Object\ClassDefinition\CustomLayout\Listing(); $customLayouts = $customLayouts->load(); foreach ($customLayouts as $customLayout) { /** @var $customLayout Object\ClassDefinition\CustomLayout */ $classId = $customLayout->getClassId(); $class = Object\ClassDefinition::getById($classId); $customLayout->className = $class->getName(); $result["customlayout"][] = $customLayout; } header("Content-type: application/json"); header("Content-Disposition: attachment; filename=\"bulk_export.json\""); $result = json_encode($result); echo $result; }