public function addAction()
 {
     $success = false;
     // check for permission
     $parentDocument = Document::getById(intval($this->_getParam("parentId")));
     if ($parentDocument->isAllowed("create")) {
         $intendedPath = $parentDocument->getFullPath() . "/" . $this->_getParam("key");
         if (!Document_Service::pathExists($intendedPath)) {
             $createValues = array("userOwner" => $this->getUser()->getId(), "userModification" => $this->getUser()->getId(), "published" => false);
             $createValues["key"] = $this->_getParam("key");
             // check for a docType
             if ($this->_getParam("docTypeId") && is_numeric($this->_getParam("docTypeId"))) {
                 $docType = Document_DocType::getById(intval($this->_getParam("docTypeId")));
                 $createValues["template"] = $docType->getTemplate();
                 $createValues["controller"] = $docType->getController();
                 $createValues["action"] = $docType->getAction();
                 $createValues["module"] = $docType->getModule();
             } else {
                 if ($this->_getParam("type") == "page" || $this->_getParam("type") == "snippet" || $this->_getParam("type") == "email") {
                     $createValues["controller"] = Pimcore_Config::getSystemConfig()->documents->default_controller;
                     $createValues["action"] = Pimcore_Config::getSystemConfig()->documents->default_action;
                 }
             }
             switch ($this->_getParam("type")) {
                 case "page":
                     $document = Document_Page::create($this->_getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "snippet":
                     $document = Document_Snippet::create($this->_getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "email":
                     //ckogler
                     $document = Document_Email::create($this->_getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "link":
                     $document = Document_Link::create($this->_getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "hardlink":
                     $document = Document_Hardlink::create($this->_getParam("parentId"), $createValues);
                     $success = true;
                     break;
                 case "folder":
                     $document = Document_Folder::create($this->_getParam("parentId"), $createValues);
                     $document->setPublished(true);
                     try {
                         $document->save();
                         $success = true;
                     } catch (Exception $e) {
                         $this->_helper->json(array("success" => false, "message" => $e->getMessage()));
                     }
                     break;
                 default:
                     Logger::debug("Unknown document type, can't add [ " . $this->_getParam("type") . " ] ");
                     break;
             }
         } else {
             Logger::debug("prevented adding a document because document with same path+key [ {$intendedPath} ] already exists");
         }
     } else {
         Logger::debug("prevented adding a document because of missing permissions");
     }
     if ($success) {
         $this->_helper->json(array("success" => $success, "id" => $document->getId(), "type" => $document->getType()));
     } else {
         $this->_helper->json(array("success" => $success));
     }
 }
Example #2
0
 public function testCopyAndDeleteDocument()
 {
     $documentList = new Document_List();
     $documentList->setCondition("`key` like '%_data%' and `type` = 'page'");
     $documents = $documentList->load();
     $parent = $documents[0];
     $this->assertTrue($parent instanceof Document_Page);
     //remove childs if there are some
     if ($parent->hasChilds()) {
         foreach ($parent->getChilds() as $child) {
             $child->delete();
         }
     }
     $this->assertFalse($parent->hasChilds());
     $service = new Document_Service(User::getById(1));
     //copy as child
     $service->copyAsChild($parent, $parent);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 1);
     //copy as child no. 2
     $service->copyAsChild($parent, $parent);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 2);
     $childs = $parent->getChilds();
     $this->assertTrue(Test_Tool::documentsAreEqual($parent, $childs[0], true));
     $this->assertTrue(Test_Tool::documentsAreEqual($parent, $childs[1], true));
     //copy recursivley
     $rootNode = Document::getById(1);
     $copy = $service->copyRecursive($rootNode, $parent);
     $this->assertTrue($copy->hasChilds());
     $this->assertTrue(count($copy->getChilds()) == 2);
     $this->assertTrue(Test_Tool::documentsAreEqual($parent, $copy, true));
     //create empty document
     $emptyDoc = Document_Page::create(1, array("userOwner" => 1, "key" => uniqid() . rand(10, 99)));
     $this->assertFalse(Test_Tool::documentsAreEqual($emptyDoc, $copy, true));
     //copy contents
     $emptyDoc = $service->copyContents($emptyDoc, $copy);
     $this->assertTrue(Test_Tool::documentsAreEqual($emptyDoc, $copy, true));
     //todo copy contents must fail if types differ
     //delete recusively
     $shouldBeDeleted[] = $copy->getId();
     $childs = $copy->getChilds();
     foreach ($childs as $child) {
         $shouldBeDeleted[] = $child->getId();
     }
     $copy->delete();
     foreach ($shouldBeDeleted as $id) {
         $o = Document::getById($id);
         $this->assertFalse($o instanceof Document);
     }
 }